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 
56  [[deprecated("use prependBytes()")]]
57  size_t
58  prependByte(uint8_t value)
59  {
60  return prependBytes({value});
61  }
62 
67  [[deprecated("use appendBytes()")]]
68  size_t
69  appendByte(uint8_t value)
70  {
71  return appendBytes({value});
72  }
73 
78  [[deprecated("use prependBytes()")]]
79  size_t
80  prependByteArray(const uint8_t* array, size_t length)
81  {
82  return prependBytes({array, length});
83  }
84 
89  [[deprecated("use appendBytes()")]]
90  size_t
91  appendByteArray(const uint8_t* array, size_t length)
92  {
93  return appendBytes({array, length});
94  }
95 
99  template<class Iterator>
100  size_t
101  prependRange(Iterator first, Iterator last);
102 
106  template<class Iterator>
107  size_t
108  appendRange(Iterator first, Iterator last);
109 
114  size_t
115  prependVarNumber(uint64_t number);
116 
121  size_t
122  appendVarNumber(uint64_t number);
123 
128  size_t
129  prependNonNegativeInteger(uint64_t integer);
130 
135  size_t
136  appendNonNegativeInteger(uint64_t integer);
137 
142  [[deprecated("use encoding::prependBinaryBlock()")]]
143  size_t
144  prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
145 
150  [[deprecated]]
151  size_t
152  appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
153 
158  [[deprecated("use encoding::prependBlock()")]]
159  size_t
160  prependBlock(const Block& block);
161 
166  [[deprecated]]
167  size_t
168  appendBlock(const Block& block);
169 
170 public: // unique interface to the Encoder
171  using value_type = Buffer::value_type;
172  using iterator = Buffer::iterator;
173  using const_iterator = Buffer::const_iterator;
174 
180  explicit
181  Encoder(size_t totalReserve = MAX_NDN_PACKET_SIZE, size_t reserveFromBack = 400);
182 
193  explicit
194  Encoder(const Block& block);
195 
205  void
206  reserve(size_t size, bool addInFront);
207 
213  void
214  reserveBack(size_t size);
215 
221  void
222  reserveFront(size_t size);
223 
227  size_t
228  capacity() const noexcept
229  {
230  return m_buffer->size();
231  }
232 
236  shared_ptr<Buffer>
237  getBuffer() const noexcept
238  {
239  return m_buffer;
240  }
241 
242 public: // accessors
246  iterator
247  begin() noexcept
248  {
249  return m_begin;
250  }
251 
256  begin() const noexcept
257  {
258  return m_begin;
259  }
260 
264  iterator
265  end() noexcept
266  {
267  return m_end;
268  }
269 
274  end() const noexcept
275  {
276  return m_end;
277  }
278 
282  uint8_t*
283  data() noexcept
284  {
285  return &*m_begin;
286  }
287 
291  const uint8_t*
292  data() const noexcept
293  {
294  return &*m_begin;
295  }
296 
300  [[deprecated("use data()")]]
301  uint8_t*
302  buf() noexcept
303  {
304  return data();
305  }
306 
310  [[deprecated("use data()")]]
311  const uint8_t*
312  buf() const noexcept
313  {
314  return data();
315  }
316 
320  size_t
321  size() const noexcept
322  {
323  return static_cast<size_t>(std::distance(m_begin, m_end));
324  }
325 
333  Block
334  block(bool verifyLength = true) const;
335 
336 private:
337  shared_ptr<Buffer> m_buffer;
338 
339  // invariant: m_begin always points to the position of last-written byte (if prepending data)
340  iterator m_begin;
341  // invariant: m_end always points to the position of next unwritten byte (if appending data)
342  iterator m_end;
343 };
344 
345 template<class Iterator>
346 size_t
347 Encoder::prependRange(Iterator first, Iterator last)
348 {
349  using ValueType = typename std::iterator_traits<Iterator>::value_type;
350  static_assert(sizeof(ValueType) == 1 && !std::is_same<ValueType, bool>::value, "");
351 
352  size_t length = std::distance(first, last);
353  reserveFront(length);
354 
355  std::advance(m_begin, -length);
356  std::copy(first, last, m_begin);
357  return length;
358 }
359 
360 template<class Iterator>
361 size_t
362 Encoder::appendRange(Iterator first, Iterator last)
363 {
364  using ValueType = typename std::iterator_traits<Iterator>::value_type;
365  static_assert(sizeof(ValueType) == 1 && !std::is_same<ValueType, bool>::value, "");
366 
367  size_t length = std::distance(first, last);
368  reserveBack(length);
369 
370  std::copy(first, last, m_end);
371  std::advance(m_end, length);
372  return length;
373 }
374 
375 } // namespace encoding
376 } // namespace ndn
377 
378 #endif // NDN_CXX_ENCODING_ENCODER_HPP
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
Helper class to perform TLV encoding.
Definition: encoder.hpp:38
void reserve(size_t size, bool addInFront)
Reserve size bytes for the underlying buffer.
Definition: encoder.cpp:65
size_t size() const noexcept
Returns the size of the encoded buffer.
Definition: encoder.hpp:321
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
Buffer::value_type value_type
Definition: encoder.hpp:171
size_t appendBytes(span< const uint8_t > bytes)
Append a sequence of bytes.
Definition: encoder.cpp:104
const_iterator end() const noexcept
Returns an iterator pointing to the past-the-end byte of the encoded buffer.
Definition: encoder.hpp:274
void reserveFront(size_t size)
Reserve at least isze bytes at the beginning of the underlying buffer.
Definition: encoder.cpp:52
size_t appendByte(uint8_t value)
Append a byte.
Definition: encoder.hpp:69
Buffer::const_iterator const_iterator
Definition: encoder.hpp:173
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:59
uint8_t * buf() noexcept
Definition: encoder.hpp:302
shared_ptr< Buffer > getBuffer() const noexcept
Get underlying buffer.
Definition: encoder.hpp:237
const uint8_t * buf() const noexcept
Definition: encoder.hpp:312
size_t prependBytes(span< const uint8_t > bytes)
Prepend a sequence of bytes.
Definition: encoder.cpp:98
size_t capacity() const noexcept
Get size of the underlying buffer.
Definition: encoder.hpp:228
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
Definition: encoder.hpp:347
size_t prependByteArray(const uint8_t *array, size_t length)
Prepend a byte array array of length length.
Definition: encoder.hpp:80
iterator begin() noexcept
Returns an iterator pointing to the first byte of the encoded buffer.
Definition: encoder.hpp:247
size_t prependVarNumber(uint64_t number)
Prepend number encoded as a VAR-NUMBER in NDN-TLV format.
Definition: encoder.cpp:110
size_t prependNonNegativeInteger(uint64_t integer)
Prepend integer encoded as a NonNegativeInteger in NDN-TLV format.
Definition: encoder.cpp:164
Buffer::iterator iterator
Definition: encoder.hpp:172
const_iterator begin() const noexcept
Returns an iterator pointing to the first byte of the encoded buffer.
Definition: encoder.hpp:256
size_t appendByteArrayBlock(uint32_t type, const uint8_t *array, size_t arraySize)
Append TLV block of type type and value from buffer array of size arraySize.
Definition: encoder.cpp:214
size_t prependByte(uint8_t value)
Prepend a byte.
Definition: encoder.hpp:58
const uint8_t * data() const noexcept
Returns a pointer to the first byte of the encoded buffer.
Definition: encoder.hpp:292
size_t appendBlock(const Block &block)
Append TLV block block.
Definition: encoder.cpp:237
size_t appendNonNegativeInteger(uint64_t integer)
Append integer encoded as a NonNegativeInteger in NDN-TLV format.
Definition: encoder.cpp:184
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
Definition: encoder.hpp:362
size_t appendVarNumber(uint64_t number)
Append number encoded as a VAR-NUMBER in NDN-TLV format.
Definition: encoder.cpp:137
size_t appendByteArray(const uint8_t *array, size_t length)
Append a byte array array of length length.
Definition: encoder.hpp:91
void reserveBack(size_t size)
Reserve at least size bytes at the back of the underlying buffer.
Definition: encoder.cpp:45
uint8_t * data() noexcept
Returns a pointer to the first byte of the encoded buffer.
Definition: encoder.hpp:283
size_t prependBlock(const Block &block)
Prepend TLV block block.
Definition: encoder.cpp:226
size_t prependByteArrayBlock(uint32_t type, const uint8_t *array, size_t arraySize)
Prepend TLV block of type type and value from buffer array of size arraySize.
Definition: encoder.cpp:204
iterator end() noexcept
Returns an iterator pointing to the past-the-end byte of the encoded buffer.
Definition: encoder.hpp:265
Definition: data.cpp:25
const size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.
Definition: tlv.hpp:41