NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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 = MAX_NDN_PACKET_SIZE, 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 
169  void
170  reserve(size_t size, bool addInFront);
171 
177  void
178  reserveBack(size_t size);
179 
185  void
186  reserveFront(size_t size);
187 
191  size_t
192  capacity() const;
193 
197  shared_ptr<Buffer>
198  getBuffer();
199 
200 public: // accessors
201 
205  iterator
206  begin();
207 
211  iterator
212  end();
213 
217  const_iterator
218  begin() const;
219 
220  const_iterator
221  end() const;
222 
226  uint8_t*
227  buf();
228 
232  const uint8_t*
233  buf() const;
234 
238  size_t
239  size() const;
240 
248  Block
249  block(bool verifyLength = true) const;
250 
251 private:
252  shared_ptr<Buffer> m_buffer;
253 
254  // invariant: m_begin always points to the position of last-written byte (if prepending data)
255  iterator m_begin;
256  // invariant: m_end always points to the position of next unwritten byte (if appending data)
257  iterator m_end;
258 };
259 
260 
261 inline size_t
263 {
264  return m_end - m_begin;
265 }
266 
267 inline shared_ptr<Buffer>
269 {
270  return m_buffer;
271 }
272 
273 inline size_t
275 {
276  return m_buffer->size();
277 }
278 
279 inline Buffer::iterator
281 {
282  return m_begin;
283 }
284 
285 inline Buffer::iterator
287 {
288  return m_end;
289 }
290 
291 inline Buffer::const_iterator
293 {
294  return m_begin;
295 }
296 
297 inline Buffer::const_iterator
299 {
300  return m_end;
301 }
302 
303 inline uint8_t*
305 {
306  return &(*m_begin);
307 }
308 
309 inline const uint8_t*
311 {
312  return &(*m_begin);
313 }
314 
315 template<class Iterator>
316 inline size_t
317 Encoder::prependRange(Iterator first, Iterator last)
318 {
319  size_t length = std::distance(first, last);
320  reserveFront(length);
321 
322  m_begin -= length;
323  std::copy(first, last, m_begin);
324  return length;
325 }
326 
327 
328 template<class Iterator>
329 inline size_t
330 Encoder::appendRange(Iterator first, Iterator last)
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 
341 } // namespace encoding
342 } // namespace ndn
343 
344 #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:280
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:330
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:286
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
Block block(bool verifyLength=true) const
Create Block from the underlying buffer.
Definition: encoder.cpp:60
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:317
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:268
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:304
size_t prependNonNegativeInteger(uint64_t integer)
Prepend non-negative integer integer of NDN TLV encoding.
Definition: encoder.cpp:201
size_t size() const
Get size of the encoded buffer.
Definition: encoder.hpp:262
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=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
Definition: tlv.hpp:39
size_t capacity() const
Get size of the underlying buffer.
Definition: encoder.hpp:274