blob: 63f367767371c4a4096145b1886fd129f8a2f6f7 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shidb7464d2017-07-13 03:11:17 +00002/*
Junxiao Shi72c0c642018-04-20 15:41:09 +00003 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08004 *
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07005 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -08006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * 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.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080022 */
23
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -070024#ifndef NDN_ENCODING_BLOCK_HPP
25#define NDN_ENCODING_BLOCK_HPP
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080026
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080027#include "buffer.hpp"
Alexander Afanasyev74633892015-02-08 18:08:46 -080028#include "encoding-buffer-fwd.hpp"
Davide Pesavento88a0d812017-08-19 21:31:42 -040029#include "tlv.hpp"
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080030
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -070031namespace boost {
32namespace asio {
33class const_buffer;
34} // namespace asio
35} // namespace boost
36
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080037namespace ndn {
38
Junxiao Shi760cc7b2017-07-22 19:17:49 +000039/** @brief Represents a TLV element of NDN packet format
40 * @sa https://named-data.net/doc/ndn-tlv/tlv.html
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080041 */
42class Block
43{
44public:
Junxiao Shidb7464d2017-07-13 03:11:17 +000045 using element_container = std::vector<Block>;
46 using element_iterator = element_container::iterator;
47 using element_const_iterator = element_container::const_iterator;
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080048
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060049 class Error : public tlv::Error
Alexander Afanasyev937aa782014-03-21 13:17:57 -070050 {
Alexander Afanasyeva465e972014-03-22 17:21:49 -070051 public:
52 explicit
53 Error(const std::string& what)
Steve DiBenedetto54ce6682014-07-22 13:22:57 -060054 : tlv::Error(what)
Alexander Afanasyeva465e972014-03-22 17:21:49 -070055 {
56 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -070057 };
58
Junxiao Shi81a6c5d2014-11-30 00:14:42 -070059public: // constructor, creation, assignment
60 /** @brief Create an empty Block
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080061 */
62 Block();
63
Junxiao Shidb7464d2017-07-13 03:11:17 +000064 /** @brief Parse Block from an EncodingBuffer
65 * @param buffer an EncodingBuffer containing one TLV element
66 * @throw tlv::Error Type-Length parsing fails, or TLV-LENGTH does not match size of TLV-VALUE
Alexander Afanasyev15151312014-02-16 00:53:51 -080067 */
68 explicit
69 Block(const EncodingBuffer& buffer);
Alexander Afanasyev937aa782014-03-21 13:17:57 -070070
Junxiao Shidb7464d2017-07-13 03:11:17 +000071 /** @brief Parse Block from a wire Buffer
72 * @param buffer a Buffer containing one TLV element
73 * @note This constructor takes shared ownership of @p buffer.
74 * @throw tlv::Error Type-Length parsing fails, or TLV-LENGTH does not match size of TLV-VALUE
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080075 */
Alexander Afanasyev197e5652014-06-13 16:56:31 -070076 explicit
Alexander Afanasyev937aa782014-03-21 13:17:57 -070077 Block(const ConstBufferPtr& buffer);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -080078
Junxiao Shidb7464d2017-07-13 03:11:17 +000079 /** @brief Parse Block within boundaries of a wire Buffer
80 * @param buffer a Buffer containing an TLV element at [@p begin,@p end)
81 * @param begin begin position of the TLV element within @p buffer
82 * @param end end position of the TLV element within @p buffer
83 * @param verifyLength if true, check TLV-LENGTH equals size of TLV-VALUE
84 * @throw std::invalid_argument @p buffer is empty, or [@p begin,@p end) range are not within @p buffer
85 * @throw tlv::Error Type-Length parsing fails, or TLV-LENGTH does not match size of TLV-VALUE
86 * @note This overload automatically detects TLV-TYPE and position of TLV-VALUE.
Alexander Afanasyev187bc482014-02-06 15:04:04 -080087 */
Junxiao Shidb7464d2017-07-13 03:11:17 +000088 Block(ConstBufferPtr buffer, Buffer::const_iterator begin, Buffer::const_iterator end,
Alexander Afanasyev5964fb72014-02-18 12:42:45 -080089 bool verifyLength = true);
Alexander Afanasyev937aa782014-03-21 13:17:57 -070090
Junxiao Shidb7464d2017-07-13 03:11:17 +000091 /** @brief Parse Block within boundaries of an existing Block, reusing underlying wire Buffer
92 * @param block a Block whose buffer contains an TLV element at [@p begin,@p end)
93 * @param begin begin position of the TLV element within @p block
94 * @param end end position of the TLV element within @p block
95 * @param verifyLength if true, check TLV-LENGTH equals size of TLV-VALUE
96 * @throw std::invalid_argument [@p begin,@p end) range are not within @p block
97 * @throw tlv::Error Type-Length parsing fails, or TLV-LENGTH does not match size of TLV-VALUE
Alexander Afanasyev4448d292015-08-09 20:11:37 -070098 */
Junxiao Shidb7464d2017-07-13 03:11:17 +000099 Block(const Block& block, Buffer::const_iterator begin, Buffer::const_iterator end,
Alexander Afanasyev4448d292015-08-09 20:11:37 -0700100 bool verifyLength = true);
101
Junxiao Shidb7464d2017-07-13 03:11:17 +0000102 /** @brief Create a Block from a wire Buffer without parsing
103 * @param buffer a Buffer containing an TLV element at [@p begin,@p end)
104 * @param type TLV-TYPE
105 * @param begin begin position of the TLV element within @p buffer
106 * @param end end position of the TLV element within @p buffer
107 * @param valueBegin begin position of TLV-VALUE within @p buffer
108 * @param valueEnd end position of TLV-VALUE within @p buffer
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800109 */
Junxiao Shidb7464d2017-07-13 03:11:17 +0000110 Block(ConstBufferPtr buffer, uint32_t type,
111 Buffer::const_iterator begin, Buffer::const_iterator end,
112 Buffer::const_iterator valueBegin, Buffer::const_iterator valueEnd);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800113
Junxiao Shidb7464d2017-07-13 03:11:17 +0000114 /** @brief Parse Block from a raw buffer
115 * @param buf pointer to the first octet of an TLV element
116 * @param bufSize size of the raw buffer; may be more than size of the TLV element
117 * @throw tlv::Error Type-Length parsing fails, or size of TLV-VALUE exceeds @p bufSize
118 * @note This overload copies the TLV element into an internal wire buffer.
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700119 */
Junxiao Shidb7464d2017-07-13 03:11:17 +0000120 Block(const uint8_t* buf, size_t bufSize);
Yingdi Yu27158392014-01-20 13:04:20 -0800121
Junxiao Shidb7464d2017-07-13 03:11:17 +0000122 /** @brief Create an empty Block with specified TLV-TYPE
123 * @param type TLV-TYPE
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800124 */
Alexander Afanasyevf42ce132014-01-07 13:32:30 -0800125 explicit
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800126 Block(uint32_t type);
127
Junxiao Shidb7464d2017-07-13 03:11:17 +0000128 /** @brief Create a Block with specified TLV-TYPE and TLV-VALUE
129 * @param type TLV-TYPE
130 * @param value a Buffer containing the TLV-VALUE
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800131 */
Junxiao Shidb7464d2017-07-13 03:11:17 +0000132 Block(uint32_t type, ConstBufferPtr value);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800133
Junxiao Shidb7464d2017-07-13 03:11:17 +0000134 /** @brief Create a Block with specified TLV-TYPE and TLV-VALUE
135 * @param type TLV-TYPE
136 * @param value a Block to be nested as TLV-VALUE
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800137 */
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700138 Block(uint32_t type, const Block& value);
139
Junxiao Shidb7464d2017-07-13 03:11:17 +0000140 /** @brief Parse Block from an input stream
Junxiao Shi760cc7b2017-07-22 19:17:49 +0000141 * @throw tlv::Error TLV-LENGTH is zero or exceeds upper bound
142 * @warning If decoding fails, bytes are still consumed from the input stream.
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700143 */
Alexander Afanasyev9c578182014-05-14 17:28:28 -0700144 static Block
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700145 fromStream(std::istream& is);
146
Junxiao Shidb7464d2017-07-13 03:11:17 +0000147 /** @brief Try to parse Block from a wire buffer
148 * @param buffer a Buffer containing an TLV element at offset @p offset
149 * @param offset begin position of the TLV element within @p buffer
150 * @note This function does not throw exceptions upon decoding failure.
151 * @return true and the Block if parsing succeeds; otherwise false
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700152 */
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700153 static std::tuple<bool, Block>
154 fromBuffer(ConstBufferPtr buffer, size_t offset);
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700155
Junxiao Shidb7464d2017-07-13 03:11:17 +0000156 /** @brief Try to parse Block from a raw buffer
157 * @param buf pointer to the first octet of an TLV element
158 * @param bufSize size of the raw buffer; may be more than size of the TLV element
159 * @note This function does not throw exceptions upon decoding failure.
160 * @note This overload copies the TLV element into an internal wire buffer.
161 * @return true and the Block if parsing succeeds; otherwise false
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700162 */
163 static std::tuple<bool, Block>
Junxiao Shidb7464d2017-07-13 03:11:17 +0000164 fromBuffer(const uint8_t* buf, size_t bufSize);
Junxiao Shi02a4bf32015-02-21 21:07:46 -0700165
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700166public: // wire format
167 /** @brief Check if the Block is empty
Junxiao Shidb7464d2017-07-13 03:11:17 +0000168 *
169 * A Block is "empty" only if it is default-constructed. A Block with zero-length TLV-VALUE is
170 * not considered empty.
Alexander Afanasyev196b9aa2014-01-31 17:19:16 -0800171 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700172 bool
Junxiao Shidb7464d2017-07-13 03:11:17 +0000173 empty() const
174 {
175 return m_type == std::numeric_limits<uint32_t>::max();
176 }
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700177
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700178 /** @brief Check if the Block has fully encoded wire
Junxiao Shidb7464d2017-07-13 03:11:17 +0000179 *
180 * A Block has fully encoded wire if the underlying buffer exists and contains full
181 * Type-Length-Value instead of just TLV-VALUE field.
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800182 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700183 bool
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800184 hasWire() const;
185
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700186 /** @brief Reset wire buffer of the element
Junxiao Shidb7464d2017-07-13 03:11:17 +0000187 * @post empty() == true
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800188 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700189 void
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800190 reset();
191
Junxiao Shidb7464d2017-07-13 03:11:17 +0000192 /** @brief Reset wire buffer but keep TLV-TYPE and sub elements (if any)
193 * @post hasWire() == false
194 * @post hasValue() == false
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800195 */
Alexander Afanasyeva465e972014-03-22 17:21:49 -0700196 void
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800197 resetWire();
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800198
Junxiao Shidb7464d2017-07-13 03:11:17 +0000199 /** @brief Get begin iterator of encoded wire
200 * @pre hasWire() == true
201 */
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700202 Buffer::const_iterator
203 begin() const;
204
Junxiao Shidb7464d2017-07-13 03:11:17 +0000205 /** @brief Get end iterator of encoded wire
206 * @pre hasWire() == true
207 */
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700208 Buffer::const_iterator
209 end() const;
210
Junxiao Shidb7464d2017-07-13 03:11:17 +0000211 /** @brief Get pointer to encoded wire
212 * @pre hasWire() == true
213 */
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700214 const uint8_t*
215 wire() const;
216
Junxiao Shidb7464d2017-07-13 03:11:17 +0000217 /** @brief Get size of encoded wire, including Type-Length-Value
218 * @pre empty() == false
219 */
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700220 size_t
221 size() const;
222
Junxiao Shidb7464d2017-07-13 03:11:17 +0000223 /** @brief Get underlying buffer
224 */
225 shared_ptr<const Buffer>
226 getBuffer() const
227 {
228 return m_buffer;
229 }
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700230
Junxiao Shidb7464d2017-07-13 03:11:17 +0000231public: // type and value
232 /** @brief Get TLV-TYPE
233 */
234 uint32_t
235 type() const
236 {
237 return m_type;
238 }
239
240 /** @brief Get begin iterator of TLV-VALUE
241 *
242 * This property reflects whether the underlying buffer contains TLV-VALUE. If this is false,
243 * TLV-VALUE has zero-length. If this is true, TLV-VALUE may be zero-length.
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700244 */
245 bool
Junxiao Shidb7464d2017-07-13 03:11:17 +0000246 hasValue() const
247 {
248 return m_buffer != nullptr;
249 }
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700250
Junxiao Shidb7464d2017-07-13 03:11:17 +0000251 /** @brief Get begin iterator of TLV-VALUE
252 * @pre hasValue() == true
253 */
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700254 Buffer::const_iterator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000255 value_begin() const
256 {
257 return m_valueBegin;
258 }
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700259
Junxiao Shidb7464d2017-07-13 03:11:17 +0000260 /** @brief Get end iterator of TLV-VALUE
261 * @pre hasValue() == true
262 */
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700263 Buffer::const_iterator
Junxiao Shidb7464d2017-07-13 03:11:17 +0000264 value_end() const
265 {
266 return m_valueEnd;
267 }
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700268
Junxiao Shidb7464d2017-07-13 03:11:17 +0000269 /** @brief Get pointer to TLV-VALUE
270 */
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700271 const uint8_t*
272 value() const;
273
Junxiao Shidb7464d2017-07-13 03:11:17 +0000274 /** @brief Get size of TLV-VALUE aka TLV-LENGTH
275 */
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700276 size_t
277 value_size() const;
278
Yingdi Yu4270f202014-01-28 14:19:16 -0800279 Block
280 blockFromValue() const;
281
Junxiao Shidb7464d2017-07-13 03:11:17 +0000282public: // sub elements
283 /** @brief Parse TLV-VALUE into sub elements
284 * @post elements() reflects sub elements found in TLV-VALUE
285 * @throw tlv::Error TLV-VALUE is not a sequence of TLV elements
286 * @note This method does not perform recursive parsing.
287 * @note This method has no effect if elements() is already populated.
288 * @note This method is not really const, but it does not modify any data.
Alexander Afanasyev74633892015-02-08 18:08:46 -0800289 */
Junxiao Shidb7464d2017-07-13 03:11:17 +0000290 void
291 parse() const;
Alexander Afanasyev74633892015-02-08 18:08:46 -0800292
Junxiao Shidb7464d2017-07-13 03:11:17 +0000293 /** @brief Encode sub elements into TLV-VALUE
294 * @post TLV-VALUE contains sub elements from elements()
295 */
296 void
297 encode();
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700298
Junxiao Shidb7464d2017-07-13 03:11:17 +0000299 /** @brief Get the first sub element of specified TLV-TYPE
300 * @pre parse() has been executed
301 * @throw Error sub element of @p type does not exist
302 */
303 const Block&
304 get(uint32_t type) const;
Junxiao Shiaf8eeea2014-03-31 20:10:56 -0700305
Junxiao Shidb7464d2017-07-13 03:11:17 +0000306 /** @brief Find the first sub element of specified TLV-TYPE
307 * @pre parse() has been executed
308 * @return iterator in elements() to the found sub element, otherwise elements_end()
309 */
310 element_const_iterator
311 find(uint32_t type) const;
312
313 /** @brief Remove all sub elements of specified TLV-TYPE
314 * @pre parse() has been executed
315 * @post find(type) == elements_end()
316 */
317 void
318 remove(uint32_t type);
319
320 /** @brief Erase a sub element
321 */
322 element_iterator
323 erase(element_const_iterator position);
324
325 /** @brief Erase a range of sub elements
326 */
327 element_iterator
328 erase(element_const_iterator first, element_const_iterator last);
329
330 /** @brief Append a sub element
331 */
332 void
333 push_back(const Block& element);
334
335 /** @brief Insert a sub element
336 * @param pos position of new sub element
337 * @param element new sub element
338 * @return iterator in elements() to the new sub element
339 */
340 element_iterator
341 insert(element_const_iterator pos, const Block& element);
342
343 /** @brief Get container of sub elements
344 * @pre parse() has been executed
345 */
346 const element_container&
347 elements() const
348 {
349 return m_elements;
350 }
351
352 /** @brief Equivalent to elements().begin()
353 */
354 element_const_iterator
355 elements_begin() const
356 {
357 return m_elements.begin();
358 }
359
360 /** @brief Equivalent to elements().end()
361 */
362 element_const_iterator
363 elements_end() const
364 {
365 return m_elements.end();
366 }
367
368 /** @brief Equivalent to elements().size()
369 */
370 size_t
371 elements_size() const
372 {
373 return m_elements.size();
374 }
375
376public: // misc
377 /** @brief Implicit conversion to const_buffer
378 */
Alexander Afanasyev6a05b4b2014-07-18 17:23:00 -0700379 operator boost::asio::const_buffer() const;
380
Junxiao Shid2777fa2017-07-27 18:35:34 +0000381private:
382 /** @brief Estimate Block size as if sub elements are encoded into TLV-VALUE
383 */
384 size_t
385 encode(EncodingEstimator& estimator) const;
386
Junxiao Shi72c0c642018-04-20 15:41:09 +0000387 /** @brief Estimate TLV-LENGTH as if sub elements are encoded into TLV-VALUE
388 */
389 size_t
390 encodeValue(EncodingEstimator& estimator) const;
391
Junxiao Shid2777fa2017-07-27 18:35:34 +0000392 /** @brief Encode sub elements into TLV-VALUE and prepend Block to encoder
393 * @post TLV-VALUE contains sub elements from elements()
394 * @post internal buffer and iterators point to Encoder's buffer
395 */
396 size_t
397 encode(EncodingBuffer& encoder);
398
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800399protected:
Junxiao Shidb7464d2017-07-13 03:11:17 +0000400 /** @brief underlying buffer storing TLV-VALUE and possibly TLV-TYPE and TLV-LENGTH fields
401 *
402 * If m_buffer is nullptr, this is an empty Block with TLV-TYPE given in m_type.
403 * Otherwise,
404 * - [m_valueBegin, m_valueEnd) point to TLV-VALUE within m_buffer.
405 * - If m_begin != m_end, [m_begin,m_end) point to Type-Length-Value of this Block within m_buffer.
406 * Otherwise, m_buffer does not contain TLV-TYPE and TLV-LENGTH fields.
407 */
Alexander Afanasyev74633892015-02-08 18:08:46 -0800408 shared_ptr<const Buffer> m_buffer;
Junxiao Shidb7464d2017-07-13 03:11:17 +0000409 Buffer::const_iterator m_begin; ///< @sa m_buffer
410 Buffer::const_iterator m_end; ///< @sa m_buffer
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800411
Junxiao Shidb7464d2017-07-13 03:11:17 +0000412 Buffer::const_iterator m_valueBegin; ///< @sa m_buffer
413 Buffer::const_iterator m_valueEnd; ///< @sa m_buffer
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700414
Junxiao Shidb7464d2017-07-13 03:11:17 +0000415 uint32_t m_type; ///< TLV-TYPE
416
417 /** @brief total size including Type-Length-Value
418 *
419 * This field is valid only if empty() is false.
420 */
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800421 uint32_t m_size;
Alexander Afanasyev937aa782014-03-21 13:17:57 -0700422
Junxiao Shidb7464d2017-07-13 03:11:17 +0000423 /** @brief sub elements
424 *
425 * This field is valid only if parse() has been executed.
426 */
427 mutable element_container m_elements;
Junxiao Shi72c0c642018-04-20 15:41:09 +0000428
429 /** @brief Print @p block to @p os.
430 *
431 * Default-constructed block is printed as: `[invalid]`.
432 * Zero-length block is printed as: `TT[empty]`, where TT is TLV-TYPE in decimal.
433 * Non-zero-length block on which @c Block::parse is not called is printed as: `TT[LL]=VVVV`,
434 * where LL is TLV-LENGTH in decimal, and VVVV is TLV-VALUE is hexadecimal.
435 * Block on which @c Block::parse has been called in printed as: `TT[LL]={SUB,SUB}`,
436 * where SUB is a sub-element printed using this format.
437 */
438 friend std::ostream&
439 operator<<(std::ostream& os, const Block& block);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800440};
441
Junxiao Shidb7464d2017-07-13 03:11:17 +0000442/** @brief Compare whether two Blocks have same TLV-TYPE, TLV-LENGTH, and TLV-VALUE
443 */
444bool
445operator==(const Block& lhs, const Block& rhs);
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800446
Junxiao Shidb7464d2017-07-13 03:11:17 +0000447inline bool
448operator!=(const Block& lhs, const Block& rhs)
Alexander Afanasyev74633892015-02-08 18:08:46 -0800449{
Junxiao Shidb7464d2017-07-13 03:11:17 +0000450 return !(lhs == rhs);
Alexander Afanasyev29e5c3d2014-02-11 00:01:10 -0800451}
452
Junxiao Shi81a6c5d2014-11-30 00:14:42 -0700453} // namespace ndn
Alexander Afanasyev13bb51a2014-01-02 19:13:26 -0800454
Alexander Afanasyev258ec2b2014-05-14 16:15:37 -0700455#endif // NDN_ENCODING_BLOCK_HPP