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 | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 127 | /** @brief Try to construct block from Buffer, referencing data block pointed by wire |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 128 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 129 | * This method does not throw upon decoding error. |
| 130 | * @return true if Block successfully created, false if block cannot be created |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 131 | */ |
| 132 | static bool |
| 133 | fromBuffer(const ConstBufferPtr& wire, size_t offset, Block& block); |
| 134 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 135 | /** @brief Try to construct block from Buffer, referencing data block pointed by wire |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 136 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 137 | * This method does not throw upon decoding error. |
| 138 | * @return true if Block successfully created, false if block cannot be created |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 139 | */ |
| 140 | static bool |
| 141 | fromBuffer(const uint8_t* buffer, size_t maxSize, Block& block); |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 142 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 143 | public: // wire format |
| 144 | /** @brief Check if the Block is empty |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 145 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 146 | bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 147 | empty() const; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 148 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 149 | /** @brief Check if the Block has fully encoded wire |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 150 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 151 | bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 152 | hasWire() const; |
| 153 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 154 | /** @brief Reset wire buffer of the element |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 155 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 156 | void |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 157 | reset(); |
| 158 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 159 | /** @brief Reset wire buffer but keep sub elements (if any) |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 160 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 161 | void |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 162 | resetWire(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 163 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 164 | Buffer::const_iterator |
| 165 | begin() const; |
| 166 | |
| 167 | Buffer::const_iterator |
| 168 | end() const; |
| 169 | |
| 170 | const uint8_t* |
| 171 | wire() const; |
| 172 | |
| 173 | size_t |
| 174 | size() const; |
| 175 | |
| 176 | public: // type and value |
| 177 | uint32_t |
| 178 | type() const; |
| 179 | |
| 180 | /** @brief Check if the Block has value block (no type and length are encoded) |
| 181 | */ |
| 182 | bool |
| 183 | hasValue() const; |
| 184 | |
| 185 | Buffer::const_iterator |
| 186 | value_begin() const; |
| 187 | |
| 188 | Buffer::const_iterator |
| 189 | value_end() const; |
| 190 | |
| 191 | const uint8_t* |
| 192 | value() const; |
| 193 | |
| 194 | size_t |
| 195 | value_size() const; |
| 196 | |
| 197 | public: // sub elements |
| 198 | /** @brief Parse wire buffer into subblocks |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 199 | * |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 200 | * This method is not really const, but it does not modify any data. It simply |
| 201 | * parses contents of the buffer into subblocks |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 202 | */ |
| 203 | void |
| 204 | parse() const; |
| 205 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 206 | /** @brief Encode subblocks into wire buffer |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 207 | */ |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 208 | void |
| 209 | encode(); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 210 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 211 | /** @brief Get the first subelement of the requested type |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 212 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 213 | const Block& |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 214 | get(uint32_t type) const; |
| 215 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 216 | element_const_iterator |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 217 | find(uint32_t type) const; |
| 218 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 219 | void |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 220 | remove(uint32_t type); |
| 221 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 222 | element_iterator |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 223 | erase(element_iterator position); |
| 224 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 225 | element_iterator |
Alexander Afanasyev | f5c35ae | 2014-01-17 16:06:31 -0800 | [diff] [blame] | 226 | erase(element_iterator first, element_iterator last); |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 227 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 228 | void |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 229 | push_back(const Block& element); |
| 230 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 231 | /** @brief Get all subelements |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 232 | */ |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 233 | const element_container& |
| 234 | elements() const; |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 235 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 236 | element_const_iterator |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 237 | elements_begin() const; |
| 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_end() const; |
| 241 | |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 242 | size_t |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 243 | elements_size() const; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 244 | |
Yingdi Yu | 4270f20 | 2014-01-28 14:19:16 -0800 | [diff] [blame] | 245 | Block |
| 246 | blockFromValue() const; |
| 247 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 248 | /** |
| 249 | * @brief Get underlying buffer |
| 250 | */ |
| 251 | shared_ptr<const Buffer> |
| 252 | getBuffer() const; |
| 253 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 254 | public: // EqualityComparable concept |
| 255 | bool |
| 256 | operator==(const Block& other) const; |
| 257 | |
| 258 | bool |
| 259 | operator!=(const Block& other) const; |
| 260 | |
Alexander Afanasyev | 6a05b4b | 2014-07-18 17:23:00 -0700 | [diff] [blame] | 261 | public: // ConvertibleToConstBuffer |
| 262 | operator boost::asio::const_buffer() const; |
| 263 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 264 | protected: |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 265 | shared_ptr<const Buffer> m_buffer; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 266 | |
| 267 | uint32_t m_type; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 268 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 269 | Buffer::const_iterator m_begin; |
| 270 | Buffer::const_iterator m_end; |
| 271 | uint32_t m_size; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 272 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 273 | Buffer::const_iterator m_value_begin; |
| 274 | Buffer::const_iterator m_value_end; |
| 275 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 276 | mutable element_container m_subBlocks; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 277 | }; |
| 278 | |
| 279 | //////////////////////////////////////////////////////////////////////////////// |
| 280 | //////////////////////////////////////////////////////////////////////////////// |
| 281 | //////////////////////////////////////////////////////////////////////////////// |
| 282 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 283 | inline shared_ptr<const Buffer> |
| 284 | Block::getBuffer() const |
| 285 | { |
| 286 | return m_buffer; |
| 287 | } |
| 288 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 289 | inline bool |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 290 | Block::empty() const |
| 291 | { |
| 292 | return m_type == std::numeric_limits<uint32_t>::max(); |
| 293 | } |
| 294 | |
Alexander Afanasyev | 196b9aa | 2014-01-31 17:19:16 -0800 | [diff] [blame] | 295 | inline bool |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 296 | Block::hasWire() const |
| 297 | { |
| 298 | return m_buffer && (m_begin != m_end); |
| 299 | } |
| 300 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 301 | inline Buffer::const_iterator |
| 302 | Block::begin() const |
| 303 | { |
| 304 | if (!hasWire()) |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 305 | throw Error("Underlying wire buffer is empty"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 306 | |
| 307 | return m_begin; |
| 308 | } |
| 309 | |
| 310 | inline Buffer::const_iterator |
| 311 | Block::end() const |
| 312 | { |
| 313 | if (!hasWire()) |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 314 | throw Error("Underlying wire buffer is empty"); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 315 | |
| 316 | return m_end; |
| 317 | } |
| 318 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 319 | inline const uint8_t* |
| 320 | Block::wire() const |
| 321 | { |
| 322 | if (!hasWire()) |
| 323 | throw Error("(Block::wire) Underlying wire buffer is empty"); |
| 324 | |
| 325 | return &*m_begin; |
| 326 | } |
| 327 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 328 | inline size_t |
| 329 | Block::size() const |
| 330 | { |
| 331 | if (hasWire() || hasValue()) { |
| 332 | return m_size; |
| 333 | } |
| 334 | else |
| 335 | throw Error("Block size cannot be determined (undefined block size)"); |
| 336 | } |
| 337 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 338 | inline uint32_t |
| 339 | Block::type() const |
| 340 | { |
| 341 | return m_type; |
| 342 | } |
| 343 | |
| 344 | inline bool |
| 345 | Block::hasValue() const |
| 346 | { |
| 347 | return static_cast<bool>(m_buffer); |
| 348 | } |
| 349 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 350 | inline Buffer::const_iterator |
| 351 | Block::value_begin() const |
| 352 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 353 | return m_value_begin; |
| 354 | } |
| 355 | |
| 356 | inline Buffer::const_iterator |
| 357 | Block::value_end() const |
| 358 | { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 359 | return m_value_end; |
| 360 | } |
| 361 | |
| 362 | inline const uint8_t* |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 363 | Block::value() const |
| 364 | { |
| 365 | if (!hasValue()) |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 366 | return 0; |
Alexander Afanasyev | 937aa78 | 2014-03-21 13:17:57 -0700 | [diff] [blame] | 367 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 368 | return &*m_value_begin; |
| 369 | } |
| 370 | |
| 371 | inline size_t |
| 372 | Block::value_size() const |
| 373 | { |
| 374 | if (!hasValue()) |
Alexander Afanasyev | 8ea763d | 2014-02-06 20:32:52 -0800 | [diff] [blame] | 375 | return 0; |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 376 | |
| 377 | return m_value_end - m_value_begin; |
| 378 | } |
| 379 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 380 | inline Block::element_iterator |
| 381 | Block::erase(Block::element_iterator position) |
| 382 | { |
| 383 | resetWire(); |
| 384 | return m_subBlocks.erase(position); |
| 385 | } |
| 386 | |
| 387 | inline Block::element_iterator |
| 388 | Block::erase(Block::element_iterator first, Block::element_iterator last) |
| 389 | { |
| 390 | resetWire(); |
| 391 | return m_subBlocks.erase(first, last); |
| 392 | } |
| 393 | |
| 394 | inline void |
| 395 | Block::push_back(const Block& element) |
| 396 | { |
| 397 | resetWire(); |
| 398 | m_subBlocks.push_back(element); |
| 399 | } |
| 400 | |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 401 | inline const Block::element_container& |
Alexander Afanasyev | a465e97 | 2014-03-22 17:21:49 -0700 | [diff] [blame] | 402 | Block::elements() const |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 403 | { |
| 404 | return m_subBlocks; |
| 405 | } |
| 406 | |
| 407 | inline Block::element_const_iterator |
| 408 | Block::elements_begin() const |
| 409 | { |
| 410 | return m_subBlocks.begin(); |
| 411 | } |
| 412 | |
| 413 | inline Block::element_const_iterator |
| 414 | Block::elements_end() const |
| 415 | { |
| 416 | return m_subBlocks.end(); |
| 417 | } |
| 418 | |
| 419 | inline size_t |
| 420 | Block::elements_size() const |
| 421 | { |
| 422 | return m_subBlocks.size(); |
| 423 | } |
| 424 | |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 425 | inline bool |
Junxiao Shi | af8eeea | 2014-03-31 20:10:56 -0700 | [diff] [blame] | 426 | Block::operator!=(const Block& other) const |
| 427 | { |
| 428 | return !this->operator==(other); |
| 429 | } |
Alexander Afanasyev | 29e5c3d | 2014-02-11 00:01:10 -0800 | [diff] [blame] | 430 | |
Junxiao Shi | 81a6c5d | 2014-11-30 00:14:42 -0700 | [diff] [blame] | 431 | } // namespace ndn |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 432 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 433 | #endif // NDN_ENCODING_BLOCK_HPP |