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  static std::tuple<bool, Block>
169  fromBuffer(ConstBufferPtr buffer, size_t offset);
170 
178  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  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
shared_ptr< const Buffer > m_buffer
Underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields.
Definition: block.hpp:452
Definition: data.cpp:26
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:391
Buffer::const_iterator m_valueBegin
Definition: block.hpp:456
Buffer::const_iterator m_begin
Definition: block.hpp:453
std::ostream & operator<<(std::ostream &os, const Data &data)
Definition: data.cpp:321
ConstBufferPtr getBuffer() const
Get underlying buffer.
Definition: block.hpp:264
element_container::const_iterator element_const_iterator
Definition: block.hpp:47
element_container m_elements
Contains the sub-elements.
Definition: block.hpp:471
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
bool empty() const noexcept
Check if the Block is empty.
Definition: block.hpp:201
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:296
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:305
Buffer::const_iterator m_valueEnd
Definition: block.hpp:457
element_container::iterator element_iterator
Definition: block.hpp:46
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:415
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
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:287
static std::tuple< bool, const uint8_t *, size_t, const uint8_t *, size_t > parse(const Data &data)
Buffer::const_iterator m_end
Definition: block.hpp:454
element_const_iterator elements_end() const
Equivalent to elements().end()
Definition: block.hpp:407
element_const_iterator elements_begin() const
Equivalent to elements().begin()
Definition: block.hpp:399
std::vector< Block > element_container
Definition: block.hpp:45
EncodingImpl< EncoderTag > EncodingBuffer
represents an error in TLV encoding or decoding
Definition: tlv.hpp:52
EncodingImpl< EstimatorTag > EncodingEstimator
shared_ptr< const Buffer > ConstBufferPtr
Definition: buffer.hpp:126