NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: 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_DETAIL_FIELD_DECL_HPP
23 #define NDN_CXX_LP_DETAIL_FIELD_DECL_HPP
24 
25 #include "../../common.hpp"
26 
27 #include "../field.hpp"
28 #include "../sequence.hpp"
29 #include "../cache-policy.hpp"
30 #include "../nack.hpp"
31 #include "../tlv.hpp"
32 
33 #include <boost/concept/requires.hpp>
34 
35 namespace ndn {
36 namespace lp {
37 namespace detail {
38 
39 template<typename TlvType, typename T>
41 {
42  static
43  BOOST_CONCEPT_REQUIRES(((WireDecodable<T>)), (T))
44  decode(const Block& wire)
45  {
46  if (wire.type() != TlvType::value) {
47  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Unexpected TLV type " + std::to_string(wire.type())));
48  }
49 
50  T type;
51  type.wireDecode(wire);
52  return type;
53  }
54 };
55 
56 template<typename TlvType>
57 struct DecodeHelper<TlvType, uint64_t>
58 {
59  static uint64_t
60  decode(const Block& wire)
61  {
62  if (wire.type() != TlvType::value) {
63  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Unexpected TLV type " + std::to_string(wire.type())));
64  }
65 
66  return readNonNegativeInteger(wire);
67  }
68 };
69 
70 template<typename TlvType>
71 struct DecodeHelper<TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
72 {
73  static std::pair<Buffer::const_iterator, Buffer::const_iterator>
74  decode(const Block& wire)
75  {
76  if (wire.type() != TlvType::value) {
77  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Unexpected TLV type " + std::to_string(wire.type())));
78  }
79 
80  if (wire.value_size() == 0) {
81  BOOST_THROW_EXCEPTION(ndn::tlv::Error(std::to_string(wire.type()) + " must not be empty"));
82  }
83 
84  return std::make_pair(wire.value_begin(), wire.value_end());
85  }
86 };
87 
88 template<typename encoding::Tag TAG, typename TlvType, typename T>
90 {
91  static
92  BOOST_CONCEPT_REQUIRES(((WireEncodable<T>)), (size_t))
93  encode(EncodingImpl<TAG>& encoder, const T& value)
94  {
95  return value.wireEncode(encoder);
96  }
97 };
98 
99 template<typename encoding::Tag TAG, typename TlvType>
100 struct EncodeHelper<TAG, TlvType, uint64_t>
101 {
102  static size_t
103  encode(EncodingImpl<TAG>& encoder, const uint64_t value)
104  {
105  return prependNonNegativeIntegerBlock(encoder, TlvType::value, value);
106  }
107 };
108 
109 template<typename encoding::Tag TAG, typename TlvType>
110 struct EncodeHelper<TAG, TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
111 {
112  static size_t
113  encode(EncodingImpl<TAG>& encoder, const std::pair<Buffer::const_iterator, Buffer::const_iterator>& value)
114  {
115  size_t length = 0;
116  length += encoder.prependRange(value.first, value.second);
117  length += encoder.prependVarNumber(length);
118  length += encoder.prependVarNumber(TlvType::value);
119  return length;
120  }
121 };
122 
123 template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false>
125 {
126 public:
127  typedef LOCATION FieldLocation;
128  typedef VALUE ValueType;
129  typedef std::integral_constant<uint64_t, TYPE> TlvType;
130  typedef std::integral_constant<bool, REPEATABLE> IsRepeatable;
131 
132  static ValueType
133  decode(const Block& wire)
134  {
136  }
137 
138  template<typename encoding::Tag TAG, typename T>
139  static size_t
140  encode(EncodingImpl<TAG>& encoder, const T& value)
141  {
142  return EncodeHelper<TAG, TlvType, T>::encode(encoder, value);
143  }
144 };
145 
146 } // namespace detail
147 } // namespace lp
148 } // namesapce ndn
149 
150 #endif // NDN_CXX_LP_DETAIL_FIELD_DECL_HPP
Copyright (c) 2011-2015 Regents of the University of California.
size_t prependNonNegativeIntegerBlock(EncodingImpl< TAG > &encoder, uint32_t type, uint64_t value)
Helper to prepend TLV block type type containing non-negative integer value.
STL namespace.
static std::pair< Buffer::const_iterator, Buffer::const_iterator > decode(const Block &wire)
Definition: field-decl.hpp:74
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
std::integral_constant< uint64_t, TYPE > TlvType
Definition: field-decl.hpp:129
static size_t encode(EncodingImpl< TAG > &encoder, const T &value)
Definition: field-decl.hpp:93
Buffer::const_iterator value_begin() const
Definition: block.hpp:352
Buffer::const_iterator value_end() const
Definition: block.hpp:358
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
static size_t encode(EncodingImpl< TAG > &encoder, const T &value)
Definition: field-decl.hpp:140
static uint64_t decode(const Block &wire)
Definition: field-decl.hpp:60
size_t value_size() const
Definition: block.cpp:529
static size_t encode(EncodingImpl< TAG > &encoder, const uint64_t value)
Definition: field-decl.hpp:103
uint32_t type() const
Definition: block.hpp:346
static size_t encode(EncodingImpl< TAG > &encoder, const std::pair< Buffer::const_iterator, Buffer::const_iterator > &value)
Definition: field-decl.hpp:113
std::integral_constant< bool, REPEATABLE > IsRepeatable
Definition: field-decl.hpp:130
static T decode(const Block &wire)
Definition: field-decl.hpp:44
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:34
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:70
static ValueType decode(const Block &wire)
Definition: field-decl.hpp:133
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50