blob: bb50c663b82c91a9620c4f2def9b539cb646ab52 [file] [log] [blame]
Junxiao Shi57df2882015-11-11 06:12:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, 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_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
110 */
111template<typename T>
112class SizeCounter
113{
114public:
115 typedef size_t rep;
116
117 constexpr
118 SizeCounter(const T& table)
119 : m_table(table)
120 {
121 }
122
123 SizeCounter(const SizeCounter&) = delete;
124
125 SizeCounter&
126 operator=(const SizeCounter&) = delete;
127
128 /** \brief observe the counter
129 */
130 operator rep() const
131 {
132 return m_table.size();
133 }
134
135private:
136 const T& m_table;
137};
138
139} // namespace nfd
140
141#endif // NFD_CORE_COUNTER_HPP