blob: 3a678ec87889c1900c72d7c4d7565c2db01b78b3 [file] [log] [blame]
Junxiao Shi57df2882015-11-11 06:12:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Eric Newberry73bcad32017-04-25 17:57:35 -07003 * Copyright (c) 2014-2017, Regents of the University of California,
Junxiao Shi57df2882015-11-11 06:12:35 -07004 * 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_CORE_COUNTER_HPP
27#define NFD_CORE_COUNTER_HPP
28
29#include "common.hpp"
30
31namespace nfd {
32
33/** \brief represents a counter that encloses an integer value
34 *
35 * SimpleCounter is noncopyable, because increment should be called on the counter,
36 * not a copy of it; it's implicitly convertible to an integral type to be observed
37 */
38class SimpleCounter
39{
40public:
41 typedef uint64_t rep;
42
43 constexpr
44 SimpleCounter()
45 : m_value(0)
46 {
47 }
48
49 SimpleCounter(const SimpleCounter&) = delete;
50
51 SimpleCounter&
52 operator=(const SimpleCounter&) = delete;
53
54 /** \brief observe the counter
55 */
56 operator rep() const
57 {
58 return m_value;
59 }
60
61 /** \brief replace the counter value
62 */
63 void
64 set(rep value)
65 {
66 m_value = value;
67 }
68
69protected:
70 rep m_value;
71};
72
73/** \brief represents a counter of number of packets
74 *
75 * \warning The counter value may wrap after exceeding the range of underlying integer type.
76 */
77class PacketCounter : public SimpleCounter
78{
79public:
80 /** \brief increment the counter by one
81 */
82 PacketCounter&
83 operator++()
84 {
85 ++m_value;
86 return *this;
87 }
88 // postfix ++ operator is not provided because it's not needed
89};
90
91/** \brief represents a counter of number of bytes
92 *
93 * \warning The counter value may wrap after exceeding the range of underlying integer type.
94 */
95class ByteCounter : public SimpleCounter
96{
97public:
98 /** \brief increase the counter
99 */
100 ByteCounter&
101 operator+=(rep n)
102 {
103 m_value += n;
104 return *this;
105 }
106};
107
108/** \brief provides a counter that observes the size of a table
109 * \tparam T a type that provides a size() const member function
Eric Newberry73bcad32017-04-25 17:57:35 -0700110 *
111 * if table not specified in constructor, it can be added later by invoking observe()
Junxiao Shi57df2882015-11-11 06:12:35 -0700112 */
113template<typename T>
114class SizeCounter
115{
116public:
Eric Newberry73bcad32017-04-25 17:57:35 -0700117 typedef size_t Rep;
Junxiao Shi57df2882015-11-11 06:12:35 -0700118
Eric Newberry73bcad32017-04-25 17:57:35 -0700119 explicit constexpr
120 SizeCounter(const T* table = nullptr)
Junxiao Shi57df2882015-11-11 06:12:35 -0700121 : m_table(table)
122 {
123 }
124
125 SizeCounter(const SizeCounter&) = delete;
126
127 SizeCounter&
128 operator=(const SizeCounter&) = delete;
129
Eric Newberry73bcad32017-04-25 17:57:35 -0700130 void
131 observe(const T* table)
132 {
133 m_table = table;
134 }
135
Junxiao Shi57df2882015-11-11 06:12:35 -0700136 /** \brief observe the counter
137 */
Eric Newberry73bcad32017-04-25 17:57:35 -0700138 operator Rep() const
Junxiao Shi57df2882015-11-11 06:12:35 -0700139 {
Eric Newberry73bcad32017-04-25 17:57:35 -0700140 BOOST_ASSERT(m_table != nullptr);
141 return m_table->size();
Junxiao Shi57df2882015-11-11 06:12:35 -0700142 }
143
144private:
Eric Newberry73bcad32017-04-25 17:57:35 -0700145 const T* m_table;
Junxiao Shi57df2882015-11-11 06:12:35 -0700146};
147
148} // namespace nfd
149
150#endif // NFD_CORE_COUNTER_HPP