Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 1 | /* -*- 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_CXX_LP_DETAIL_FIELD_DECL_HPP |
| 23 | #define NDN_CXX_LP_DETAIL_FIELD_DECL_HPP |
| 24 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 25 | #include "../field.hpp" |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 26 | #include "../tlv.hpp" |
| 27 | |
Junxiao Shi | 4b46998 | 2015-12-03 18:20:19 +0000 | [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 { |
| 34 | namespace detail { |
| 35 | |
| 36 | template<typename TlvType, typename T> |
| 37 | struct DecodeHelper |
| 38 | { |
| 39 | static |
| 40 | BOOST_CONCEPT_REQUIRES(((WireDecodable<T>)), (T)) |
| 41 | decode(const Block& wire) |
| 42 | { |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 43 | T type; |
| 44 | type.wireDecode(wire); |
| 45 | return type; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | template<typename TlvType> |
| 50 | struct DecodeHelper<TlvType, uint64_t> |
| 51 | { |
| 52 | static uint64_t |
| 53 | decode(const Block& wire) |
| 54 | { |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 55 | return readNonNegativeInteger(wire); |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | template<typename TlvType> |
| 60 | struct DecodeHelper<TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>> |
| 61 | { |
| 62 | static std::pair<Buffer::const_iterator, Buffer::const_iterator> |
| 63 | decode(const Block& wire) |
| 64 | { |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 65 | if (wire.value_size() == 0) { |
Davide Pesavento | 96b96af | 2015-09-19 23:00:40 +0200 | [diff] [blame] | 66 | 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] | 67 | } |
| 68 | |
| 69 | return std::make_pair(wire.value_begin(), wire.value_end()); |
| 70 | } |
| 71 | }; |
| 72 | |
| 73 | template<typename encoding::Tag TAG, typename TlvType, typename T> |
| 74 | struct EncodeHelper |
| 75 | { |
| 76 | static |
| 77 | BOOST_CONCEPT_REQUIRES(((WireEncodable<T>)), (size_t)) |
| 78 | encode(EncodingImpl<TAG>& encoder, const T& value) |
| 79 | { |
| 80 | return value.wireEncode(encoder); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | template<typename encoding::Tag TAG, typename TlvType> |
| 85 | struct EncodeHelper<TAG, TlvType, uint64_t> |
| 86 | { |
| 87 | static size_t |
| 88 | encode(EncodingImpl<TAG>& encoder, const uint64_t value) |
| 89 | { |
| 90 | return prependNonNegativeIntegerBlock(encoder, TlvType::value, value); |
| 91 | } |
| 92 | }; |
| 93 | |
| 94 | template<typename encoding::Tag TAG, typename TlvType> |
| 95 | struct EncodeHelper<TAG, TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>> |
| 96 | { |
| 97 | static size_t |
| 98 | encode(EncodingImpl<TAG>& encoder, const std::pair<Buffer::const_iterator, Buffer::const_iterator>& value) |
| 99 | { |
| 100 | size_t length = 0; |
| 101 | length += encoder.prependRange(value.first, value.second); |
| 102 | length += encoder.prependVarNumber(length); |
| 103 | length += encoder.prependVarNumber(TlvType::value); |
| 104 | return length; |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false> |
| 109 | class FieldDecl |
| 110 | { |
| 111 | public: |
| 112 | typedef LOCATION FieldLocation; |
| 113 | typedef VALUE ValueType; |
| 114 | typedef std::integral_constant<uint64_t, TYPE> TlvType; |
| 115 | typedef std::integral_constant<bool, REPEATABLE> IsRepeatable; |
| 116 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 117 | /** \brief decodes a field |
| 118 | * \param wire a Block with top-level type \p TYPE |
| 119 | * \return value of the field |
| 120 | */ |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 121 | static ValueType |
| 122 | decode(const Block& wire) |
| 123 | { |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 124 | if (wire.type() != TlvType::value) { |
Junxiao Shi | 9697354 | 2015-09-27 10:30:29 +0000 | [diff] [blame] | 125 | 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] | 126 | } |
| 127 | |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 128 | return DecodeHelper<TlvType, ValueType>::decode(wire); |
| 129 | } |
| 130 | |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 131 | /** \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] | 132 | * \param encoder Instance of the buffer encoder or buffer estimator |
Eric Newberry | 83872fd | 2015-08-06 17:01:24 -0700 | [diff] [blame] | 133 | * \param value value of the field |
| 134 | */ |
Eric Newberry | 261dbc2 | 2015-07-22 23:18:18 -0700 | [diff] [blame] | 135 | template<typename encoding::Tag TAG, typename T> |
| 136 | static size_t |
| 137 | encode(EncodingImpl<TAG>& encoder, const T& value) |
| 138 | { |
| 139 | return EncodeHelper<TAG, TlvType, T>::encode(encoder, value); |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | } // namespace detail |
| 144 | } // namespace lp |
| 145 | } // namesapce ndn |
| 146 | |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 147 | #endif // NDN_CXX_LP_DETAIL_FIELD_DECL_HPP |