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