Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame^] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [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 | |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame^] | 22 | #ifndef NDN_CXX_LP_FIELD_DECL_HPP |
| 23 | #define NDN_CXX_LP_FIELD_DECL_HPP |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame^] | 25 | #include "field.hpp" |
| 26 | #include "tlv.hpp" |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame^] | 28 | #include "../encoding/block-helpers.hpp" |
| 29 | #include "../util/concepts.hpp" |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 30 | #include <boost/concept/requires.hpp> |
| 31 | |
| 32 | namespace ndn { |
| 33 | namespace lp { |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 34 | |
| 35 | template<typename TlvType, typename T> |
| 36 | struct DecodeHelper |
| 37 | { |
| 38 | static |
| 39 | BOOST_CONCEPT_REQUIRES(((WireDecodable<T>)), (T)) |
| 40 | decode(const Block& wire) |
| 41 | { |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 42 | T type; |
| 43 | type.wireDecode(wire); |
| 44 | return type; |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | template<typename TlvType> |
| 49 | struct DecodeHelper<TlvType, uint64_t> |
| 50 | { |
| 51 | static uint64_t |
| 52 | decode(const Block& wire) |
| 53 | { |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 54 | return readNonNegativeInteger(wire); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | template<typename TlvType> |
| 59 | struct DecodeHelper<TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>> |
| 60 | { |
| 61 | static std::pair<Buffer::const_iterator, Buffer::const_iterator> |
| 62 | decode(const Block& wire) |
| 63 | { |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 64 | if (wire.value_size() == 0) { |
Davide Pesavento | 96b96af | 2015-09-19 23:00:40 +0200 | [diff] [blame] | 65 | BOOST_THROW_EXCEPTION(ndn::tlv::Error(to_string(wire.type()) + " must not be empty")); |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | return std::make_pair(wire.value_begin(), wire.value_end()); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | template<typename encoding::Tag TAG, typename TlvType, typename T> |
| 73 | struct EncodeHelper |
| 74 | { |
| 75 | static |
| 76 | BOOST_CONCEPT_REQUIRES(((WireEncodable<T>)), (size_t)) |
| 77 | encode(EncodingImpl<TAG>& encoder, const T& value) |
| 78 | { |
| 79 | return value.wireEncode(encoder); |
| 80 | } |
| 81 | }; |
| 82 | |
| 83 | template<typename encoding::Tag TAG, typename TlvType> |
| 84 | struct EncodeHelper<TAG, TlvType, uint64_t> |
| 85 | { |
| 86 | static size_t |
| 87 | encode(EncodingImpl<TAG>& encoder, const uint64_t value) |
| 88 | { |
| 89 | return prependNonNegativeIntegerBlock(encoder, TlvType::value, value); |
| 90 | } |
| 91 | }; |
| 92 | |
| 93 | template<typename encoding::Tag TAG, typename TlvType> |
| 94 | struct EncodeHelper<TAG, TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>> |
| 95 | { |
| 96 | static size_t |
| 97 | encode(EncodingImpl<TAG>& encoder, const std::pair<Buffer::const_iterator, Buffer::const_iterator>& value) |
| 98 | { |
| 99 | size_t length = 0; |
| 100 | length += encoder.prependRange(value.first, value.second); |
| 101 | length += encoder.prependVarNumber(length); |
| 102 | length += encoder.prependVarNumber(TlvType::value); |
| 103 | return length; |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false> |
| 108 | class FieldDecl |
| 109 | { |
| 110 | public: |
| 111 | typedef LOCATION FieldLocation; |
| 112 | typedef VALUE ValueType; |
| 113 | typedef std::integral_constant<uint64_t, TYPE> TlvType; |
| 114 | typedef std::integral_constant<bool, REPEATABLE> IsRepeatable; |
| 115 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 116 | /** \brief decodes a field |
| 117 | * \param wire a Block with top-level type \p TYPE |
| 118 | * \return value of the field |
| 119 | */ |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 120 | static ValueType |
| 121 | decode(const Block& wire) |
| 122 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 123 | if (wire.type() != TlvType::value) { |
Junxiao Shi | 9697354 | 2015-09-27 10:30:29 +0000 | [diff] [blame] | 124 | BOOST_THROW_EXCEPTION(ndn::tlv::Error("Unexpected TLV type " + to_string(wire.type()))); |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 127 | return DecodeHelper<TlvType, ValueType>::decode(wire); |
| 128 | } |
| 129 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 130 | /** \brief encodes a field and prepends to \p encoder its Block with top-level type \p TYPE |
Alexander Afanasyev | 45312f5 | 2015-09-27 12:06:50 -0700 | [diff] [blame] | 131 | * \param encoder Instance of the buffer encoder or buffer estimator |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 132 | * \param value value of the field |
| 133 | */ |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 134 | template<typename encoding::Tag TAG, typename T> |
| 135 | static size_t |
| 136 | encode(EncodingImpl<TAG>& encoder, const T& value) |
| 137 | { |
| 138 | return EncodeHelper<TAG, TlvType, T>::encode(encoder, value); |
| 139 | } |
| 140 | }; |
| 141 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 142 | } // namespace lp |
Junxiao Shi | 09bcab5 | 2016-07-18 02:43:34 +0000 | [diff] [blame] | 143 | } // namespace ndn |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | e387423 | 2017-03-26 16:58:59 -0500 | [diff] [blame^] | 145 | #endif // NDN_CXX_LP_FIELD_DECL_HPP |