NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.3: 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; -*- */
22 #ifndef NDN_CXX_LP_FIELD_DECL_HPP
23 #define NDN_CXX_LP_FIELD_DECL_HPP
24 
25 #include "field.hpp"
26 #include "tlv.hpp"
27 
28 #include "../encoding/block-helpers.hpp"
29 #include "../util/concepts.hpp"
30 #include <boost/concept/requires.hpp>
31 
32 namespace ndn {
33 namespace lp {
34 
35 template<typename TlvType, typename T>
37 {
38  static
39  BOOST_CONCEPT_REQUIRES(((WireDecodable<T>)), (T))
40  decode(const Block& wire)
41  {
42  T type;
43  type.wireDecode(wire);
44  return type;
45  }
46 };
47 
48 template<typename TlvType>
49 struct DecodeHelper<TlvType, uint64_t>
50 {
51  static uint64_t
52  decode(const Block& wire)
53  {
54  return readNonNegativeInteger(wire);
55  }
56 };
57 
58 template<typename TlvType>
59 struct 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  {
64  if (wire.value_size() == 0) {
65  BOOST_THROW_EXCEPTION(ndn::tlv::Error(to_string(wire.type()) + " must not be empty"));
66  }
67 
68  return std::make_pair(wire.value_begin(), wire.value_end());
69  }
70 };
71 
72 template<typename encoding::Tag TAG, typename TlvType, typename T>
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 
83 template<typename encoding::Tag TAG, typename TlvType>
84 struct 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 
93 template<typename encoding::Tag TAG, typename TlvType>
94 struct 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 
107 template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false>
109 {
110 public:
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 
120  static ValueType
121  decode(const Block& wire)
122  {
123  if (wire.type() != TlvType::value) {
124  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Unexpected TLV type " + to_string(wire.type())));
125  }
126 
128  }
129 
134  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 
142 } // namespace lp
143 } // namespace ndn
144 
145 #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:40
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Prepend a TLV element containing a non-negative integer.
STL namespace.
Represents a TLV element of NDN packet format.
Definition: block.hpp:42
Buffer::const_iterator value_begin() const
Get begin iterator of TLV-VALUE.
Definition: block.hpp:255
Buffer::const_iterator value_end() const
Get end iterator of TLV-VALUE.
Definition: block.hpp:264
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:77
std::integral_constant< uint64_t, TYPE > TlvType
Definition: field-decl.hpp:113
static ValueType decode(const Block &wire)
decodes a field
Definition: field-decl.hpp:121
size_t value_size() const
Get size of TLV-VALUE aka TLV-LENGTH.
Definition: block.cpp:317
uint32_t type() const
Get TLV-TYPE.
Definition: block.hpp:235
static size_t encode(EncodingImpl< TAG > &encoder, const std::pair< Buffer::const_iterator, Buffer::const_iterator > &value)
Definition: field-decl.hpp:97
std::integral_constant< bool, REPEATABLE > IsRepeatable
Definition: field-decl.hpp:114
static std::pair< Buffer::const_iterator, Buffer::const_iterator > decode(const Block &wire)
Definition: field-decl.hpp:62
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:136
static uint64_t decode(const Block &wire)
Definition: field-decl.hpp:52
static size_t encode(EncodingImpl< TAG > &encoder, const uint64_t value)
Definition: field-decl.hpp:87
LOCATION FieldLocation
Definition: field-decl.hpp:111
std::string to_string(const V &v)
Definition: backports.hpp:84
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:44
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:80
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50