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 "tlv.hpp"
28 
29 #include "../encoding/block-helpers.hpp"
30 #include "../util/concepts.hpp"
31 #include <boost/concept/requires.hpp>
32 
33 namespace ndn {
34 namespace lp {
35 
36 template<typename TlvType, typename T>
38 {
39  static
40  BOOST_CONCEPT_REQUIRES(((WireDecodable<T>)), (T))
41  decode(const Block& wire)
42  {
43  T type;
44  type.wireDecode(wire);
45  return type;
46  }
47 };
48 
49 template<typename TlvType>
50 struct DecodeHelper<TlvType, EmptyValue>
51 {
52  static EmptyValue
53  decode(const Block& wire)
54  {
55  if (wire.value_size() != 0) {
56  BOOST_THROW_EXCEPTION(ndn::tlv::Error("NDNLP field of TLV-TYPE " + to_string(wire.type()) +
57  " must be empty"));
58  }
59 
60  return EmptyValue{};
61  }
62 };
63 
64 template<typename TlvType>
65 struct DecodeHelper<TlvType, uint64_t>
66 {
67  static uint64_t
68  decode(const Block& wire)
69  {
70  return readNonNegativeInteger(wire);
71  }
72 };
73 
74 template<typename TlvType>
75 struct DecodeHelper<TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
76 {
77  static std::pair<Buffer::const_iterator, Buffer::const_iterator>
78  decode(const Block& wire)
79  {
80  if (wire.value_size() == 0) {
81  BOOST_THROW_EXCEPTION(ndn::tlv::Error("NDNLP field of TLV-TYPE " + to_string(wire.type()) +
82  " cannot be empty"));
83  }
84 
85  return std::make_pair(wire.value_begin(), wire.value_end());
86  }
87 };
88 
89 template<typename encoding::Tag TAG, typename TlvType, typename T>
91 {
92  static
93  BOOST_CONCEPT_REQUIRES(((WireEncodableWithEncodingBuffer<T>)), (size_t))
94  encode(EncodingImpl<TAG>& encoder, const T& value)
95  {
96  return value.wireEncode(encoder);
97  }
98 };
99 
100 template<typename encoding::Tag TAG, typename TlvType>
101 struct EncodeHelper<TAG, TlvType, EmptyValue>
102 {
103  static size_t
104  encode(EncodingImpl<TAG>& encoder, const EmptyValue value)
105  {
106  size_t length = 0;
107  length += encoder.prependVarNumber(0);
108  length += encoder.prependVarNumber(TlvType::value);
109  return length;
110  }
111 };
112 
113 template<typename encoding::Tag TAG, typename TlvType>
114 struct EncodeHelper<TAG, TlvType, uint64_t>
115 {
116  static size_t
117  encode(EncodingImpl<TAG>& encoder, const uint64_t value)
118  {
119  return prependNonNegativeIntegerBlock(encoder, TlvType::value, value);
120  }
121 };
122 
123 template<typename encoding::Tag TAG, typename TlvType>
124 struct EncodeHelper<TAG, TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
125 {
126  static size_t
127  encode(EncodingImpl<TAG>& encoder, const std::pair<Buffer::const_iterator, Buffer::const_iterator>& value)
128  {
129  size_t length = 0;
130  length += encoder.prependRange(value.first, value.second);
131  length += encoder.prependVarNumber(length);
132  length += encoder.prependVarNumber(TlvType::value);
133  return length;
134  }
135 };
136 
137 template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false>
139 {
140 public:
141  typedef LOCATION FieldLocation;
142  typedef VALUE ValueType;
143  typedef std::integral_constant<uint64_t, TYPE> TlvType;
144  typedef std::integral_constant<bool, REPEATABLE> IsRepeatable;
145 
150  static ValueType
151  decode(const Block& wire)
152  {
153  if (wire.type() != TlvType::value) {
154  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Unexpected TLV type " + to_string(wire.type())));
155  }
156 
158  }
159 
164  template<typename encoding::Tag TAG, typename T>
165  static size_t
166  encode(EncodingImpl<TAG>& encoder, const T& value)
167  {
168  return EncodeHelper<TAG, TlvType, T>::encode(encoder, value);
169  }
170 };
171 
172 } // namespace lp
173 } // namespace ndn
174 
175 #endif // NDN_CXX_LP_FIELD_DECL_HPP
Copyright (c) 2011-2015 Regents of the University of California.
static T decode(const Block &wire)
Definition: field-decl.hpp:41
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
Definition: block.cpp:318
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:104
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:94
std::integral_constant< uint64_t, TYPE > TlvType
Definition: field-decl.hpp:143
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:255
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:60
static ValueType decode(const Block &wire)
decodes a field
Definition: field-decl.hpp:151
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 std::pair< Buffer::const_iterator, Buffer::const_iterator > &value)
Definition: field-decl.hpp:127
std::integral_constant< bool, REPEATABLE > IsRepeatable
Definition: field-decl.hpp:144
static std::pair< Buffer::const_iterator, Buffer::const_iterator > decode(const Block &wire)
Definition: field-decl.hpp:78
static size_t encode(EncodingImpl< TAG > &encoder, const T &value)
encodes a field and prepends to encoder its Block with top-level type TYPE
Definition: field-decl.hpp:166
static uint64_t decode(const Block &wire)
Definition: field-decl.hpp:68
static size_t encode(EncodingImpl< TAG > &encoder, const uint64_t value)
Definition: field-decl.hpp:117
LOCATION FieldLocation
Definition: field-decl.hpp:141
std::string to_string(const V &v)
Definition: backports.hpp:84
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
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:53