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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -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). |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -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 Wentao Shang <http://irl.cs.ucla.edu/~wentao/> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 23 | */ |
| 24 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 25 | #ifndef NDN_ENCODING_ENCODING_BUFFER_HPP |
| 26 | #define NDN_ENCODING_ENCODING_BUFFER_HPP |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 27 | |
| 28 | #include "../common.hpp" |
| 29 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 30 | #include "buffer.hpp" |
Alexander Afanasyev | 233750e | 2014-02-16 00:50:07 -0800 | [diff] [blame] | 31 | #include "block.hpp" |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 32 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 33 | namespace ndn { |
| 34 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 35 | namespace encoding { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 36 | static const bool Buffer = true; |
| 37 | static const bool Estimator = false; |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 38 | } // encoding |
| 39 | |
| 40 | template<bool isRealEncoderNotEstimator> |
| 41 | class EncodingImpl; |
| 42 | |
| 43 | typedef EncodingImpl<encoding::Buffer> EncodingBuffer; |
| 44 | typedef EncodingImpl<encoding::Estimator> EncodingEstimator; |
| 45 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 46 | /** |
| 47 | * @brief Class representing wire element of the NDN packet |
| 48 | */ |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 49 | template<> |
| 50 | class EncodingImpl<encoding::Buffer> |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 51 | { |
| 52 | public: |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 53 | /** |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 54 | * @brief Constructor to create a EncodingImpl with specified reserved sizes |
| 55 | * |
| 56 | * The caller should make sure that that reserveFromBack does not exceed totalReserve, |
| 57 | * otherwise behavior is undefined. |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 58 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 59 | EncodingImpl(size_t totalReserve = 8800, |
| 60 | size_t reserveFromBack = 400) |
| 61 | : m_buffer(new Buffer(totalReserve)) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 62 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 63 | m_begin = m_end = m_buffer->end() - (reserveFromBack < totalReserve ? reserveFromBack : 0); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 64 | } |
| 65 | |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 66 | /** |
| 67 | * @brief Create EncodingBlock from existing block |
| 68 | * |
| 69 | * This is a dangerous constructor and should be used with caution. |
| 70 | * It will modify contents of the buffer that is used by block and may |
| 71 | * impact data in other blocks. |
| 72 | * |
| 73 | * The primary purpose for this method is to be used to extend Block |
| 74 | * after sign operation. |
| 75 | */ |
| 76 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 77 | EncodingImpl(const Block& block) |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 78 | : m_buffer(const_pointer_cast<Buffer>(block.m_buffer)) |
| 79 | , m_begin(m_buffer->begin() + (block.begin() - m_buffer->begin())) |
| 80 | , m_end(m_buffer->begin() + (block.end() - m_buffer->begin())) |
| 81 | { |
| 82 | } |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 83 | |
| 84 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 85 | size() const; |
| 86 | |
| 87 | inline size_t |
| 88 | capacity() const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 89 | |
| 90 | inline uint8_t* |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | buf(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 92 | |
| 93 | inline const uint8_t* |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 94 | buf() const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 96 | /** |
| 97 | * @brief Create Block from the underlying EncodingBuffer |
| 98 | * |
| 99 | * @param verifyLength If this parameter set to true, Block's constructor |
| 100 | * will be requested to verify consistency of the encoded |
| 101 | * length in the Block, otherwise ignored |
| 102 | */ |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 103 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 104 | block(bool verifyLength = true) const; |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 105 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 106 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 107 | resize(size_t size, bool addInFront); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 108 | |
| 109 | inline Buffer::iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 110 | begin(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 111 | |
| 112 | inline Buffer::iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 113 | end(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 114 | |
| 115 | inline Buffer::const_iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 116 | begin() const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 117 | |
| 118 | inline Buffer::const_iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 119 | end() const; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 120 | |
| 121 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 122 | prependByte(uint8_t value); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 123 | |
| 124 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 125 | prependByteArray(const uint8_t* array, size_t length); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 126 | |
| 127 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 128 | prependNonNegativeInteger(uint64_t varNumber); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 129 | |
| 130 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 131 | prependVarNumber(uint64_t varNumber); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 132 | |
| 133 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 134 | appendByte(uint8_t value); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 135 | |
| 136 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 137 | appendByteArray(const uint8_t* array, size_t length); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 138 | |
| 139 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 140 | appendNonNegativeInteger(uint64_t varNumber); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 141 | |
| 142 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 143 | appendVarNumber(uint64_t varNumber); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 145 | // inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 146 | // removeByteFromFront(); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 147 | |
| 148 | // inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 149 | // removeByteFromEnd(); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 150 | |
| 151 | // inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 152 | // removeVarNumberFromFront(uint64_t varNumber); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 153 | |
| 154 | // inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 155 | // removeVarNumberFromBack(uint64_t varNumber); |
| 156 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 157 | private: |
| 158 | BufferPtr m_buffer; |
| 159 | |
| 160 | // invariant: m_begin always points to the position of last-written byte (if prepending data) |
| 161 | Buffer::iterator m_begin; |
| 162 | // invariant: m_end always points to the position of next unwritten byte (if appending data) |
| 163 | Buffer::iterator m_end; |
Alexander Afanasyev | 1515131 | 2014-02-16 00:53:51 -0800 | [diff] [blame] | 164 | |
| 165 | friend class Block; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 166 | }; |
| 167 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 168 | |
| 169 | /** |
| 170 | * @brief Class representing wire element of the NDN packet |
| 171 | */ |
| 172 | template<> |
| 173 | class EncodingImpl<encoding::Estimator> |
| 174 | { |
| 175 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 176 | EncodingImpl(size_t totalReserve = 8800, |
| 177 | size_t reserveFromBack = 400) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 178 | { |
| 179 | } |
| 180 | |
| 181 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 182 | prependByte(uint8_t value); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 183 | |
| 184 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 185 | prependByteArray(const uint8_t* array, size_t length); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 186 | |
| 187 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 188 | prependNonNegativeInteger(uint64_t varNumber); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 189 | |
| 190 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 191 | prependVarNumber(uint64_t varNumber); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 192 | |
| 193 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 194 | appendByte(uint8_t value); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 195 | |
| 196 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 197 | appendByteArray(const uint8_t* array, size_t length); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 198 | |
| 199 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 200 | appendNonNegativeInteger(uint64_t varNumber); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 201 | |
| 202 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 203 | appendVarNumber(uint64_t varNumber); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 204 | }; |
| 205 | |
| 206 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 207 | //////////////////////////////////////////////////////////////////////////////// |
| 208 | //////////////////////////////////////////////////////////////////////////////// |
| 209 | //////////////////////////////////////////////////////////////////////////////// |
| 210 | |
| 211 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 212 | EncodingImpl<encoding::Buffer>::size() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 213 | { |
| 214 | return m_end - m_begin; |
| 215 | } |
| 216 | |
| 217 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 218 | EncodingImpl<encoding::Buffer>::capacity() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 219 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 220 | return m_buffer->size(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | inline uint8_t* |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 224 | EncodingImpl<encoding::Buffer>::buf() |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 225 | { |
| 226 | return &(*m_begin); |
| 227 | } |
| 228 | |
| 229 | inline const uint8_t* |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 230 | EncodingImpl<encoding::Buffer>::buf() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 231 | { |
| 232 | return &(*m_begin); |
| 233 | } |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 234 | |
| 235 | inline Block |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 236 | EncodingImpl<encoding::Buffer>::block(bool verifyLength/* = true*/) const |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 237 | { |
| 238 | return Block(m_buffer, |
Alexander Afanasyev | 5964fb7 | 2014-02-18 12:42:45 -0800 | [diff] [blame] | 239 | m_begin, m_end, |
| 240 | verifyLength); |
Alexander Afanasyev | 187bc48 | 2014-02-06 15:04:04 -0800 | [diff] [blame] | 241 | } |
| 242 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 243 | inline void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 244 | EncodingImpl<encoding::Buffer>::resize(size_t size, bool addInFront) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 245 | { |
| 246 | if (addInFront) |
| 247 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 248 | size_t diff_end = m_buffer->end() - m_end; |
| 249 | size_t diff_begin = m_buffer->end() - m_begin; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 250 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 251 | Buffer* buf = new Buffer(size); |
| 252 | std::copy_backward(m_buffer->begin(), m_buffer->end(), buf->end()); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 253 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 254 | m_buffer.reset(buf); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 255 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 256 | m_end = m_buffer->end() - diff_end; |
| 257 | m_begin = m_buffer->end() - diff_begin; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 258 | } |
| 259 | else |
| 260 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 261 | size_t diff_end = m_end - m_buffer->begin(); |
| 262 | size_t diff_begin = m_begin - m_buffer->begin(); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 263 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 264 | Buffer* buf = new Buffer(size); |
| 265 | std::copy(m_buffer->begin(), m_buffer->end(), buf->begin()); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 266 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 267 | m_buffer.reset(buf); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 268 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 269 | m_end = m_buffer->begin() + diff_end; |
| 270 | m_begin = m_buffer->begin() + diff_begin; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
| 274 | inline Buffer::iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 275 | EncodingImpl<encoding::Buffer>::begin() |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 276 | { |
| 277 | return m_begin; |
| 278 | } |
| 279 | |
| 280 | inline Buffer::iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 281 | EncodingImpl<encoding::Buffer>::end() |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 282 | { |
| 283 | return m_end; |
| 284 | } |
| 285 | |
| 286 | inline Buffer::const_iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 287 | EncodingImpl<encoding::Buffer>::begin() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 288 | { |
| 289 | return m_begin; |
| 290 | } |
| 291 | |
| 292 | inline Buffer::const_iterator |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 293 | EncodingImpl<encoding::Buffer>::end() const |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 294 | { |
| 295 | return m_end; |
| 296 | } |
| 297 | |
| 298 | |
| 299 | ////////////////////////////////////////////////////////// |
| 300 | // Prepend to the back of the buffer. Resize if needed. // |
| 301 | ////////////////////////////////////////////////////////// |
| 302 | |
| 303 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 304 | EncodingImpl<encoding::Buffer>::prependByte(uint8_t value) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 305 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 306 | if (m_begin == m_buffer->begin()) |
| 307 | resize(m_buffer->size() * 2, true); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 308 | |
| 309 | m_begin--; |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 310 | *m_begin = value; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 311 | return 1; |
| 312 | } |
| 313 | |
| 314 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 315 | EncodingImpl<encoding::Estimator>::prependByte(uint8_t value) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 316 | { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 317 | return 1; |
| 318 | } |
| 319 | |
| 320 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 321 | EncodingImpl<encoding::Buffer>::prependByteArray(const uint8_t* array, size_t length) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 322 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 323 | if ((m_buffer->begin() + length) > m_begin) |
| 324 | resize(m_buffer->size() * 2 + length, true); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 325 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 326 | m_begin -= length; |
| 327 | std::copy(array, array + length, m_begin); |
| 328 | return length; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 332 | EncodingImpl<encoding::Estimator>::prependByteArray(const uint8_t* array, size_t length) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 333 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 334 | return length; |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 338 | EncodingImpl<encoding::Buffer>::prependNonNegativeInteger(uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 339 | { |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 340 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 341 | return prependByte(static_cast<uint8_t>(varNumber)); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 342 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 343 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 344 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
| 345 | return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 346 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 347 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 348 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
| 349 | return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 350 | } |
| 351 | else { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 352 | uint64_t value = htobe64(varNumber); |
| 353 | return prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 354 | } |
| 355 | } |
| 356 | |
| 357 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 358 | EncodingImpl<encoding::Estimator>::prependNonNegativeInteger(uint64_t varNumber) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 359 | { |
Alexander Afanasyev | 2d0b457 | 2014-02-24 16:02:12 -0800 | [diff] [blame] | 360 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 361 | return 1; |
| 362 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 363 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 364 | return 2; |
| 365 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 366 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 367 | return 4; |
| 368 | } |
| 369 | else { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 370 | return 8; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 375 | EncodingImpl<encoding::Buffer>::prependVarNumber(uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 376 | { |
| 377 | if (varNumber < 253) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 378 | prependByte(static_cast<uint8_t>(varNumber)); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 379 | return 1; |
| 380 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 381 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 382 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
| 383 | prependByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
| 384 | prependByte(253); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 385 | return 3; |
| 386 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 387 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 388 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
| 389 | prependByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
| 390 | prependByte(254); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 391 | return 5; |
| 392 | } |
| 393 | else { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 394 | uint64_t value = htobe64(varNumber); |
| 395 | prependByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
| 396 | prependByte(255); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 397 | return 9; |
| 398 | } |
| 399 | } |
| 400 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 401 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 402 | EncodingImpl<encoding::Estimator>::prependVarNumber(uint64_t varNumber) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 403 | { |
| 404 | if (varNumber < 253) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 405 | return 1; |
| 406 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 407 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 408 | return 3; |
| 409 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 410 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 411 | return 5; |
| 412 | } |
| 413 | else { |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 414 | return 9; |
| 415 | } |
| 416 | } |
| 417 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 418 | ///////////////////////////////////////////////////////// |
| 419 | // Append to the back of the buffer. Resize if needed. // |
| 420 | ///////////////////////////////////////////////////////// |
| 421 | |
| 422 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 423 | EncodingImpl<encoding::Buffer>::appendByte(uint8_t value) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 424 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 425 | if (m_end == m_buffer->end()) |
| 426 | resize(m_buffer->size() * 2, false); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 427 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 428 | *m_end = value; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 429 | m_end++; |
| 430 | return 1; |
| 431 | } |
| 432 | |
| 433 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 434 | EncodingImpl<encoding::Estimator>::appendByte(uint8_t value) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 435 | { |
| 436 | return 1; |
| 437 | } |
| 438 | |
| 439 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 440 | EncodingImpl<encoding::Buffer>::appendByteArray(const uint8_t* array, size_t length) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 441 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 442 | if ((m_end + length) > m_buffer->end()) |
| 443 | resize(m_buffer->size() * 2 + length, false); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 444 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 445 | std::copy(array, array + length, m_end); |
| 446 | m_end += length; |
| 447 | return length; |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 451 | EncodingImpl<encoding::Estimator>::appendByteArray(const uint8_t* array, size_t length) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 452 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 453 | return prependByteArray(array, length); |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 457 | EncodingImpl<encoding::Buffer>::appendNonNegativeInteger(uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 458 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 459 | if (varNumber <= std::numeric_limits<uint8_t>::max()) { |
| 460 | return appendByte(static_cast<uint8_t>(varNumber)); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 461 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 462 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 463 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
| 464 | return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 465 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 466 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 467 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
| 468 | return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 469 | } |
| 470 | else { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 471 | uint64_t value = htobe64(varNumber); |
| 472 | return appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | |
| 476 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 477 | EncodingImpl<encoding::Estimator>::appendNonNegativeInteger(uint64_t varNumber) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 478 | { |
| 479 | return prependNonNegativeInteger(varNumber); |
| 480 | } |
| 481 | |
| 482 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 483 | EncodingImpl<encoding::Buffer>::appendVarNumber(uint64_t varNumber) |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 484 | { |
| 485 | if (varNumber < 253) { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 486 | appendByte(static_cast<uint8_t>(varNumber)); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 487 | return 1; |
| 488 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 489 | else if (varNumber <= std::numeric_limits<uint16_t>::max()) { |
| 490 | appendByte(253); |
| 491 | uint16_t value = htobe16(static_cast<uint16_t>(varNumber)); |
| 492 | appendByteArray(reinterpret_cast<const uint8_t*>(&value), 2); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 493 | return 3; |
| 494 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 495 | else if (varNumber <= std::numeric_limits<uint32_t>::max()) { |
| 496 | appendByte(254); |
| 497 | uint32_t value = htobe32(static_cast<uint32_t>(varNumber)); |
| 498 | appendByteArray(reinterpret_cast<const uint8_t*>(&value), 4); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 499 | return 5; |
| 500 | } |
| 501 | else { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 502 | appendByte(255); |
| 503 | uint64_t value = htobe64(varNumber); |
| 504 | appendByteArray(reinterpret_cast<const uint8_t*>(&value), 8); |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 505 | return 9; |
| 506 | } |
| 507 | } |
| 508 | |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 509 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 510 | EncodingImpl<encoding::Estimator>::appendVarNumber(uint64_t varNumber) |
Alexander Afanasyev | 217325b | 2014-02-06 15:03:28 -0800 | [diff] [blame] | 511 | { |
| 512 | return prependVarNumber(varNumber); |
| 513 | } |
| 514 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 515 | /// helper methods |
| 516 | |
| 517 | template<bool P> |
| 518 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 519 | prependNonNegativeIntegerBlock(EncodingImpl<P>& encoder, uint32_t type, uint64_t number) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 520 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 521 | size_t valueLength = encoder.prependNonNegativeInteger(number); |
| 522 | size_t totalLength = valueLength; |
| 523 | totalLength += encoder.prependVarNumber(valueLength); |
| 524 | totalLength += encoder.prependVarNumber(type); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 525 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 526 | return totalLength; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | template<bool P> |
| 530 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 531 | prependByteArrayBlock(EncodingImpl<P>& encoder, uint32_t type, |
| 532 | const uint8_t* array, size_t arraySize) |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 533 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 534 | size_t valueLength = encoder.prependByteArray(array, arraySize); |
| 535 | size_t totalLength = valueLength; |
| 536 | totalLength += encoder.prependVarNumber(valueLength); |
| 537 | totalLength += encoder.prependVarNumber(type); |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 538 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 539 | return totalLength; |
Alexander Afanasyev | 0553cd5 | 2014-02-18 02:34:45 -0800 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | template<bool P> |
| 543 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 544 | prependBooleanBlock(EncodingImpl<P>& encoder, uint32_t type) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 545 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 546 | size_t totalLength = encoder.prependVarNumber(0); |
| 547 | totalLength += encoder.prependVarNumber(type); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 548 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 549 | return totalLength; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | |
| 553 | template<bool P, class U> |
| 554 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 555 | prependNestedBlock(EncodingImpl<P>& encoder, uint32_t type, const U& nestedBlock) |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 556 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 557 | size_t valueLength = nestedBlock.wireEncode(encoder); |
| 558 | size_t totalLength = valueLength; |
| 559 | totalLength += encoder.prependVarNumber(valueLength); |
| 560 | totalLength += encoder.prependVarNumber(type); |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 561 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 562 | return totalLength; |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 563 | } |
| 564 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 565 | template<bool P> |
| 566 | inline size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 567 | prependBlock(EncodingImpl<P>& encoder, const Block& block) |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 568 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 569 | return encoder.prependByteArray(block.wire(), block.size()); |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 570 | } |
| 571 | |
Alexander Afanasyev | c348f83 | 2014-02-17 16:35:17 -0800 | [diff] [blame] | 572 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 573 | } // ndn |
| 574 | |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 575 | #endif // NDN_ENCODING_ENCODING_BUFFER_HPP |