Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 25 | #include "block.hpp" |
| 26 | |
| 27 | namespace ndn { |
| 28 | namespace 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 Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 35 | class Estimator : noncopyable |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 36 | { |
| 37 | public: // common interface between Encoder and Estimator |
| 38 | /** |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 39 | * @brief Prepend a byte |
| 40 | */ |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 41 | constexpr size_t |
| 42 | prependByte(uint8_t) const noexcept |
| 43 | { |
| 44 | return 1; |
| 45 | } |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * @brief Append a byte |
| 49 | */ |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 50 | constexpr size_t |
| 51 | appendByte(uint8_t) const noexcept |
| 52 | { |
| 53 | return 1; |
| 54 | } |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * @brief Prepend a byte array @p array of length @p length |
| 58 | */ |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 59 | constexpr size_t |
| 60 | prependByteArray(const uint8_t*, size_t length) const noexcept |
| 61 | { |
| 62 | return length; |
| 63 | } |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 64 | |
| 65 | /** |
| 66 | * @brief Append a byte array @p array of length @p length |
| 67 | */ |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 68 | constexpr size_t |
| 69 | appendByteArray(const uint8_t*, size_t length) const noexcept |
| 70 | { |
| 71 | return length; |
| 72 | } |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 73 | |
| 74 | /** |
| 75 | * @brief Prepend range of bytes from the range [@p first, @p last) |
| 76 | */ |
| 77 | template<class Iterator> |
| 78 | size_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 79 | prependRange(Iterator first, Iterator last) const noexcept |
| 80 | { |
| 81 | return std::distance(first, last); |
| 82 | } |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * @brief Append range of bytes from the range [@p first, @p last) |
| 86 | */ |
| 87 | template<class Iterator> |
| 88 | size_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 89 | appendRange(Iterator first, Iterator last) const noexcept |
| 90 | { |
| 91 | return std::distance(first, last); |
| 92 | } |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 93 | |
| 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 Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 99 | prependVarNumber(uint64_t varNumber) const noexcept; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 100 | |
| 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 Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 106 | appendVarNumber(uint64_t varNumber) const noexcept; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 107 | |
| 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 Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 113 | prependNonNegativeInteger(uint64_t integer) const noexcept; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 114 | |
| 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 Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 120 | appendNonNegativeInteger(uint64_t integer) const noexcept; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 121 | |
| 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 Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 126 | prependByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) const noexcept; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 127 | |
| 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 Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 132 | appendByteArrayBlock(uint32_t type, const uint8_t* array, size_t arraySize) const noexcept; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 133 | |
| 134 | /** |
| 135 | * @brief Prepend TLV block @p block |
| 136 | */ |
| 137 | size_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 138 | prependBlock(const Block& block) const; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * @brief Append TLV block @p block |
| 142 | */ |
| 143 | size_t |
Davide Pesavento | 570b20d | 2018-07-15 21:53:14 -0400 | [diff] [blame] | 144 | appendBlock(const Block& block) const; |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 147 | } // namespace encoding |
| 148 | } // namespace ndn |
| 149 | |
| 150 | #endif // NDN_ENCODING_ESTIMATOR_HPP |