lemon/counter.h
changeset 156 e561aa7675de
parent 119 82a2639a05bb
child 160 b1bd0c2a7f57
equal deleted inserted replaced
0:717d32e0676a 1:30470f389555
    27 ///\brief Tools for counting steps and events
    27 ///\brief Tools for counting steps and events
    28 
    28 
    29 namespace lemon 
    29 namespace lemon 
    30 {
    30 {
    31 
    31 
    32   template<class P> class _SubNoCounter;
    32   template<class P> class _NoSubCounter;
    33 
    33 
    34   template<class P>
    34   template<class P>
    35   class _SubCounter 
    35   class _SubCounter 
    36   {
    36   {
    37     P &_parent;
    37     P &_parent;
    39     std::ostream &_os;
    39     std::ostream &_os;
    40     int count;
    40     int count;
    41   public:
    41   public:
    42 
    42 
    43     typedef _SubCounter<_SubCounter<P> > SubCounter;
    43     typedef _SubCounter<_SubCounter<P> > SubCounter;
    44     typedef _SubNoCounter<_SubCounter<P> > SubNoCounter;
    44     typedef _NoSubCounter<_SubCounter<P> > NoSubCounter;
    45 
    45 
    46     _SubCounter(P &parent)
    46     _SubCounter(P &parent)
    47       : _parent(parent), _title(), _os(std::cerr), count(0) {}
    47       : _parent(parent), _title(), _os(std::cerr), count(0) {}
    48     _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
    48     _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
    49       : _parent(parent), _title(title), _os(os), count(0) {}
    49       : _parent(parent), _title(title), _os(os), count(0) {}
    62     void reset(int c=0) {count=c;}
    62     void reset(int c=0) {count=c;}
    63     operator int() {return count;}
    63     operator int() {return count;}
    64   };
    64   };
    65 
    65 
    66   template<class P>
    66   template<class P>
    67   class _SubNoCounter 
    67   class _NoSubCounter 
    68   {
    68   {
    69     P &_parent;
    69     P &_parent;
    70   public:
    70   public:
    71     typedef _SubNoCounter<_SubNoCounter<P> > SubCounter;
    71     typedef _NoSubCounter<_NoSubCounter<P> > SubCounter;
    72     typedef _SubNoCounter<_SubNoCounter<P> > SubNoCounter;
    72     typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter;
    73   
    73   
    74     _SubNoCounter(P &parent) :_parent(parent) {}
    74     _NoSubCounter(P &parent) :_parent(parent) {}
    75     _SubNoCounter(P &parent,std::string,std::ostream &) 
    75     _NoSubCounter(P &parent,std::string,std::ostream &) 
    76       :_parent(parent) {}
    76       :_parent(parent) {}
    77     _SubNoCounter(P &parent,std::string) 
    77     _NoSubCounter(P &parent,std::string) 
    78       :_parent(parent) {}
    78       :_parent(parent) {}
    79     _SubNoCounter(P &parent,const char *,std::ostream &)
    79     _NoSubCounter(P &parent,const char *,std::ostream &)
    80       :_parent(parent) {}
    80       :_parent(parent) {}
    81     _SubNoCounter(P &parent,const char *)
    81     _NoSubCounter(P &parent,const char *)
    82       :_parent(parent) {}
    82       :_parent(parent) {}
    83     ~_SubNoCounter() {}
    83     ~_NoSubCounter() {}
    84     _SubNoCounter &operator++() { ++_parent; return *this;}
    84     _NoSubCounter &operator++() { ++_parent; return *this;}
    85     int operator++(int) { _parent++; return 0;}
    85     int operator++(int) { _parent++; return 0;}
    86     _SubNoCounter &operator--() { --_parent; return *this;}
    86     _NoSubCounter &operator--() { --_parent; return *this;}
    87     int operator--(int) { _parent--; return 0;}
    87     int operator--(int) { _parent--; return 0;}
    88     _SubNoCounter &operator+=(int c) { _parent+=c; return *this;}
    88     _NoSubCounter &operator+=(int c) { _parent+=c; return *this;}
    89     _SubNoCounter &operator-=(int c) { _parent-=c; return *this;}
    89     _NoSubCounter &operator-=(int c) { _parent-=c; return *this;}
    90     void reset(int) {}
    90     void reset(int) {}
    91     void reset() {}
    91     void reset() {}
    92     operator int() {return 0;}
    92     operator int() {return 0;}
    93   };
    93   };
    94 
    94 
   115     typedef _SubCounter<Counter> SubCounter;
   115     typedef _SubCounter<Counter> SubCounter;
   116     ///\e
   116     ///\e
   117 
   117 
   118     ///\todo document please.
   118     ///\todo document please.
   119     ///
   119     ///
   120     typedef _SubNoCounter<Counter> SubNoCounter;
   120     typedef _NoSubCounter<Counter> NoSubCounter;
   121 
   121 
   122     ///\e
   122     ///\e
   123     Counter() : _title(), _os(std::cerr), count(0) {}
   123     Counter() : _title(), _os(std::cerr), count(0) {}
   124     ///\e
   124     ///\e
   125     Counter(std::string title,std::ostream &os=std::cerr) 
   125     Counter(std::string title,std::ostream &os=std::cerr) 
   154   ///'Do nothing' version of \ref Counter.
   154   ///'Do nothing' version of \ref Counter.
   155   ///\sa Counter
   155   ///\sa Counter
   156   class NoCounter
   156   class NoCounter
   157   {
   157   {
   158   public:
   158   public:
   159     typedef _SubNoCounter<NoCounter> SubCounter;
   159     typedef _NoSubCounter<NoCounter> SubCounter;
   160     typedef _SubNoCounter<NoCounter> SubNoCounter;
   160     typedef _NoSubCounter<NoCounter> NoSubCounter;
   161 
   161 
   162     NoCounter() {}
   162     NoCounter() {}
   163     NoCounter(std::string,std::ostream &) {}
   163     NoCounter(std::string,std::ostream &) {}
   164     NoCounter(const char *,std::ostream &) {}
   164     NoCounter(const char *,std::ostream &) {}
   165     NoCounter(std::string) {}
   165     NoCounter(std::string) {}