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 | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 28 | #include <iterator> |
| 29 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 30 | namespace ndn { |
| 31 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 32 | /** |
| 33 | * @deprecated Use Encoder::prependBlock and Estimator::prependBlock instead |
| 34 | */ |
| 35 | template<bool P> |
| 36 | inline size_t |
| 37 | prependBlock(EncodingImpl<P>& encoder, const Block& block) |
| 38 | { |
| 39 | return encoder.prependBlock(block); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * @deprecated Use Encoder::prependByteArrayBlock and Estimator::prependByteArrayBlock instead |
| 44 | */ |
| 45 | template<bool P> |
| 46 | inline size_t |
| 47 | prependByteArrayBlock(EncodingImpl<P>& encoder, |
| 48 | uint32_t type, const uint8_t* array, size_t arraySize) |
| 49 | { |
| 50 | return encoder.prependByteArrayBlock(type, array, arraySize); |
| 51 | } |
| 52 | |
| 53 | template<bool P> |
| 54 | inline size_t |
| 55 | prependNonNegativeIntegerBlock(EncodingImpl<P>& encoder, uint32_t type, uint64_t number) |
| 56 | { |
| 57 | size_t valueLength = encoder.prependNonNegativeInteger(number); |
| 58 | size_t totalLength = valueLength; |
| 59 | totalLength += encoder.prependVarNumber(valueLength); |
| 60 | totalLength += encoder.prependVarNumber(type); |
| 61 | |
| 62 | return totalLength; |
| 63 | } |
| 64 | |
| 65 | template<bool P> |
| 66 | inline size_t |
| 67 | prependBooleanBlock(EncodingImpl<P>& encoder, uint32_t type) |
| 68 | { |
| 69 | size_t totalLength = encoder.prependVarNumber(0); |
| 70 | totalLength += encoder.prependVarNumber(type); |
| 71 | |
| 72 | return totalLength; |
| 73 | } |
| 74 | |
| 75 | template<bool P, class U> |
| 76 | inline size_t |
| 77 | prependNestedBlock(EncodingImpl<P>& encoder, uint32_t type, const U& nestedBlock) |
| 78 | { |
| 79 | size_t valueLength = nestedBlock.wireEncode(encoder); |
| 80 | size_t totalLength = valueLength; |
| 81 | totalLength += encoder.prependVarNumber(valueLength); |
| 82 | totalLength += encoder.prependVarNumber(type); |
| 83 | |
| 84 | return totalLength; |
| 85 | } |
| 86 | |
| 87 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 88 | inline Block |
| 89 | nonNegativeIntegerBlock(uint32_t type, uint64_t value) |
| 90 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 91 | EncodingEstimator estimator; |
| 92 | size_t totalLength = prependNonNegativeIntegerBlock(estimator, type, value); |
| 93 | |
| 94 | EncodingBuffer encoder(totalLength, 0); |
| 95 | prependNonNegativeIntegerBlock(encoder, type, value); |
| 96 | |
| 97 | return encoder.block(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | inline uint64_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 101 | readNonNegativeInteger(const Block& block) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 102 | { |
| 103 | Buffer::const_iterator begin = block.value_begin(); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 104 | return tlv::readNonNegativeInteger(block.value_size(), begin, block.value_end()); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | inline Block |
| 108 | booleanBlock(uint32_t type) |
| 109 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 110 | EncodingEstimator estimator; |
| 111 | size_t totalLength = prependBooleanBlock(estimator, type); |
| 112 | |
| 113 | EncodingBuffer encoder(totalLength, 0); |
| 114 | prependBooleanBlock(encoder, type); |
| 115 | |
| 116 | return encoder.block(); |
| 117 | } |
| 118 | |
| 119 | inline Block |
| 120 | dataBlock(uint32_t type, const uint8_t* data, size_t dataSize) |
| 121 | { |
| 122 | EncodingEstimator estimator; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 123 | size_t totalLength = estimator.prependByteArrayBlock(type, data, dataSize); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 124 | |
| 125 | EncodingBuffer encoder(totalLength, 0); |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 126 | encoder.prependByteArrayBlock(type, data, dataSize); |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 127 | |
| 128 | return encoder.block(); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 132 | dataBlock(uint32_t type, const char* data, size_t dataSize) |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 133 | { |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 134 | return dataBlock(type, reinterpret_cast<const uint8_t*>(data), dataSize); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 137 | /** |
| 138 | * @brief Helper class template to create a data block when RandomAccessIterator is used |
| 139 | */ |
| 140 | template<class Iterator> |
| 141 | class DataBlockFast |
| 142 | { |
| 143 | public: |
| 144 | BOOST_CONCEPT_ASSERT((boost::RandomAccessIterator<Iterator>)); |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 145 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 146 | static Block |
| 147 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 148 | { |
| 149 | EncodingEstimator estimator; |
| 150 | size_t valueLength = last - first; |
| 151 | size_t totalLength = valueLength; |
| 152 | totalLength += estimator.prependVarNumber(valueLength); |
| 153 | totalLength += estimator.prependVarNumber(type); |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 154 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 155 | EncodingBuffer encoder(totalLength, 0); |
| 156 | encoder.prependRange(first, last); |
| 157 | encoder.prependVarNumber(valueLength); |
| 158 | encoder.prependVarNumber(type); |
| 159 | |
| 160 | return encoder.block(); |
| 161 | } |
| 162 | }; |
| 163 | |
| 164 | /** |
| 165 | * @brief Helper class template to create a data block when generic InputIterator is used |
| 166 | */ |
| 167 | template<class Iterator> |
| 168 | class DataBlockSlow |
| 169 | { |
| 170 | public: |
| 171 | BOOST_CONCEPT_ASSERT((boost::InputIterator<Iterator>)); |
| 172 | |
| 173 | static Block |
| 174 | makeBlock(uint32_t type, Iterator first, Iterator last) |
| 175 | { |
| 176 | // reserve 4 bytes in front (common for 1(type)-3(length) encoding |
| 177 | // Actual size will be adjusted as necessary by the encoder |
| 178 | EncodingBuffer encoder(4, 4); |
| 179 | size_t valueLength = encoder.appendRange(first, last); |
| 180 | encoder.prependVarNumber(valueLength); |
| 181 | encoder.prependVarNumber(type); |
| 182 | |
| 183 | return encoder.block(); |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | /** |
| 188 | * @brief Free function to create a block given @p type and range [@p first, @p last) of bytes |
| 189 | * @tparam Iterator iterator type satisfying at least InputIterator concept. Implementation |
| 190 | * is more optimal when the iterator type satisfies RandomAccessIterator concept |
| 191 | * It is required that sizeof(std::iterator_traits<Iterator>::value_type) == 1. |
| 192 | */ |
| 193 | template<class Iterator> |
| 194 | inline Block |
| 195 | dataBlock(uint32_t type, Iterator first, Iterator last) |
| 196 | { |
| 197 | static_assert(sizeof(typename std::iterator_traits<Iterator>::value_type) == 1, |
| 198 | "Iterator should point only to char or unsigned char"); |
| 199 | |
| 200 | typedef typename boost::mpl::if_< |
| 201 | std::is_base_of<std::random_access_iterator_tag, |
| 202 | typename std::iterator_traits<Iterator>::iterator_category>, |
| 203 | DataBlockFast<Iterator>, |
| 204 | DataBlockSlow<Iterator>>::type DataBlock; |
| 205 | |
| 206 | return DataBlock::makeBlock(type, first, last); |
| 207 | } |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 208 | |
Alexander Afanasyev | 13bb51a | 2014-01-02 19:13:26 -0800 | [diff] [blame] | 209 | } // namespace ndn |
| 210 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 211 | #endif // NDN_ENCODING_BLOCK_HELPERS_HPP |