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