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