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-2017 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_ENCODING_ENCODER_HPP
23 #define NDN_ENCODING_ENCODER_HPP
24 
25 #include "block.hpp"
26 
27 namespace ndn {
28 namespace encoding {
29 
35 class Encoder
36 {
37 public: // common interface between Encoder and Estimator
43  explicit
44  Encoder(size_t totalReserve = MAX_NDN_PACKET_SIZE, size_t reserveFromBack = 400);
45 
46  Encoder(const Encoder&) = delete;
47 
48  Encoder&
49  operator=(const Encoder&) = delete;
50 
54  size_t
55  prependByte(uint8_t value);
56 
60  size_t
61  appendByte(uint8_t value);
62 
66  size_t
67  prependByteArray(const uint8_t* array, size_t length);
68 
72  size_t
73  appendByteArray(const uint8_t* array, size_t length);
74 
78  template<class Iterator>
79  size_t
80  prependRange(Iterator first, Iterator last);
81 
85  template<class Iterator>
86  size_t
87  appendRange(Iterator first, Iterator last);
88 
93  size_t
94  prependVarNumber(uint64_t varNumber);
95 
100  size_t
101  appendVarNumber(uint64_t varNumber);
102 
107  size_t
108  prependNonNegativeInteger(uint64_t integer);
109 
114  size_t
115  appendNonNegativeInteger(uint64_t integer);
116 
120  size_t
121  prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
122 
126  size_t
127  appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
128 
132  size_t
133  prependBlock(const Block& block);
134 
138  size_t
139  appendBlock(const Block& block);
140 
141 public: // unique interface to the Encoder
142  using value_type = Buffer::value_type;
143  using iterator = Buffer::iterator;
144  using const_iterator = Buffer::const_iterator;
145 
156  explicit
157  Encoder(const Block& block);
158 
168  void
169  reserve(size_t size, bool addInFront);
170 
176  void
177  reserveBack(size_t size);
178 
184  void
185  reserveFront(size_t size);
186 
190  size_t
191  capacity() const;
192 
196  shared_ptr<Buffer>
197  getBuffer();
198 
199 public: // accessors
200 
204  iterator
205  begin();
206 
210  iterator
211  end();
212 
217  begin() const;
218 
220  end() const;
221 
225  uint8_t*
226  buf();
227 
231  const uint8_t*
232  buf() const;
233 
237  size_t
238  size() const;
239 
247  Block
248  block(bool verifyLength = true) const;
249 
250 private:
251  shared_ptr<Buffer> m_buffer;
252 
253  // invariant: m_begin always points to the position of last-written byte (if prepending data)
254  iterator m_begin;
255  // invariant: m_end always points to the position of next unwritten byte (if appending data)
256  iterator m_end;
257 };
258 
259 
260 inline size_t
262 {
263  return m_end - m_begin;
264 }
265 
266 inline shared_ptr<Buffer>
268 {
269  return m_buffer;
270 }
271 
272 inline size_t
274 {
275  return m_buffer->size();
276 }
277 
278 inline Buffer::iterator
280 {
281  return m_begin;
282 }
283 
284 inline Buffer::iterator
286 {
287  return m_end;
288 }
289 
290 inline Buffer::const_iterator
292 {
293  return m_begin;
294 }
295 
296 inline Buffer::const_iterator
298 {
299  return m_end;
300 }
301 
302 inline uint8_t*
304 {
305  return &(*m_begin);
306 }
307 
308 inline const uint8_t*
310 {
311  return &(*m_begin);
312 }
313 
314 template<class Iterator>
315 inline size_t
316 Encoder::prependRange(Iterator first, Iterator last)
317 {
318  using ValueType = typename std::iterator_traits<Iterator>::value_type;
319  static_assert(sizeof(ValueType) == 1 && !std::is_same<ValueType, bool>::value, "");
320 
321  size_t length = std::distance(first, last);
322  reserveFront(length);
323 
324  m_begin -= length;
325  std::copy(first, last, m_begin);
326  return length;
327 }
328 
329 
330 template<class Iterator>
331 inline size_t
332 Encoder::appendRange(Iterator first, Iterator last)
333 {
334  using ValueType = typename std::iterator_traits<Iterator>::value_type;
335  static_assert(sizeof(ValueType) == 1 && !std::is_same<ValueType, bool>::value, "");
336 
337  size_t length = std::distance(first, last);
338  reserveBack(length);
339 
340  std::copy(first, last, m_end);
341  m_end += length;
342  return length;
343 }
344 
345 
346 } // namespace encoding
347 } // namespace ndn
348 
349 #endif // NDN_ENCODING_ENCODER_HPP
size_t prependByteArray(const uint8_t *array, size_t length)
Prepend a byte array array of length length.
Definition: encoder.cpp:125
void reserveFront(size_t size)
Reserve at least isze bytes at the beginning of the underlying buffer.
Definition: encoder.cpp:52
size_t appendVarNumber(uint64_t varNumber)
Prepend VarNumber varNumber of NDN TLV encoding.
Definition: encoder.cpp:173
iterator begin()
Get an iterator pointing to the first byte of the encoded buffer.
Definition: encoder.hpp:279
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:66
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
Definition: encoder.hpp:332
size_t capacity() const
Get size of the underlying buffer.
Definition: encoder.hpp:273
size_t size() const
Get size of the encoded buffer.
Definition: encoder.hpp:261
size_t prependByte(uint8_t value)
Prepend a byte.
Definition: encoder.cpp:104
iterator end()
Get an iterator pointing to the past-the-end byte of the encoded buffer.
Definition: encoder.hpp:285
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
void reserve(size_t size, bool addInFront)
Reserve size bytes for the underlying buffer.
Definition: encoder.cpp:68
void reserveBack(size_t size)
Reserve at least size bytes at the back of the underlying buffer.
Definition: encoder.cpp:45
size_t prependVarNumber(uint64_t varNumber)
Prepend VarNumber varNumber of NDN TLV encoding.
Definition: encoder.cpp:146
size_t prependBlock(const Block &block)
Prepend TLV block block.
Definition: encoder.cpp:261
size_t appendBlock(const Block &block)
Append TLV block block.
Definition: encoder.cpp:272
Buffer::iterator iterator
Definition: encoder.hpp:143
Buffer::value_type value_type
Definition: encoder.hpp:142
Buffer::const_iterator const_iterator
Definition: encoder.hpp:144
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
Definition: encoder.hpp:316
Helper class to perform TLV encoding Interface of this class (mostly) matches interface of Estimator ...
Definition: encoder.hpp:35
shared_ptr< Buffer > getBuffer()
Get underlying buffer.
Definition: encoder.hpp:267
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:60
size_t appendByte(uint8_t value)
Append a byte.
Definition: encoder.cpp:114
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:241
Encoder & operator=(const Encoder &)=delete
size_t appendByteArray(const uint8_t *array, size_t length)
Append a byte array array of length length.
Definition: encoder.cpp:135
uint8_t * buf()
Get a pointer to the first byte of the encoded buffer.
Definition: encoder.hpp:303
size_t prependNonNegativeInteger(uint64_t integer)
Prepend non-negative integer integer of NDN TLV encoding.
Definition: encoder.cpp:201
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:251
size_t appendNonNegativeInteger(uint64_t integer)
Append non-negative integer integer of NDN TLV encoding.
Definition: encoder.cpp:221
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:27
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size