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 "../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 namespace detail {
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, uint64_t>
51 {
52  static uint64_t
53  decode(const Block& wire)
54  {
55  return readNonNegativeInteger(wire);
56  }
57 };
58 
59 template<typename TlvType>
60 struct DecodeHelper<TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
61 {
62  static std::pair<Buffer::const_iterator, Buffer::const_iterator>
63  decode(const Block& wire)
64  {
65  if (wire.value_size() == 0) {
66  BOOST_THROW_EXCEPTION(ndn::tlv::Error(to_string(wire.type()) + " must not be empty"));
67  }
68 
69  return std::make_pair(wire.value_begin(), wire.value_end());
70  }
71 };
72 
73 template<typename encoding::Tag TAG, typename TlvType, typename T>
75 {
76  static
77  BOOST_CONCEPT_REQUIRES(((WireEncodable<T>)), (size_t))
78  encode(EncodingImpl<TAG>& encoder, const T& value)
79  {
80  return value.wireEncode(encoder);
81  }
82 };
83 
84 template<typename encoding::Tag TAG, typename TlvType>
85 struct EncodeHelper<TAG, TlvType, uint64_t>
86 {
87  static size_t
88  encode(EncodingImpl<TAG>& encoder, const uint64_t value)
89  {
90  return prependNonNegativeIntegerBlock(encoder, TlvType::value, value);
91  }
92 };
93 
94 template<typename encoding::Tag TAG, typename TlvType>
95 struct EncodeHelper<TAG, TlvType, std::pair<Buffer::const_iterator, Buffer::const_iterator>>
96 {
97  static size_t
98  encode(EncodingImpl<TAG>& encoder, const std::pair<Buffer::const_iterator, Buffer::const_iterator>& value)
99  {
100  size_t length = 0;
101  length += encoder.prependRange(value.first, value.second);
102  length += encoder.prependVarNumber(length);
103  length += encoder.prependVarNumber(TlvType::value);
104  return length;
105  }
106 };
107 
108 template<typename LOCATION, typename VALUE, uint64_t TYPE, bool REPEATABLE = false>
110 {
111 public:
112  typedef LOCATION FieldLocation;
113  typedef VALUE ValueType;
114  typedef std::integral_constant<uint64_t, TYPE> TlvType;
115  typedef std::integral_constant<bool, REPEATABLE> IsRepeatable;
116 
121  static ValueType
122  decode(const Block& wire)
123  {
124  if (wire.type() != TlvType::value) {
125  BOOST_THROW_EXCEPTION(ndn::tlv::Error("Unexpected TLV type " + to_string(wire.type())));
126  }
127 
129  }
130 
135  template<typename encoding::Tag TAG, typename T>
136  static size_t
137  encode(EncodingImpl<TAG>& encoder, const T& value)
138  {
139  return EncodeHelper<TAG, TlvType, T>::encode(encoder, value);
140  }
141 };
142 
143 } // namespace detail
144 } // namespace lp
145 } // namesapce ndn
146 
147 #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.
size_t value_size() const
Definition: block.cpp:529
STL namespace.
static std::pair< Buffer::const_iterator, Buffer::const_iterator > decode(const Block &wire)
Definition: field-decl.hpp:63
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:114
static size_t encode(EncodingImpl< TAG > &encoder, const T &value)
Definition: field-decl.hpp:78
uint64_t readNonNegativeInteger(const Block &block)
Helper to read a non-negative integer from a block.
Buffer::const_iterator value_begin() const
Definition: block.hpp:352
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:137
static uint64_t decode(const Block &wire)
Definition: field-decl.hpp:53
Buffer::const_iterator value_end() const
Definition: block.hpp:358
static size_t encode(EncodingImpl< TAG > &encoder, const uint64_t value)
Definition: field-decl.hpp:88
static size_t encode(EncodingImpl< TAG > &encoder, const std::pair< Buffer::const_iterator, Buffer::const_iterator > &value)
Definition: field-decl.hpp:98
std::integral_constant< bool, REPEATABLE > IsRepeatable
Definition: field-decl.hpp:115
std::string to_string(const V &v)
Definition: backports.hpp:55
static T decode(const Block &wire)
Definition: field-decl.hpp:41
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)
decodes a field
Definition: field-decl.hpp:122
uint32_t type() const
Definition: block.hpp:346
represents an error in TLV encoding or decoding
Definition: tlv.hpp:50