NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: 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; -*- */
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  * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22  */
23 
24 #ifndef NDN_ENCODING_BLOCK_HPP
25 #define NDN_ENCODING_BLOCK_HPP
26 
29 #include "ndn-cxx/encoding/tlv.hpp"
30 
31 namespace boost {
32 namespace asio {
33 class const_buffer;
34 } // namespace asio
35 } // namespace boost
36 
37 namespace ndn {
38 
42 class Block
43 {
44 public:
45  using element_container = std::vector<Block>;
47  using element_const_iterator = element_container::const_iterator;
48 
49  class Error : public tlv::Error
50  {
51  public:
52  using tlv::Error::Error;
53  };
54 
55 public: // constructor, creation, assignment
58  Block();
59 
62  Block(const Block&);
63 
66  Block&
67  operator=(const Block&);
68 
71  Block(Block&&) noexcept;
72 
75  Block&
76  operator=(Block&&) noexcept;
77 
82  explicit
83  Block(const EncodingBuffer& buffer);
84 
90  explicit
91  Block(const ConstBufferPtr& buffer);
92 
102  Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
103  bool verifyLength = true);
104 
113  Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
114  bool verifyLength = true);
115 
124  Block(ConstBufferPtr buffer, uint32_t type,
125  Buffer::const_iterator begin, Buffer::const_iterator end,
126  Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd);
127 
134  Block(const uint8_t* buf, size_t bufSize);
135 
139  explicit
140  Block(uint32_t type);
141 
146  Block(uint32_t type, ConstBufferPtr value);
147 
152  Block(uint32_t type, const Block& value);
153 
158  static Block
159  fromStream(std::istream& is);
160 
167  static std::tuple<bool, Block>
168  fromBuffer(ConstBufferPtr buffer, size_t offset);
169 
177  static std::tuple<bool, Block>
178  fromBuffer(const uint8_t* buf, size_t bufSize);
179 
180 public: // wire format
186  bool
187  empty() const
188  {
189  return m_type == std::numeric_limits<uint32_t>::max();
190  }
191 
197  bool
198  hasWire() const;
199 
203  void
204  reset();
205 
210  void
211  resetWire();
212 
216  Buffer::const_iterator
217  begin() const;
218 
222  Buffer::const_iterator
223  end() const;
224 
228  const uint8_t*
229  wire() const;
230 
234  size_t
235  size() const;
236 
239  shared_ptr<const Buffer>
240  getBuffer() const
241  {
242  return m_buffer;
243  }
244 
245 public: // type and value
248  uint32_t
249  type() const
250  {
251  return m_type;
252  }
253 
259  bool
260  hasValue() const
261  {
262  return m_buffer != nullptr;
263  }
264 
268  Buffer::const_iterator
269  value_begin() const
270  {
271  return m_valueBegin;
272  }
273 
277  Buffer::const_iterator
278  value_end() const
279  {
280  return m_valueEnd;
281  }
282 
285  const uint8_t*
286  value() const;
287 
290  size_t
291  value_size() const;
292 
293  Block
294  blockFromValue() const;
295 
296 public: // sub elements
304  void
305  parse() const;
306 
310  void
311  encode();
312 
317  const Block&
318  get(uint32_t type) const;
319 
325  find(uint32_t type) const;
326 
331  void
332  remove(uint32_t type);
333 
337  erase(element_const_iterator position);
338 
343 
346  void
347  push_back(const Block& element);
348 
355  insert(element_const_iterator pos, const Block& element);
356 
360  const element_container&
361  elements() const
362  {
363  return m_elements;
364  }
365 
370  {
371  return m_elements.begin();
372  }
373 
377  elements_end() const
378  {
379  return m_elements.end();
380  }
381 
384  size_t
386  {
387  return m_elements.size();
388  }
389 
390 public: // misc
393  operator boost::asio::const_buffer() const;
394 
395 private:
398  size_t
399  encode(EncodingEstimator& estimator) const;
400 
403  size_t
404  encodeValue(EncodingEstimator& estimator) const;
405 
410  size_t
411  encode(EncodingBuffer& encoder);
412 
413 protected:
422  shared_ptr<const Buffer> m_buffer;
423  Buffer::const_iterator m_begin;
424  Buffer::const_iterator m_end;
425 
426  Buffer::const_iterator m_valueBegin;
427  Buffer::const_iterator m_valueEnd;
428 
429  uint32_t m_type = std::numeric_limits<uint32_t>::max();
430 
435  size_t m_size = 0;
436 
442 
452  friend std::ostream&
453  operator<<(std::ostream& os, const Block& block);
454 };
455 
456 inline
457 Block::Block(Block&&) noexcept = default;
458 
459 inline Block&
460 Block::operator=(Block&&) noexcept = default;
461 
464 bool
465 operator==(const Block& lhs, const Block& rhs);
466 
467 inline bool
468 operator!=(const Block& lhs, const Block& rhs)
469 {
470  return !(lhs == rhs);
471 }
472 
486 Block
487 operator "" _block(const char* input, std::size_t len);
488 
489 } // namespace ndn
490 
491 #endif // NDN_ENCODING_BLOCK_HPP
size_t m_size
total size including Type-Length-Value
Definition: block.hpp:435
shared_ptr< const Buffer > m_buffer
underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields
Definition: block.hpp:422
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:249
static Block fromStream(std::istream &is)
Parse Block from an input stream.
Definition: block.cpp:161
Copyright (c) 2011-2015 Regents of the University of California.
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to parse Block from a wire buffer.
Definition: block.cpp:193
Buffer::const_iterator m_valueBegin
Definition: block.hpp:426
Buffer::const_iterator m_begin
Definition: block.hpp:423
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
Definition: block.cpp:316
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
const uint8_t * wire() const
Get pointer to encoded wire.
Definition: block.cpp:289
void parse() const
Parse TLV-VALUE into sub elements.
Definition: block.cpp:333
element_container m_elements
sub elements
Definition: block.hpp:441
friend std::ostream & operator<<(std::ostream &os, const Block &block)
Print block to os.
Definition: block.cpp:495
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:369
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub element.
Definition: block.cpp:472
const uint8_t * value() const
Get pointer to TLV-VALUE.
Definition: block.cpp:310
element_iterator erase(element_const_iterator position)
Erase a sub element.
Definition: block.cpp:451
void resetWire()
Reset wire buffer but keep TLV-TYPE and sub elements (if any)
Definition: block.cpp:264
const element_container & elements() const
Get container of sub elements.
Definition: block.hpp:361
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:269
Table::const_iterator iterator
Definition: cs-internal.hpp:41
Buffer::const_iterator m_valueEnd
Definition: block.hpp:427
shared_ptr< const Buffer > getBuffer() const
Get underlying buffer.
Definition: block.hpp:240
bool empty() const
Check if the Block is empty.
Definition: block.hpp:187
size_t size() const
Get size of encoded wire, including Type-Length-Value.
Definition: block.cpp:298
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:377
const Block & get(uint32_t type) const
Get the first sub element of specified TLV-TYPE.
Definition: block.cpp:422
element_const_iterator find(uint32_t type) const
Find the first sub element of specified TLV-TYPE.
Definition: block.cpp:434
Block & operator=(const Block &)
Copy assignment operator.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:278
Buffer::const_iterator begin() const
Get begin iterator of encoded wire.
Definition: block.cpp:271
element_container::iterator element_iterator
Definition: block.hpp:46
void remove(uint32_t type)
Remove all sub elements of specified TLV-TYPE.
Definition: block.cpp:441
void reset()
Reset wire buffer of the element.
Definition: block.cpp:255
void push_back(const Block &element)
Append a sub element.
Definition: block.cpp:465
Buffer::const_iterator end() const
Get end iterator of encoded wire.
Definition: block.cpp:280
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:385
void encode()
Encode sub elements into TLV-VALUE.
Definition: block.cpp:361
Block blockFromValue() const
Definition: block.cpp:322
Buffer::const_iterator m_end
Definition: block.hpp:424
uint32_t m_type
TLV-TYPE.
Definition: block.hpp:429
bool hasValue() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:260
Block()
Create an empty Block.
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:40
std::vector< Block > element_container
Definition: block.hpp:45
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:249
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
EncodingImpl< EstimatorTag > EncodingEstimator
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126