NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
block.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
24 #ifndef NDN_ENCODING_BLOCK_HPP
25 #define NDN_ENCODING_BLOCK_HPP
26 
27 #include "../common.hpp"
28 
29 #include "buffer.hpp"
30 #include "tlv.hpp"
31 #include "encoding-buffer-fwd.hpp"
32 
33 namespace boost {
34 namespace asio {
35 class const_buffer;
36 } // namespace asio
37 } // namespace boost
38 
39 namespace ndn {
40 
43 class Block
44 {
45 public:
46  typedef std::vector<Block> element_container;
48  typedef element_container::const_iterator element_const_iterator;
49 
50  class Error : public tlv::Error
51  {
52  public:
53  explicit
54  Error(const std::string& what)
55  : tlv::Error(what)
56  {
57  }
58  };
59 
60 public: // constructor, creation, assignment
63  Block();
64 
67  explicit
68  Block(const EncodingBuffer& buffer);
69 
72  explicit
73  Block(const ConstBufferPtr& buffer);
74 
80  Block(const ConstBufferPtr& buffer,
81  const Buffer::const_iterator& begin, const Buffer::const_iterator& end,
82  bool verifyLength = true);
83 
89  Block(const Block& block,
90  const Buffer::const_iterator& begin, const Buffer::const_iterator& end,
91  bool verifyLength = true);
92 
95  Block(const uint8_t* buffer, size_t maxlength);
96 
99  Block(const void* buffer, size_t maxlength);
100 
105  Block(const ConstBufferPtr& wire,
106  uint32_t type,
107  const Buffer::const_iterator& begin, const Buffer::const_iterator& end,
108  const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd);
109 
112  explicit
113  Block(uint32_t type);
114 
121  Block(uint32_t type, const ConstBufferPtr& value);
122 
129  Block(uint32_t type, const Block& value);
130 
133  static Block
134  fromStream(std::istream& is);
135 
147  static std::tuple<bool, Block>
148  fromBuffer(ConstBufferPtr buffer, size_t offset);
149 
153  static bool
154  fromBuffer(const ConstBufferPtr& buffer, size_t offset, Block& block))
155  {
156  bool isOk = false;
157  std::tie(isOk, block) = Block::fromBuffer(buffer, offset);
158  return isOk;
159  }
160 
171  static std::tuple<bool, Block>
172  fromBuffer(const uint8_t* buffer, size_t maxSize);
173 
177  static bool
178  fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block))
179  {
180  bool isOk = false;
181  std::tie(isOk, block) = Block::fromBuffer(buffer, maxSize);
182  return isOk;
183  }
184 
185 public: // wire format
188  bool
189  empty() const;
190 
193  bool
194  hasWire() const;
195 
198  void
199  reset();
200 
203  void
204  resetWire();
205 
206  Buffer::const_iterator
207  begin() const;
208 
209  Buffer::const_iterator
210  end() const;
211 
212  const uint8_t*
213  wire() const;
214 
215  size_t
216  size() const;
217 
218 public: // type and value
219  uint32_t
220  type() const;
221 
224  bool
225  hasValue() const;
226 
227  Buffer::const_iterator
228  value_begin() const;
229 
230  Buffer::const_iterator
231  value_end() const;
232 
233  const uint8_t*
234  value() const;
235 
236  size_t
237  value_size() const;
238 
239 public: // sub elements
245  void
246  parse() const;
247 
250  void
251  encode();
252 
255  const Block&
256  get(uint32_t type) const;
257 
258  element_const_iterator
259  find(uint32_t type) const;
260 
266  void
267  remove(uint32_t type);
268 
269  element_iterator
270  erase(element_const_iterator position);
271 
272  element_iterator
273  erase(element_const_iterator first, element_const_iterator last);
274 
275  void
276  push_back(const Block& element);
277 
284  element_iterator
285  insert(element_const_iterator pos, const Block& element);
286 
289  const element_container&
290  elements() const;
291 
292  element_const_iterator
293  elements_begin() const;
294 
295  element_const_iterator
296  elements_end() const;
297 
298  size_t
299  elements_size() const;
300 
301  Block
302  blockFromValue() const;
303 
307  shared_ptr<const Buffer>
308  getBuffer() const;
309 
310 public: // EqualityComparable concept
311  bool
312  operator==(const Block& other) const;
313 
314  bool
315  operator!=(const Block& other) const;
316 
317 public: // ConvertibleToConstBuffer
318  operator boost::asio::const_buffer() const;
319 
320 protected:
321  shared_ptr<const Buffer> m_buffer;
322 
323  uint32_t m_type;
324 
325  Buffer::const_iterator m_begin;
326  Buffer::const_iterator m_end;
327  uint32_t m_size;
328 
329  Buffer::const_iterator m_value_begin;
330  Buffer::const_iterator m_value_end;
331 
332  mutable element_container m_subBlocks;
333 };
334 
338 
339 inline shared_ptr<const Buffer>
340 Block::getBuffer() const
341 {
342  return m_buffer;
343 }
344 
345 inline uint32_t
346 Block::type() const
347 {
348  return m_type;
349 }
350 
351 inline Buffer::const_iterator
352 Block::value_begin() const
353 {
354  return m_value_begin;
355 }
356 
357 inline Buffer::const_iterator
358 Block::value_end() const
359 {
360  return m_value_end;
361 }
362 
363 inline const Block::element_container&
364 Block::elements() const
365 {
366  return m_subBlocks;
367 }
368 
369 } // namespace ndn
370 
371 #endif // NDN_ENCODING_BLOCK_HPP
shared_ptr< const Buffer > m_buffer
Definition: block.hpp:321
Copyright (c) 2011-2015 Regents of the University of California.
Error(const std::string &what)
Definition: block.hpp:54
Copyright (c) 2013-2015 Regents of the University of California.
Buffer::const_iterator m_begin
Definition: block.hpp:325
element_container::iterator element_iterator
Definition: block.hpp:47
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
uint32_t m_size
Definition: block.hpp:327
DEPRECATED(static bool fromBuffer(const ConstBufferPtr &buffer, size_t offset, Block &block))
Definition: block.hpp:152
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Buffer::const_iterator m_value_end
Definition: block.hpp:330
EncodingImpl< EncoderTag > EncodingBuffer
DEPRECATED(static bool fromBuffer(const uint8_t *buffer, size_t maxSize, Block &block))
Definition: block.hpp:176
element_container::const_iterator element_const_iterator
Definition: block.hpp:48
std::vector< Block > element_container
Definition: block.hpp:46
bool operator!=(const GlobalRouter::Incidency &a, const GlobalRouter::Incidency &b)
element_container m_subBlocks
Definition: block.hpp:332
Buffer::const_iterator m_end
Definition: block.hpp:326
uint32_t m_type
Definition: block.hpp:323
Buffer::const_iterator m_value_begin
Definition: block.hpp:329
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:33
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
bool operator==(const GlobalRouter::Incidency &a, const GlobalRouter::Incidency &b)