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 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 251 | protected: |
| 252 | ConstBufferPtr m_buffer; |
| 253 | |
| 254 | uint32_t m_type; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 255 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 256 | Buffer::const_iterator m_begin; |
| 257 | Buffer::const_iterator m_end; |
| 258 | uint32_t m_size; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 259 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 260 | Buffer::const_iterator m_value_begin; |
| 261 | Buffer::const_iterator m_value_end; |
| 262 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 263 | mutable element_container m_subBlocks; |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 264 | friend class EncodingImpl<true>; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | //////////////////////////////////////////////////////////////////////////////// |
| 268 | //////////////////////////////////////////////////////////////////////////////// |
| 269 | //////////////////////////////////////////////////////////////////////////////// |
| 270 | |
| 271 | inline bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 272 | Block::empty() const |
| 273 | { |
| 274 | return m_type == std::numeric_limits<uint32_t>::max(); |
| 275 | } |
| 276 | |
| 277 | |
| 278 | inline bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 279 | Block::hasWire() const |
| 280 | { |
| 281 | return m_buffer && (m_begin != m_end); |
| 282 | } |
| 283 | |
| 284 | inline bool |
| 285 | Block::hasValue() const |
| 286 | { |
| 287 | return static_cast<bool>(m_buffer); |
| 288 | } |
| 289 | |
| 290 | inline void |
| 291 | Block::reset() |
| 292 | { |
| 293 | m_buffer.reset(); // reset of the shared_ptr |
| 294 | m_subBlocks.clear(); // remove all parsed subelements |
| 295 | |
| 296 | m_type = std::numeric_limits<uint32_t>::max(); |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 297 | 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] | 298 | } |
| 299 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 300 | inline void |
| 301 | Block::resetWire() |
| 302 | { |
| 303 | m_buffer.reset(); // reset of the shared_ptr |
| 304 | // keep subblocks |
| 305 | |
| 306 | // keep type |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 307 | 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] | 308 | } |
| 309 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 310 | inline uint32_t |
| 311 | Block::type() const |
| 312 | { |
| 313 | return m_type; |
| 314 | } |
| 315 | |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 316 | inline const Block& |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 317 | Block::get(uint32_t type) const |
| 318 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 319 | for (element_const_iterator i = m_subBlocks.begin(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 320 | i != m_subBlocks.end(); |
| 321 | i++) |
| 322 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 323 | if (i->type() == type) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 324 | { |
| 325 | return *i; |
| 326 | } |
| 327 | } |
| 328 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 329 | throw Error("(Block::get) Requested a non-existed type [" + |
| 330 | boost::lexical_cast<std::string>(type) + "] from Block"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 331 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 332 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 333 | inline Block::element_const_iterator |
| 334 | Block::find(uint32_t type) const |
| 335 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 336 | for (element_const_iterator i = m_subBlocks.begin(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 337 | i != m_subBlocks.end(); |
| 338 | i++) |
| 339 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 340 | if (i->type() == type) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 341 | { |
| 342 | return i; |
| 343 | } |
| 344 | } |
| 345 | return m_subBlocks.end(); |
| 346 | } |
| 347 | |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 348 | inline void |
| 349 | Block::remove(uint32_t type) |
| 350 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 351 | resetWire(); |
| 352 | |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 353 | element_container newContainer; |
| 354 | newContainer.reserve(m_subBlocks.size()); |
| 355 | for (element_iterator i = m_subBlocks.begin(); |
| 356 | i != m_subBlocks.end(); |
| 357 | ++i) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 358 | { |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 359 | if (i->type() != type) |
| 360 | newContainer.push_back(*i); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 361 | } |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 362 | m_subBlocks.swap(newContainer); |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 363 | } |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 364 | |
| 365 | inline Block::element_iterator |
| 366 | Block::erase(Block::element_iterator position) |
| 367 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 368 | resetWire(); |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 369 | return m_subBlocks.erase(position); |
| 370 | } |
| 371 | |
| 372 | inline Block::element_iterator |
| 373 | Block::erase(Block::element_iterator first, Block::element_iterator last) |
| 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(first, last); |
| 377 | } |
| 378 | |
| 379 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 380 | inline void |
| 381 | Block::push_back(const Block &element) |
| 382 | { |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 383 | resetWire(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 384 | m_subBlocks.push_back(element); |
| 385 | } |
| 386 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 387 | inline Buffer::const_iterator |
| 388 | Block::begin() const |
| 389 | { |
| 390 | if (!hasWire()) |
| 391 | throw Error("Underlying wire buffer is empty"); |
| 392 | |
| 393 | return m_begin; |
| 394 | } |
| 395 | |
| 396 | inline Buffer::const_iterator |
| 397 | Block::end() const |
| 398 | { |
| 399 | if (!hasWire()) |
| 400 | throw Error("Underlying wire buffer is empty"); |
| 401 | |
| 402 | return m_end; |
| 403 | } |
| 404 | |
| 405 | inline size_t |
| 406 | Block::size() const |
| 407 | { |
| 408 | if (hasWire() || hasValue()) { |
| 409 | return m_size; |
| 410 | } |
| 411 | else |
| 412 | throw Error("Block size cannot be determined (undefined block size)"); |
| 413 | } |
| 414 | |
| 415 | inline Buffer::const_iterator |
| 416 | Block::value_begin() const |
| 417 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 418 | return m_value_begin; |
| 419 | } |
| 420 | |
| 421 | inline Buffer::const_iterator |
| 422 | Block::value_end() const |
| 423 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 424 | return m_value_end; |
| 425 | } |
| 426 | |
| 427 | inline const uint8_t* |
| 428 | Block::wire() const |
| 429 | { |
| 430 | if (!hasWire()) |
| 431 | throw Error("(Block::wire) Underlying wire buffer is empty"); |
| 432 | |
| 433 | return &*m_begin; |
| 434 | } |
| 435 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 436 | inline const uint8_t* |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 437 | Block::value() const |
| 438 | { |
| 439 | if (!hasValue()) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 440 | return 0; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 441 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 442 | return &*m_value_begin; |
| 443 | } |
| 444 | |
| 445 | inline size_t |
| 446 | Block::value_size() const |
| 447 | { |
| 448 | if (!hasValue()) |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 449 | return 0; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 450 | |
| 451 | return m_value_end - m_value_begin; |
| 452 | } |
| 453 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 454 | inline const Block::element_container& |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame^] | 455 | Block::elements() const |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 456 | { |
| 457 | return m_subBlocks; |
| 458 | } |
| 459 | |
| 460 | inline Block::element_const_iterator |
| 461 | Block::elements_begin() const |
| 462 | { |
| 463 | return m_subBlocks.begin(); |
| 464 | } |
| 465 | |
| 466 | inline Block::element_const_iterator |
| 467 | Block::elements_end() const |
| 468 | { |
| 469 | return m_subBlocks.end(); |
| 470 | } |
| 471 | |
| 472 | inline size_t |
| 473 | Block::elements_size() const |
| 474 | { |
| 475 | return m_subBlocks.size(); |
| 476 | } |
| 477 | |
| 478 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 479 | } // ndn |
| 480 | |
| 481 | #include "block-helpers.hpp" |
| 482 | |
| 483 | #endif // NDN_BLOCK_HPP |