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-2019 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 
26 #include "ndn-cxx/lp/field.hpp"
27 #include "ndn-cxx/lp/sequence.hpp"
28 #include "ndn-cxx/lp/tlv.hpp"
31 
32 #include <boost/concept/requires.hpp>
33 #include <boost/endian/conversion.hpp>
34 
35 namespace ndn {
36 namespace lp {
37 
40 struct NonNegativeIntegerTag;
41 
42 template<typename TlvType, typename T>
44 {
45  static
46  BOOST_CONCEPT_REQUIRES(((WireDecodable<T>)), (T))
47  decode(const Block& wire)
48  {
49  T type;
50  type.wireDecode(wire);
51  return type;
52  }
53 };
54 
55 template<typename TlvType>
56 struct DecodeHelper<TlvType, EmptyValue>
57 {
58  static EmptyValue
59  decode(const Block& wire)
60  {
61  if (wire.value_size() != 0) {
62  NDN_THROW(ndn::tlv::Error("NDNLP field of TLV-TYPE " + to_string(wire.type()) +
63  " must be empty"));
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  if (wire.value_size() != sizeof(uint64_t)) {
86  NDN_THROW(ndn::tlv::Error("NDNLP field of TLV-TYPE " + to_string(wire.type()) +
87  " must contain a 64-bit integer"));
88  }
89  uint64_t n = 0;
90  std::memcpy(&n, wire.value(), sizeof(n));
91  return boost::endian::big_to_native(n);
92  }
93 };
94 
95 template<typename TlvType>
96 struct DecodeHelper<TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
97 {
98  static std::pair<Buffer::const_iterator, Buffer::const_iterator>
99  decode(const Block& wire)
100  {
101  if (wire.value_size() == 0) {
102  NDN_THROW(ndn::tlv::Error("NDNLP field of TLV-TYPE " + to_string(wire.type()) +
103  " cannot be empty"));
104  }
105  return std::make_pair(wire.value_begin(), wire.value_end());
106  }
107 };
108 
109 template<typename encoding::Tag TAG, typename TlvType, typename T>
111 {
112  static
113  BOOST_CONCEPT_REQUIRES(((WireEncodableWithEncodingBuffer<T>)), (size_t))
114  encode(EncodingImpl<TAG>& encoder, const T& value)
115  {
116  return value.wireEncode(encoder);
117  }
118 };
119 
120 template<typename encoding::Tag TAG, typename TlvType>
121 struct EncodeHelper<TAG, TlvType, EmptyValue>
122 {
123  static size_t
124  encode(EncodingImpl<TAG>& encoder, const EmptyValue value)
125  {
126  size_t length = 0;
127  length += encoder.prependVarNumber(0);
128  length += encoder.prependVarNumber(TlvType::value);
129  return length;
130  }
131 };
132 
133 template<typename encoding::Tag TAG, typename TlvType>
134 struct EncodeHelper<TAG, TlvType, NonNegativeIntegerTag>
135 {
136  static size_t
137  encode(EncodingImpl<TAG>& encoder, uint64_t value)
138  {
139  return prependNonNegativeIntegerBlock(encoder, TlvType::value, value);
140  }
141 };
142 
143 template<typename encoding::Tag TAG, typename TlvType>
144 struct EncodeHelper<TAG, TlvType, uint64_t>
145 {
146  static size_t
147  encode(EncodingImpl<TAG>& encoder, uint64_t value)
148  {
149  boost::endian::native_to_big_inplace(value);
150  return encoder.prependByteArrayBlock(TlvType::value,
151  reinterpret_cast<const uint8_t*>(&value), sizeof(value));
152  }
153 };
154 
155 template<typename encoding::Tag TAG, typename TlvType>
156 struct EncodeHelper<TAG, TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
157 {
158  static size_t
159  encode(EncodingImpl<TAG>& encoder, const std::pair<Buffer::const_iterator, Buffer::const_iterator>& value)
160  {
161  size_t length = 0;
162  length += encoder.prependRange(value.first, value.second);
163  length += encoder.prependVarNumber(length);
164  length += encoder.prependVarNumber(TlvType::value);
165  return length;
166  }
167 };
168 
177 template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false,
178  typename DECODER_TAG = VALUE, typename ENCODER_TAG = VALUE>
180 {
181 public:
182  typedef LOCATION FieldLocation;
183  typedef VALUE ValueType;
184  typedef std::integral_constant<uint64_t, TYPE> TlvType;
185  typedef std::integral_constant<bool, REPEATABLE> IsRepeatable;
186 
192  static ValueType
193  decode(const Block& wire)
194  {
195  if (wire.type() != TlvType::value) {
196  NDN_THROW(ndn::tlv::Error("Unexpected TLV-TYPE " + to_string(wire.type())));
197  }
198 
200  }
201 
206  template<typename encoding::Tag TAG>
207  static size_t
208  encode(EncodingImpl<TAG>& encoder, const ValueType& value)
209  {
211  }
212 };
213 
214 } // namespace lp
215 } // namespace ndn
216 
217 #endif // NDN_CXX_LP_FIELD_DECL_HPP
ndn::lp::FieldDecl::ValueType
VALUE ValueType
Definition: field-decl.hpp:183
ndn::lp::EncodeHelper< TAG, TlvType, EmptyValue >::encode
static size_t encode(EncodingImpl< TAG > &encoder, const EmptyValue value)
Definition: field-decl.hpp:124
ndn::Block::value_size
size_t value_size() const noexcept
Return the size of TLV-VALUE, aka TLV-LENGTH.
Definition: block.cpp:308
ndn::lp::FieldDecl::TlvType
std::integral_constant< uint64_t, TYPE > TlvType
Definition: field-decl.hpp:184
ndn::lp::EncodeHelper< TAG, TlvType, uint64_t >::encode
static size_t encode(EncodingImpl< TAG > &encoder, uint64_t value)
Definition: field-decl.hpp:147
ndn::lp::DecodeHelper< TlvType, NonNegativeIntegerTag >::decode
static uint64_t decode(const Block &wire)
Definition: field-decl.hpp:73
ndn::lp::EncodeHelper::encode
static size_t encode(EncodingImpl< TAG > &encoder, const T &value)
Definition: field-decl.hpp:114
empty-value.hpp
ndn::lp::FieldDecl::FieldLocation
LOCATION FieldLocation
Definition: field-decl.hpp:182
ndn::lp::FieldDecl::decode
static ValueType decode(const Block &wire)
Decode a field.
Definition: field-decl.hpp:193
sequence.hpp
ndn::Block::value_end
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:305
concepts.hpp
ndn::lp::FieldDecl::IsRepeatable
std::integral_constant< bool, REPEATABLE > IsRepeatable
Definition: field-decl.hpp:185
ndn::lp::EncodeHelper
Definition: field-decl.hpp:111
ndn::WireDecodable
a concept check for TLV abstraction with .wireDecode method and constructible from Block
Definition: concepts.hpp:81
ndn::encoding::readNonNegativeInteger
uint64_t readNonNegativeInteger(const Block &block)
Read a non-negative integer from a TLV element.
Definition: block-helpers.cpp:64
ndn::lp::DecodeHelper
Definition: field-decl.hpp:44
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::Block::value_begin
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:296
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::lp::FieldDecl
Declare a field.
Definition: field-decl.hpp:180
tlv.hpp
field.hpp
ndn::lp::EncodeHelper< TAG, TlvType, std::pair< Buffer::const_iterator, Buffer::const_iterator > >::encode
static size_t encode(EncodingImpl< TAG > &encoder, const std::pair< Buffer::const_iterator, Buffer::const_iterator > &value)
Definition: field-decl.hpp:159
ndn::lp::DecodeHelper< TlvType, EmptyValue >::decode
static EmptyValue decode(const Block &wire)
Definition: field-decl.hpp:59
ndn::encoding::prependNonNegativeIntegerBlock
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
Definition: block-helpers.cpp:35
ndn::lp::DecodeHelper< TlvType, uint64_t >::decode
static uint64_t decode(const Block &wire)
Definition: field-decl.hpp:83
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
block-helpers.hpp
ndn::Block::value
const uint8_t * value() const noexcept
Return a raw pointer to the beginning of TLV-VALUE.
Definition: block.cpp:302
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::lp::EncodeHelper< TAG, TlvType, NonNegativeIntegerTag >::encode
static size_t encode(EncodingImpl< TAG > &encoder, uint64_t value)
Definition: field-decl.hpp:137
ndn::to_string
std::string to_string(const T &val)
Definition: backports.hpp:102
ndn::lp
Definition: cache-policy.cpp:28
ndn::lp::DecodeHelper::decode
static T decode(const Block &wire)
Definition: field-decl.hpp:47
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition: tlv.hpp:53
ndn::lp::EmptyValue
represents a zero-length TLV-VALUE
Definition: empty-value.hpp:34
ndn::WireEncodableWithEncodingBuffer
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:61
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::lp::FieldDecl::encode
static size_t encode(EncodingImpl< TAG > &encoder, const ValueType &value)
Encode a field and prepend to encoder.
Definition: field-decl.hpp:208