NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
encoder.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_ENCODER_HPP
23 #define NDN_CXX_ENCODING_ENCODER_HPP
24 
26 
27 namespace ndn {
28 namespace encoding {
29 
37 class Encoder : noncopyable
38 {
39 public: // common interface between Encoder and Estimator
43  size_t
44  prependBytes(span<const uint8_t> bytes);
45 
49  size_t
50  appendBytes(span<const uint8_t> bytes);
51 
55  template<class Iterator>
56  size_t
57  prependRange(Iterator first, Iterator last);
58 
62  template<class Iterator>
63  size_t
64  appendRange(Iterator first, Iterator last);
65 
70  size_t
71  prependVarNumber(uint64_t number);
72 
77  size_t
78  appendVarNumber(uint64_t number);
79 
84  size_t
85  prependNonNegativeInteger(uint64_t integer);
86 
91  size_t
92  appendNonNegativeInteger(uint64_t integer);
93 
94 public: // unique interface to the Encoder
95  using value_type = Buffer::value_type;
96  using iterator = Buffer::iterator;
97  using const_iterator = Buffer::const_iterator;
98 
104  explicit
105  Encoder(size_t totalReserve = MAX_NDN_PACKET_SIZE, size_t reserveFromBack = 400);
106 
117  explicit
118  Encoder(const Block& block);
119 
129  void
130  reserve(size_t size, bool addInFront);
131 
137  void
138  reserveBack(size_t size);
139 
145  void
146  reserveFront(size_t size);
147 
151  size_t
152  capacity() const noexcept
153  {
154  return m_buffer->size();
155  }
156 
160  shared_ptr<Buffer>
161  getBuffer() const noexcept
162  {
163  return m_buffer;
164  }
165 
166 public: // accessors
170  iterator
171  begin() noexcept
172  {
173  return m_begin;
174  }
175 
180  begin() const noexcept
181  {
182  return m_begin;
183  }
184 
188  iterator
189  end() noexcept
190  {
191  return m_end;
192  }
193 
198  end() const noexcept
199  {
200  return m_end;
201  }
202 
206  uint8_t*
207  data() noexcept
208  {
209  return &*m_begin;
210  }
211 
215  const uint8_t*
216  data() const noexcept
217  {
218  return &*m_begin;
219  }
220 
224  size_t
225  size() const noexcept
226  {
227  return static_cast<size_t>(std::distance(m_begin, m_end));
228  }
229 
237  Block
238  block(bool verifyLength = true) const;
239 
240 private:
241  shared_ptr<Buffer> m_buffer;
242 
243  // invariant: m_begin always points to the position of last-written byte (if prepending data)
244  iterator m_begin;
245  // invariant: m_end always points to the position of next unwritten byte (if appending data)
246  iterator m_end;
247 };
248 
249 template<class Iterator>
250 size_t
251 Encoder::prependRange(Iterator first, Iterator last)
252 {
253  using ValueType = typename std::iterator_traits<Iterator>::value_type;
254  static_assert(sizeof(ValueType) == 1 && !std::is_same<ValueType, bool>::value, "");
255 
256  size_t length = std::distance(first, last);
257  reserveFront(length);
258 
259  std::advance(m_begin, -length);
260  std::copy(first, last, m_begin);
261  return length;
262 }
263 
264 template<class Iterator>
265 size_t
266 Encoder::appendRange(Iterator first, Iterator last)
267 {
268  using ValueType = typename std::iterator_traits<Iterator>::value_type;
269  static_assert(sizeof(ValueType) == 1 && !std::is_same<ValueType, bool>::value, "");
270 
271  size_t length = std::distance(first, last);
272  reserveBack(length);
273 
274  std::copy(first, last, m_end);
275  std::advance(m_end, length);
276  return length;
277 }
278 
279 } // namespace encoding
280 } // namespace ndn
281 
282 #endif // NDN_CXX_ENCODING_ENCODER_HPP
void reserveFront(size_t size)
Reserve at least isze bytes at the beginning of the underlying buffer.
Definition: encoder.cpp:52
Copyright (c) 2011-2015 Regents of the University of California.
iterator begin() noexcept
Returns an iterator pointing to the first byte of the encoded buffer.
Definition: encoder.hpp:171
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
Definition: encoder.hpp:266
size_t prependBytes(span< const uint8_t > bytes)
Prepend a sequence of bytes.
Definition: encoder.cpp:98
const uint8_t * data() const noexcept
Returns a pointer to the first byte of the encoded buffer.
Definition: encoder.hpp:216
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
void reserve(size_t size, bool addInFront)
Reserve size bytes for the underlying buffer.
Definition: encoder.cpp:65
size_t prependVarNumber(uint64_t number)
Prepend number encoded as a VAR-NUMBER in NDN-TLV format.
Definition: encoder.cpp:110
const_iterator begin() const noexcept
Returns an iterator pointing to the first byte of the encoded buffer.
Definition: encoder.hpp:180
uint8_t * data() noexcept
Returns a pointer to the first byte of the encoded buffer.
Definition: encoder.hpp:207
size_t size() const noexcept
Returns the size of the encoded buffer.
Definition: encoder.hpp:225
void reserveBack(size_t size)
Reserve at least size bytes at the back of the underlying buffer.
Definition: encoder.cpp:45
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:59
Buffer::iterator iterator
Definition: encoder.hpp:96
Buffer::value_type value_type
Definition: encoder.hpp:95
Buffer::const_iterator const_iterator
Definition: encoder.hpp:97
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
Definition: encoder.hpp:251
Helper class to perform TLV encoding.
Definition: encoder.hpp:37
shared_ptr< Buffer > getBuffer() const noexcept
Get underlying buffer.
Definition: encoder.hpp:161
iterator end() noexcept
Returns an iterator pointing to the past-the-end byte of the encoded buffer.
Definition: encoder.hpp:189
size_t capacity() const noexcept
Get size of the underlying buffer.
Definition: encoder.hpp:152
const_iterator end() const noexcept
Returns an iterator pointing to the past-the-end byte of the encoded buffer.
Definition: encoder.hpp:198
size_t prependNonNegativeInteger(uint64_t integer)
Prepend integer encoded as a NonNegativeInteger in NDN-TLV format.
Definition: encoder.cpp:164
size_t appendNonNegativeInteger(uint64_t integer)
Append integer encoded as a NonNegativeInteger in NDN-TLV format.
Definition: encoder.cpp:184
size_t appendVarNumber(uint64_t number)
Append number encoded as a VAR-NUMBER in NDN-TLV format.
Definition: encoder.cpp:137
Encoder(size_t totalReserve=MAX_NDN_PACKET_SIZE, size_t reserveFromBack=400)
Create instance of the encoder with the specified reserved sizes.
Definition: encoder.cpp:31
size_t appendBytes(span< const uint8_t > bytes)
Append a sequence of bytes.
Definition: encoder.cpp:104
const size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.
Definition: tlv.hpp:41