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 | /** |
Junxiao Shi | 19d7a57 | 2016-07-26 12:55:09 +0000 | [diff] [blame^] | 3 | * Copyright (c) 2013-2016 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 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 223 | } // namespace encoding |
| 224 | |
| 225 | using encoding::makeNonNegativeIntegerBlock; |
| 226 | using encoding::readNonNegativeInteger; |
| 227 | using encoding::makeEmptyBlock; |
| 228 | using encoding::makeStringBlock; |
| 229 | using encoding::readString; |
| 230 | using encoding::makeBinaryBlock; |
| 231 | using encoding::makeNestedBlock; |
| 232 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 233 | } // namespace ndn |
| 234 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 235 | #endif // NDN_ENCODING_BLOCK_HELPERS_HPP |