esp8266ndn
NDN Arduino library for ESP8266 and more
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ev-decoder.hpp
Go to the documentation of this file.
1#ifndef NDNPH_TLV_EV_DECODER_HPP
2#define NDNPH_TLV_EV_DECODER_HPP
3
4#include "nni.hpp"
5
6namespace ndnph {
7namespace detail {
8
9template<int type, bool repeatable, int order>
11 using TT = std::integral_constant<int, type>;
12 using Repeatable = std::integral_constant<bool, repeatable>;
13 using Order = std::integral_constant<int, order>;
14};
15
16template<int type, bool repeatable, int order, typename Fn>
17class EvdElementDefVoid : public EvdElementDefBase<type, repeatable, order> {
18public:
19 explicit EvdElementDefVoid(const Fn& f)
20 : m_f(f) {}
21
22 bool operator()(const Decoder::Tlv& d) const {
23 m_f(d);
24 return true;
25 }
26
27private:
28 const Fn& m_f;
29};
30
31template<int type, bool repeatable, int order, typename Fn>
32class EvdElementDefBool : public EvdElementDefBase<type, repeatable, order> {
33public:
34 explicit EvdElementDefBool(const Fn& f)
35 : m_f(f) {}
36
37 bool operator()(const Decoder::Tlv& d) const {
38 return m_f(d);
39 }
40
41private:
42 const Fn& m_f;
43};
44
45template<int type, bool repeatable, int order, typename Fn,
46 typename R = typename std::conditional<
47 std::is_convertible<decltype(std::declval<Fn>()(Decoder::Tlv())), bool>::value,
48 EvdElementDefBool<type, repeatable, order, Fn>,
49 EvdElementDefVoid<type, repeatable, order, Fn>>::type>
50class EvdElementDefFn : public R {
51public:
52 using R::R;
53};
54
55template<int type, bool repeatable, int order, typename Decodable>
56class EvdElementDefDecodable : public EvdElementDefBase<type, repeatable, order> {
57public:
58 explicit EvdElementDefDecodable(Decodable* obj)
59 : m_obj(obj) {}
60
61 bool operator()(const Decoder::Tlv& d) const {
62 return m_obj->decodeFrom(d);
63 }
64
65private:
66 Decodable* m_obj;
67};
68
69template<int type, bool repeatable, int order>
70struct EvdElementDefIgnore : public EvdElementDefBase<type, repeatable, order> {
71 bool operator()(const Decoder::Tlv&) const {
72 return true;
73 }
74};
75
76template<int type, int order, typename NniClass, typename ValueType>
77class EvdElementDefNni : public EvdElementDefBase<type, false, order> {
78public:
79 explicit EvdElementDefNni(ValueType* value)
80 : m_value(value) {}
81
82 bool operator()(const Decoder::Tlv& d) const {
83 return NniClass::decode(d, *m_value);
84 }
85
86private:
87 ValueType* m_value;
88};
89
90} // namespace detail
91
93class EvDecoder {
94public:
96 public:
97 bool operator()(const Decoder::Tlv&, int&) const {
98 return false;
99 }
100 };
101
103 public:
104 bool operator()(uint32_t type) const {
105 return type <= 0x1F || type % 2 == 1;
106 }
107 };
108
114 template<typename... E>
115 static bool decode(const Decoder::Tlv& input, std::initializer_list<uint32_t> topTypes,
116 const E&... defs) {
117 return decodeEx(input, topTypes, DefaultUnknownCb(), DefaultIsCritical(), defs...);
118 }
119
125 template<typename... E>
126 static bool decodeValue(const Decoder& input, const E&... defs) {
127 return decodeValueEx(input, DefaultUnknownCb(), DefaultIsCritical(), defs...);
128 }
129
144 template<typename UnknownCallback, typename IsCritical, typename... E>
145 static bool decodeEx(const Decoder::Tlv& input, std::initializer_list<uint32_t> topTypes,
146
147 const UnknownCallback& unknownCb, const IsCritical& isCritical,
148 const E&... defs) {
149 if (topTypes.size() > 0 &&
150 std::find(topTypes.begin(), topTypes.end(), input.type) == topTypes.end()) {
151 return false;
152 }
153 return decodeValueEx(input.vd(), unknownCb, isCritical, defs...);
154 }
155
157 template<typename UnknownCallback, typename IsCritical, typename... E>
158 static bool decodeValueEx(const Decoder& input, const UnknownCallback& unknownCb,
159 const IsCritical& isCritical, const E&... defs) {
160 int currentOrder = 0;
161 for (const auto& d : input) {
162 bool ok = decodeElement<AUTO_ORDER_SKIP>(d, currentOrder, unknownCb, isCritical, defs...);
163 if (!ok) {
164 return false;
165 }
166 }
167 return true;
168 }
169
181 template<int type, bool repeatable = false, int order = 0, typename Fn = void,
183 static R def(const Fn& f, decltype(&Fn::operator()) = nullptr) {
184 return R(f);
185 }
186
191 template<int type, bool repeatable = false, int order = 0, typename Decodable = void,
193 static R def(Decodable* decodable, decltype(&Decodable::decodeFrom) = nullptr) {
194 return R(decodable);
195 }
196
198 template<int type, bool repeatable = false, int order = 0,
200 static R defIgnore() {
201 return R();
202 }
203
208 template<int type, typename NniClass = tlv::NNI, int order = 0, typename ValueType = void,
210 static R defNni(ValueType* value) {
211 return R(value);
212 }
213
214private:
215 EvDecoder() = delete;
216
217 enum {
218 AUTO_ORDER_SKIP = 100,
219 };
220
221 template<int autoOrder, typename UnknownCallback, typename IsCritical, typename First,
222 typename... E>
223 static bool decodeElement(const Decoder::Tlv& d, int& currentOrder,
224 const UnknownCallback& unknownCb, const IsCritical& isCritical,
225 const First& first, const E&... defs) {
226 if (d.type == First::TT::value) {
227 return useDef<autoOrder>(d, currentOrder, isCritical, first);
228 }
229 return decodeElement<autoOrder + AUTO_ORDER_SKIP>(d, currentOrder, unknownCb, isCritical,
230 defs...);
231 }
232
233 template<int autoOrder, typename UnknownCallback, typename IsCritical>
234 static bool decodeElement(const Decoder::Tlv& d, int& currentOrder,
235 const UnknownCallback& unknownCb, const IsCritical& isCritical) {
236 return unknownCb(d, currentOrder) || handleUnrecognized(d, isCritical);
237 }
238
239 template<int autoOrder, typename IsCritical, typename E>
240 static bool useDef(const Decoder::Tlv& d, int& currentOrder, const IsCritical& isCritical,
241 const E& def) {
242 int defOrder = E::Order::value == 0 ? autoOrder : E::Order::value;
243 if (currentOrder > defOrder) {
244 return handleUnrecognized(d, isCritical); // out of order
245 }
246 if (currentOrder == defOrder && !E::Repeatable::value) {
247 return false; // cannot repeat
248 }
249 currentOrder = defOrder;
250 return def(d);
251 }
252
253 template<typename IsCritical>
254 static bool handleUnrecognized(const Decoder::Tlv& d, const IsCritical& isCritical) {
255 return !isCritical(d.type);
256 }
257};
258
259} // namespace ndnph
260
261#endif // NDNPH_TLV_EV_DECODER_HPP
Decoded TLV.
Definition decoder.hpp:13
TLV decoder.
Definition decoder.hpp:10
Definition ev-decoder.hpp:102
bool operator()(uint32_t type) const
Definition ev-decoder.hpp:104
Definition ev-decoder.hpp:95
bool operator()(const Decoder::Tlv &, int &) const
Definition ev-decoder.hpp:97
TLV decoder that understands Packet Format v0.3 evolvability guidelines.
Definition ev-decoder.hpp:93
static R def(const Fn &f, decltype(&Fn::operator())=nullptr)
Create an element definition.
Definition ev-decoder.hpp:183
static R defIgnore()
Create an element definition to ignore a field.
Definition ev-decoder.hpp:200
static bool decodeEx(const Decoder::Tlv &input, std::initializer_list< uint32_t > topTypes, const UnknownCallback &unknownCb, const IsCritical &isCritical, const E &... defs)
Decode input TLV with a sequence of element definitions.
Definition ev-decoder.hpp:145
static R defNni(ValueType *value)
Create an element definition for Non-Negative Integer field.
Definition ev-decoder.hpp:210
static R def(Decodable *decodable, decltype(&Decodable::decodeFrom)=nullptr)
Create an element definition.
Definition ev-decoder.hpp:193
static bool decodeValue(const Decoder &input, const E &... defs)
Decode input TLV-VALUE with a sequence of element definitions.
Definition ev-decoder.hpp:126
static bool decodeValueEx(const Decoder &input, const UnknownCallback &unknownCb, const IsCritical &isCritical, const E &... defs)
Decode input TLV-VALUE with a sequence of element definitions.
Definition ev-decoder.hpp:158
static bool decode(const Decoder::Tlv &input, std::initializer_list< uint32_t > topTypes, const E &... defs)
Decode input TLV with a sequence of element definitions.
Definition ev-decoder.hpp:115
Definition ev-decoder.hpp:32
EvdElementDefBool(const Fn &f)
Definition ev-decoder.hpp:34
bool operator()(const Decoder::Tlv &d) const
Definition ev-decoder.hpp:37
Definition ev-decoder.hpp:56
bool operator()(const Decoder::Tlv &d) const
Definition ev-decoder.hpp:61
EvdElementDefDecodable(Decodable *obj)
Definition ev-decoder.hpp:58
Definition ev-decoder.hpp:50
Definition ev-decoder.hpp:77
EvdElementDefNni(ValueType *value)
Definition ev-decoder.hpp:79
bool operator()(const Decoder::Tlv &d) const
Definition ev-decoder.hpp:82
Definition ev-decoder.hpp:17
EvdElementDefVoid(const Fn &f)
Definition ev-decoder.hpp:19
bool operator()(const Decoder::Tlv &d) const
Definition ev-decoder.hpp:22
NonNegativeInteger encoding.
Definition nni.hpp:118
Definition fs.hpp:33
Definition ev-decoder.hpp:10
std::integral_constant< int, type > TT
Definition ev-decoder.hpp:11
std::integral_constant< int, order > Order
Definition ev-decoder.hpp:13
std::integral_constant< bool, repeatable > Repeatable
Definition ev-decoder.hpp:12
Definition ev-decoder.hpp:70
bool operator()(const Decoder::Tlv &) const
Definition ev-decoder.hpp:71