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