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-2023 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 #include <algorithm>
28 
29 namespace ndn::encoding {
30 
38 class Encoder : noncopyable
39 {
40 public: // common interface between Encoder and Estimator
44  size_t
45  prependBytes(span<const uint8_t> bytes);
46 
50  size_t
51  appendBytes(span<const uint8_t> bytes);
52 
56  template<class Iterator>
57  size_t
58  prependRange(Iterator first, Iterator last);
59 
63  template<class Iterator>
64  size_t
65  appendRange(Iterator first, Iterator last);
66 
71  size_t
72  prependVarNumber(uint64_t number);
73 
78  size_t
79  appendVarNumber(uint64_t number);
80 
85  size_t
86  prependNonNegativeInteger(uint64_t integer);
87 
92  size_t
93  appendNonNegativeInteger(uint64_t integer);
94 
95 public: // unique interface to the Encoder
96  using value_type = Buffer::value_type;
97  using iterator = Buffer::iterator;
98  using const_iterator = Buffer::const_iterator;
99 
105  explicit
106  Encoder(size_t totalReserve = MAX_NDN_PACKET_SIZE, size_t reserveFromBack = 400);
107 
118  explicit
119  Encoder(const Block& block);
120 
130  void
131  reserve(size_t size, bool addInFront);
132 
138  void
139  reserveBack(size_t size);
140 
146  void
147  reserveFront(size_t size);
148 
152  size_t
153  capacity() const noexcept
154  {
155  return m_buffer->size();
156  }
157 
161  shared_ptr<Buffer>
162  getBuffer() const noexcept
163  {
164  return m_buffer;
165  }
166 
167 public: // accessors
171  iterator
172  begin() noexcept
173  {
174  return m_begin;
175  }
176 
181  begin() const noexcept
182  {
183  return m_begin;
184  }
185 
189  iterator
190  end() noexcept
191  {
192  return m_end;
193  }
194 
199  end() const noexcept
200  {
201  return m_end;
202  }
203 
207  uint8_t*
208  data() noexcept
209  {
210  return &*m_begin;
211  }
212 
216  const uint8_t*
217  data() const noexcept
218  {
219  return &*m_begin;
220  }
221 
225  size_t
226  size() const noexcept
227  {
228  return static_cast<size_t>(std::distance(m_begin, m_end));
229  }
230 
238  Block
239  block(bool verifyLength = true) const;
240 
241 private:
242  shared_ptr<Buffer> m_buffer;
243 
244  // invariant: m_begin always points to the position of last-written byte (if prepending data)
245  iterator m_begin;
246  // invariant: m_end always points to the position of next unwritten byte (if appending data)
247  iterator m_end;
248 };
249 
250 template<class Iterator>
251 size_t
252 Encoder::prependRange(Iterator first, Iterator last)
253 {
254  using ValueType = typename std::iterator_traits<Iterator>::value_type;
255  static_assert(sizeof(ValueType) == 1 && !std::is_same_v<ValueType, bool>);
256 
257  size_t length = std::distance(first, last);
258  reserveFront(length);
259 
260  std::advance(m_begin, -length);
261  std::copy(first, last, m_begin);
262  return length;
263 }
264 
265 template<class Iterator>
266 size_t
267 Encoder::appendRange(Iterator first, Iterator last)
268 {
269  using ValueType = typename std::iterator_traits<Iterator>::value_type;
270  static_assert(sizeof(ValueType) == 1 && !std::is_same_v<ValueType, bool>);
271 
272  size_t length = std::distance(first, last);
273  reserveBack(length);
274 
275  std::copy(first, last, m_end);
276  std::advance(m_end, length);
277  return length;
278 }
279 
280 } // namespace ndn::encoding
281 
282 #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:39
void reserve(size_t size, bool addInFront)
Reserve size bytes for the underlying buffer.
Definition: encoder.cpp:64
size_t size() const noexcept
Returns the size of the encoded buffer.
Definition: encoder.hpp:226
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:30
Buffer::value_type value_type
Definition: encoder.hpp:96
size_t appendBytes(span< const uint8_t > bytes)
Append a sequence of bytes.
Definition: encoder.cpp:103
const_iterator end() const noexcept
Returns an iterator pointing to the past-the-end byte of the encoded buffer.
Definition: encoder.hpp:199
void reserveFront(size_t size)
Reserve at least isze bytes at the beginning of the underlying buffer.
Definition: encoder.cpp:51
Buffer::const_iterator const_iterator
Definition: encoder.hpp:98
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:58
shared_ptr< Buffer > getBuffer() const noexcept
Get underlying buffer.
Definition: encoder.hpp:162
size_t prependBytes(span< const uint8_t > bytes)
Prepend a sequence of bytes.
Definition: encoder.cpp:97
size_t capacity() const noexcept
Get size of the underlying buffer.
Definition: encoder.hpp:153
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
Definition: encoder.hpp:252
iterator begin() noexcept
Returns an iterator pointing to the first byte of the encoded buffer.
Definition: encoder.hpp:172
size_t prependVarNumber(uint64_t number)
Prepend number encoded as a VAR-NUMBER in NDN-TLV format.
Definition: encoder.cpp:109
size_t prependNonNegativeInteger(uint64_t integer)
Prepend integer encoded as a NonNegativeInteger in NDN-TLV format.
Definition: encoder.cpp:163
Buffer::iterator iterator
Definition: encoder.hpp:97
const_iterator begin() const noexcept
Returns an iterator pointing to the first byte of the encoded buffer.
Definition: encoder.hpp:181
const uint8_t * data() const noexcept
Returns a pointer to the first byte of the encoded buffer.
Definition: encoder.hpp:217
size_t appendNonNegativeInteger(uint64_t integer)
Append integer encoded as a NonNegativeInteger in NDN-TLV format.
Definition: encoder.cpp:183
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
Definition: encoder.hpp:267
size_t appendVarNumber(uint64_t number)
Append number encoded as a VAR-NUMBER in NDN-TLV format.
Definition: encoder.cpp:136
void reserveBack(size_t size)
Reserve at least size bytes at the back of the underlying buffer.
Definition: encoder.cpp:44
uint8_t * data() noexcept
Returns a pointer to the first byte of the encoded buffer.
Definition: encoder.hpp:208
iterator end() noexcept
Returns an iterator pointing to the past-the-end byte of the encoded buffer.
Definition: encoder.hpp:190
constexpr size_t MAX_NDN_PACKET_SIZE
Practical size limit of a network-layer packet.
Definition: tlv.hpp:41