Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
stats_counter.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/stats_counter.hpp
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  * Part of Project Thrill - http://project-thrill.org
9  *
10  * Copyright (C) 2014 Thomas Keh <[email protected]>
11  * Copyright (C) 2015 Timo Bingmann <[email protected]>
12  *
13  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
14  ******************************************************************************/
15 
16 #pragma once
17 #ifndef THRILL_COMMON_STATS_COUNTER_HEADER
18 #define THRILL_COMMON_STATS_COUNTER_HEADER
19 
20 #include <algorithm>
21 
22 namespace thrill {
23 namespace common {
24 
25 /*!
26  * This class provides a statistical counter that can easily be deactivated
27  * using a boolean template switch. It's basically a wrapper for an counter
28  * type, like unsigned long long value. If further operators are needed, they
29  * should be added.
30  */
31 template <typename ValueType_, bool Active = true>
33 { };
34 
35 template <typename ValueType_>
36 class StatsCounter<ValueType_, true>
37 {
38 public:
39  //! The counter's value type
40  using ValueType = ValueType_;
41 
42 private:
43  //! The counter's value
45 
46 public:
47  //! The constructor. Initializes the counter to 0.
48  StatsCounter(const ValueType& initial = ValueType()) // NOLINT
49  : value_(initial)
50  { }
51 
52  //! Whether the counter is active
53  bool Real() const { return true; }
54 
55  //! Increases the counter by right.
57  value_ += right;
58  return *this;
59  }
60 
61  //! Increases the counter by 1 (prefix).
63  ++value_;
64  return *this;
65  }
66 
67  //! Increases the counter by 1 (postfix).
68  StatsCounter operator ++ (int) { // NOLINT
69  StatsCounter copy = *this;
70  ++value_;
71  return copy;
72  }
73 
74  //! Set the counter to other if other is larger than the current counter
75  //! value.
76  void set_max(const ValueType& other) {
77  value_ = std::max(value_, other);
78  }
79 
80  /*!
81  * Cast to counter_type: Returns the counter's value as a regular integer
82  * value. This can be used as a getter as well as for printing with
83  * std::out.
84  */
85  operator ValueType () const
86  {
87  return value_;
88  }
89 
90  ValueType value() const {
91  return value_;
92  }
93 };
94 
95 template <typename ValueType_>
96 class StatsCounter<ValueType_, false>
97 {
98 public:
99  //! The counter's value type
100  using ValueType = ValueType_;
101 
102 public:
103  StatsCounter(const ValueType& = ValueType()) // NOLINT
104  { }
105 
106  //! Whether the counter is active
107  bool Real() const { return false; }
108 
110  { return *this; }
111 
113  { return *this; }
114 
115  StatsCounter& operator ++ (int) // NOLINT
116  { return *this; }
117 
118  void set_max(const ValueType&)
119  { }
120 
121  operator ValueType () const
122  {
123  return ValueType();
124  }
125 
126  ValueType value() const {
127  return ValueType();
128  }
129 };
130 
131 } // namespace common
132 } // namespace thrill
133 
134 #endif // !THRILL_COMMON_STATS_COUNTER_HEADER
135 
136 /******************************************************************************/
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.hpp:226
This class provides a statistical counter that can easily be deactivated using a boolean template swi...
StatsCounter(const ValueType &initial=ValueType())
The constructor. Initializes the counter to 0.
uint_pair & operator+=(const uint_pair &b)
addition operator (uses 64-bit arithmetic)
Definition: uint_types.hpp:166
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.hpp:146
bool Real() const
Whether the counter is active.
ValueType_ ValueType
The counter's value type.
ValueType_ ValueType
The counter's value type.
bool Real() const
Whether the counter is active.