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-2022 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_CXX_ENCODING_BLOCK_HPP
25 #define NDN_CXX_ENCODING_BLOCK_HPP
26 
29 #include "ndn-cxx/encoding/tlv.hpp"
30 #include "ndn-cxx/util/span.hpp"
31 
32 namespace boost {
33 namespace asio {
34 class const_buffer;
35 } // namespace asio
36 } // namespace boost
37 
38 namespace ndn {
39 
44 class Block
45 {
46 public:
47  using value_type = Buffer::value_type;
48  using const_iterator = Buffer::const_iterator;
49  using element_container = std::vector<Block>;
50  using element_iterator = element_container::iterator;
51  using element_const_iterator = element_container::const_iterator;
52 
53  class Error : public tlv::Error
54  {
55  public:
56  using tlv::Error::Error;
57  };
58 
59 public: // construction, assignment
63  Block();
64 
67  Block(const Block&);
68 
71  Block&
72  operator=(const Block&);
73 
76  Block(Block&&) noexcept;
77 
80  Block&
81  operator=(Block&&) noexcept;
82 
89  explicit
90  Block(span<const uint8_t> buffer);
91 
96  explicit
97  Block(const EncodingBuffer& buffer);
98 
104  explicit
105  Block(const ConstBufferPtr& buffer);
106 
115  Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
116  bool verifyLength = true);
117 
126  Block(const Block& block, Block::const_iterator begin, Block::const_iterator end,
127  bool verifyLength = true);
128 
137  Block(ConstBufferPtr buffer, uint32_t type,
138  Buffer::const_iterator begin, Buffer::const_iterator end,
139  Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd);
140 
144  explicit
145  Block(uint32_t type);
146 
151  Block(uint32_t type, ConstBufferPtr value);
152 
157  Block(uint32_t type, const Block& value);
158 
165  NDN_CXX_NODISCARD static std::tuple<bool, Block>
166  fromBuffer(ConstBufferPtr buffer, size_t offset = 0);
167 
175  NDN_CXX_NODISCARD static std::tuple<bool, Block>
176  fromBuffer(span<const uint8_t> buffer);
177 
182  static Block
183  fromStream(std::istream& is);
184 
185 public: // wire format
191  bool
192  isValid() const noexcept
193  {
194  return m_type != tlv::Invalid;
195  }
196 
204  void
205  reset() noexcept;
206 
212  void
213  resetWire() noexcept;
214 
220  bool
221  hasWire() const noexcept
222  {
223  return m_buffer != nullptr && m_begin != m_end;
224  }
225 
230  begin() const;
231 
236  end() const;
237 
243  const uint8_t*
244  data() const;
245 
249  const uint8_t*
250  wire() const
251  {
252  return data();
253  }
254 
260  size_t
261  size() const;
262 
266  getBuffer() const
267  {
268  return m_buffer;
269  }
270 
271 public: // type and value
276  uint32_t
277  type() const noexcept
278  {
279  return m_type;
280  }
281 
290  bool
291  hasValue() const noexcept
292  {
293  return m_buffer != nullptr;
294  }
295 
301  value_begin() const noexcept
302  {
303  return m_valueBegin;
304  }
305 
311  value_end() const noexcept
312  {
313  return m_valueEnd;
314  }
315 
320  size_t
321  value_size() const noexcept
322  {
323  return hasValue() ? static_cast<size_t>(m_valueEnd - m_valueBegin) : 0;
324  }
325 
329  span<const uint8_t>
330  value_bytes() const noexcept
331  {
332  if (hasValue())
333  return {m_valueBegin, m_valueEnd};
334  else
335  return {};
336  }
337 
342  const uint8_t*
343  value() const noexcept;
344 
349  Block
350  blockFromValue() const;
351 
352 public: // sub-elements
360  void
361  parse() const;
362 
366  void
367  encode();
368 
373  const Block&
374  get(uint32_t type) const;
375 
382  find(uint32_t type) const;
383 
388  void
389  remove(uint32_t type);
390 
394  erase(element_const_iterator position);
395 
400 
404  void
405  push_back(const Block& element);
406 
410  void
411  push_back(Block&& element);
412 
419  insert(element_const_iterator pos, const Block& element);
420 
424  const element_container&
425  elements() const
426  {
427  return m_elements;
428  }
429 
434  {
435  return m_elements.begin();
436  }
437 
441  elements_end() const
442  {
443  return m_elements.end();
444  }
445 
448  size_t
450  {
451  return m_elements.size();
452  }
453 
454 public: // misc
457  operator boost::asio::const_buffer() const;
458 
459 private:
462  size_t
463  encode(EncodingEstimator& estimator) const;
464 
467  size_t
468  encodeValue(EncodingEstimator& estimator) const;
469 
474  size_t
475  encode(EncodingBuffer& encoder);
476 
477 protected:
486  shared_ptr<const Buffer> m_buffer;
487  Buffer::const_iterator m_begin;
488  Buffer::const_iterator m_end;
489 
490  Buffer::const_iterator m_valueBegin;
491  Buffer::const_iterator m_valueEnd;
492 
493  uint32_t m_type = tlv::Invalid;
494 
499  size_t m_size = 0;
500 
506 
516  friend std::ostream&
517  operator<<(std::ostream& os, const Block& block);
518 };
519 
520 inline
521 Block::Block(Block&&) noexcept = default;
522 
523 inline Block&
524 Block::operator=(Block&&) noexcept = default;
525 
528 bool
529 operator==(const Block& lhs, const Block& rhs);
530 
531 inline bool
532 operator!=(const Block& lhs, const Block& rhs)
533 {
534  return !(lhs == rhs);
535 }
536 
550 Block
551 operator ""_block(const char* input, std::size_t len);
552 
553 } // namespace ndn
554 
555 #endif // NDN_CXX_ENCODING_BLOCK_HPP
shared_ptr< const Buffer > m_buffer
Underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields.
Definition: block.hpp:486
Copyright (c) 2011-2015 Regents of the University of California.
Buffer::const_iterator m_valueBegin
Definition: block.hpp:490
Definition: block.hpp:32
Buffer::const_iterator m_begin
Definition: block.hpp:487
std::ostream & operator<<(std::ostream &os, const Face &face)
Definition: ndn-common.hpp:85
span_CONFIG_SIZE_TYPE size_t
Definition: span-lite.hpp:565
const_iterator value_begin() const noexcept
Get begin iterator of TLV-VALUE.
Definition: block.hpp:301
element_container::const_iterator element_const_iterator
Definition: block.hpp:51
size_t value_size() const noexcept
Return the size of TLV-VALUE, i.e., the TLV-LENGTH.
Definition: block.hpp:321
element_container m_elements
Contains the sub-elements.
Definition: block.hpp:505
Represents a TLV element of the NDN packet format.
Definition: block.hpp:44
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:221
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:433
const uint8_t * wire() const
Definition: block.hpp:250
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:425
Buffer::const_iterator m_valueEnd
Definition: block.hpp:491
ConstBufferPtr getBuffer() const
Get underlying buffer.
Definition: block.hpp:266
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:441
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
element_container::iterator element_iterator
Definition: block.hpp:50
Buffer::value_type value_type
Definition: block.hpp:47
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:449
CFReleaser< CFStringRef > fromBuffer(const uint8_t *buf, size_t buflen)
Create a CFString by copying bytes from a raw buffer.
bool isValid() const noexcept
Check if the Block is valid.
Definition: block.hpp:192
span< const uint8_t > value_bytes() const noexcept
Return a read-only view of TLV-VALUE as a contiguous range of bytes.
Definition: block.hpp:330
bool hasValue() const noexcept
Check if the Block has a non-empty TLV-VALUE.
Definition: block.hpp:291
uint32_t type() const noexcept
Return the TLV-TYPE of the Block.
Definition: block.hpp:277
Buffer::const_iterator m_end
Definition: block.hpp:488
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span-lite.hpp:1535
const_iterator value_end() const noexcept
Get end iterator of TLV-VALUE.
Definition: block.hpp:311
static ParseResult parse(const Data &data)
std::vector< Block > element_container
Definition: block.hpp:49
EncodingImpl< EncoderTag > EncodingBuffer
Buffer::const_iterator const_iterator
Definition: block.hpp:48
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
EncodingImpl< EstimatorTag > EncodingEstimator
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139