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-2023, 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  using rep = uint64_t;
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 
71 {
72 public:
77  operator++() noexcept
78  {
79  ++m_value;
80  return *this;
81  }
82 };
83 
89 class ByteCounter : public SimpleCounter
90 {
91 public:
96  operator+=(rep n) noexcept
97  {
98  m_value += n;
99  return *this;
100  }
101 };
102 
109 template<typename T>
110 class SizeCounter : noncopyable
111 {
112 public:
113  using rep = size_t;
114 
115  explicit constexpr
116  SizeCounter(const T* table = nullptr) noexcept
117  : m_table(table)
118  {
119  }
120 
121  void
122  observe(const T* table) noexcept
123  {
124  m_table = table;
125  }
126 
130  operator rep() const
131  {
132  BOOST_ASSERT(m_table != nullptr);
133  return m_table->size();
134  }
135 
136 private:
137  const T* m_table = nullptr;
138 };
139 
140 } // namespace nfd
141 
142 #endif // NFD_DAEMON_COMMON_COUNTER_HPP
Represents a counter of number of bytes.
Definition: counter.hpp:90
ByteCounter & operator+=(rep n) noexcept
Increase the counter.
Definition: counter.hpp:96
Represents a counter of number of packets.
Definition: counter.hpp:71
PacketCounter & operator++() noexcept
Increment the counter by one.
Definition: counter.hpp:77
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:111
constexpr SizeCounter(const T *table=nullptr) noexcept
Definition: counter.hpp:116
void observe(const T *table) noexcept
Definition: counter.hpp:122
-status-http-server
Definition: common.hpp:71