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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 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 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 43 | /** |
| 44 | * @brief Class representing wire element of the NDN packet |
| 45 | */ |
| 46 | class Block |
| 47 | { |
| 48 | public: |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 49 | typedef std::vector<Block> element_container; |
| 50 | typedef element_container::iterator element_iterator; |
| 51 | typedef element_container::const_iterator element_const_iterator; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 52 | |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 53 | /// @brief Error that can be thrown from Block |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 54 | class Error : public tlv::Error |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 55 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 56 | public: |
| 57 | explicit |
| 58 | Error(const std::string& what) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame^] | 59 | : tlv::Error(what) |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 60 | { |
| 61 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 64 | /** |
| 65 | * @brief Default constructor to create an empty Block |
| 66 | */ |
| 67 | Block(); |
| 68 | |
| 69 | /** |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 70 | * @brief Create block based on EncodingBuffer object |
| 71 | */ |
| 72 | explicit |
| 73 | Block(const EncodingBuffer& buffer); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 74 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 75 | /** |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 76 | * @brief A helper version of a constructor to create Block from the raw buffer (type |
| 77 | * and value-length parsing) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 78 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 79 | explicit |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 80 | Block(const ConstBufferPtr& buffer); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 81 | |
| 82 | /** |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 83 | * @brief Another helper to create block from a buffer, directly specifying boundaries |
| 84 | * of the block within the buffer |
| 85 | * |
| 86 | * This version will automatically detect type and position of the value within the block |
| 87 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 88 | Block(const ConstBufferPtr& buffer, |
| 89 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 90 | bool verifyLength = true); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 91 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 92 | /** |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 93 | * @brief A helper version of a constructor to create Block from the raw buffer (type |
| 94 | * and value-length parsing) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 95 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 96 | Block(const uint8_t* buffer, size_t maxlength); |
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 void* buffer, size_t maxlength); |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 99 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 100 | /** |
| 101 | * @brief Create Block from the wire buffer (no parsing) |
| 102 | * |
| 103 | * This version of the constructor does not do any parsing |
| 104 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 105 | Block(const ConstBufferPtr& wire, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 106 | uint32_t type, |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 107 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
| 108 | const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 109 | |
| 110 | /** |
| 111 | * @brief Create Block of a specific type with empty wire buffer |
| 112 | */ |
Alexander Afanasyev | f42ce13 | 2014-01-07 13:32:30 -0800 | [diff] [blame] | 113 | explicit |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 114 | Block(uint32_t type); |
| 115 | |
| 116 | /** |
| 117 | * @brief Create Block of a specific type with the specified value |
| 118 | * |
| 119 | * The underlying buffer hold only value, additional operations are needed |
| 120 | * to construct wire encoding, one need to prepend the wire buffer with type |
| 121 | * and value-length VAR-NUMBERs |
| 122 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 123 | Block(uint32_t type, const ConstBufferPtr& value); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * @brief Create nested Block of a specific type with the specified value |
| 127 | * |
| 128 | * The underlying buffer hold only value, additional operations are needed |
| 129 | * to construct wire encoding, one need to prepend the wire buffer with type |
| 130 | * and value-length VAR-NUMBERs |
| 131 | */ |
Alexander Afanasyev | f42ce13 | 2014-01-07 13:32:30 -0800 | [diff] [blame] | 132 | explicit |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 133 | Block(uint32_t type, const Block& value); |
| 134 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 135 | /* |
| 136 | * @brief A helper version of a constructor to create Block from the stream |
| 137 | * |
| 138 | * @deprecated Use Block::fromStream instead |
| 139 | */ |
| 140 | explicit |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 141 | DEPRECATED(Block(std::istream& is)) |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 142 | { |
| 143 | *this = Block::fromStream(is); |
| 144 | } |
| 145 | |
| 146 | /* |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 147 | * @brief Constructor Block from the stream |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 148 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 149 | static Block |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 150 | fromStream(std::istream& is); |
| 151 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 152 | /** |
| 153 | * @brief Try to construct block from Buffer, referencing data block pointed by wire |
| 154 | * |
| 155 | * @throws This method never throws an exception |
| 156 | * |
| 157 | * @returns true if Block successfully created, false if block cannot be created |
| 158 | */ |
| 159 | static bool |
| 160 | fromBuffer(const ConstBufferPtr& wire, size_t offset, Block& block); |
| 161 | |
| 162 | /** |
| 163 | * @brief Try to construct block from Buffer, referencing data block pointed by wire |
| 164 | * |
| 165 | * @throws This method never throws an exception |
| 166 | * |
| 167 | * @returns true if Block successfully created, false if block cannot be created |
| 168 | */ |
| 169 | static bool |
| 170 | fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block); |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 171 | |
| 172 | /** |
| 173 | * @brief Check if the Block is empty |
| 174 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 175 | bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 176 | empty() const; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 178 | /** |
| 179 | * @brief Check if the Block has fully encoded wire |
| 180 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 181 | bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 182 | hasWire() const; |
| 183 | |
| 184 | /** |
| 185 | * @brief Check if the Block has value block (no type and length are encoded) |
| 186 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 187 | bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 188 | hasValue() const; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 189 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 190 | /** |
| 191 | * @brief Reset wire buffer of the element |
| 192 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 193 | void |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 194 | reset(); |
| 195 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 196 | /** |
| 197 | * @brief Reset wire buffer but keep sub elements (if any) |
| 198 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 199 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 200 | resetWire(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 201 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 202 | /** |
| 203 | * @brief Parse wire buffer into subblocks |
| 204 | * |
| 205 | * This method is not really const, but it does not modify any data. It simply |
| 206 | * parses contents of the buffer into subblocks |
| 207 | */ |
| 208 | void |
| 209 | parse() const; |
| 210 | |
| 211 | /** |
| 212 | * @brief Encode subblocks into wire buffer |
| 213 | */ |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 214 | void |
| 215 | encode(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 216 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 217 | uint32_t |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 218 | type() const; |
| 219 | |
| 220 | /** |
| 221 | * @brief Get the first subelement of the requested type |
| 222 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 223 | const Block& |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 224 | get(uint32_t type) const; |
| 225 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 226 | element_const_iterator |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 227 | find(uint32_t type) const; |
| 228 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 229 | void |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 230 | remove(uint32_t type); |
| 231 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 232 | element_iterator |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 233 | erase(element_iterator position); |
| 234 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 235 | element_iterator |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 236 | erase(element_iterator first, element_iterator last); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 237 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 238 | void |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 239 | push_back(const Block& element); |
| 240 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 241 | Buffer::const_iterator |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 242 | begin() const; |
| 243 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 244 | Buffer::const_iterator |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 245 | end() const; |
| 246 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 247 | const uint8_t* |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 248 | wire() const; |
| 249 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 250 | size_t |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 251 | size() const; |
| 252 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 253 | Buffer::const_iterator |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 254 | value_begin() const; |
| 255 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 256 | Buffer::const_iterator |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 257 | value_end() const; |
| 258 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 259 | const uint8_t* |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 260 | value() const; |
| 261 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 262 | size_t |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 263 | value_size() const; |
| 264 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 265 | /** |
| 266 | * @brief Get all subelements |
| 267 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 268 | const element_container& |
| 269 | elements() const; |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 270 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 271 | element_const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 272 | elements_begin() const; |
| 273 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 274 | element_const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 275 | elements_end() const; |
| 276 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 277 | size_t |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 278 | elements_size() const; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 279 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 280 | Block |
| 281 | blockFromValue() const; |
| 282 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 283 | public: // EqualityComparable concept |
| 284 | bool |
| 285 | operator==(const Block& other) const; |
| 286 | |
| 287 | bool |
| 288 | operator!=(const Block& other) const; |
| 289 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 290 | public: // ConvertibleToConstBuffer |
| 291 | operator boost::asio::const_buffer() const; |
| 292 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 293 | protected: |
| 294 | ConstBufferPtr m_buffer; |
| 295 | |
| 296 | uint32_t m_type; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 297 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 298 | Buffer::const_iterator m_begin; |
| 299 | Buffer::const_iterator m_end; |
| 300 | uint32_t m_size; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 301 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 302 | Buffer::const_iterator m_value_begin; |
| 303 | Buffer::const_iterator m_value_end; |
| 304 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 305 | mutable element_container m_subBlocks; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 306 | friend class EncodingImpl<true>; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 307 | }; |
| 308 | |
| 309 | //////////////////////////////////////////////////////////////////////////////// |
| 310 | //////////////////////////////////////////////////////////////////////////////// |
| 311 | //////////////////////////////////////////////////////////////////////////////// |
| 312 | |
| 313 | inline bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 314 | Block::empty() const |
| 315 | { |
| 316 | return m_type == std::numeric_limits<uint32_t>::max(); |
| 317 | } |
| 318 | |
| 319 | |
| 320 | inline bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 321 | Block::hasWire() const |
| 322 | { |
| 323 | return m_buffer && (m_begin != m_end); |
| 324 | } |
| 325 | |
| 326 | inline bool |
| 327 | Block::hasValue() const |
| 328 | { |
| 329 | return static_cast<bool>(m_buffer); |
| 330 | } |
| 331 | |
| 332 | inline void |
| 333 | Block::reset() |
| 334 | { |
| 335 | m_buffer.reset(); // reset of the shared_ptr |
| 336 | m_subBlocks.clear(); // remove all parsed subelements |
| 337 | |
| 338 | m_type = std::numeric_limits<uint32_t>::max(); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 339 | m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 340 | } |
| 341 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 342 | inline void |
| 343 | Block::resetWire() |
| 344 | { |
| 345 | m_buffer.reset(); // reset of the shared_ptr |
| 346 | // keep subblocks |
| 347 | |
| 348 | // keep type |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 349 | m_begin = m_end = m_value_begin = m_value_end = Buffer::const_iterator(); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 350 | } |
| 351 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 352 | inline uint32_t |
| 353 | Block::type() const |
| 354 | { |
| 355 | return m_type; |
| 356 | } |
| 357 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 358 | inline Block::element_const_iterator |
| 359 | Block::find(uint32_t type) const |
| 360 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 361 | for (element_const_iterator i = m_subBlocks.begin(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 362 | i != m_subBlocks.end(); |
| 363 | i++) |
| 364 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 365 | if (i->type() == type) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 366 | { |
| 367 | return i; |
| 368 | } |
| 369 | } |
| 370 | return m_subBlocks.end(); |
| 371 | } |
| 372 | |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 373 | inline void |
| 374 | Block::remove(uint32_t type) |
| 375 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 376 | resetWire(); |
| 377 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 378 | element_container newContainer; |
| 379 | newContainer.reserve(m_subBlocks.size()); |
| 380 | for (element_iterator i = m_subBlocks.begin(); |
| 381 | i != m_subBlocks.end(); |
| 382 | ++i) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 383 | { |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 384 | if (i->type() != type) |
| 385 | newContainer.push_back(*i); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 386 | } |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 387 | m_subBlocks.swap(newContainer); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 388 | } |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 389 | |
| 390 | inline Block::element_iterator |
| 391 | Block::erase(Block::element_iterator position) |
| 392 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 393 | resetWire(); |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 394 | return m_subBlocks.erase(position); |
| 395 | } |
| 396 | |
| 397 | inline Block::element_iterator |
| 398 | Block::erase(Block::element_iterator first, Block::element_iterator last) |
| 399 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 400 | resetWire(); |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 401 | return m_subBlocks.erase(first, last); |
| 402 | } |
| 403 | |
| 404 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 405 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 406 | Block::push_back(const Block& element) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 407 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 408 | resetWire(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 409 | m_subBlocks.push_back(element); |
| 410 | } |
| 411 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 412 | inline Buffer::const_iterator |
| 413 | Block::begin() const |
| 414 | { |
| 415 | if (!hasWire()) |
| 416 | throw Error("Underlying wire buffer is empty"); |
| 417 | |
| 418 | return m_begin; |
| 419 | } |
| 420 | |
| 421 | inline Buffer::const_iterator |
| 422 | Block::end() const |
| 423 | { |
| 424 | if (!hasWire()) |
| 425 | throw Error("Underlying wire buffer is empty"); |
| 426 | |
| 427 | return m_end; |
| 428 | } |
| 429 | |
| 430 | inline size_t |
| 431 | Block::size() const |
| 432 | { |
| 433 | if (hasWire() || hasValue()) { |
| 434 | return m_size; |
| 435 | } |
| 436 | else |
| 437 | throw Error("Block size cannot be determined (undefined block size)"); |
| 438 | } |
| 439 | |
| 440 | inline Buffer::const_iterator |
| 441 | Block::value_begin() const |
| 442 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 443 | return m_value_begin; |
| 444 | } |
| 445 | |
| 446 | inline Buffer::const_iterator |
| 447 | Block::value_end() const |
| 448 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 449 | return m_value_end; |
| 450 | } |
| 451 | |
| 452 | inline const uint8_t* |
| 453 | Block::wire() const |
| 454 | { |
| 455 | if (!hasWire()) |
| 456 | throw Error("(Block::wire) Underlying wire buffer is empty"); |
| 457 | |
| 458 | return &*m_begin; |
| 459 | } |
| 460 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 461 | inline const uint8_t* |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 462 | Block::value() const |
| 463 | { |
| 464 | if (!hasValue()) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 465 | return 0; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 466 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 467 | return &*m_value_begin; |
| 468 | } |
| 469 | |
| 470 | inline size_t |
| 471 | Block::value_size() const |
| 472 | { |
| 473 | if (!hasValue()) |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 474 | return 0; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 475 | |
| 476 | return m_value_end - m_value_begin; |
| 477 | } |
| 478 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 479 | inline const Block::element_container& |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 480 | Block::elements() const |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 481 | { |
| 482 | return m_subBlocks; |
| 483 | } |
| 484 | |
| 485 | inline Block::element_const_iterator |
| 486 | Block::elements_begin() const |
| 487 | { |
| 488 | return m_subBlocks.begin(); |
| 489 | } |
| 490 | |
| 491 | inline Block::element_const_iterator |
| 492 | Block::elements_end() const |
| 493 | { |
| 494 | return m_subBlocks.end(); |
| 495 | } |
| 496 | |
| 497 | inline size_t |
| 498 | Block::elements_size() const |
| 499 | { |
| 500 | return m_subBlocks.size(); |
| 501 | } |
| 502 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 503 | inline bool |
| 504 | Block::operator==(const Block& other) const |
| 505 | { |
| 506 | return (this->size() == other.size()) && |
| 507 | std::equal(this->begin(), this->end(), other.begin()); |
| 508 | } |
| 509 | |
| 510 | inline bool |
| 511 | Block::operator!=(const Block& other) const |
| 512 | { |
| 513 | return !this->operator==(other); |
| 514 | } |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 515 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 516 | } // ndn |
| 517 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 518 | #endif // NDN_ENCODING_BLOCK_HPP |