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 | 7463389 | 2015-02-08 18:08:46 -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 | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 22 | #ifndef NDN_ENCODING_BLOCK_HELPERS_HPP |
| 23 | #define NDN_ENCODING_BLOCK_HELPERS_HPP |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 24 | |
| 25 | #include "block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 26 | #include "encoding-buffer.hpp" |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 27 | #include "../util/concepts.hpp" |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 29 | #include <iterator> |
| 30 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 31 | namespace ndn { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 32 | namespace encoding { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 34 | /** |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 35 | * @brief Helper to prepend TLV block type @p type containing non-negative integer @p value |
| 36 | * @see makeNonNegativeIntegerBlock, readNonNegativeInteger |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 37 | */ |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 38 | template<Tag TAG> |
| 39 | size_t |
| 40 | prependNonNegativeIntegerBlock(EncodingImpl<TAG>& encoder, uint32_t type, uint64_t value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 41 | |
| 42 | /** |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 43 | * @brief Create a TLV block type @p type containing non-negative integer @p value |
| 44 | * @see prependNonNegativeIntegerBlock, readNonNegativeInteger |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 45 | */ |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 46 | Block |
| 47 | makeNonNegativeIntegerBlock(uint32_t type, uint64_t value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 49 | /** |
| 50 | * @brief Helper to read a non-negative integer from a block |
| 51 | * @see prependNonNegativeIntegerBlock, makeNonNegativeIntegerBlock |
| 52 | * @throw tlv::Error if block does not contain a valid nonNegativeInteger |
| 53 | */ |
| 54 | uint64_t |
| 55 | readNonNegativeInteger(const Block& block); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 56 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 57 | //////// |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 59 | /** |
| 60 | * @brief Helper to prepend TLV block type @p type containing no value (i.e., a boolean block) |
| 61 | * @see makeEmptyBlock |
| 62 | */ |
| 63 | template<Tag TAG> |
| 64 | size_t |
| 65 | prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 67 | /** |
| 68 | * @brief Create a TLV block type @p type containing no value (i.e., a boolean block) |
| 69 | * @see prependEmptyBlock |
| 70 | */ |
| 71 | Block |
| 72 | makeEmptyBlock(uint32_t type); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 74 | //////// |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 75 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 76 | /** |
| 77 | * @brief Helper to prepend TLV block type @p type with value from a string @p value |
| 78 | * @see makeStringBlock, readString |
| 79 | */ |
| 80 | template<Tag TAG> |
| 81 | size_t |
| 82 | prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 84 | /** |
Alexander Afanasyev | f2a4622 | 2015-09-17 18:01:30 -0700 | [diff] [blame] | 85 | * @brief Create a TLV block type @p type with value from a string @p value |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 86 | * @see prependStringBlock, readString |
| 87 | */ |
| 88 | Block |
| 89 | makeStringBlock(uint32_t type, const std::string& value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 90 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 91 | /** |
| 92 | * @brief Helper to read a string value from a block |
| 93 | * @see prependStringBlock, makeStringBlock |
| 94 | */ |
| 95 | std::string |
| 96 | readString(const Block& block); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 97 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 98 | //////// |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 99 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 100 | /** |
| 101 | * @brief Create a TLV block type @p type with value from a buffer @p value of size @p length |
| 102 | */ |
| 103 | Block |
| 104 | makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 105 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 106 | /** |
| 107 | * @brief Create a TLV block type @p type with value from a buffer @p value of size @p length |
| 108 | */ |
| 109 | Block |
| 110 | makeBinaryBlock(uint32_t type, const char* value, size_t length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 111 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 112 | /** |
| 113 | * @brief Helper class template to create a data block when RandomAccessIterator is used |
| 114 | */ |
| 115 | template<class Iterator> |
| 116 | class DataBlockFast |
| 117 | { |
| 118 | public: |
| 119 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Iterator>)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 121 | static Block |
| 122 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 123 | { |
| 124 | EncodingEstimator estimator; |
| 125 | size_t valueLength = last - first; |
| 126 | size_t totalLength = valueLength; |
| 127 | totalLength += estimator.prependVarNumber(valueLength); |
| 128 | totalLength += estimator.prependVarNumber(type); |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 130 | EncodingBuffer encoder(totalLength, 0); |
| 131 | encoder.prependRange(first, last); |
| 132 | encoder.prependVarNumber(valueLength); |
| 133 | encoder.prependVarNumber(type); |
| 134 | |
| 135 | return encoder.block(); |
| 136 | } |
| 137 | }; |
| 138 | |
| 139 | /** |
| 140 | * @brief Helper class template to create a data block when generic InputIterator is used |
| 141 | */ |
| 142 | template<class Iterator> |
| 143 | class DataBlockSlow |
| 144 | { |
| 145 | public: |
| 146 | BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>)); |
| 147 | |
| 148 | static Block |
| 149 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 150 | { |
| 151 | // reserve 4 bytes in front (common for 1(type)-3(length) encoding |
| 152 | // Actual size will be adjusted as necessary by the encoder |
| 153 | EncodingBuffer encoder(4, 4); |
| 154 | size_t valueLength = encoder.appendRange(first, last); |
| 155 | encoder.prependVarNumber(valueLength); |
| 156 | encoder.prependVarNumber(type); |
| 157 | |
| 158 | return encoder.block(); |
| 159 | } |
| 160 | }; |
| 161 | |
| 162 | /** |
| 163 | * @brief Free function to create a block given @p type and range [@p first, @p last) of bytes |
| 164 | * @tparam Iterator iterator type satisfying at least InputIterator concept. Implementation |
| 165 | * is more optimal when the iterator type satisfies RandomAccessIterator concept |
| 166 | * It is required that sizeof(std::iterator_traits<Iterator>::value_type) == 1. |
| 167 | */ |
| 168 | template<class Iterator> |
| 169 | inline Block |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 170 | makeBinaryBlock(uint32_t type, Iterator first, Iterator last) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 171 | { |
| 172 | static_assert(sizeof(typename std::iterator_traits<Iterator>::value_type) == 1, |
| 173 | "Iterator should point only to char or unsigned char"); |
| 174 | |
| 175 | typedef typename boost::mpl::if_< |
| 176 | std::is_base_of<std::random_access_iterator_tag, |
| 177 | typename std::iterator_traits<Iterator>::iterator_category>, |
| 178 | DataBlockFast<Iterator>, |
| 179 | DataBlockSlow<Iterator>>::type DataBlock; |
| 180 | |
| 181 | return DataBlock::makeBlock(type, first, last); |
| 182 | } |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 183 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 184 | //////// |
| 185 | |
| 186 | /** |
| 187 | * @brief Prepend a TLV block of type @p type with WireEncodable @p value as a value |
| 188 | * @tparam U type that satisfies WireEncodableWithEncodingBuffer concept |
| 189 | * @see makeNestedBlock |
| 190 | */ |
| 191 | template<Tag TAG, class U> |
| 192 | inline size_t |
| 193 | prependNestedBlock(EncodingImpl<TAG>& encoder, uint32_t type, const U& value) |
| 194 | { |
| 195 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<U>)); |
| 196 | |
| 197 | size_t valueLength = value.wireEncode(encoder); |
| 198 | size_t totalLength = valueLength; |
| 199 | totalLength += encoder.prependVarNumber(valueLength); |
| 200 | totalLength += encoder.prependVarNumber(type); |
| 201 | |
| 202 | return totalLength; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * @brief Create a TLV block of type @p type with WireEncodable @p value as a value |
| 207 | * @tparam U type that satisfies WireEncodableWithEncodingBuffer concept |
| 208 | * @see prependNestedBlock |
| 209 | */ |
| 210 | template<class U> |
| 211 | inline Block |
| 212 | makeNestedBlock(uint32_t type, const U& value) |
| 213 | { |
| 214 | EncodingEstimator estimator; |
| 215 | size_t totalLength = prependNestedBlock(estimator, type, value); |
| 216 | |
| 217 | EncodingBuffer encoder(totalLength, 0); |
| 218 | prependNestedBlock(encoder, type, value); |
| 219 | |
| 220 | return encoder.block(); |
| 221 | } |
| 222 | |
| 223 | #define NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS |
| 224 | #ifdef NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS |
| 225 | |
| 226 | /** |
| 227 | * @deprecated Use Encoder::prependBlock and Estimator::prependBlock instead |
| 228 | */ |
| 229 | template<Tag TAG> |
| 230 | inline size_t |
| 231 | prependBlock(EncodingImpl<TAG>& encoder, const Block& block) |
| 232 | { |
| 233 | return encoder.prependBlock(block); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * @deprecated Use Encoder::prependByteArrayBlock and Estimator::prependByteArrayBlock instead |
| 238 | */ |
| 239 | template<Tag TAG> |
| 240 | inline size_t |
| 241 | prependByteArrayBlock(EncodingImpl<TAG>& encoder, |
| 242 | uint32_t type, const uint8_t* array, size_t arraySize) |
| 243 | { |
| 244 | return encoder.prependByteArrayBlock(type, array, arraySize); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @deprecated Use makeNonNegativeIntegerBlock instead |
| 249 | */ |
| 250 | inline Block |
| 251 | nonNegativeIntegerBlock(uint32_t type, uint64_t value) |
| 252 | { |
| 253 | return makeNonNegativeIntegerBlock(type, value); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * @deprecated Use prependEmptyBlock instead |
| 258 | */ |
| 259 | template<Tag TAG> |
| 260 | size_t |
| 261 | prependBooleanBlock(EncodingImpl<TAG>& encoder, uint32_t type) |
| 262 | { |
| 263 | return prependEmptyBlock(encoder, type); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * @deprecated Use makeEmptyBlock instead |
| 268 | */ |
| 269 | inline Block |
| 270 | booleanBlock(uint32_t type) |
| 271 | { |
| 272 | return makeEmptyBlock(type); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * @deprecated Use makeBinaryBlock instead |
| 277 | */ |
| 278 | inline Block |
| 279 | dataBlock(uint32_t type, const uint8_t* data, size_t dataSize) |
| 280 | { |
| 281 | return makeBinaryBlock(type, data, dataSize); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * @deprecated Use makeBinaryBlock instead |
| 286 | */ |
| 287 | inline Block |
| 288 | dataBlock(uint32_t type, const char* data, size_t dataSize) |
| 289 | { |
| 290 | return makeBinaryBlock(type, data, dataSize); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * @deprecated Use makeBinaryBlock instead |
| 295 | */ |
| 296 | template<class Iterator> |
| 297 | inline Block |
| 298 | dataBlock(uint32_t type, Iterator first, Iterator last) |
| 299 | { |
| 300 | return makeBinaryBlock(type, first, last); |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * @deprecated Use makeNestedBlock instead |
| 305 | */ |
| 306 | template<class U> |
| 307 | inline Block |
| 308 | nestedBlock(uint32_t type, const U& value) |
| 309 | { |
| 310 | return makeNestedBlock(type, value); |
| 311 | } |
| 312 | |
| 313 | #endif // NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS |
| 314 | |
| 315 | } // namespace encoding |
| 316 | |
| 317 | using encoding::makeNonNegativeIntegerBlock; |
| 318 | using encoding::readNonNegativeInteger; |
| 319 | using encoding::makeEmptyBlock; |
| 320 | using encoding::makeStringBlock; |
| 321 | using encoding::readString; |
| 322 | using encoding::makeBinaryBlock; |
| 323 | using encoding::makeNestedBlock; |
| 324 | |
| 325 | #ifdef NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS |
| 326 | |
| 327 | using encoding::nonNegativeIntegerBlock; |
| 328 | using encoding::booleanBlock; |
| 329 | using encoding::dataBlock; |
| 330 | |
| 331 | #endif // NDN_CXX_ENABLE_DEPRECATED_BLOCK_HELPERS |
| 332 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 333 | } // namespace ndn |
| 334 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 335 | #endif // NDN_ENCODING_BLOCK_HELPERS_HPP |