... | ... |
@@ -38,78 +38,75 @@ |
38 | 38 |
std::string _title; |
39 | 39 |
std::ostream &_os; |
40 | 40 |
int count; |
41 | 41 |
public: |
42 | 42 |
|
43 | 43 |
typedef _SubCounter<_SubCounter<P> > SubCounter; |
44 | 44 |
typedef _NoSubCounter<_SubCounter<P> > NoSubCounter; |
45 | 45 |
|
46 | 46 |
_SubCounter(P &parent) |
47 | 47 |
: _parent(parent), _title(), _os(std::cerr), count(0) {} |
48 | 48 |
_SubCounter(P &parent,std::string title,std::ostream &os=std::cerr) |
49 | 49 |
: _parent(parent), _title(title), _os(os), count(0) {} |
50 | 50 |
_SubCounter(P &parent,const char *title,std::ostream &os=std::cerr) |
51 | 51 |
: _parent(parent), _title(title), _os(os), count(0) {} |
52 | 52 |
~_SubCounter() { |
53 | 53 |
_os << _title << count <<std::endl; |
54 | 54 |
_parent+=count; |
55 | 55 |
} |
56 | 56 |
_SubCounter &operator++() { count++; return *this;} |
57 | 57 |
int operator++(int) { return count++; } |
58 | 58 |
_SubCounter &operator--() { count--; return *this;} |
59 | 59 |
int operator--(int) { return count--; } |
60 | 60 |
_SubCounter &operator+=(int c) { count+=c; return *this;} |
61 | 61 |
_SubCounter &operator-=(int c) { count-=c; return *this;} |
62 |
void reset(int c=0) {count=c;} |
|
63 | 62 |
operator int() {return count;} |
64 | 63 |
}; |
65 | 64 |
|
66 | 65 |
template<class P> |
67 | 66 |
class _NoSubCounter |
68 | 67 |
{ |
69 | 68 |
P &_parent; |
70 | 69 |
public: |
71 | 70 |
typedef _NoSubCounter<_NoSubCounter<P> > SubCounter; |
72 | 71 |
typedef _NoSubCounter<_NoSubCounter<P> > NoSubCounter; |
73 | 72 |
|
74 | 73 |
_NoSubCounter(P &parent) :_parent(parent) {} |
75 | 74 |
_NoSubCounter(P &parent,std::string,std::ostream &) |
76 | 75 |
:_parent(parent) {} |
77 | 76 |
_NoSubCounter(P &parent,std::string) |
78 | 77 |
:_parent(parent) {} |
79 | 78 |
_NoSubCounter(P &parent,const char *,std::ostream &) |
80 | 79 |
:_parent(parent) {} |
81 | 80 |
_NoSubCounter(P &parent,const char *) |
82 | 81 |
:_parent(parent) {} |
83 | 82 |
~_NoSubCounter() {} |
84 | 83 |
_NoSubCounter &operator++() { ++_parent; return *this;} |
85 | 84 |
int operator++(int) { _parent++; return 0;} |
86 | 85 |
_NoSubCounter &operator--() { --_parent; return *this;} |
87 | 86 |
int operator--(int) { _parent--; return 0;} |
88 | 87 |
_NoSubCounter &operator+=(int c) { _parent+=c; return *this;} |
89 | 88 |
_NoSubCounter &operator-=(int c) { _parent-=c; return *this;} |
90 |
void reset(int) {} |
|
91 |
void reset() {} |
|
92 | 89 |
operator int() {return 0;} |
93 | 90 |
}; |
94 | 91 |
|
95 | 92 |
|
96 | 93 |
/// \addtogroup timecount |
97 | 94 |
/// @{ |
98 | 95 |
|
99 | 96 |
/// A counter class |
100 | 97 |
|
101 | 98 |
/// This class makes it easier to count certain events (e.g. for debug |
102 | 99 |
/// reasons). |
103 | 100 |
/// You can increment or decrement the counter using \c operator++, |
104 | 101 |
/// \c operator--, \c operator+= and \c operator-=. You can also |
105 | 102 |
/// define subcounters for the different phases of the algorithm or |
106 | 103 |
/// for different types of operations. |
107 | 104 |
/// A report containing the given title and the value of the counter |
108 | 105 |
/// is automatically printed on destruction. |
109 | 106 |
/// |
110 | 107 |
/// The following example shows the usage of counters and subcounters. |
111 | 108 |
/// \code |
112 | 109 |
/// // Bubble sort |
113 | 110 |
/// std::vector<T> v; |
114 | 111 |
/// ... |
115 | 112 |
/// Counter op("Operations: "); |
... | ... |
@@ -182,48 +179,53 @@ |
182 | 179 |
Counter() : _title(), _os(std::cerr), count(0) {} |
183 | 180 |
/// Constructor. |
184 | 181 |
Counter(std::string title,std::ostream &os=std::cerr) |
185 | 182 |
: _title(title), _os(os), count(0) {} |
186 | 183 |
/// Constructor. |
187 | 184 |
Counter(const char *title,std::ostream &os=std::cerr) |
188 | 185 |
: _title(title), _os(os), count(0) {} |
189 | 186 |
/// Destructor. Prints the given title and the value of the counter. |
190 | 187 |
~Counter() { |
191 | 188 |
_os << _title << count <<std::endl; |
192 | 189 |
} |
193 | 190 |
///\e |
194 | 191 |
Counter &operator++() { count++; return *this;} |
195 | 192 |
///\e |
196 | 193 |
int operator++(int) { return count++;} |
197 | 194 |
///\e |
198 | 195 |
Counter &operator--() { count--; return *this;} |
199 | 196 |
///\e |
200 | 197 |
int operator--(int) { return count--;} |
201 | 198 |
///\e |
202 | 199 |
Counter &operator+=(int c) { count+=c; return *this;} |
203 | 200 |
///\e |
204 | 201 |
Counter &operator-=(int c) { count-=c; return *this;} |
205 | 202 |
/// Resets the counter to the given value. |
203 |
|
|
204 |
/// Resets the counter to the given value. |
|
205 |
/// \note This function does not reset the values of |
|
206 |
/// \ref SubCounter "SubCounter"s but it resets \ref NoSubCounter |
|
207 |
/// "NoSubCounter"s along with the main counter. |
|
206 | 208 |
void reset(int c=0) {count=c;} |
207 | 209 |
/// Returns the value of the counter. |
208 | 210 |
operator int() {return count;} |
209 | 211 |
}; |
210 | 212 |
|
211 | 213 |
/// 'Do nothing' version of Counter. |
212 | 214 |
|
213 | 215 |
/// This class can be used in the same way as \ref Counter however it |
214 | 216 |
/// does not count at all and does not print report on destruction. |
215 | 217 |
/// |
216 | 218 |
/// Replacing a \ref Counter with a \ref NoCounter makes it possible |
217 | 219 |
/// to turn off all counting and reporting (SubCounters should also |
218 | 220 |
/// be replaced with NoSubCounters), so it does not affect the |
219 | 221 |
/// efficiency of the program at all. |
220 | 222 |
/// |
221 | 223 |
/// \sa Counter |
222 | 224 |
class NoCounter |
223 | 225 |
{ |
224 | 226 |
public: |
225 | 227 |
typedef _NoSubCounter<NoCounter> SubCounter; |
226 | 228 |
typedef _NoSubCounter<NoCounter> NoSubCounter; |
227 | 229 |
|
228 | 230 |
NoCounter() {} |
229 | 231 |
NoCounter(std::string,std::ostream &) {} |
0 comments (0 inline)