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