NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
field-decl.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2018 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_FIELD_DECL_HPP
23 #define NDN_CXX_LP_FIELD_DECL_HPP
24 
25 #include "empty-value.hpp"
26 #include "field.hpp"
27 #include "sequence.hpp"
28 #include "tlv.hpp"
29 
30 #include "../encoding/block-helpers.hpp"
31 #include "../util/concepts.hpp"
32 #include <boost/concept/requires.hpp>
33 
34 namespace ndn {
35 namespace lp {
36 
39 struct NonNegativeIntegerTag;
40 
41 template<typename TlvType, typename T>
43 {
44  static
45  BOOST_CONCEPT_REQUIRES(((WireDecodable<T>)), (T))
46  decode(const Block& wire)
47  {
48  T type;
49  type.wireDecode(wire);
50  return type;
51  }
52 };
53 
54 template<typename TlvType>
55 struct DecodeHelper<TlvType, EmptyValue>
56 {
57  static EmptyValue
58  decode(const Block& wire)
59  {
60  if (wire.value_size() != 0) {
61  BOOST_THROW_EXCEPTION(ndn::tlv::Error("NDNLP field of TLV-TYPE " + to_string(wire.type()) +
62  " must be empty"));
63  }
64 
65  return EmptyValue{};
66  }
67 };
68 
69 template<typename TlvType>
70 struct DecodeHelper<TlvType, NonNegativeIntegerTag>
71 {
72  static uint64_t
73  decode(const Block& wire)
74  {
75  return readNonNegativeInteger(wire);
76  }
77 };
78 
79 template<typename TlvType>
80 struct DecodeHelper<TlvType, uint64_t>
81 {
82  static uint64_t
83  decode(const Block& wire)
84  {
85  // NDNLPv2 spec defines sequence number fields to be encoded as a fixed-width unsigned integer,
86  // but previous versions of ndn-cxx encode it as a NonNegativeInteger, so we decode it as such
87  // for backwards compatibility. In a future version, the decoder will be changed to accept
88  // 8-byte big endian only, to allow faster decoding.
89  return readNonNegativeInteger(wire);
90  }
91 };
92 
93 template<typename TlvType>
94 struct DecodeHelper<TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
95 {
96  static std::pair<Buffer::const_iterator, Buffer::const_iterator>
97  decode(const Block& wire)
98  {
99  if (wire.value_size() == 0) {
100  BOOST_THROW_EXCEPTION(ndn::tlv::Error("NDNLP field of TLV-TYPE " + to_string(wire.type()) +
101  " cannot be empty"));
102  }
103 
104  return std::make_pair(wire.value_begin(), wire.value_end());
105  }
106 };
107 
108 template<typename encoding::Tag TAG, typename TlvType, typename T>
110 {
111  static
112  BOOST_CONCEPT_REQUIRES(((WireEncodableWithEncodingBuffer<T>)), (size_t))
113  encode(EncodingImpl<TAG>& encoder, const T& value)
114  {
115  return value.wireEncode(encoder);
116  }
117 };
118 
119 template<typename encoding::Tag TAG, typename TlvType>
120 struct EncodeHelper<TAG, TlvType, EmptyValue>
121 {
122  static size_t
123  encode(EncodingImpl<TAG>& encoder, const EmptyValue value)
124  {
125  size_t length = 0;
126  length += encoder.prependVarNumber(0);
127  length += encoder.prependVarNumber(TlvType::value);
128  return length;
129  }
130 };
131 
132 template<typename encoding::Tag TAG, typename TlvType>
133 struct EncodeHelper<TAG, TlvType, NonNegativeIntegerTag>
134 {
135  static size_t
136  encode(EncodingImpl<TAG>& encoder, uint64_t value)
137  {
138  return prependNonNegativeIntegerBlock(encoder, TlvType::value, value);
139  }
140 };
141 
142 template<typename encoding::Tag TAG, typename TlvType>
143 struct EncodeHelper<TAG, TlvType, uint64_t>
144 {
145  static size_t
146  encode(EncodingImpl<TAG>& encoder, uint64_t value)
147  {
148  uint64_t be = htobe64(value);
149  const uint8_t* buf = reinterpret_cast<const uint8_t*>(&be);
150  return encoder.prependByteArrayBlock(TlvType::value, buf, sizeof(be));
151  }
152 };
153 
154 template<typename encoding::Tag TAG, typename TlvType>
155 struct EncodeHelper<TAG, TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
156 {
157  static size_t
158  encode(EncodingImpl<TAG>& encoder, const std::pair<Buffer::const_iterator, Buffer::const_iterator>& value)
159  {
160  size_t length = 0;
161  length += encoder.prependRange(value.first, value.second);
162  length += encoder.prependVarNumber(length);
163  length += encoder.prependVarNumber(TlvType::value);
164  return length;
165  }
166 };
167 
176 template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false,
177  typename DECODER_TAG = VALUE, typename ENCODER_TAG = VALUE>
179 {
180 public:
181  typedef LOCATION FieldLocation;
182  typedef VALUE ValueType;
183  typedef std::integral_constant<uint64_t, TYPE> TlvType;
184  typedef std::integral_constant<bool, REPEATABLE> IsRepeatable;
185 
191  static ValueType
192  decode(const Block& wire)
193  {
194  if (wire.type() != TlvType::value) {
195  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Unexpected TLV-TYPE " + to_string(wire.type())));
196  }
197 
199  }
200 
205  template<typename encoding::Tag TAG>
206  static size_t
207  encode(EncodingImpl<TAG>& encoder, const ValueType& value)
208  {
210  }
211 };
212 
213 } // namespace lp
214 } // namespace ndn
215 
216 #endif // NDN_CXX_LP_FIELD_DECL_HPP
static ValueType decode(const Block &wire)
Decode a field.
Definition: field-decl.hpp:192
Copyright (c) 2011-2015 Regents of the University of California.
static T decode(const Block &wire)
Definition: field-decl.hpp:46
LOCATION FieldLocation
Definition: field-decl.hpp:181
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
std::integral_constant< bool, REPEATABLE > IsRepeatable
Definition: field-decl.hpp:184
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
Definition: block.cpp:319
static size_t encode(EncodingImpl< TAG > &encoder, uint64_t value)
Definition: field-decl.hpp:136
STL namespace.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
static size_t encode(EncodingImpl< TAG > &encoder, const EmptyValue value)
Definition: field-decl.hpp:123
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
static size_t encode(EncodingImpl< TAG > &encoder, const T &value)
Definition: field-decl.hpp:113
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:255
Declare a field.
Definition: field-decl.hpp:178
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:60
static size_t encode(EncodingImpl< TAG > &encoder, uint64_t value)
Definition: field-decl.hpp:146
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:264
represents a zero-length TLV-VALUE
Definition: empty-value.hpp:33
static size_t encode(EncodingImpl< TAG > &encoder, const ValueType &value)
Encode a field and prepend to encoder.
Definition: field-decl.hpp:207
static size_t encode(EncodingImpl< TAG > &encoder, const std::pair< Buffer::const_iterator, Buffer::const_iterator > &value)
Definition: field-decl.hpp:158
static std::pair< Buffer::const_iterator, Buffer::const_iterator > decode(const Block &wire)
Definition: field-decl.hpp:97
static uint64_t decode(const Block &wire)
Definition: field-decl.hpp:83
std::string to_string(const V &v)
Definition: backports.hpp:107
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
std::integral_constant< uint64_t, TYPE > TlvType
Definition: field-decl.hpp:183
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50
static EmptyValue decode(const Block &wire)
Definition: field-decl.hpp:58