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 element_container = std::vector<Block>;
48  using element_iterator = element_container::iterator;
49  using element_const_iterator = element_container::const_iterator;
50 
51  class Error : public tlv::Error
52  {
53  public:
54  using tlv::Error::Error;
55  };
56 
57 public: // construction, assignment
61  Block();
62 
65  Block(const Block&);
66 
69  Block&
70  operator=(const Block&);
71 
74  Block(Block&&) noexcept;
75 
78  Block&
79  operator=(Block&&) noexcept;
80 
87  explicit
88  Block(span<const uint8_t> buffer);
89 
94  explicit
95  Block(const EncodingBuffer& buffer);
96 
102  explicit
103  Block(const ConstBufferPtr& buffer);
104 
113  Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
114  bool verifyLength = true);
115 
124  Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
125  bool verifyLength = true);
126 
135  Block(ConstBufferPtr buffer, uint32_t type,
136  Buffer::const_iterator begin, Buffer::const_iterator end,
137  Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd);
138 
146  [[deprecated("use the constructor that takes a span<>")]]
147  Block(const uint8_t* buf, size_t bufSize);
148 
152  explicit
153  Block(uint32_t type);
154 
159  Block(uint32_t type, ConstBufferPtr value);
160 
165  Block(uint32_t type, const Block& value);
166 
173  NDN_CXX_NODISCARD static std::tuple<bool, Block>
174  fromBuffer(ConstBufferPtr buffer, size_t offset = 0);
175 
183  NDN_CXX_NODISCARD static std::tuple<bool, Block>
184  fromBuffer(span<const uint8_t> buffer);
185 
194  [[deprecated("use the overload that takes a span<>")]]
195  NDN_CXX_NODISCARD static std::tuple<bool, Block>
196  fromBuffer(const uint8_t* buf, size_t bufSize);
197 
202  static Block
203  fromStream(std::istream& is);
204 
205 public: // wire format
211  bool
212  isValid() const noexcept
213  {
214  return m_type != tlv::Invalid;
215  }
216 
224  void
225  reset() noexcept;
226 
232  void
233  resetWire() noexcept;
234 
240  bool
241  hasWire() const noexcept
242  {
243  return m_buffer != nullptr && m_begin != m_end;
244  }
245 
249  Buffer::const_iterator
250  begin() const;
251 
255  Buffer::const_iterator
256  end() const;
257 
262  const uint8_t*
263  wire() const;
264 
269  size_t
270  size() const;
271 
275  getBuffer() const
276  {
277  return m_buffer;
278  }
279 
280 public: // type and value
284  uint32_t
285  type() const
286  {
287  return m_type;
288  }
289 
297  bool
298  hasValue() const noexcept
299  {
300  return m_buffer != nullptr;
301  }
302 
306  Buffer::const_iterator
307  value_begin() const
308  {
309  return m_valueBegin;
310  }
311 
315  Buffer::const_iterator
316  value_end() const
317  {
318  return m_valueEnd;
319  }
320 
324  const uint8_t*
325  value() const noexcept;
326 
330  size_t
331  value_size() const noexcept;
332 
333  Block
334  blockFromValue() const;
335 
336 public: // sub-elements
344  void
345  parse() const;
346 
350  void
351  encode();
352 
357  const Block&
358  get(uint32_t type) const;
359 
366  find(uint32_t type) const;
367 
372  void
373  remove(uint32_t type);
374 
378  erase(element_const_iterator position);
379 
384 
387  void
388  push_back(const Block& element);
389 
396  insert(element_const_iterator pos, const Block& element);
397 
401  const element_container&
402  elements() const
403  {
404  return m_elements;
405  }
406 
411  {
412  return m_elements.begin();
413  }
414 
418  elements_end() const
419  {
420  return m_elements.end();
421  }
422 
425  size_t
427  {
428  return m_elements.size();
429  }
430 
431 public: // misc
434  operator boost::asio::const_buffer() const;
435 
436 private:
439  size_t
440  encode(EncodingEstimator& estimator) const;
441 
444  size_t
445  encodeValue(EncodingEstimator& estimator) const;
446 
451  size_t
452  encode(EncodingBuffer& encoder);
453 
454 protected:
463  shared_ptr<const Buffer> m_buffer;
464  Buffer::const_iterator m_begin;
465  Buffer::const_iterator m_end;
466 
467  Buffer::const_iterator m_valueBegin;
468  Buffer::const_iterator m_valueEnd;
469 
470  uint32_t m_type = tlv::Invalid;
471 
476  size_t m_size = 0;
477 
483 
493  friend std::ostream&
494  operator<<(std::ostream& os, const Block& block);
495 };
496 
497 inline
498 Block::Block(Block&&) noexcept = default;
499 
500 inline Block&
501 Block::operator=(Block&&) noexcept = default;
502 
505 bool
506 operator==(const Block& lhs, const Block& rhs);
507 
508 inline bool
509 operator!=(const Block& lhs, const Block& rhs)
510 {
511  return !(lhs == rhs);
512 }
513 
527 Block
528 operator "" _block(const char* input, std::size_t len);
529 
530 } // namespace ndn
531 
532 #endif // NDN_CXX_ENCODING_BLOCK_HPP
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Represents a TLV element of the NDN packet format.
Definition: block.hpp:45
element_container::const_iterator element_const_iterator
Definition: block.hpp:49
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:426
uint32_t m_type
TLV-TYPE.
Definition: block.hpp:470
const uint8_t * wire() const
Return a raw pointer to the beginning of the encoded wire.
Definition: block.cpp:296
element_const_iterator find(uint32_t type) const
Find the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:441
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:285
Block blockFromValue() const
Definition: block.cpp:329
element_iterator erase(element_const_iterator position)
Erase a sub-element.
Definition: block.cpp:458
friend std::ostream & operator<<(std::ostream &os, const Block &block)
Print block to os.
Definition: block.cpp:502
Buffer::const_iterator begin() const
Get begin iterator of encoded wire.
Definition: block.cpp:278
void remove(uint32_t type)
Remove all sub-elements of the specified TLV-TYPE.
Definition: block.cpp:448
size_t size() const
Return the size of the encoded wire, i.e.
Definition: block.cpp:305
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:241
Buffer::const_iterator end() const
Get end iterator of encoded wire.
Definition: block.cpp:287
size_t m_size
Total size including Type-Length-Value.
Definition: block.hpp:476
Buffer::const_iterator m_valueEnd
Definition: block.hpp:468
void resetWire() noexcept
Reset wire buffer but keep TLV-TYPE and sub-elements (if any)
Definition: block.cpp:271
element_container m_elements
Contains the sub-elements.
Definition: block.hpp:482
static std::tuple< bool, Block > fromBuffer(ConstBufferPtr buffer, size_t offset=0)
Try to parse Block from a wire buffer.
Definition: block.cpp:167
void push_back(const Block &element)
Append a sub-element.
Definition: block.cpp:472
Block(Block &&) noexcept
Move constructor.
Block & operator=(const Block &)
Copy assignment operator.
Buffer::const_iterator m_end
Definition: block.hpp:465
Buffer::const_iterator m_valueBegin
Definition: block.hpp:467
Block(const Block &)
Copy constructor.
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:316
ConstBufferPtr getBuffer() const
Get underlying buffer.
Definition: block.hpp:275
bool hasValue() const noexcept
Check if the Block has a non-empty TLV-VALUE.
Definition: block.hpp:298
bool isValid() const noexcept
Check if the Block is valid.
Definition: block.hpp:212
element_container::iterator element_iterator
Definition: block.hpp:48
void encode()
Encode sub-elements into TLV-VALUE.
Definition: block.cpp:368
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:307
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:418
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:410
static Block fromStream(std::istream &is)
Parse Block from an input stream.
Definition: block.cpp:231
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:265
Buffer::const_iterator m_begin
Definition: block.hpp:464
shared_ptr< const Buffer > m_buffer
Underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields.
Definition: block.hpp:463
std::vector< Block > element_container
Definition: block.hpp:47
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:402
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:341
const Block & get(uint32_t type) const
Return the first sub-element of the specified TLV-TYPE.
Definition: block.cpp:429
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Definition: block.cpp:323
Block()
Create an invalid Block.
element_iterator insert(element_const_iterator pos, const Block &element)
Insert a sub-element.
Definition: block.cpp:479
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition: block.cpp:317
General-purpose automatically managed/resized buffer.
Definition: buffer.hpp:42
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
Error(const char *expectedType, uint32_t actualType)
Definition: tlv.cpp:27
EncodingImpl< EstimatorTag > EncodingEstimator
EncodingImpl< EncoderTag > EncodingBuffer
@ Invalid
Definition: tlv.hpp:64
Definition: data.cpp:25
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:139
constexpr detail::make_overload_t overload
Definition: overload.hpp:108