counter.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2022, Regents of the University of California,
4  * Arizona Board of Regents,
5  * Colorado State University,
6  * University Pierre & Marie Curie, Sorbonne University,
7  * Washington University in St. Louis,
8  * Beijing Institute of Technology,
9  * The University of Memphis.
10  *
11  * This file is part of NFD (Named Data Networking Forwarding Daemon).
12  * See AUTHORS.md for complete list of NFD authors and contributors.
13  *
14  * NFD is free software: you can redistribute it and/or modify it under the terms
15  * of the GNU General Public License as published by the Free Software Foundation,
16  * either version 3 of the License, or (at your option) any later version.
17  *
18  * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20  * PURPOSE. See the GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License along with
23  * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
24  */
25 
26 #ifndef NFD_DAEMON_COMMON_COUNTER_HPP
27 #define NFD_DAEMON_COMMON_COUNTER_HPP
28 
29 #include "core/common.hpp"
30 
31 namespace nfd {
32 
39 class SimpleCounter : noncopyable
40 {
41 public:
42  typedef uint64_t rep;
43 
47  operator rep() const noexcept
48  {
49  return m_value;
50  }
51 
55  void
56  set(rep value) noexcept
57  {
58  m_value = value;
59  }
60 
61 protected:
62  rep m_value = 0;
63 };
64 
70 {
71 public:
75  operator++() noexcept
76  {
77  ++m_value;
78  return *this;
79  }
80  // postfix ++ operator is not provided because it's not needed
81 };
82 
87 class ByteCounter : public SimpleCounter
88 {
89 public:
93  operator+=(rep n) noexcept
94  {
95  m_value += n;
96  return *this;
97  }
98 };
99 
105 template<typename T>
106 class SizeCounter : noncopyable
107 {
108 public:
109  typedef size_t Rep;
110 
111  explicit constexpr
112  SizeCounter(const T* table = nullptr) noexcept
113  : m_table(table)
114  {
115  }
116 
117  void
118  observe(const T* table) noexcept
119  {
120  m_table = table;
121  }
122 
125  operator Rep() const
126  {
127  BOOST_ASSERT(m_table != nullptr);
128  return m_table->size();
129  }
130 
131 private:
132  const T* m_table;
133 };
134 
135 } // namespace nfd
136 
137 #endif // NFD_DAEMON_COMMON_COUNTER_HPP
Represents a counter of number of bytes.
Definition: counter.hpp:88
ByteCounter & operator+=(rep n) noexcept
Increase the counter.
Definition: counter.hpp:93
Represents a counter of number of packets.
Definition: counter.hpp:70
PacketCounter & operator++() noexcept
Increment the counter by one.
Definition: counter.hpp:75
Represents a counter that encloses an integer value.
Definition: counter.hpp:40
void set(rep value) noexcept
Replace the counter's value.
Definition: counter.hpp:56
Provides a counter that observes the size of a table.
Definition: counter.hpp:107
constexpr SizeCounter(const T *table=nullptr) noexcept
Definition: counter.hpp:112
void observe(const T *table) noexcept
Definition: counter.hpp:118
Definition: common.hpp:77