COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/counter.h @ 1852:ffa7c6e96330

Last change on this file since 1852:ffa7c6e96330 was 1851:78b5ea23f0f1, checked in by Alpar Juttner, 14 years ago

Doc improvements

File size: 4.9 KB
Line 
1/* -*- C++ -*-
2 * lemon/counter.h -
3 * Part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
6 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 *
8 * Permission to use, modify and distribute this software is granted
9 * provided that this copyright notice appears in all copies. For
10 * precise terms see the accompanying LICENSE file.
11 *
12 * This software is provided "AS IS" with no warranty of any kind,
13 * express or implied, and with no claim as to its suitability for any
14 * purpose.
15 *
16 */
17
18#ifndef LEMON_COUNTER_H
19#define LEMON_COUNTER_H
20
21#include <string>
22#include <iostream>
23
24///\ingroup timecount
25///\file
26///\brief Tools for counting steps and events
27
28namespace lemon
29{
30
31  template<class P> class _SubNoCounter;
32
33  template<class P>
34  class _SubCounter
35  {
36    P &_parent;
37    std::string _title;
38    std::ostream &_os;
39    int count;
40  public:
41
42    typedef _SubCounter<_SubCounter<P> > SubCounter;
43    typedef _SubNoCounter<_SubCounter<P> > SubNoCounter;
44
45    _SubCounter(P &parent)
46      : _parent(parent), _title(), _os(std::cerr), count(0) {}
47    _SubCounter(P &parent,std::string title,std::ostream &os=std::cerr)
48      : _parent(parent), _title(title), _os(os), count(0) {}
49    _SubCounter(P &parent,const char *title,std::ostream &os=std::cerr)
50      : _parent(parent), _title(title), _os(os), count(0) {}
51    ~_SubCounter() {
52      _os << _title << count <<std::endl;
53      _parent+=count;
54    }
55    _SubCounter &operator++() { count++; return *this;}
56    int operator++(int) { return count++; }
57    _SubCounter &operator--() { count--; return *this;}
58    int operator--(int) { return count--; }
59    _SubCounter &operator+=(int c) { count+=c; return *this;}
60    _SubCounter &operator-=(int c) { count-=c; return *this;}
61    void reset(int c=0) {count=c;}
62    operator int() {return count;}
63  };
64
65  template<class P>
66  class _SubNoCounter
67  {
68    P &_parent;
69  public:
70    typedef _SubNoCounter<_SubNoCounter<P> > SubCounter;
71    typedef _SubNoCounter<_SubNoCounter<P> > SubNoCounter;
72 
73    _SubNoCounter(P &parent) :_parent(parent) {}
74    _SubNoCounter(P &parent,std::string title,std::ostream &os=std::cerr)
75      :_parent(parent) {}
76    _SubNoCounter(P &parent,const char *title,std::ostream &os=std::cerr)
77      :_parent(parent) {}
78    ~_SubNoCounter() {}
79    _SubNoCounter &operator++() { ++_parent; return *this;}
80    int operator++(int) { _parent++; return 0;}
81    _SubNoCounter &operator--() { --_parent; return *this;}
82    int operator--(int) { _parent--; return 0;}
83    _SubNoCounter &operator+=(int c) { _parent+=c; return *this;}
84    _SubNoCounter &operator-=(int c) { _parent-=c; return *this;}
85    void reset(int c=0) {}
86    operator int() {return 0;}
87  };
88
89
90  /// \addtogroup timecount
91  /// @{
92
93  ///A counter class
94
95  ///This class makes it easier to count certain events. You can increment
96  ///or decrement the counter using operator++ and operator--.
97  ///A report is automatically printed on destruction.
98  ///\todo More doc
99  class Counter
100  {
101    std::string _title;
102    std::ostream &_os;
103    int count;
104  public:
105    ///\e
106
107    ///\todo document please.
108    ///
109    typedef _SubCounter<Counter> SubCounter;
110    ///\e
111
112    ///\todo document please.
113    ///
114    typedef _SubNoCounter<Counter> SubNoCounter;
115
116    ///\e
117    Counter() : _title(), _os(std::cerr), count(0) {}
118    ///\e
119    Counter(std::string title,std::ostream &os=std::cerr)
120      : _title(title), _os(os), count(0) {}
121    ///\e
122    Counter(const char *title,std::ostream &os=std::cerr)
123      : _title(title), _os(os), count(0) {}
124    ///Destructor. Prints the given title and the value of the counter.
125    ~Counter() {
126      _os << _title << count <<std::endl;
127    }
128    ///\e
129    Counter &operator++() { count++; return *this;}
130    ///\e
131    int operator++(int) { return count++;}
132    ///\e
133    Counter &operator--() { count--; return *this;}
134    ///\e
135    int operator--(int) { return count--;}
136    ///\e
137    Counter &operator+=(int c) { count+=c; return *this;}
138    ///\e
139    Counter &operator-=(int c) { count-=c; return *this;}
140    ///\e
141    void reset(int c=0) {count=c;}
142    ///\e
143    operator int() {return count;}
144  };
145
146  ///'Do nothing' version of \ref Counter
147
148  ///'Do nothing' version of \ref Counter.
149  ///\sa Counter
150  class NoCounter
151  {
152  public:
153    typedef _SubNoCounter<NoCounter> SubCounter;
154    typedef _SubNoCounter<NoCounter> SubNoCounter;
155
156    NoCounter() {}
157    NoCounter(std::string title,std::ostream &os=std::cerr) {}
158    NoCounter(const char *title,std::ostream &os=std::cerr) {}
159    NoCounter &operator++() { return *this; }
160    int operator++(int) { return 0; }
161    NoCounter &operator--() { return *this; }
162    int operator--(int) { return 0; }
163    NoCounter &operator+=(int c) { return *this;}
164    NoCounter &operator-=(int c) { return *this;}
165    void reset(int c=0) {}
166    operator int() {return 0;}
167  };
168
169  ///@}
170}
171
172#endif
Note: See TracBrowser for help on using the repository browser.