blob: a71aceb822565ae3e4af37e8c1beecba2a436444 [file] [log] [blame]
Eric Newberry261dbc22015-07-22 23:18:18 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyeve3874232017-03-26 16:58:59 -05003 * Copyright (c) 2013-2017 Regents of the University of California.
Eric Newberry261dbc22015-07-22 23:18:18 -07004 *
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 Afanasyeve3874232017-03-26 16:58:59 -050022#ifndef NDN_CXX_LP_FIELD_DECL_HPP
23#define NDN_CXX_LP_FIELD_DECL_HPP
Eric Newberry261dbc22015-07-22 23:18:18 -070024
Alexander Afanasyeve3874232017-03-26 16:58:59 -050025#include "field.hpp"
26#include "tlv.hpp"
Eric Newberry261dbc22015-07-22 23:18:18 -070027
Alexander Afanasyeve3874232017-03-26 16:58:59 -050028#include "../encoding/block-helpers.hpp"
29#include "../util/concepts.hpp"
Eric Newberry261dbc22015-07-22 23:18:18 -070030#include <boost/concept/requires.hpp>
31
32namespace ndn {
33namespace lp {
Eric Newberry261dbc22015-07-22 23:18:18 -070034
35template<typename TlvType, typename T>
36struct DecodeHelper
37{
38 static
39 BOOST_CONCEPT_REQUIRES(((WireDecodable<T>)), (T))
40 decode(const Block& wire)
41 {
Eric Newberry261dbc22015-07-22 23:18:18 -070042 T type;
43 type.wireDecode(wire);
44 return type;
45 }
46};
47
48template<typename TlvType>
49struct DecodeHelper<TlvType, uint64_t>
50{
51 static uint64_t
52 decode(const Block& wire)
53 {
Eric Newberry261dbc22015-07-22 23:18:18 -070054 return readNonNegativeInteger(wire);
55 }
56};
57
58template<typename TlvType>
59struct 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 Newberry261dbc22015-07-22 23:18:18 -070064 if (wire.value_size() == 0) {
Davide Pesavento96b96af2015-09-19 23:00:40 +020065 BOOST_THROW_EXCEPTION(ndn::tlv::Error(to_string(wire.type()) + " must not be empty"));
Eric Newberry261dbc22015-07-22 23:18:18 -070066 }
67
68 return std::make_pair(wire.value_begin(), wire.value_end());
69 }
70};
71
72template<typename encoding::Tag TAG, typename TlvType, typename T>
73struct 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
83template<typename encoding::Tag TAG, typename TlvType>
84struct 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
93template<typename encoding::Tag TAG, typename TlvType>
94struct 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
107template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false>
108class FieldDecl
109{
110public:
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 Newberry83872fd2015-08-06 17:01:24 -0700116 /** \brief decodes a field
117 * \param wire a Block with top-level type \p TYPE
118 * \return value of the field
119 */
Eric Newberry261dbc22015-07-22 23:18:18 -0700120 static ValueType
121 decode(const Block& wire)
122 {
Eric Newberry83872fd2015-08-06 17:01:24 -0700123 if (wire.type() != TlvType::value) {
Junxiao Shi96973542015-09-27 10:30:29 +0000124 BOOST_THROW_EXCEPTION(ndn::tlv::Error("Unexpected TLV type " + to_string(wire.type())));
Eric Newberry83872fd2015-08-06 17:01:24 -0700125 }
126
Eric Newberry261dbc22015-07-22 23:18:18 -0700127 return DecodeHelper<TlvType, ValueType>::decode(wire);
128 }
129
Eric Newberry83872fd2015-08-06 17:01:24 -0700130 /** \brief encodes a field and prepends to \p encoder its Block with top-level type \p TYPE
Alexander Afanasyev45312f52015-09-27 12:06:50 -0700131 * \param encoder Instance of the buffer encoder or buffer estimator
Eric Newberry83872fd2015-08-06 17:01:24 -0700132 * \param value value of the field
133 */
Eric Newberry261dbc22015-07-22 23:18:18 -0700134 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 Newberry261dbc22015-07-22 23:18:18 -0700142} // namespace lp
Junxiao Shi09bcab52016-07-18 02:43:34 +0000143} // namespace ndn
Eric Newberry261dbc22015-07-22 23:18:18 -0700144
Alexander Afanasyeve3874232017-03-26 16:58:59 -0500145#endif // NDN_CXX_LP_FIELD_DECL_HPP