blob: 11f9e39fd9f4176ea7599dd5f2aeb9b1e8126b72 [file] [log] [blame]
Alexander Afanasyev74633892015-02-08 18:08:46 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento570b20d2018-07-15 21:53:14 -04002/*
3 * Copyright (c) 2013-2018 Regents of the University of California.
Alexander Afanasyev74633892015-02-08 18:08:46 -08004 *
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
Alexander Afanasyev74633892015-02-08 18:08:46 -080025#include "block.hpp"
26
27namespace ndn {
28namespace encoding {
29
30/**
31 * @brief Helper class to estimate size of TLV encoding
32 * Interface of this class (mostly) matches interface of Encoder class
33 * @sa Encoder
34 */
Davide Pesavento570b20d2018-07-15 21:53:14 -040035class Estimator : noncopyable
Alexander Afanasyev74633892015-02-08 18:08:46 -080036{
37public: // common interface between Encoder and Estimator
38 /**
Alexander Afanasyev74633892015-02-08 18:08:46 -080039 * @brief Prepend a byte
40 */
Davide Pesavento570b20d2018-07-15 21:53:14 -040041 constexpr size_t
42 prependByte(uint8_t) const noexcept
43 {
44 return 1;
45 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080046
47 /**
48 * @brief Append a byte
49 */
Davide Pesavento570b20d2018-07-15 21:53:14 -040050 constexpr size_t
51 appendByte(uint8_t) const noexcept
52 {
53 return 1;
54 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080055
56 /**
57 * @brief Prepend a byte array @p array of length @p length
58 */
Davide Pesavento570b20d2018-07-15 21:53:14 -040059 constexpr size_t
60 prependByteArray(const uint8_t*, size_t length) const noexcept
61 {
62 return length;
63 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080064
65 /**
66 * @brief Append a byte array @p array of length @p length
67 */
Davide Pesavento570b20d2018-07-15 21:53:14 -040068 constexpr size_t
69 appendByteArray(const uint8_t*, size_t length) const noexcept
70 {
71 return length;
72 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080073
74 /**
75 * @brief Prepend range of bytes from the range [@p first, @p last)
76 */
77 template<class Iterator>
78 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -040079 prependRange(Iterator first, Iterator last) const noexcept
80 {
81 return std::distance(first, last);
82 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080083
84 /**
85 * @brief Append range of bytes from the range [@p first, @p last)
86 */
87 template<class Iterator>
88 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -040089 appendRange(Iterator first, Iterator last) const noexcept
90 {
91 return std::distance(first, last);
92 }
Alexander Afanasyev74633892015-02-08 18:08:46 -080093
94 /**
95 * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
96 * @sa http://named-data.net/doc/ndn-tlv/
97 */
98 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -040099 prependVarNumber(uint64_t varNumber) const noexcept;
Alexander Afanasyev74633892015-02-08 18:08:46 -0800100
101 /**
102 * @brief Prepend VarNumber @p varNumber of NDN TLV encoding
103 * @sa http://named-data.net/doc/ndn-tlv/
104 */
105 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -0400106 appendVarNumber(uint64_t varNumber) const noexcept;
Alexander Afanasyev74633892015-02-08 18:08:46 -0800107
108 /**
109 * @brief Prepend non-negative integer @p integer of NDN TLV encoding
110 * @sa http://named-data.net/doc/ndn-tlv/
111 */
112 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -0400113 prependNonNegativeInteger(uint64_t integer) const noexcept;
Alexander Afanasyev74633892015-02-08 18:08:46 -0800114
115 /**
116 * @brief Append non-negative integer @p integer of NDN TLV encoding
117 * @sa http://named-data.net/doc/ndn-tlv/
118 */
119 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -0400120 appendNonNegativeInteger(uint64_t integer) const noexcept;
Alexander Afanasyev74633892015-02-08 18:08:46 -0800121
122 /**
123 * @brief Prepend TLV block of type @p type and value from buffer @p array of size @p arraySize
124 */
125 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -0400126 prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) const noexcept;
Alexander Afanasyev74633892015-02-08 18:08:46 -0800127
128 /**
129 * @brief Append TLV block of type @p type and value from buffer @p array of size @p arraySize
130 */
131 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -0400132 appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) const noexcept;
Alexander Afanasyev74633892015-02-08 18:08:46 -0800133
134 /**
135 * @brief Prepend TLV block @p block
136 */
137 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -0400138 prependBlock(const Block& block) const;
Alexander Afanasyev74633892015-02-08 18:08:46 -0800139
140 /**
141 * @brief Append TLV block @p block
142 */
143 size_t
Davide Pesavento570b20d2018-07-15 21:53:14 -0400144 appendBlock(const Block& block) const;
Alexander Afanasyev74633892015-02-08 18:08:46 -0800145};
146
Alexander Afanasyev74633892015-02-08 18:08:46 -0800147} // namespace encoding
148} // namespace ndn
149
150#endif // NDN_ENCODING_ESTIMATOR_HPP