Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 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. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 24 | #ifndef NDN_ENCODING_BLOCK_HPP |
| 25 | #define NDN_ENCODING_BLOCK_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 27 | #include "../common.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 29 | #include "buffer.hpp" |
| 30 | #include "tlv.hpp" |
| 31 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 32 | namespace boost { |
| 33 | namespace asio { |
| 34 | class const_buffer; |
| 35 | } // namespace asio |
| 36 | } // namespace boost |
| 37 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 38 | namespace ndn { |
| 39 | |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 40 | template<bool> class EncodingImpl; |
| 41 | typedef EncodingImpl<true> EncodingBuffer; |
| 42 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 43 | /** @brief Class representing a wire element of NDN-TLV packet format |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 44 | */ |
| 45 | class Block |
| 46 | { |
| 47 | public: |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 48 | typedef std::vector<Block> element_container; |
| 49 | typedef element_container::iterator element_iterator; |
| 50 | typedef element_container::const_iterator element_const_iterator; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 51 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 52 | class Error : public tlv::Error |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 53 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 54 | public: |
| 55 | explicit |
| 56 | Error(const std::string& what) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 57 | : tlv::Error(what) |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 58 | { |
| 59 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 60 | }; |
| 61 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 62 | public: // constructor, creation, assignment |
| 63 | /** @brief Create an empty Block |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 64 | */ |
| 65 | Block(); |
| 66 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 67 | /** @brief Create a Block based on EncodingBuffer object |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 68 | */ |
| 69 | explicit |
| 70 | Block(const EncodingBuffer& buffer); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 71 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 72 | /** @brief Create a Block from the raw buffer with Type-Length parsing |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 73 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 74 | explicit |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 75 | Block(const ConstBufferPtr& buffer); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 76 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 77 | /** @brief Create a Block from a buffer, directly specifying boundaries |
| 78 | * of the block within the buffer |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 79 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 80 | * This overload will automatically detect type and position of the value within the block |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 81 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 82 | Block(const ConstBufferPtr& buffer, |
| 83 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 84 | bool verifyLength = true); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 85 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 86 | /** @brief Create a Block from the raw buffer with Type-Length parsing |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 87 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 88 | Block(const uint8_t* buffer, size_t maxlength); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 89 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 90 | /** @brief Create a Block from the raw buffer with Type-Length parsing |
| 91 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 92 | Block(const void* buffer, size_t maxlength); |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 93 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 94 | /** @brief Create a Block from the wire buffer (no parsing) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 95 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 96 | * This overload does not do any parsing |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 97 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 98 | Block(const ConstBufferPtr& wire, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 99 | uint32_t type, |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 100 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
| 101 | const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 102 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 103 | /** @brief Create Block of a specific type with empty wire buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 104 | */ |
Alexander Afanasyev | f42ce13 | 2014-01-07 13:32:30 -0800 | [diff] [blame] | 105 | explicit |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 106 | Block(uint32_t type); |
| 107 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 108 | /** @brief Create a Block of a specific type with the specified value |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 109 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 110 | * The underlying buffer holds only value Additional operations are needed |
| 111 | * to construct wire encoding, one need to prepend the wire buffer with type |
| 112 | * and value-length VAR-NUMBERs |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 113 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 114 | Block(uint32_t type, const ConstBufferPtr& value); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 115 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 116 | /** @brief Create a nested Block of a specific type with the specified value |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 117 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 118 | * The underlying buffer holds only value. Additional operations are needed |
| 119 | * to construct wire encoding, one need to prepend the wire buffer with type |
| 120 | * and value-length VAR-NUMBERs |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 121 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 122 | Block(uint32_t type, const Block& value); |
| 123 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 124 | /** @brief Create a Block from an input stream |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 125 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 126 | static Block |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 127 | fromStream(std::istream& is); |
| 128 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 129 | /** @brief Try to construct block from Buffer, referencing data block pointed by wire |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 130 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 131 | * This method does not throw upon decoding error. |
| 132 | * @return true if Block successfully created, false if block cannot be created |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 133 | */ |
| 134 | static bool |
| 135 | fromBuffer(const ConstBufferPtr& wire, size_t offset, Block& block); |
| 136 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 137 | /** @brief Try to construct block from Buffer, referencing data block pointed by wire |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 138 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 139 | * This method does not throw upon decoding error. |
| 140 | * @return true if Block successfully created, false if block cannot be created |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 141 | */ |
| 142 | static bool |
| 143 | fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block); |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 144 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 145 | public: // wire format |
| 146 | /** @brief Check if the Block is empty |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 147 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 148 | bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 149 | empty() const; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 150 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 151 | /** @brief Check if the Block has fully encoded wire |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 152 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 153 | bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 154 | hasWire() const; |
| 155 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 156 | /** @brief Reset wire buffer of the element |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 157 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 158 | void |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 159 | reset(); |
| 160 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 161 | /** @brief Reset wire buffer but keep sub elements (if any) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 162 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 163 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 164 | resetWire(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 165 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 166 | Buffer::const_iterator |
| 167 | begin() const; |
| 168 | |
| 169 | Buffer::const_iterator |
| 170 | end() const; |
| 171 | |
| 172 | const uint8_t* |
| 173 | wire() const; |
| 174 | |
| 175 | size_t |
| 176 | size() const; |
| 177 | |
| 178 | public: // type and value |
| 179 | uint32_t |
| 180 | type() const; |
| 181 | |
| 182 | /** @brief Check if the Block has value block (no type and length are encoded) |
| 183 | */ |
| 184 | bool |
| 185 | hasValue() const; |
| 186 | |
| 187 | Buffer::const_iterator |
| 188 | value_begin() const; |
| 189 | |
| 190 | Buffer::const_iterator |
| 191 | value_end() const; |
| 192 | |
| 193 | const uint8_t* |
| 194 | value() const; |
| 195 | |
| 196 | size_t |
| 197 | value_size() const; |
| 198 | |
| 199 | public: // sub elements |
| 200 | /** @brief Parse wire buffer into subblocks |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 201 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 202 | * This method is not really const, but it does not modify any data. It simply |
| 203 | * parses contents of the buffer into subblocks |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 204 | */ |
| 205 | void |
| 206 | parse() const; |
| 207 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 208 | /** @brief Encode subblocks into wire buffer |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 209 | */ |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 210 | void |
| 211 | encode(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 212 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 213 | /** @brief Get the first subelement of the requested type |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 214 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 215 | const Block& |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 216 | get(uint32_t type) const; |
| 217 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 218 | element_const_iterator |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 219 | find(uint32_t type) const; |
| 220 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 221 | void |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 222 | remove(uint32_t type); |
| 223 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 224 | element_iterator |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 225 | erase(element_iterator position); |
| 226 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 227 | element_iterator |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 228 | erase(element_iterator first, element_iterator last); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 229 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 230 | void |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 231 | push_back(const Block& element); |
| 232 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 233 | /** @brief Get all subelements |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 234 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 235 | const element_container& |
| 236 | elements() const; |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 237 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 238 | element_const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 239 | elements_begin() const; |
| 240 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 241 | element_const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 242 | elements_end() const; |
| 243 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 244 | size_t |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 245 | elements_size() const; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 246 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 247 | Block |
| 248 | blockFromValue() const; |
| 249 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 250 | public: // EqualityComparable concept |
| 251 | bool |
| 252 | operator==(const Block& other) const; |
| 253 | |
| 254 | bool |
| 255 | operator!=(const Block& other) const; |
| 256 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 257 | public: // ConvertibleToConstBuffer |
| 258 | operator boost::asio::const_buffer() const; |
| 259 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 260 | protected: |
| 261 | ConstBufferPtr m_buffer; |
| 262 | |
| 263 | uint32_t m_type; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 264 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 265 | Buffer::const_iterator m_begin; |
| 266 | Buffer::const_iterator m_end; |
| 267 | uint32_t m_size; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 268 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 269 | Buffer::const_iterator m_value_begin; |
| 270 | Buffer::const_iterator m_value_end; |
| 271 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 272 | mutable element_container m_subBlocks; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 273 | friend class EncodingImpl<true>; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 274 | }; |
| 275 | |
| 276 | //////////////////////////////////////////////////////////////////////////////// |
| 277 | //////////////////////////////////////////////////////////////////////////////// |
| 278 | //////////////////////////////////////////////////////////////////////////////// |
| 279 | |
| 280 | inline bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 281 | Block::empty() const |
| 282 | { |
| 283 | return m_type == std::numeric_limits<uint32_t>::max(); |
| 284 | } |
| 285 | |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 286 | inline bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 287 | Block::hasWire() const |
| 288 | { |
| 289 | return m_buffer && (m_begin != m_end); |
| 290 | } |
| 291 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 292 | inline Buffer::const_iterator |
| 293 | Block::begin() const |
| 294 | { |
| 295 | if (!hasWire()) |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 296 | throw Error("Underlying wire buffer is empty"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 297 | |
| 298 | return m_begin; |
| 299 | } |
| 300 | |
| 301 | inline Buffer::const_iterator |
| 302 | Block::end() const |
| 303 | { |
| 304 | if (!hasWire()) |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 305 | throw Error("Underlying wire buffer is empty"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 306 | |
| 307 | return m_end; |
| 308 | } |
| 309 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 310 | inline const uint8_t* |
| 311 | Block::wire() const |
| 312 | { |
| 313 | if (!hasWire()) |
| 314 | throw Error("(Block::wire) Underlying wire buffer is empty"); |
| 315 | |
| 316 | return &*m_begin; |
| 317 | } |
| 318 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 319 | inline size_t |
| 320 | Block::size() const |
| 321 | { |
| 322 | if (hasWire() || hasValue()) { |
| 323 | return m_size; |
| 324 | } |
| 325 | else |
| 326 | throw Error("Block size cannot be determined (undefined block size)"); |
| 327 | } |
| 328 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 329 | inline uint32_t |
| 330 | Block::type() const |
| 331 | { |
| 332 | return m_type; |
| 333 | } |
| 334 | |
| 335 | inline bool |
| 336 | Block::hasValue() const |
| 337 | { |
| 338 | return static_cast<bool>(m_buffer); |
| 339 | } |
| 340 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 341 | inline Buffer::const_iterator |
| 342 | Block::value_begin() const |
| 343 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 344 | return m_value_begin; |
| 345 | } |
| 346 | |
| 347 | inline Buffer::const_iterator |
| 348 | Block::value_end() const |
| 349 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 350 | return m_value_end; |
| 351 | } |
| 352 | |
| 353 | inline const uint8_t* |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 354 | Block::value() const |
| 355 | { |
| 356 | if (!hasValue()) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 357 | return 0; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 358 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 359 | return &*m_value_begin; |
| 360 | } |
| 361 | |
| 362 | inline size_t |
| 363 | Block::value_size() const |
| 364 | { |
| 365 | if (!hasValue()) |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 366 | return 0; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 367 | |
| 368 | return m_value_end - m_value_begin; |
| 369 | } |
| 370 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 371 | inline Block::element_iterator |
| 372 | Block::erase(Block::element_iterator position) |
| 373 | { |
| 374 | resetWire(); |
| 375 | return m_subBlocks.erase(position); |
| 376 | } |
| 377 | |
| 378 | inline Block::element_iterator |
| 379 | Block::erase(Block::element_iterator first, Block::element_iterator last) |
| 380 | { |
| 381 | resetWire(); |
| 382 | return m_subBlocks.erase(first, last); |
| 383 | } |
| 384 | |
| 385 | inline void |
| 386 | Block::push_back(const Block& element) |
| 387 | { |
| 388 | resetWire(); |
| 389 | m_subBlocks.push_back(element); |
| 390 | } |
| 391 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 392 | inline const Block::element_container& |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 393 | Block::elements() const |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 394 | { |
| 395 | return m_subBlocks; |
| 396 | } |
| 397 | |
| 398 | inline Block::element_const_iterator |
| 399 | Block::elements_begin() const |
| 400 | { |
| 401 | return m_subBlocks.begin(); |
| 402 | } |
| 403 | |
| 404 | inline Block::element_const_iterator |
| 405 | Block::elements_end() const |
| 406 | { |
| 407 | return m_subBlocks.end(); |
| 408 | } |
| 409 | |
| 410 | inline size_t |
| 411 | Block::elements_size() const |
| 412 | { |
| 413 | return m_subBlocks.size(); |
| 414 | } |
| 415 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 416 | inline bool |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 417 | Block::operator!=(const Block& other) const |
| 418 | { |
| 419 | return !this->operator==(other); |
| 420 | } |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 421 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 422 | } // namespace ndn |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 423 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 424 | #endif // NDN_ENCODING_BLOCK_HPP |