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