Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2014-2017, Regents of the University of California, |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 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 | |
| 31 | namespace 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 | */ |
| 38 | class SimpleCounter |
| 39 | { |
| 40 | public: |
| 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 | |
| 69 | protected: |
| 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 | */ |
| 77 | class PacketCounter : public SimpleCounter |
| 78 | { |
| 79 | public: |
| 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 | */ |
| 95 | class ByteCounter : public SimpleCounter |
| 96 | { |
| 97 | public: |
| 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 Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame^] | 110 | * |
| 111 | * if table not specified in constructor, it can be added later by invoking observe() |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 112 | */ |
| 113 | template<typename T> |
| 114 | class SizeCounter |
| 115 | { |
| 116 | public: |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame^] | 117 | typedef size_t Rep; |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 118 | |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame^] | 119 | explicit constexpr |
| 120 | SizeCounter(const T* table = nullptr) |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 121 | : m_table(table) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | SizeCounter(const SizeCounter&) = delete; |
| 126 | |
| 127 | SizeCounter& |
| 128 | operator=(const SizeCounter&) = delete; |
| 129 | |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame^] | 130 | void |
| 131 | observe(const T* table) |
| 132 | { |
| 133 | m_table = table; |
| 134 | } |
| 135 | |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 136 | /** \brief observe the counter |
| 137 | */ |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame^] | 138 | operator Rep() const |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 139 | { |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame^] | 140 | BOOST_ASSERT(m_table != nullptr); |
| 141 | return m_table->size(); |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | private: |
Eric Newberry | 73bcad3 | 2017-04-25 17:57:35 -0700 | [diff] [blame^] | 145 | const T* m_table; |
Junxiao Shi | 57df288 | 2015-11-11 06:12:35 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | } // namespace nfd |
| 149 | |
| 150 | #endif // NFD_CORE_COUNTER_HPP |