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