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-2019 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>;
46  using element_iterator = element_container::iterator;
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: // construction, assignment
59  Block();
60 
63  Block(const Block&);
64 
67  Block&
68  operator=(const Block&);
69 
72  Block(Block&&) noexcept;
73 
76  Block&
77  operator=(Block&&) noexcept;
78 
83  explicit
84  Block(const EncodingBuffer& buffer);
85 
91  explicit
92  Block(const ConstBufferPtr& buffer);
93 
103  Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
104  bool verifyLength = true);
105 
114  Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
115  bool verifyLength = true);
116 
125  Block(ConstBufferPtr buffer, uint32_t type,
126  Buffer::const_iterator begin, Buffer::const_iterator end,
127  Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd);
128 
135  Block(const uint8_t* buf, size_t bufSize);
136 
140  explicit
141  Block(uint32_t type);
142 
147  Block(uint32_t type, ConstBufferPtr value);
148 
153  Block(uint32_t type, const Block& value);
154 
159  static Block
160  fromStream(std::istream& is);
161 
168  NDN_CXX_NODISCARD static std::tuple<bool, Block>
169  fromBuffer(ConstBufferPtr buffer, size_t offset);
170 
178  NDN_CXX_NODISCARD static std::tuple<bool, Block>
179  fromBuffer(const uint8_t* buf, size_t bufSize);
180 
181 public: // wire format
187  bool
188  isValid() const noexcept
189  {
190  return m_type != tlv::Invalid;
191  }
192 
200  NDN_CXX_NODISCARD bool
201  empty() const noexcept
202  {
203  return !isValid();
204  }
205 
213  void
214  reset() noexcept;
215 
221  void
222  resetWire() noexcept;
223 
229  bool
230  hasWire() const noexcept
231  {
232  return m_buffer != nullptr && m_begin != m_end;
233  }
234 
238  Buffer::const_iterator
239  begin() const;
240 
244  Buffer::const_iterator
245  end() const;
246 
251  const uint8_t*
252  wire() const;
253 
258  size_t
259  size() const;
260 
264  getBuffer() const
265  {
266  return m_buffer;
267  }
268 
269 public: // type and value
273  uint32_t
274  type() const
275  {
276  return m_type;
277  }
278 
286  bool
287  hasValue() const noexcept
288  {
289  return m_buffer != nullptr;
290  }
291 
295  Buffer::const_iterator
296  value_begin() const
297  {
298  return m_valueBegin;
299  }
300 
304  Buffer::const_iterator
305  value_end() const
306  {
307  return m_valueEnd;
308  }
309 
313  const uint8_t*
314  value() const noexcept;
315 
319  size_t
320  value_size() const noexcept;
321 
322  Block
323  blockFromValue() const;
324 
325 public: // sub-elements
333  void
334  parse() const;
335 
339  void
340  encode();
341 
346  const Block&
347  get(uint32_t type) const;
348 
355  find(uint32_t type) const;
356 
361  void
362  remove(uint32_t type);
363 
367  erase(element_const_iterator position);
368 
373 
376  void
377  push_back(const Block& element);
378 
385  insert(element_const_iterator pos, const Block& element);
386 
390  const element_container&
391  elements() const
392  {
393  return m_elements;
394  }
395 
400  {
401  return m_elements.begin();
402  }
403 
407  elements_end() const
408  {
409  return m_elements.end();
410  }
411 
414  size_t
416  {
417  return m_elements.size();
418  }
419 
420 public: // misc
423  operator boost::asio::const_buffer() const;
424 
425 private:
428  size_t
429  encode(EncodingEstimator& estimator) const;
430 
433  size_t
434  encodeValue(EncodingEstimator& estimator) const;
435 
440  size_t
441  encode(EncodingBuffer& encoder);
442 
443 protected:
452  shared_ptr<const Buffer> m_buffer;
453  Buffer::const_iterator m_begin;
454  Buffer::const_iterator m_end;
455 
456  Buffer::const_iterator m_valueBegin;
457  Buffer::const_iterator m_valueEnd;
458 
459  uint32_t m_type = tlv::Invalid;
460 
465  size_t m_size = 0;
466 
472 
482  friend std::ostream&
483  operator<<(std::ostream& os, const Block& block);
484 };
485 
486 inline
487 Block::Block(Block&&) noexcept = default;
488 
489 inline Block&
490 Block::operator=(Block&&) noexcept = default;
491 
494 bool
495 operator==(const Block& lhs, const Block& rhs);
496 
497 inline bool
498 operator!=(const Block& lhs, const Block& rhs)
499 {
500  return !(lhs == rhs);
501 }
502 
516 Block
517 operator "" _block(const char* input, std::size_t len);
518 
519 } // namespace ndn
520 
521 #endif // NDN_ENCODING_BLOCK_HPP
ndn::Block::elements
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:391
buf
const uint8_t * buf
Definition: verification-helpers.cpp:47
ndn::Block::m_size
size_t m_size
Total size including Type-Length-Value.
Definition: block.hpp:465
tlv.hpp
ndn::Block::blockFromValue
Block blockFromValue() const
Definition: block.cpp:314
ndn::Block::value_size
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Definition: block.cpp:308
ndn::Block::insert
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
Definition: block.cpp:464
ndn::Block::Block
Block()
Create an invalid Block.
ndn::Block::operator<<
friend std::ostream & operator<<(std::ostream &os, const Block &block)
Print block to os.
Definition: block.cpp:487
ndn::Block::hasValue
bool hasValue() const noexcept
Check if the Block has a non-empty TLV-VALUE.
Definition: block.hpp:287
ndn::Block::begin
Buffer::const_iterator begin() const
Get begin iterator of encoded wire.
Definition: block.cpp:263
ndn::Buffer
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:41
ndn::Block::elements_begin
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
ndn::Block::m_valueBegin
Buffer::const_iterator m_valueBegin
Definition: block.hpp:456
ndn::Block::find
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:426
boost
Definition: block.hpp:31
ndn::Block::empty
NDN_CXX_NODISCARD bool empty() const noexcept
Check if the Block is empty.
Definition: block.hpp:201
ndn::Block::m_type
uint32_t m_type
TLV-TYPE.
Definition: block.hpp:459
ndn::Block::value_end
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:305
ndn::Block::reset
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:250
ndn::Block::isValid
bool isValid() const noexcept
Check if the Block is valid.
Definition: block.hpp:188
ndn::Block::operator=
Block & operator=(const Block &)
Copy assignment operator.
ndn::Block::element_const_iterator
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
ndn::Block::push_back
void push_back(const Block &element)
Append a sub-element.
Definition: block.cpp:457
ndn::Block::m_elements
element_container m_elements
Contains the sub-elements.
Definition: block.hpp:471
ndn::encoding::EncodingEstimator
EncodingImpl< EstimatorTag > EncodingEstimator
Definition: encoding-buffer-fwd.hpp:39
ndn::Block::get
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:414
ndn::Block::remove
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
Definition: block.cpp:433
NDN_CXX_NODISCARD
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
ndn::Block::fromBuffer
static NDN_CXX_NODISCARD std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset)
Try to parse Block from a wire buffer.
Definition: block.cpp:194
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::Block::erase
element_iterator erase(element_const_iterator position)
Erase a sub-element.
Definition: block.cpp:443
ndn::Block::value_begin
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:296
ndn::Block::m_end
Buffer::const_iterator m_end
Definition: block.hpp:454
ndn::Block::element_iterator
element_container::iterator element_iterator
Definition: block.hpp:46
ndn::Block::element_container
std::vector< Block > element_container
Definition: block.hpp:45
encoding-buffer-fwd.hpp
ndn::Block::resetWire
void resetWire() noexcept
Reset wire buffer but keep TLV-TYPE and sub-elements (if any)
Definition: block.cpp:256
ndn::Block::getBuffer
ConstBufferPtr getBuffer() const
Get underlying buffer.
Definition: block.hpp:264
ndn::Block::parse
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
ndn::Block::m_begin
Buffer::const_iterator m_begin
Definition: block.hpp:453
ndn::Block::end
Buffer::const_iterator end() const
Get end iterator of encoded wire.
Definition: block.cpp:272
ndn::Block::fromStream
static Block fromStream(std::istream &is)
Parse Block from an input stream.
Definition: block.cpp:162
ndn::Block::Error
Definition: block.hpp:50
ndn::Block::value
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition: block.cpp:302
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Block::elements_size
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:415
ndn::Block::size
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:290
ndn::Block::elements_end
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:407
ndn::Block::m_valueEnd
Buffer::const_iterator m_valueEnd
Definition: block.hpp:457
ndn::Block::Block
Block(Block &&) noexcept
Move constructor.
ndn::Block::hasWire
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
buffer.hpp
ndn::Block::Block
Block(const Block &)
Copy constructor.
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
ndn::Block::wire
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition: block.cpp:281
ndn::tlv::Invalid
@ Invalid
Definition: tlv.hpp:64
ndn::Block::encode
void encode()
Encode sub-elements into TLV-VALUE.
Definition: block.cpp:353
ndn::tlv::Error::Error
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
ndn::ConstBufferPtr
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38
ndn::Block::m_buffer
shared_ptr< const Buffer > m_buffer
Underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields.
Definition: block.hpp:452