STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
custom_stats.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/custom_stats.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2014 Thomas Keh <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_COMMON_CUSTOM_STATS_HEADER
14 #define STXXL_COMMON_CUSTOM_STATS_HEADER
15 
16 #include <string>
17 #include <utility>
18 #include <algorithm>
21 
23 
24 /*!
25  * This class provides a statistical counter that can easily be deactivated
26  * using a typedef to dummy_custom_stats_counter. It's basically a wrapper for
27  * a unsigned long long value.
28  *
29  * \see dummy_custom_stats_counter
30  */
31 template <typename ValueType>
33 {
34 public:
35  //! The counter's value type
36  typedef ValueType value_type;
37 
38 protected:
39  //! The counter's value
41 
42 public:
43  //! The constructor. Initializes the counter to 0.
45  : m_value(0)
46  { }
47 
48  //! Increases the counter by right.
49  //! \param right The corresponding integer value
51  {
52  m_value += right;
53  return *this;
54  }
55  //! Increases the counter by 1 (prefix).
57  {
58  ++m_value;
59  return *this;
60  }
61  //! Increases the counter by 1 (postfix).
63  {
64  custom_stats_counter copy = *this;
65  ++m_value;
66  return copy;
67  }
68  //! Assignment operator
69  //! \param other The corresponding integer value
70  custom_stats_counter& operator = (const value_type& other)
71  {
72  m_value = other;
73  return *this;
74  }
75  /*!
76  * Set the counter to other if other is larger than the current counter
77  * value.
78  *
79  * \param other The corresponding integer value
80  */
81  void set_max(const value_type& other)
82  {
83  m_value = std::max(m_value, other);
84  }
85  /*!
86  * Return the counter value interpreted as a memory amount in IEC units as
87  * a string. For that purpose the counter value is multiplied with the
88  * byte_per_element argument.
89  *
90  * \param byte_per_element The memory amount per "counter element".
91  */
92  std::string as_memory_amount(const value_type& byte_per_element) const
93  {
94  return format_IEC_size(m_value * byte_per_element) + "B";
95  }
96  /*!
97  * Cast to counter_type: Returns the counter's value as a regular integer
98  * value. This can be used as a getter as well as for printing with
99  * std::out.
100  */
101  operator value_type () const
102  {
103  return m_value;
104  }
105 };
106 
107 /*!
108  * Dummy class for custom_stats_counter. The methods do nothing. The compiler
109  * should optimize out the code.
110  *
111  * \see custom_stats_counter
112  */
113 template <typename ValueType>
115 {
116 public:
117  typedef ValueType value_type;
118 
119 public:
122  {
123  return *this;
124  }
126  {
127  return *this;
128  }
130  {
131  return *this;
132  }
134  {
135  return *this;
136  }
137  void set_max(value_type) { }
138  std::string as_memory_amount(const value_type&) const
139  {
140  return "";
141  }
142  operator value_type () const
143  {
144  return value_type();
145  }
146 };
147 
149 
150 #endif // !STXXL_COMMON_CUSTOM_STATS_HEADER
Dummy class for custom_stats_counter.
Definition: custom_stats.h:114
custom_stats_counter()
The constructor. Initializes the counter to 0.
Definition: custom_stats.h:44
std::string as_memory_amount(const value_type &byte_per_element) const
Return the counter value interpreted as a memory amount in IEC units as a string. ...
Definition: custom_stats.h:92
This class provides a statistical counter that can easily be deactivated using a typedef to dummy_cus...
Definition: custom_stats.h:32
value_type m_value
The counter&#39;s value.
Definition: custom_stats.h:40
uint_pair & operator++()
prefix increment operator (directly manipulates the integer parts)
Definition: uint_types.h:163
uint_pair & operator+=(const uint_pair &b)
addition operator (uses 64-bit arithmetic)
Definition: uint_types.h:183
std::string as_memory_amount(const value_type &) const
Definition: custom_stats.h:138
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
ValueType value_type
The counter&#39;s value type.
Definition: custom_stats.h:36
void set_max(const value_type &other)
Set the counter to other if other is larger than the current counter value.
Definition: custom_stats.h:81
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:241
std::string format_IEC_size(uint64 number)
Format a byte size using IEC (Ki, Mi, Gi, Ti) suffixes (powers of two). Returns &quot;123 Ki&quot; or similar...
Definition: utils.cpp:111
#define STXXL_END_NAMESPACE
Definition: namespace.h:17