estimator.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2022 Regents of the University of California.
4  *
5  * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6  *
7  * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8  * terms of the GNU Lesser General Public License as published by the Free Software
9  * Foundation, either version 3 of the License, or (at your option) any later version.
10  *
11  * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13  * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14  *
15  * You should have received copies of the GNU General Public License and GNU Lesser
16  * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17  * <http://www.gnu.org/licenses/>.
18  *
19  * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20  */
21 
22 #ifndef NDN_CXX_ENCODING_ESTIMATOR_HPP
23 #define NDN_CXX_ENCODING_ESTIMATOR_HPP
24 
26 
27 namespace ndn {
28 namespace encoding {
29 
37 class Estimator : noncopyable
38 {
39 public: // common interface between Encoder and Estimator
43  constexpr size_t
44  prependBytes(span<const uint8_t> bytes) const noexcept
45  {
46  return bytes.size();
47  }
48 
52  constexpr size_t
53  appendBytes(span<const uint8_t> bytes) const noexcept
54  {
55  return bytes.size();
56  }
57 
62  [[deprecated("use prependBytes()")]]
63  constexpr size_t
64  prependByte(uint8_t) const noexcept
65  {
66  return 1;
67  }
68 
73  [[deprecated("use appendBytes()")]]
74  constexpr size_t
75  appendByte(uint8_t) const noexcept
76  {
77  return 1;
78  }
79 
84  [[deprecated("use prependBytes()")]]
85  constexpr size_t
86  prependByteArray(const uint8_t*, size_t length) const noexcept
87  {
88  return length;
89  }
90 
95  [[deprecated("use appendBytes()")]]
96  constexpr size_t
97  appendByteArray(const uint8_t*, size_t length) const noexcept
98  {
99  return length;
100  }
101 
105  template<class Iterator>
106  constexpr size_t
107  prependRange(Iterator first, Iterator last) const noexcept
108  {
109  return std::distance(first, last);
110  }
111 
115  template<class Iterator>
116  constexpr size_t
117  appendRange(Iterator first, Iterator last) const noexcept
118  {
119  return std::distance(first, last);
120  }
121 
125  constexpr size_t
126  prependVarNumber(uint64_t n) const noexcept
127  {
128  return tlv::sizeOfVarNumber(n);
129  }
130 
134  constexpr size_t
135  appendVarNumber(uint64_t n) const noexcept
136  {
137  return tlv::sizeOfVarNumber(n);
138  }
139 
143  constexpr size_t
144  prependNonNegativeInteger(uint64_t n) const noexcept
145  {
147  }
148 
152  constexpr size_t
153  appendNonNegativeInteger(uint64_t n) const noexcept
154  {
156  }
157 
162  [[deprecated("use encoding::prependBinaryBlock()")]]
163  constexpr size_t
164  prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) const noexcept
165  {
166  return tlv::sizeOfVarNumber(type) + tlv::sizeOfVarNumber(arraySize) + arraySize;
167  }
168 
173  [[deprecated]]
174  constexpr size_t
175  appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) const noexcept
176  {
177  return tlv::sizeOfVarNumber(type) + tlv::sizeOfVarNumber(arraySize) + arraySize;
178  }
179 
184  [[deprecated("use encoding::prependBlock()")]]
185  size_t
186  prependBlock(const Block& block) const;
187 
192  [[deprecated]]
193  size_t
194  appendBlock(const Block& block) const;
195 };
196 
197 } // namespace encoding
198 } // namespace ndn
199 
200 #endif // NDN_CXX_ENCODING_ESTIMATOR_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
Helper class to estimate size of TLV encoding.
Definition: estimator.hpp:38
size_t appendBlock(const Block &block) const
Append TLV block block.
Definition: estimator.cpp:41
constexpr size_t prependBytes(span< const uint8_t > bytes) const noexcept
Prepend a sequence of bytes.
Definition: estimator.hpp:44
constexpr size_t prependByte(uint8_t) const noexcept
Prepend a single byte.
Definition: estimator.hpp:64
constexpr size_t appendRange(Iterator first, Iterator last) const noexcept
Append bytes from the range [first, last)
Definition: estimator.hpp:117
constexpr size_t prependByteArrayBlock(uint32_t type, const uint8_t *array, size_t arraySize) const noexcept
Prepend TLV block of type type and value from buffer array of size arraySize.
Definition: estimator.hpp:164
constexpr size_t prependNonNegativeInteger(uint64_t n) const noexcept
Prepend n in NonNegativeInteger encoding.
Definition: estimator.hpp:144
constexpr size_t appendByteArrayBlock(uint32_t type, const uint8_t *array, size_t arraySize) const noexcept
Append TLV block of type type and value from buffer array of size arraySize.
Definition: estimator.hpp:175
constexpr size_t appendByte(uint8_t) const noexcept
Append a single byte.
Definition: estimator.hpp:75
constexpr size_t appendNonNegativeInteger(uint64_t n) const noexcept
Append n in NonNegativeInteger encoding.
Definition: estimator.hpp:153
constexpr size_t prependByteArray(const uint8_t *, size_t length) const noexcept
Prepend a byte array array of length length.
Definition: estimator.hpp:86
constexpr size_t appendBytes(span< const uint8_t > bytes) const noexcept
Append a sequence of bytes.
Definition: estimator.hpp:53
constexpr size_t appendVarNumber(uint64_t n) const noexcept
Append n in VarNumber encoding.
Definition: estimator.hpp:135
constexpr size_t appendByteArray(const uint8_t *, size_t length) const noexcept
Append a byte array array of length length.
Definition: estimator.hpp:97
constexpr size_t prependRange(Iterator first, Iterator last) const noexcept
Prepend bytes from the range [first, last)
Definition: estimator.hpp:107
size_t prependBlock(const Block &block) const
Prepend TLV block block.
Definition: estimator.cpp:30
constexpr size_t prependVarNumber(uint64_t n) const noexcept
Prepend n in VarNumber encoding.
Definition: estimator.hpp:126
constexpr size_t sizeOfVarNumber(uint64_t number) noexcept
Get the number of bytes necessary to hold the value of number encoded as VAR-NUMBER.
Definition: tlv.hpp:454
constexpr size_t sizeOfNonNegativeInteger(uint64_t integer) noexcept
Get the number of bytes necessary to hold the value of integer encoded as NonNegativeInteger.
Definition: tlv.hpp:506
Definition: data.cpp:25