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