NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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; -*- */
22 #ifndef NDN_ENCODING_ENCODER_HPP
23 #define NDN_ENCODING_ENCODER_HPP
24 
25 #include "../common.hpp"
26 #include "block.hpp"
27 
28 namespace ndn {
29 namespace encoding {
30 
36 class Encoder
37 {
38 public: // common interface between Encoder and Estimator
44  explicit
45  Encoder(size_t totalReserve = 8800, size_t reserveFromBack = 400);
46 
47  Encoder(const Encoder&) = delete;
48 
49  Encoder&
50  operator=(const Encoder&) = delete;
51 
55  size_t
56  prependByte(uint8_t value);
57 
61  size_t
62  appendByte(uint8_t value);
63 
67  size_t
68  prependByteArray(const uint8_t* array, size_t length);
69 
73  size_t
74  appendByteArray(const uint8_t* array, size_t length);
75 
79  template<class Iterator>
80  size_t
81  prependRange(Iterator first, Iterator last);
82 
86  template<class Iterator>
87  size_t
88  appendRange(Iterator first, Iterator last);
89 
94  size_t
95  prependVarNumber(uint64_t varNumber);
96 
101  size_t
102  appendVarNumber(uint64_t varNumber);
103 
108  size_t
109  prependNonNegativeInteger(uint64_t integer);
110 
115  size_t
116  appendNonNegativeInteger(uint64_t integer);
117 
121  size_t
122  prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
123 
127  size_t
128  appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
129 
133  size_t
134  prependBlock(const Block& block);
135 
139  size_t
140  appendBlock(const Block& block);
141 
142 public: // unique interface to the Encoder
143  typedef Buffer::value_type value_type;
145  typedef Buffer::const_iterator const_iterator;
146 
157  explicit
158  Encoder(const Block& block);
159 
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 
216  const_iterator
217  begin() const;
218 
219  const_iterator
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  size_t length = std::distance(first, last);
319  reserveFront(length);
320 
321  m_begin -= length;
322  std::copy(first, last, m_begin);
323  return length;
324 }
325 
326 
327 template<class Iterator>
328 inline size_t
329 Encoder::appendRange(Iterator first, Iterator last)
330 {
331  size_t length = std::distance(first, last);
332  reserveBack(length);
333 
334  std::copy(first, last, m_end);
335  m_end += length;
336  return length;
337 }
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: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) 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:329
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
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
void reserve(size_t size, bool addInFront)
Reserve size bytes for the underlying buffer.
Definition: encoder.cpp:68
Buffer::iterator iterator
Definition: encoder.hpp:144
Buffer::const_iterator const_iterator
Definition: encoder.hpp:145
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: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
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:36
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
Buffer::value_type value_type
Definition: encoder.hpp:143
Encoder(size_t totalReserve=8800, size_t reserveFromBack=400)
Create instance of the encoder with the specified reserved sizes.
Definition: encoder.cpp:27