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