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-2018 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 
26 
27 namespace ndn {
28 namespace encoding {
29 
35 class Encoder : noncopyable
36 {
37 public: // common interface between Encoder and Estimator
41  size_t
42  prependByte(uint8_t value);
43 
47  size_t
48  appendByte(uint8_t value);
49 
53  size_t
54  prependByteArray(const uint8_t* array, size_t length);
55 
59  size_t
60  appendByteArray(const uint8_t* array, size_t length);
61 
65  template<class Iterator>
66  size_t
67  prependRange(Iterator first, Iterator last);
68 
72  template<class Iterator>
73  size_t
74  appendRange(Iterator first, Iterator last);
75 
80  size_t
81  prependVarNumber(uint64_t varNumber);
82 
87  size_t
88  appendVarNumber(uint64_t varNumber);
89 
94  size_t
95  prependNonNegativeInteger(uint64_t integer);
96 
101  size_t
102  appendNonNegativeInteger(uint64_t integer);
103 
107  size_t
108  prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
109 
113  size_t
114  appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
115 
119  size_t
120  prependBlock(const Block& block);
121 
125  size_t
126  appendBlock(const Block& block);
127 
128 public: // unique interface to the Encoder
129  using value_type = Buffer::value_type;
131  using const_iterator = Buffer::const_iterator;
132 
138  explicit
139  Encoder(size_t totalReserve = MAX_NDN_PACKET_SIZE, size_t reserveFromBack = 400);
140 
151  explicit
152  Encoder(const Block& block);
153 
163  void
164  reserve(size_t size, bool addInFront);
165 
171  void
172  reserveBack(size_t size);
173 
179  void
180  reserveFront(size_t size);
181 
185  size_t
186  capacity() const noexcept;
187 
191  shared_ptr<Buffer>
192  getBuffer() const noexcept;
193 
194 public: // accessors
198  iterator
199  begin();
200 
204  iterator
205  end();
206 
211  begin() const;
212 
217  end() const;
218 
222  uint8_t*
223  buf();
224 
228  const uint8_t*
229  buf() const;
230 
234  size_t
235  size() const noexcept;
236 
244  Block
245  block(bool verifyLength = true) const;
246 
247 private:
248  shared_ptr<Buffer> m_buffer;
249 
250  // invariant: m_begin always points to the position of last-written byte (if prepending data)
251  iterator m_begin;
252  // invariant: m_end always points to the position of next unwritten byte (if appending data)
253  iterator m_end;
254 };
255 
256 inline size_t
257 Encoder::size() const noexcept
258 {
259  return m_end - m_begin;
260 }
261 
262 inline shared_ptr<Buffer>
263 Encoder::getBuffer() const noexcept
264 {
265  return m_buffer;
266 }
267 
268 inline size_t
269 Encoder::capacity() const noexcept
270 {
271  return m_buffer->size();
272 }
273 
274 inline Buffer::iterator
276 {
277  return m_begin;
278 }
279 
280 inline Buffer::iterator
282 {
283  return m_end;
284 }
285 
286 inline Buffer::const_iterator
288 {
289  return m_begin;
290 }
291 
292 inline Buffer::const_iterator
294 {
295  return m_end;
296 }
297 
298 inline uint8_t*
300 {
301  return &(*m_begin);
302 }
303 
304 inline const uint8_t*
306 {
307  return &(*m_begin);
308 }
309 
310 template<class Iterator>
311 size_t
312 Encoder::prependRange(Iterator first, Iterator last)
313 {
314  using ValueType = typename std::iterator_traits<Iterator>::value_type;
315  static_assert(sizeof(ValueType) == 1 && !std::is_same<ValueType, bool>::value, "");
316 
317  size_t length = std::distance(first, last);
318  reserveFront(length);
319 
320  m_begin -= length;
321  std::copy(first, last, m_begin);
322  return length;
323 }
324 
325 template<class Iterator>
326 size_t
327 Encoder::appendRange(Iterator first, Iterator last)
328 {
329  using ValueType = typename std::iterator_traits<Iterator>::value_type;
330  static_assert(sizeof(ValueType) == 1 && !std::is_same<ValueType, bool>::value, "");
331 
332  size_t length = std::distance(first, last);
333  reserveBack(length);
334 
335  std::copy(first, last, m_end);
336  m_end += length;
337  return length;
338 }
339 
340 } // namespace encoding
341 } // namespace ndn
342 
343 #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:118
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:165
iterator begin()
Get an iterator pointing to the first byte of the encoded buffer.
Definition: encoder.hpp:275
Copyright (c) 2011-2015 Regents of the University of California.
size_t appendRange(Iterator first, Iterator last)
Append range of bytes from the range [first, last)
Definition: encoder.hpp:327
size_t prependByte(uint8_t value)
Prepend a byte.
Definition: encoder.cpp:98
iterator end()
Get an iterator pointing to the past-the-end byte of the encoded buffer.
Definition: encoder.hpp:281
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:65
size_t size() const noexcept
Get the size of the encoded buffer.
Definition: encoder.hpp:257
Table::const_iterator iterator
Definition: cs-internal.hpp:41
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:138
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:59
size_t prependBlock(const Block &block)
Prepend TLV block block.
Definition: encoder.cpp:252
size_t appendBlock(const Block &block)
Append TLV block block.
Definition: encoder.cpp:263
Buffer::iterator iterator
Definition: encoder.hpp:130
Buffer::value_type value_type
Definition: encoder.hpp:129
Buffer::const_iterator const_iterator
Definition: encoder.hpp:131
size_t prependRange(Iterator first, Iterator last)
Prepend range of bytes from the range [first, last)
Definition: encoder.hpp:312
Helper class to perform TLV encoding Interface of this class (mostly) matches interface of Estimator ...
Definition: encoder.hpp:35
shared_ptr< Buffer > getBuffer() const noexcept
Get underlying buffer.
Definition: encoder.hpp:263
size_t appendByte(uint8_t value)
Append a byte.
Definition: encoder.cpp:108
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:232
size_t appendByteArray(const uint8_t *array, size_t length)
Append a byte array array of length length.
Definition: encoder.cpp:128
uint8_t * buf()
Get a pointer to the first byte of the encoded buffer.
Definition: encoder.hpp:299
size_t capacity() const noexcept
Get size of the underlying buffer.
Definition: encoder.hpp:269
size_t prependNonNegativeInteger(uint64_t integer)
Prepend non-negative integer integer of NDN TLV encoding.
Definition: encoder.cpp:192
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:242
size_t appendNonNegativeInteger(uint64_t integer)
Append non-negative integer integer of NDN TLV encoding.
Definition: encoder.cpp:212
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:40
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
const size_t MAX_NDN_PACKET_SIZE
practical limit of network layer packet size
Definition: tlv.hpp:41