Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | af99f46 | 2015-01-19 21:43:09 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2015 Regents of the University of California. |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 4 | * |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 22 | */ |
| 23 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 24 | #ifndef NDN_ENCODING_BLOCK_HPP |
| 25 | #define NDN_ENCODING_BLOCK_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 26 | |
Alexander Afanasyev | 1950885 | 2014-01-29 01:01:51 -0800 | [diff] [blame] | 27 | #include "../common.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 29 | #include "buffer.hpp" |
| 30 | #include "tlv.hpp" |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 31 | #include "encoding-buffer-fwd.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 33 | namespace boost { |
| 34 | namespace asio { |
| 35 | class const_buffer; |
| 36 | } // namespace asio |
| 37 | } // namespace boost |
| 38 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 39 | namespace ndn { |
| 40 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 41 | /** @brief Class representing a wire element of NDN-TLV packet format |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 42 | */ |
| 43 | class Block |
| 44 | { |
| 45 | public: |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 46 | typedef std::vector<Block> element_container; |
| 47 | typedef element_container::iterator element_iterator; |
| 48 | typedef element_container::const_iterator element_const_iterator; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 49 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 50 | class Error : public tlv::Error |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 51 | { |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 52 | public: |
| 53 | explicit |
| 54 | Error(const std::string& what) |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 55 | : tlv::Error(what) |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 56 | { |
| 57 | } |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 58 | }; |
| 59 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 60 | public: // constructor, creation, assignment |
| 61 | /** @brief Create an empty Block |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 62 | */ |
| 63 | Block(); |
| 64 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 65 | /** @brief Create a Block based on EncodingBuffer object |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 66 | */ |
| 67 | explicit |
| 68 | Block(const EncodingBuffer& buffer); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 69 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 70 | /** @brief Create a Block from the raw buffer with Type-Length parsing |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 71 | */ |
Alexander Afanasyev | 197e565 | 2014-06-13 16:56:31 -0700 | [diff] [blame] | 72 | explicit |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 73 | Block(const ConstBufferPtr& buffer); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 74 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 75 | /** @brief Create a Block from a buffer, directly specifying boundaries |
| 76 | * of the block within the buffer |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 77 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 78 | * This overload will automatically detect type and position of the value within the block |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 79 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 80 | Block(const ConstBufferPtr& buffer, |
| 81 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 82 | bool verifyLength = true); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 83 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 84 | /** @brief Create a Block from the raw buffer with Type-Length parsing |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 85 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 86 | Block(const uint8_t* buffer, size_t maxlength); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 87 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 88 | /** @brief Create a Block from the raw buffer with Type-Length parsing |
| 89 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 90 | Block(const void* buffer, size_t maxlength); |
Yingdi Yu | 2715839 | 2014-01-20 13:04:20 -0800 | [diff] [blame] | 91 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 92 | /** @brief Create a Block from the wire buffer (no parsing) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 93 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 94 | * This overload does not do any parsing |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 95 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 96 | Block(const ConstBufferPtr& wire, |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 97 | uint32_t type, |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 98 | const Buffer::const_iterator& begin, const Buffer::const_iterator& end, |
| 99 | const Buffer::const_iterator& valueBegin, const Buffer::const_iterator& valueEnd); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 100 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 101 | /** @brief Create Block of a specific type with empty wire buffer |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 102 | */ |
Alexander Afanasyev | f42ce13 | 2014-01-07 13:32:30 -0800 | [diff] [blame] | 103 | explicit |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 104 | Block(uint32_t type); |
| 105 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 106 | /** @brief Create a Block of a specific type with the specified value |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 107 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 108 | * The underlying buffer holds only value Additional operations are needed |
| 109 | * to construct wire encoding, one need to prepend the wire buffer with type |
| 110 | * and value-length VAR-NUMBERs |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 111 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 112 | Block(uint32_t type, const ConstBufferPtr& value); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 113 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 114 | /** @brief Create a nested Block of a specific type with the specified value |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 115 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 116 | * The underlying buffer holds only value. Additional operations are needed |
| 117 | * to construct wire encoding, one need to prepend the wire buffer with type |
| 118 | * and value-length VAR-NUMBERs |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 119 | */ |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 120 | Block(uint32_t type, const Block& value); |
| 121 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 122 | /** @brief Create a Block from an input stream |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 123 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 124 | static Block |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 125 | fromStream(std::istream& is); |
| 126 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame^] | 127 | /** @brief Try to construct block from Buffer |
| 128 | * @param buffer the buffer to construct block from |
| 129 | * @note buffer is passed by value because the constructed block |
| 130 | * takes shared ownership of the buffer |
| 131 | * @param offset offset from beginning of \p buffer to construct Block from |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 132 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 133 | * This method does not throw upon decoding error. |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame^] | 134 | * This method does not copy the bytes. |
| 135 | * |
| 136 | * @return true and the Block, if Block is successfully created; otherwise false |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 137 | */ |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame^] | 138 | static std::tuple<bool, Block> |
| 139 | fromBuffer(ConstBufferPtr buffer, size_t offset); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 140 | |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame^] | 141 | /** @deprecated use fromBuffer(ConstBufferPtr, size_t) |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 142 | */ |
| 143 | static bool |
Junxiao Shi | 02a4bf3 | 2015-02-21 21:07:46 -0700 | [diff] [blame^] | 144 | fromBuffer(const ConstBufferPtr& buffer, size_t offset, Block& block) |
| 145 | { |
| 146 | bool isOk = false; |
| 147 | std::tie(isOk, block) = Block::fromBuffer(buffer, offset); |
| 148 | return isOk; |
| 149 | } |
| 150 | |
| 151 | /** @brief Try to construct block from raw buffer |
| 152 | * @param buffer the raw buffer to copy bytes from |
| 153 | * @param maxSize the maximum size of constructed block; |
| 154 | * @p buffer must have a size of at least @p maxSize |
| 155 | * |
| 156 | * This method does not throw upon decoding error. |
| 157 | * This method copies the bytes into a new Buffer. |
| 158 | * |
| 159 | * @return true and the Block, if Block is successfully created; otherwise false |
| 160 | */ |
| 161 | static std::tuple<bool, Block> |
| 162 | fromBuffer(const uint8_t* buffer, size_t maxSize); |
| 163 | |
| 164 | /** @deprecated use fromBuffer(const uint8_t*, size_t) |
| 165 | */ |
| 166 | static bool |
| 167 | fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block) |
| 168 | { |
| 169 | bool isOk = false; |
| 170 | std::tie(isOk, block) = Block::fromBuffer(buffer, maxSize); |
| 171 | return isOk; |
| 172 | } |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 173 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 174 | public: // wire format |
| 175 | /** @brief Check if the Block is empty |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 176 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 177 | bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 178 | empty() const; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 179 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 180 | /** @brief Check if the Block has fully encoded wire |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 181 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 182 | bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 183 | hasWire() const; |
| 184 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 185 | /** @brief Reset wire buffer of the element |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 186 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 187 | void |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 188 | reset(); |
| 189 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 190 | /** @brief Reset wire buffer but keep sub elements (if any) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 191 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 192 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 193 | resetWire(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 194 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 195 | Buffer::const_iterator |
| 196 | begin() const; |
| 197 | |
| 198 | Buffer::const_iterator |
| 199 | end() const; |
| 200 | |
| 201 | const uint8_t* |
| 202 | wire() const; |
| 203 | |
| 204 | size_t |
| 205 | size() const; |
| 206 | |
| 207 | public: // type and value |
| 208 | uint32_t |
| 209 | type() const; |
| 210 | |
| 211 | /** @brief Check if the Block has value block (no type and length are encoded) |
| 212 | */ |
| 213 | bool |
| 214 | hasValue() const; |
| 215 | |
| 216 | Buffer::const_iterator |
| 217 | value_begin() const; |
| 218 | |
| 219 | Buffer::const_iterator |
| 220 | value_end() const; |
| 221 | |
| 222 | const uint8_t* |
| 223 | value() const; |
| 224 | |
| 225 | size_t |
| 226 | value_size() const; |
| 227 | |
| 228 | public: // sub elements |
| 229 | /** @brief Parse wire buffer into subblocks |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 230 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 231 | * This method is not really const, but it does not modify any data. It simply |
| 232 | * parses contents of the buffer into subblocks |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 233 | */ |
| 234 | void |
| 235 | parse() const; |
| 236 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 237 | /** @brief Encode subblocks into wire buffer |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 238 | */ |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 239 | void |
| 240 | encode(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 241 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 242 | /** @brief Get the first subelement of the requested type |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 243 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 244 | const Block& |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 245 | get(uint32_t type) const; |
| 246 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 247 | element_const_iterator |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 248 | find(uint32_t type) const; |
| 249 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 250 | void |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 251 | remove(uint32_t type); |
| 252 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 253 | element_iterator |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 254 | erase(element_iterator position); |
| 255 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 256 | element_iterator |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 257 | erase(element_iterator first, element_iterator last); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 258 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 259 | void |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 260 | push_back(const Block& element); |
| 261 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 262 | /** @brief Get all subelements |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 263 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 264 | const element_container& |
| 265 | elements() const; |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 266 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 267 | element_const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 268 | elements_begin() const; |
| 269 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 270 | element_const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 271 | elements_end() const; |
| 272 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 273 | size_t |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 274 | elements_size() const; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 275 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 276 | Block |
| 277 | blockFromValue() const; |
| 278 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 279 | /** |
| 280 | * @brief Get underlying buffer |
| 281 | */ |
| 282 | shared_ptr<const Buffer> |
| 283 | getBuffer() const; |
| 284 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 285 | public: // EqualityComparable concept |
| 286 | bool |
| 287 | operator==(const Block& other) const; |
| 288 | |
| 289 | bool |
| 290 | operator!=(const Block& other) const; |
| 291 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 292 | public: // ConvertibleToConstBuffer |
| 293 | operator boost::asio::const_buffer() const; |
| 294 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 295 | protected: |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 296 | shared_ptr<const Buffer> m_buffer; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 297 | |
| 298 | uint32_t m_type; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 299 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 300 | Buffer::const_iterator m_begin; |
| 301 | Buffer::const_iterator m_end; |
| 302 | uint32_t m_size; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 303 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 304 | Buffer::const_iterator m_value_begin; |
| 305 | Buffer::const_iterator m_value_end; |
| 306 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 307 | mutable element_container m_subBlocks; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 308 | }; |
| 309 | |
| 310 | //////////////////////////////////////////////////////////////////////////////// |
| 311 | //////////////////////////////////////////////////////////////////////////////// |
| 312 | //////////////////////////////////////////////////////////////////////////////// |
| 313 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 314 | inline shared_ptr<const Buffer> |
| 315 | Block::getBuffer() const |
| 316 | { |
| 317 | return m_buffer; |
| 318 | } |
| 319 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 320 | inline bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 321 | Block::empty() const |
| 322 | { |
| 323 | return m_type == std::numeric_limits<uint32_t>::max(); |
| 324 | } |
| 325 | |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 326 | inline bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 327 | Block::hasWire() const |
| 328 | { |
| 329 | return m_buffer && (m_begin != m_end); |
| 330 | } |
| 331 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 332 | inline Buffer::const_iterator |
| 333 | Block::begin() const |
| 334 | { |
| 335 | if (!hasWire()) |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 336 | throw Error("Underlying wire buffer is empty"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 337 | |
| 338 | return m_begin; |
| 339 | } |
| 340 | |
| 341 | inline Buffer::const_iterator |
| 342 | Block::end() const |
| 343 | { |
| 344 | if (!hasWire()) |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 345 | throw Error("Underlying wire buffer is empty"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 346 | |
| 347 | return m_end; |
| 348 | } |
| 349 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 350 | inline const uint8_t* |
| 351 | Block::wire() const |
| 352 | { |
| 353 | if (!hasWire()) |
| 354 | throw Error("(Block::wire) Underlying wire buffer is empty"); |
| 355 | |
| 356 | return &*m_begin; |
| 357 | } |
| 358 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 359 | inline size_t |
| 360 | Block::size() const |
| 361 | { |
| 362 | if (hasWire() || hasValue()) { |
| 363 | return m_size; |
| 364 | } |
| 365 | else |
| 366 | throw Error("Block size cannot be determined (undefined block size)"); |
| 367 | } |
| 368 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 369 | inline uint32_t |
| 370 | Block::type() const |
| 371 | { |
| 372 | return m_type; |
| 373 | } |
| 374 | |
| 375 | inline bool |
| 376 | Block::hasValue() const |
| 377 | { |
| 378 | return static_cast<bool>(m_buffer); |
| 379 | } |
| 380 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 381 | inline Buffer::const_iterator |
| 382 | Block::value_begin() const |
| 383 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 384 | return m_value_begin; |
| 385 | } |
| 386 | |
| 387 | inline Buffer::const_iterator |
| 388 | Block::value_end() const |
| 389 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 390 | return m_value_end; |
| 391 | } |
| 392 | |
| 393 | inline const uint8_t* |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 394 | Block::value() const |
| 395 | { |
| 396 | if (!hasValue()) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 397 | return 0; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 398 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 399 | return &*m_value_begin; |
| 400 | } |
| 401 | |
| 402 | inline size_t |
| 403 | Block::value_size() const |
| 404 | { |
| 405 | if (!hasValue()) |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 406 | return 0; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 407 | |
| 408 | return m_value_end - m_value_begin; |
| 409 | } |
| 410 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 411 | inline Block::element_iterator |
| 412 | Block::erase(Block::element_iterator position) |
| 413 | { |
| 414 | resetWire(); |
| 415 | return m_subBlocks.erase(position); |
| 416 | } |
| 417 | |
| 418 | inline Block::element_iterator |
| 419 | Block::erase(Block::element_iterator first, Block::element_iterator last) |
| 420 | { |
| 421 | resetWire(); |
| 422 | return m_subBlocks.erase(first, last); |
| 423 | } |
| 424 | |
| 425 | inline void |
| 426 | Block::push_back(const Block& element) |
| 427 | { |
| 428 | resetWire(); |
| 429 | m_subBlocks.push_back(element); |
| 430 | } |
| 431 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 432 | inline const Block::element_container& |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 433 | Block::elements() const |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 434 | { |
| 435 | return m_subBlocks; |
| 436 | } |
| 437 | |
| 438 | inline Block::element_const_iterator |
| 439 | Block::elements_begin() const |
| 440 | { |
| 441 | return m_subBlocks.begin(); |
| 442 | } |
| 443 | |
| 444 | inline Block::element_const_iterator |
| 445 | Block::elements_end() const |
| 446 | { |
| 447 | return m_subBlocks.end(); |
| 448 | } |
| 449 | |
| 450 | inline size_t |
| 451 | Block::elements_size() const |
| 452 | { |
| 453 | return m_subBlocks.size(); |
| 454 | } |
| 455 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 456 | inline bool |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 457 | Block::operator!=(const Block& other) const |
| 458 | { |
| 459 | return !this->operator==(other); |
| 460 | } |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 461 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 462 | } // namespace ndn |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 463 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 464 | #endif // NDN_ENCODING_BLOCK_HPP |