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-2020 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 
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  void
201  reset() noexcept;
202 
208  void
209  resetWire() noexcept;
210 
216  bool
217  hasWire() const noexcept
218  {
219  return m_buffer != nullptr && m_begin != m_end;
220  }
221 
225  Buffer::const_iterator
226  begin() const;
227 
231  Buffer::const_iterator
232  end() const;
233 
238  const uint8_t*
239  wire() const;
240 
245  size_t
246  size() const;
247 
251  getBuffer() const
252  {
253  return m_buffer;
254  }
255 
256 public: // type and value
260  uint32_t
261  type() const
262  {
263  return m_type;
264  }
265 
273  bool
274  hasValue() const noexcept
275  {
276  return m_buffer != nullptr;
277  }
278 
282  Buffer::const_iterator
283  value_begin() const
284  {
285  return m_valueBegin;
286  }
287 
291  Buffer::const_iterator
292  value_end() const
293  {
294  return m_valueEnd;
295  }
296 
300  const uint8_t*
301  value() const noexcept;
302 
306  size_t
307  value_size() const noexcept;
308 
309  Block
310  blockFromValue() const;
311 
312 public: // sub-elements
320  void
321  parse() const;
322 
326  void
327  encode();
328 
333  const Block&
334  get(uint32_t type) const;
335 
342  find(uint32_t type) const;
343 
348  void
349  remove(uint32_t type);
350 
354  erase(element_const_iterator position);
355 
360 
363  void
364  push_back(const Block& element);
365 
372  insert(element_const_iterator pos, const Block& element);
373 
377  const element_container&
378  elements() const
379  {
380  return m_elements;
381  }
382 
387  {
388  return m_elements.begin();
389  }
390 
394  elements_end() const
395  {
396  return m_elements.end();
397  }
398 
401  size_t
403  {
404  return m_elements.size();
405  }
406 
407 public: // misc
410  operator boost::asio::const_buffer() const;
411 
412 private:
415  size_t
416  encode(EncodingEstimator& estimator) const;
417 
420  size_t
421  encodeValue(EncodingEstimator& estimator) const;
422 
427  size_t
428  encode(EncodingBuffer& encoder);
429 
430 protected:
439  shared_ptr<const Buffer> m_buffer;
440  Buffer::const_iterator m_begin;
441  Buffer::const_iterator m_end;
442 
443  Buffer::const_iterator m_valueBegin;
444  Buffer::const_iterator m_valueEnd;
445 
446  uint32_t m_type = tlv::Invalid;
447 
452  size_t m_size = 0;
453 
459 
469  friend std::ostream&
470  operator<<(std::ostream& os, const Block& block);
471 };
472 
473 inline
474 Block::Block(Block&&) noexcept = default;
475 
476 inline Block&
477 Block::operator=(Block&&) noexcept = default;
478 
481 bool
482 operator==(const Block& lhs, const Block& rhs);
483 
484 inline bool
485 operator!=(const Block& lhs, const Block& rhs)
486 {
487  return !(lhs == rhs);
488 }
489 
503 Block
504 operator "" _block(const char* input, std::size_t len);
505 
506 } // namespace ndn
507 
508 #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:439
Definition: data.cpp:26
Buffer::const_iterator m_valueBegin
Definition: block.hpp:443
Buffer::const_iterator m_begin
Definition: block.hpp:440
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:383
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
element_container m_elements
Contains the sub-elements.
Definition: block.hpp:458
Represents a TLV element of the NDN packet format.
Definition: block.hpp:42
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:217
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:386
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:378
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:283
Buffer::const_iterator m_valueEnd
Definition: block.hpp:444
ConstBufferPtr getBuffer() const
Get underlying buffer.
Definition: block.hpp:251
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:394
#define NDN_CXX_NODISCARD
Definition: backports.hpp:68
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:292
element_container::iterator element_iterator
Definition: block.hpp:46
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:402
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:188
bool hasValue() const noexcept
Check if the Block has a non-empty TLV-VALUE.
Definition: block.hpp:274
Buffer::const_iterator m_end
Definition: block.hpp:441
static ParseResult parse(const Data &data)
std::vector< Block > element_container
Definition: block.hpp:45
EncodingImpl< EncoderTag > EncodingBuffer
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:261
represents an error in TLV encoding or decoding
Definition: tlv.hpp:51
EncodingImpl< EstimatorTag > EncodingEstimator
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126