Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 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 | |
| 29 | namespace ndn { |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 30 | namespace encoding { |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 31 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 32 | /** @brief Prepend a TLV element containing a non-negative integer. |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 33 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 34 | * @param type TLV-TYPE number |
| 35 | * @param value non-negative integer value |
| 36 | * @sa 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 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 42 | extern template size_t |
| 43 | prependNonNegativeIntegerBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, uint64_t); |
| 44 | |
| 45 | extern template size_t |
| 46 | prependNonNegativeIntegerBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, uint64_t); |
| 47 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 48 | /** @brief Create a TLV block containing a non-negative integer. |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 49 | * @param type TLV-TYPE number |
| 50 | * @param value non-negative integer value |
| 51 | * @sa prependNonNegativeIntegerBlock, readNonNegativeInteger |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 52 | */ |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 53 | Block |
| 54 | makeNonNegativeIntegerBlock(uint32_t type, uint64_t value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 55 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 56 | /** @brief Read a non-negative integer from a TLV element. |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 57 | * @param block the TLV element |
| 58 | * @throw tlv::Error block does not contain a non-negative integer |
| 59 | * @sa prependNonNegativeIntegerBlock, makeNonNegativeIntegerBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 60 | */ |
| 61 | uint64_t |
| 62 | readNonNegativeInteger(const Block& block); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 63 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 64 | /** @brief Read a non-negative integer from a TLV element and cast to the specified type. |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 65 | * @tparam R result type, must be an integral type |
| 66 | * @param block the TLV element |
| 67 | * @throw tlv::Error block does not contain a valid non-negative integer or the number cannot be |
| 68 | * represented in R |
| 69 | */ |
| 70 | template<typename R> |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 71 | std::enable_if_t<std::is_integral<R>::value, R> |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 72 | readNonNegativeIntegerAs(const Block& block) |
| 73 | { |
| 74 | uint64_t value = readNonNegativeInteger(block); |
| 75 | if (value > std::numeric_limits<R>::max()) { |
| 76 | BOOST_THROW_EXCEPTION(tlv::Error("Value in TLV element of type " + to_string(block.type()) + |
| 77 | " is too large")); |
| 78 | } |
| 79 | return static_cast<R>(value); |
| 80 | } |
| 81 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 82 | /** @brief Read a non-negative integer from a TLV element and cast to the specified type. |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 83 | * @tparam R result type, must be an enumeration type |
| 84 | * @param block the TLV element |
| 85 | * @throw tlv::Error block does not contain a valid non-negative integer or the number cannot be |
| 86 | * represented in R |
| 87 | * @warning If R is an unscoped enum type, it must have a fixed underlying type. Otherwise, this |
| 88 | * function may trigger unspecified behavior. |
| 89 | */ |
| 90 | template<typename R> |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 91 | std::enable_if_t<std::is_enum<R>::value, R> |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 92 | readNonNegativeIntegerAs(const Block& block) |
| 93 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 94 | return static_cast<R>(readNonNegativeIntegerAs<std::underlying_type_t<R>>(block)); |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 97 | /** @brief Prepend an empty TLV element. |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 98 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 99 | * @param type TLV-TYPE number |
| 100 | * @details The TLV element has zero-length TLV-VALUE. |
| 101 | * @sa makeEmptyBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 102 | */ |
| 103 | template<Tag TAG> |
| 104 | size_t |
| 105 | prependEmptyBlock(EncodingImpl<TAG>& encoder, uint32_t type); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 106 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 107 | extern template size_t |
| 108 | prependEmptyBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t); |
| 109 | |
| 110 | extern template size_t |
| 111 | prependEmptyBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t); |
| 112 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 113 | /** @brief Create an empty TLV block. |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 114 | * @param type TLV-TYPE number |
| 115 | * @return A TLV block with zero-length TLV-VALUE |
| 116 | * @sa prependEmptyBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 117 | */ |
| 118 | Block |
| 119 | makeEmptyBlock(uint32_t type); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 120 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 121 | /** @brief Prepend a TLV element containing a string. |
| 122 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 123 | * @param type TLV-TYPE number |
| 124 | * @param value string value, may contain NUL octets |
| 125 | * @sa makeStringBlock, readString |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 126 | */ |
| 127 | template<Tag TAG> |
| 128 | size_t |
| 129 | prependStringBlock(EncodingImpl<TAG>& encoder, uint32_t type, const std::string& value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 130 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 131 | extern template size_t |
| 132 | prependStringBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, const std::string&); |
| 133 | |
| 134 | extern template size_t |
| 135 | prependStringBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, const std::string&); |
| 136 | |
| 137 | /** @brief Create a TLV block containing a string. |
| 138 | * @param type TLV-TYPE number |
| 139 | * @param value string value, may contain NUL octets |
| 140 | * @sa prependStringBlock, readString |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 141 | */ |
| 142 | Block |
| 143 | makeStringBlock(uint32_t type, const std::string& value); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 144 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 145 | /** @brief Read TLV-VALUE of a TLV element as a string. |
| 146 | * @param block the TLV element |
| 147 | * @return a string, may contain NUL octets |
| 148 | * @sa prependStringBlock, makeStringBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 149 | */ |
| 150 | std::string |
| 151 | readString(const Block& block); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 152 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 153 | /** @brief Prepend a TLV element containing an IEEE 754 double-precision floating-point number. |
| 154 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 155 | * @param type TLV-TYPE number |
| 156 | * @param value a floating point number value |
| 157 | * @sa makeDoubleBlock, readDouble |
| 158 | */ |
| 159 | template<Tag TAG> |
| 160 | size_t |
| 161 | prependDoubleBlock(EncodingImpl<TAG>& encoder, uint32_t type, double value); |
| 162 | |
| 163 | extern template size_t |
| 164 | prependDoubleBlock<EstimatorTag>(EncodingImpl<EstimatorTag>&, uint32_t, double); |
| 165 | |
| 166 | extern template size_t |
| 167 | prependDoubleBlock<EncoderTag>(EncodingImpl<EncoderTag>&, uint32_t, double); |
| 168 | |
| 169 | /** @brief Create a TLV element containing an IEEE 754 double-precision floating-point number. |
| 170 | * @param type TLV-TYPE number |
| 171 | * @param value floating point number value |
| 172 | * @sa prependDoubleBlock, readDouble |
| 173 | */ |
| 174 | Block |
| 175 | makeDoubleBlock(uint32_t type, double value); |
| 176 | |
| 177 | /** @brief Read TLV-VALUE of a TLV element as an IEEE 754 double-precision floating-point number. |
| 178 | * @param block the TLV element |
| 179 | * @return a floating point number value |
| 180 | * @sa prependDoubleBlock, makeDoubleBlock |
| 181 | */ |
| 182 | double |
| 183 | readDouble(const Block& block); |
| 184 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 185 | /** @brief Create a TLV block copying TLV-VALUE from raw buffer. |
| 186 | * @param type TLV-TYPE number |
| 187 | * @param value raw buffer as TLV-VALUE |
| 188 | * @param length length of value buffer |
| 189 | * @sa Encoder::prependByteArrayBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 190 | */ |
| 191 | Block |
| 192 | makeBinaryBlock(uint32_t type, const uint8_t* value, size_t length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 193 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 194 | /** @brief Create a TLV block copying TLV-VALUE from raw buffer. |
| 195 | * @param type TLV-TYPE number |
| 196 | * @param value raw buffer as TLV-VALUE |
| 197 | * @param length length of value buffer |
| 198 | * @sa Encoder::prependByteArrayBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 199 | */ |
| 200 | Block |
| 201 | makeBinaryBlock(uint32_t type, const char* value, size_t length); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 202 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 203 | namespace detail { |
| 204 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 205 | /** @brief Create a binary block copying from RandomAccessIterator. |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 206 | */ |
| 207 | template<class Iterator> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 208 | class BinaryBlockFast |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 209 | { |
| 210 | public: |
| 211 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Iterator>)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 212 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 213 | static Block |
| 214 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 215 | { |
| 216 | EncodingEstimator estimator; |
| 217 | size_t valueLength = last - first; |
| 218 | size_t totalLength = valueLength; |
| 219 | totalLength += estimator.prependVarNumber(valueLength); |
| 220 | totalLength += estimator.prependVarNumber(type); |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 221 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 222 | EncodingBuffer encoder(totalLength, 0); |
| 223 | encoder.prependRange(first, last); |
| 224 | encoder.prependVarNumber(valueLength); |
| 225 | encoder.prependVarNumber(type); |
| 226 | |
| 227 | return encoder.block(); |
| 228 | } |
| 229 | }; |
| 230 | |
Tianxing Ma | 241df42 | 2018-10-09 22:21:47 -0500 | [diff] [blame] | 231 | /** @brief Create a binary block copying from generic InputIterator. |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 232 | */ |
| 233 | template<class Iterator> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 234 | class BinaryBlockSlow |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 235 | { |
| 236 | public: |
| 237 | BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>)); |
| 238 | |
| 239 | static Block |
| 240 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 241 | { |
| 242 | // reserve 4 bytes in front (common for 1(type)-3(length) encoding |
| 243 | // Actual size will be adjusted as necessary by the encoder |
| 244 | EncodingBuffer encoder(4, 4); |
| 245 | size_t valueLength = encoder.appendRange(first, last); |
| 246 | encoder.prependVarNumber(valueLength); |
| 247 | encoder.prependVarNumber(type); |
| 248 | |
| 249 | return encoder.block(); |
| 250 | } |
| 251 | }; |
| 252 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 253 | } // namespace detail |
| 254 | |
| 255 | /** @brief Create a TLV block copying TLV-VALUE from iterators. |
| 256 | * @tparam Iterator an InputIterator dereferencable to an 1-octet type; faster implementation is |
| 257 | * available for RandomAccessIterator |
| 258 | * @param type TLV-TYPE number |
| 259 | * @param first begin iterator |
| 260 | * @param last past-the-end iterator |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 261 | */ |
| 262 | template<class Iterator> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 263 | Block |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 264 | makeBinaryBlock(uint32_t type, Iterator first, Iterator last) |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 265 | { |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 266 | using BinaryBlockHelper = std::conditional_t< |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 267 | std::is_base_of<std::random_access_iterator_tag, |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 268 | typename std::iterator_traits<Iterator>::iterator_category>::value, |
| 269 | detail::BinaryBlockFast<Iterator>, |
Davide Pesavento | db4da5e | 2018-06-15 11:37:52 -0400 | [diff] [blame] | 270 | detail::BinaryBlockSlow<Iterator>>; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 271 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 272 | return BinaryBlockHelper::makeBlock(type, first, last); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 273 | } |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 274 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 275 | /** @brief Prepend a TLV element containing a nested TLV element. |
| 276 | * @tparam U type that satisfies WireEncodableWithEncodingBuffer concept |
| 277 | * @param encoder an EncodingBuffer or EncodingEstimator |
| 278 | * @param type TLV-TYPE number for outer TLV element |
| 279 | * @param value an object to be encoded as inner TLV element |
| 280 | * @sa makeNestedBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 281 | */ |
| 282 | template<Tag TAG, class U> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 283 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 284 | prependNestedBlock(EncodingImpl<TAG>& encoder, uint32_t type, const U& value) |
| 285 | { |
| 286 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<U>)); |
| 287 | |
| 288 | size_t valueLength = value.wireEncode(encoder); |
| 289 | size_t totalLength = valueLength; |
| 290 | totalLength += encoder.prependVarNumber(valueLength); |
| 291 | totalLength += encoder.prependVarNumber(type); |
| 292 | |
| 293 | return totalLength; |
| 294 | } |
| 295 | |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 296 | /** @brief Create a TLV block containing a nested TLV element. |
| 297 | * @tparam U type that satisfies WireEncodableWithEncodingBuffer concept |
| 298 | * @param type TLV-TYPE number for outer TLV element |
| 299 | * @param value an object to be encoded as inner TLV element |
| 300 | * @sa prependNestedBlock |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 301 | */ |
| 302 | template<class U> |
Junxiao Shi | 9da07c5 | 2017-08-06 16:59:30 +0000 | [diff] [blame] | 303 | Block |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 304 | makeNestedBlock(uint32_t type, const U& value) |
| 305 | { |
| 306 | EncodingEstimator estimator; |
| 307 | size_t totalLength = prependNestedBlock(estimator, type, value); |
| 308 | |
| 309 | EncodingBuffer encoder(totalLength, 0); |
| 310 | prependNestedBlock(encoder, type, value); |
| 311 | |
| 312 | return encoder.block(); |
| 313 | } |
| 314 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 315 | } // namespace encoding |
| 316 | |
| 317 | using encoding::makeNonNegativeIntegerBlock; |
| 318 | using encoding::readNonNegativeInteger; |
Junxiao Shi | 5d75fd9 | 2017-08-08 18:09:20 +0000 | [diff] [blame] | 319 | using encoding::readNonNegativeIntegerAs; |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 320 | using encoding::makeEmptyBlock; |
| 321 | using encoding::makeStringBlock; |
| 322 | using encoding::readString; |
| 323 | using encoding::makeBinaryBlock; |
| 324 | using encoding::makeNestedBlock; |
| 325 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 326 | } // namespace ndn |
| 327 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 328 | #endif // NDN_ENCODING_BLOCK_HELPERS_HPP |