NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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 
160  static std::tuple<bool, Block>
161  fromBuffer(const uint8_t* buffer, size_t maxSize);
162 
163 public: // wire format
166  bool
167  empty() const;
168 
171  bool
172  hasWire() const;
173 
176  void
177  reset();
178 
181  void
182  resetWire();
183 
184  Buffer::const_iterator
185  begin() const;
186 
187  Buffer::const_iterator
188  end() const;
189 
190  const uint8_t*
191  wire() const;
192 
193  size_t
194  size() const;
195 
196 public: // type and value
197  uint32_t
198  type() const;
199 
202  bool
203  hasValue() const;
204 
205  Buffer::const_iterator
206  value_begin() const;
207 
208  Buffer::const_iterator
209  value_end() const;
210 
211  const uint8_t*
212  value() const;
213 
214  size_t
215  value_size() const;
216 
217 public: // sub elements
223  void
224  parse() const;
225 
228  void
229  encode();
230 
233  const Block&
234  get(uint32_t type) const;
235 
236  element_const_iterator
237  find(uint32_t type) const;
238 
244  void
245  remove(uint32_t type);
246 
247  element_iterator
248  erase(element_const_iterator position);
249 
250  element_iterator
251  erase(element_const_iterator first, element_const_iterator last);
252 
253  void
254  push_back(const Block& element);
255 
262  element_iterator
263  insert(element_const_iterator pos, const Block& element);
264 
267  const element_container&
268  elements() const;
269 
270  element_const_iterator
271  elements_begin() const;
272 
273  element_const_iterator
274  elements_end() const;
275 
276  size_t
277  elements_size() const;
278 
279  Block
280  blockFromValue() const;
281 
285  shared_ptr<const Buffer>
286  getBuffer() const;
287 
288 public: // EqualityComparable concept
289  bool
290  operator==(const Block& other) const;
291 
292  bool
293  operator!=(const Block& other) const;
294 
295 public: // ConvertibleToConstBuffer
296  operator boost::asio::const_buffer() const;
297 
298 protected:
299  shared_ptr<const Buffer> m_buffer;
300 
301  uint32_t m_type;
302 
303  Buffer::const_iterator m_begin;
304  Buffer::const_iterator m_end;
305  uint32_t m_size;
306 
307  Buffer::const_iterator m_value_begin;
308  Buffer::const_iterator m_value_end;
309 
310  mutable element_container m_subBlocks;
311 };
312 
316 
317 inline shared_ptr<const Buffer>
318 Block::getBuffer() const
319 {
320  return m_buffer;
321 }
322 
323 inline uint32_t
324 Block::type() const
325 {
326  return m_type;
327 }
328 
329 inline Buffer::const_iterator
330 Block::value_begin() const
331 {
332  return m_value_begin;
333 }
334 
335 inline Buffer::const_iterator
336 Block::value_end() const
337 {
338  return m_value_end;
339 }
340 
341 inline const Block::element_container&
342 Block::elements() const
343 {
344  return m_subBlocks;
345 }
346 
347 } // namespace ndn
348 
349 #endif // NDN_ENCODING_BLOCK_HPP
shared_ptr< const Buffer > m_buffer
Definition: block.hpp:299
Copyright (c) 2011-2015 Regents of the University of California.
Error(const std::string &what)
Definition: block.hpp:54
Copyright (c) 2013-2016 Regents of the University of California.
Buffer::const_iterator m_begin
Definition: block.hpp:303
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:305
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Buffer::const_iterator m_value_end
Definition: block.hpp:308
EncodingImpl< EncoderTag > EncodingBuffer
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:310
Buffer::const_iterator m_end
Definition: block.hpp:304
uint32_t m_type
Definition: block.hpp:301
Buffer::const_iterator m_value_begin
Definition: block.hpp:307
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)