blob: 364f9534a9c80f893bef02c437964caf9812cb75 [file] [log] [blame]
Alexander Afanasyev74633892015-02-08 18:08:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
4 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
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.
20 */
21
22#ifndef NDN_ENCODING_ESTIMATOR_HPP
23#define NDN_ENCODING_ESTIMATOR_HPP
24
25#include "../common.hpp"
26#include "block.hpp"
27
28namespace ndn {
29namespace encoding {
30
31/**
32 * @brief Helper class to estimate size of TLV encoding
33 * Interface of this class (mostly) matches interface of Encoder class
34 * @sa Encoder
35 */
36class Estimator
37{
38public: // common interface between Encoder and Estimator
39 /**
40 * @brief Create instance of the estimator
Davide Pesavento18cf81b2015-09-12 23:36:43 +020041 * @param totalReserve not used (for compatibility with the Encoder)
42 * @param reserveFromBack not used (for compatibility with the Encoder)
Alexander Afanasyev74633892015-02-08 18:08:46 -080043 */
44 explicit
45 Estimator(size_t totalReserve = 0, size_t reserveFromBack = 0);
46
47 Estimator(const Estimator&) = delete;
48
49 Estimator&
50 operator=(const Estimator&) = delete;
51
52 /**
53 * @brief Prepend a byte
54 */
55 size_t
56 prependByte(uint8_t value);
57
58 /**
59 * @brief Append a byte
60 */
61 size_t
62 appendByte(uint8_t value);
63
64 /**
65 * @brief Prepend a byte array @p array of length @p length
66 */
67 size_t
68 prependByteArray(const uint8_t* array, size_t length);
69
70 /**
71 * @brief Append a byte array @p array of length @p length
72 */
73 size_t
74 appendByteArray(const uint8_t* array, size_t length);
75
76 /**
77 * @brief Prepend range of bytes from the range [@p first, @p last)
78 */
79 template<class Iterator>
80 size_t
81 prependRange(Iterator first, Iterator last);
82
83 /**
84 * @brief Append range of bytes from the range [@p first, @p last)
85 */
86 template<class Iterator>
87 size_t
88 appendRange(Iterator first, Iterator last);
89
90 /**
91 * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
92 * @sa http://named-data.net/doc/ndn-tlv/
93 */
94 size_t
95 prependVarNumber(uint64_t varNumber);
96
97 /**
98 * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
99 * @sa http://named-data.net/doc/ndn-tlv/
100 */
101 size_t
102 appendVarNumber(uint64_t varNumber);
103
104 /**
105 * @brief Prepend non-negative integer @p integer of NDN TLV encoding
106 * @sa http://named-data.net/doc/ndn-tlv/
107 */
108 size_t
109 prependNonNegativeInteger(uint64_t integer);
110
111 /**
112 * @brief Append non-negative integer @p integer of NDN TLV encoding
113 * @sa http://named-data.net/doc/ndn-tlv/
114 */
115 size_t
116 appendNonNegativeInteger(uint64_t integer);
117
118 /**
119 * @brief Prepend TLV block of type @p type and value from buffer @p array of size @p arraySize
120 */
121 size_t
122 prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
123
124 /**
125 * @brief Append TLV block of type @p type and value from buffer @p array of size @p arraySize
126 */
127 size_t
128 appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize);
129
130 /**
131 * @brief Prepend TLV block @p block
132 */
133 size_t
134 prependBlock(const Block& block);
135
136 /**
137 * @brief Append TLV block @p block
138 */
139 size_t
140 appendBlock(const Block& block);
141};
142
143
144template<class Iterator>
145inline size_t
146Estimator::prependRange(Iterator first, Iterator last)
147{
148 return std::distance(first, last);
149}
150
151
152template<class Iterator>
153inline size_t
154Estimator::appendRange(Iterator first, Iterator last)
155{
156 return prependRange(first, last);
157}
158
159} // namespace encoding
160} // namespace ndn
161
162#endif // NDN_ENCODING_ESTIMATOR_HPP