iscsi

debug.h

Jan 8th, 2012
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. #if HOME
  2.  
  3. #include <vector>
  4. #include <list>
  5. #include <queue>
  6. #include <set>
  7. #include <map>
  8.  
  9. // Author :  Nikhil Garg
  10.  
  11. #define debug(args,__VA_ARGS__)     (Debugger()) , args
  12.  
  13. class Debugger
  14. {
  15. public:
  16.    Debugger(const std::string& _separator = ", ") :
  17.    first(true), separator(_separator){}
  18.    
  19.    template<typename ObjectType>
  20.    Debugger& operator , (const ObjectType& v)
  21.    {
  22.        if(!first)
  23.            std::cerr << separator;
  24.        std::cerr << v;
  25.        first = false;
  26.        return *this;
  27.    }
  28.    ~Debugger() {  std::cerr << endl;}
  29.    
  30. private:
  31.    bool first;
  32.    std::string separator;
  33. };
  34.  
  35. template <typename T1, typename T2>
  36. inline std::ostream& operator << (std::ostream& os, const std::pair<T1, T2>& p)
  37. {
  38.    return os << "(" << p.first << ", " << p.second << ")";
  39. }
  40.  
  41. template<typename T>
  42. inline std::ostream &operator << (std::ostream & os,const std::vector<T>& v)
  43. {
  44.    bool first = true;
  45.    os << "[";
  46.    for(unsigned int i = 0; i < v.size(); i++)
  47.    {
  48.        if(!first)
  49.            os << ", ";
  50.        os << v[i];
  51.        first = false;
  52.    }
  53.    return os << "]";
  54. }
  55.  
  56. template<typename T>
  57. inline std::ostream &operator << (std::ostream & os,const std::list<T>& l)
  58. {
  59.    bool first = true;
  60.    os << "[";
  61.    for(typename std::list<T>::const_iterator it = l.begin(); it != l.end(); ++it)
  62.    {
  63.        if(!first)
  64.            os << ", ";
  65.        os << *it;
  66.        first = false;
  67.    }
  68.    return os << "]";
  69. }
  70.  
  71. template<typename T>
  72. inline std::ostream &operator << (std::ostream & os,const std::set<T>& v)
  73. {
  74.    bool first = true;
  75.    os << "[";
  76.    for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
  77.    {
  78.        if(!first)
  79.            os << ", ";
  80.        os << *ii;
  81.        first = false;
  82.    }
  83.    return os << "]";
  84. }
  85.  
  86. template<typename T1, typename T2>
  87. inline std::ostream &operator << (std::ostream & os,const std::map<T1, T2>& v)
  88. {
  89.    bool first = true;
  90.    os << "[";
  91.    for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
  92.    {
  93.        if(!first)
  94.            os << ", ";
  95.        os << *ii ;
  96.        first = false;
  97.    }
  98.    return os << "]";
  99. }
  100.  
  101. #endif
Advertisement
Add Comment
Please, Sign In to add comment