OpenSDN source code
proto_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
3  * Copyright Contributors to the OpenSDN project
4  */
5 
6 #ifndef __BASE_PROTO_IMPL_H__
7 #define __BASE_PROTO_IMPL_H__
8 
9 #include <vector>
10 
11 namespace detail {
12 
13 extern bool debug_;
14 #define PROTO_DEBUG(args...) if (detail::debug_) LOG(DEBUG, ##args)
15 
16 template <typename T>
17 struct ApplySetter {
18  template <typename U>
19  void operator()(const uint8_t *data, int element_size, U *obj) {
20  int value = get_value(data, element_size);
21  T::set(obj, value);
22  }
23 };
24 
25 template <>
26 struct ApplySetter<void> {
27  void operator()(const uint8_t *data, int element_size, void *obj) {
28  }
29 };
30 
31 template <typename T>
32 struct ApplyGetter {
33  template <typename U>
34  void operator()(uint8_t *data, int element_size, U *obj) {
35  uint64_t value = T::get(obj);
36  put_value(data, element_size, value);
37  }
38 };
39 template <>
40 struct ApplyGetter<void> {
41  template <typename U>
42  void operator()(uint8_t *data, int element_size, U *obj) {
43  memset(data, 0, element_size);
44  }
45 };
46 
47 template <typename Accessor, typename T>
49  template <typename Iterator>
50  int Copy(Iterator begin, Iterator end, uint8_t *data) {
51  int count = 0;
52  for (Iterator iter = begin; iter != end; ++iter) {
53  put_value(data, sizeof(*iter), *iter);
54  data += sizeof(*iter);
55  count+= sizeof(*iter);
56  }
57  return count;
58  }
59  int operator()(uint8_t *data, int element_size, T *obj) {
60  int count = Copy(Accessor::begin(obj), Accessor::end(obj), data);
61  return count;
62  }
63 };
64 template <typename T>
65 struct VariableLengthWriter<void, T> {
66  int operator()(uint8_t *data, int element_size, T *obj) {
67  return 0;
68  }
69 };
70 
71 template <typename P, typename C>
72 struct ContextPush {
73  C * operator()(ParseContext *context, P *obj) {
74  C *child_obj = new C;
75  context->Push(child_obj);
76  return child_obj;
77  }
78 };
79 template <typename P>
80 struct ContextPush<P, void> {
81  P *operator()(ParseContext *context, P *obj) {
82  context->ReleaseData();
83  context->Push(obj);
84  return obj;
85  }
86 };
87 
88 template <>
89 struct ContextPush<void, void> {
90  void *operator()(ParseContext *context, void *obj) {
91  return obj;
92  }
93 };
94 template <typename T>
95 struct NoContextPush {
96  T *operator()(ParseContext *context, T *obj) {
97  return obj;
98  }
99 };
100 
101 template<typename T, typename ChildContextType>
102 struct StoreContext {
103  template <typename C>
104  void operator()(C *obj, ParseObject *context_obj) {
105  ChildContextType *child_obj =
106  dynamic_cast<ChildContextType *>(context_obj);
107  assert(child_obj);
108  T::insert(obj, child_obj);
109  }
110 };
111 
112 template <typename ChildContextType>
113 struct StoreContext<void, ChildContextType> {
114  void operator()(void *obj, ParseObject *child_obj) {
115  delete child_obj;
116  }
117 };
118 
119 template <typename Parent, typename T, typename ChildContextType>
120 struct ContextPop {
121  void operator()(ParseContext *context, T *obj) {
123  storer(obj, context->Pop());
124  }
125 };
126 
127 template<typename Parent, typename T>
128 struct ContextPop<Parent, T, void> {
129  void operator()(ParseContext *context, void *obj) {
130  context->SwapData(context->Pop());
131  }
132 };
133 
134 template<typename Parent>
135 struct ContextPop<Parent, void, void> {
136  void operator()(ParseContext *context, void *obj) {
137  }
138 };
139 
140 // If the child's ContextType is void the obj is copied to child_obj.
141 template <typename Child, typename T, typename opt_ctx_t>
143  opt_ctx_t *operator()(ParseContext *context, T *obj) {
145  return pushfn(context, obj);
146  }
147 };
148 
149 template <typename Child, typename T>
150 struct DescendentContextPush<Child, T, void>
151  {
152  typedef T ctx_t;
153  typedef void pctx_t;
154  T *operator()(ParseContext *context, T *obj) {
155  return obj;
156  }
157 };
158 template <typename Child>
160  typedef typename Child::ContextType ctx_t;
161  template <typename U>
162  ctx_t *operator()(ParseContext *context, U *obj) {
163  typedef typename Child::ContextSwap swap_t;
164  ctx_t *nobj = swap_t()(obj);
165  if (nobj == NULL) {
166  // TODO:
167  }
168  if (nobj != obj) {
169  context->SwapData(nobj);
170  }
171  return nobj;
172  }
173 };
174 
175 template <class Parent, typename Child>
177  template <typename T>
178  static int Parse(const uint8_t *data, size_t size, ParseContext *context,
179  T *obj) {
180  // If the child defines ContextType, this changes the type of the
181  // top of stack element and causes us to push a new context into the
182  // stack.
183  // Context push and pop is only relevant if the child doesn't
184  // define a context swap. If it does, the swap operation will
185  // replace the existing stack frame data object.
186  typedef typename Child::ContextType opt_ctx_t;
187 
188  typedef typename boost::mpl::if_<
189  boost::is_same<opt_ctx_t, void>,
190  T, opt_ctx_t>::type ctx_t;
191 
192  typedef typename boost::mpl::if_<
193  boost::is_same<typename Child::ContextSwap, void>,
195  DescendentContextSwap<Child> >::type context_op_t;
196 
197  ctx_t *child_obj = context_op_t()(context, obj);
198 
199  int result = Child::Parse(data, size, context, child_obj);
200  if (result < 0) return result;
201  typedef typename boost::mpl::if_<
202  boost::is_same<typename Child::ContextSwap, void>,
203  opt_ctx_t,
204  void>::type push_ctx_t;
205  typedef typename boost::mpl::if_<
206  boost::is_same<push_ctx_t, void>,
207  void, T>::type pctx_t;
209  popfn(context, obj);
210  return result;
211  }
212 };
213 
214 template <typename T, typename Context>
216  int operator()(ParseContext *context, const uint8_t *data, size_t size,
217  size_t msgsize, Context *obj) {
218  T t;
219  int value = t(obj, data, size);
220  context->set_lensize(size);
221  if ((size_t) value > msgsize) {
222  PROTO_DEBUG(TYPE_NAME(Context) << " Sequence length error: "
223  << value << " > " << msgsize);
224  return -1;
225  }
226  context->set_size(value);
227  return 0;
228  }
229 };
230 
231 template <typename Context>
232 struct SequenceLengthSetter<int, Context> {
233  int operator()(ParseContext *context, const uint8_t *data, size_t size,
234  size_t msgsize, Context *obj) {
235  int value = get_value(data, size);
236  context->set_lensize(size);
237  if ((size_t) value > msgsize) {
238  PROTO_DEBUG(TYPE_NAME(Context) << " Sequence length error: "
239  << value << " > " << msgsize);
240  return -1;
241  }
242  context->set_size(value);
243  return 0;
244  }
245 };
246 
247 template <typename Context>
248 struct SequenceLengthSetter<void, Context> {
249  int operator()(ParseContext *context, const uint8_t *data, size_t size,
250  size_t msgsize, Context *obj) {
251  return 0;
252  }
253 };
254 
255 template<typename T>
258  uint8_t *data, int arg) {
259  context->AddCallback(cb, data, arg);
260  }
261 };
262 template<>
265  uint8_t *data, int arg) {
266  }
267 };
268 
269 template<typename Accessor, typename T>
271  int operator()(const uint8_t *data, size_t size, ParseContext *context,
272  T *obj) {
273  int element_size = context->size();
274  // If element size is unknown, read till the end of buffer
275  if (element_size < 0) element_size = size;
276  else if (size < (size_t) element_size) {
277  PROTO_DEBUG(TYPE_NAME(T) << " Variable Length Setter failed "
278  << size << " < " << element_size);
279  return -1;
280  }
281  Accessor::set(obj, data, element_size);
282  return element_size;
283  }
284 };
285 template<typename T>
286 struct VariableLengthSetter<void, T> {
287  int operator()(const uint8_t *data, size_t size, ParseContext *context,
288  T *obj) {
289  return 0;
290  }
291 };
292 
293 template<typename SizeSetter, typename T>
295  int operator()(const uint8_t *data, size_t size, ParseContext *context,
296  T *obj) {
297  return SizeSetter::get(obj);
298  }
299 };
300 
301 template<typename Derived, typename T>
303  int operator()(const uint8_t *data, size_t size, ParseContext *context,
304  T *obj) {
305  typedef typename Derived::Setter setter_t;
307  setter(data, Derived::kSize, obj);
308  return Derived::kSize;
309  }
310 };
311 
312 template<typename Derived, typename T>
313 struct NopSetter {
314  int operator()(const uint8_t *data, size_t size, ParseContext *context,
315  T *obj) {
316  return Derived::kSize;
317  }
318 };
319 
320 template <typename Derived>
321 struct SizeComparer {
322  bool operator()(int size) const {
323  return size >= Derived::kSize;
324  }
325 };
326 
327 struct NopComparer {
328  bool operator()(int size) const {
329  return true;
330  }
331 };
332 
333 template <typename Setter, typename T>
335  static int get(const T *msg) {
336  return Setter::size(msg);
337  }
338 };
339 template <typename T>
340 struct VarLengthSizeValue<void, T> {
341  static int get(const T *msg) {
342  return 0;
343  }
344 };
345 template <typename Derived>
347  static int get(const void *msg) {
348  return Derived::kSize;
349  }
350 };
351 
352 template <typename T>
353 struct AddCallback {
354  void operator()(EncodeContext *context, uint8_t *data, int arg) {
355  context->AddCallback(&T::Callback, data, arg);
356  }
357 };
358 template <>
359 struct AddCallback<void>{
360  void operator()(EncodeContext *context, uint8_t *data, int arg) {
361  }
362 };
363 
364 template <typename ContextAccessor>
366  typedef typename ContextAccessor::ValueType ValueType;
367 };
368 template <>
369 struct ContextElementType<void> {
370  typedef void ValueType;
371 };
372 
373 template <typename Accessor>
375  typedef typename Accessor::ValueType ValueType;
376  template <typename Obj>
377  ContextIterator(const Obj *obj)
378  : iter(accessor.begin(obj)) {
379 
380  }
381 
383  ValueType *obj = *iter;
384  ++iter;
385  return obj;
386  }
387 
388  template <typename Obj>
389  bool HasNext(Obj *obj) const {
390  return (iter != accessor.end(obj));
391  }
392 
393 private:
395  typename Accessor::CollectionType::const_iterator iter;
396 };
397 template <>
398 struct ContextIterator<void> {
399  ContextIterator(void *obj) {
400  }
401  void * Next() { return NULL; }
402  bool HasNext(void *obj) const { return false; }
403 };
404 
405 template <typename T>
406 struct SaveOffset {
408  ctx->SaveOffset(T()());
409  }
410 };
411 
412 template <>
413 struct SaveOffset<void> {
414  void operator()(EncodeContext *ctx) { }
415 };
416 
417 } // detail
418 
419 
420 #endif
boost::function< void(EncodeContext *, uint8_t *, int, int)> CallbackType
void AddCallback(CallbackType cb, uint8_t *data, int arg)
void set_lensize(int lensize)
void set_size(size_t length)
void Push(ParseObject *data)
void SwapData(ParseObject *obj)
uint8_t type
Definition: load_balance.h:2
#define TYPE_NAME(_type)
Definition: logging.h:33
static void put_value(uint8_t *data, int size, uint64_t value)
Definition: parse_object.h:56
static uint64_t get_value(const uint8_t *data, int size)
Definition: parse_object.h:40
#define PROTO_DEBUG(args...)
Definition: proto_impl.h:14
static void set(C *obj, T value)
void operator()(EncodeContext *context, uint8_t *data, int arg)
Definition: proto_impl.h:360
void operator()(EncodeContext *context, uint8_t *data, int arg)
Definition: proto_impl.h:354
void operator()(uint8_t *data, int element_size, U *obj)
Definition: proto_impl.h:42
void operator()(uint8_t *data, int element_size, U *obj)
Definition: proto_impl.h:34
void operator()(const uint8_t *data, int element_size, void *obj)
Definition: proto_impl.h:27
void operator()(const uint8_t *data, int element_size, U *obj)
Definition: proto_impl.h:19
ContextAccessor::ValueType ValueType
Definition: proto_impl.h:366
bool HasNext(void *obj) const
Definition: proto_impl.h:402
ValueType * Next()
Definition: proto_impl.h:382
ContextIterator(const Obj *obj)
Definition: proto_impl.h:377
Accessor::ValueType ValueType
Definition: proto_impl.h:375
Accessor::CollectionType::const_iterator iter
Definition: proto_impl.h:395
bool HasNext(Obj *obj) const
Definition: proto_impl.h:389
void operator()(ParseContext *context, void *obj)
Definition: proto_impl.h:129
void operator()(ParseContext *context, void *obj)
Definition: proto_impl.h:136
void operator()(ParseContext *context, T *obj)
Definition: proto_impl.h:121
P * operator()(ParseContext *context, P *obj)
Definition: proto_impl.h:81
void * operator()(ParseContext *context, void *obj)
Definition: proto_impl.h:90
C * operator()(ParseContext *context, P *obj)
Definition: proto_impl.h:73
T * operator()(ParseContext *context, T *obj)
Definition: proto_impl.h:154
opt_ctx_t * operator()(ParseContext *context, T *obj)
Definition: proto_impl.h:143
Child::ContextType ctx_t
Definition: proto_impl.h:160
ctx_t * operator()(ParseContext *context, U *obj)
Definition: proto_impl.h:162
static int Parse(const uint8_t *data, size_t size, ParseContext *context, T *obj)
Definition: proto_impl.h:178
int operator()(const uint8_t *data, size_t size, ParseContext *context, T *obj)
Definition: proto_impl.h:303
static int get(const void *msg)
Definition: proto_impl.h:347
int operator()(const uint8_t *data, size_t size, ParseContext *context, T *obj)
Definition: proto_impl.h:295
T * operator()(ParseContext *context, T *obj)
Definition: proto_impl.h:96
bool operator()(int size) const
Definition: proto_impl.h:328
int operator()(const uint8_t *data, size_t size, ParseContext *context, T *obj)
Definition: proto_impl.h:314
void operator()(EncodeContext *ctx)
Definition: proto_impl.h:414
void operator()(EncodeContext *ctx)
Definition: proto_impl.h:407
void operator()(EncodeContext::CallbackType cb, EncodeContext *context, uint8_t *data, int arg)
Definition: proto_impl.h:264
void operator()(EncodeContext::CallbackType cb, EncodeContext *context, uint8_t *data, int arg)
Definition: proto_impl.h:257
int operator()(ParseContext *context, const uint8_t *data, size_t size, size_t msgsize, Context *obj)
Definition: proto_impl.h:233
int operator()(ParseContext *context, const uint8_t *data, size_t size, size_t msgsize, Context *obj)
Definition: proto_impl.h:249
int operator()(ParseContext *context, const uint8_t *data, size_t size, size_t msgsize, Context *obj)
Definition: proto_impl.h:216
bool operator()(int size) const
Definition: proto_impl.h:322
void operator()(void *obj, ParseObject *child_obj)
Definition: proto_impl.h:114
void operator()(C *obj, ParseObject *context_obj)
Definition: proto_impl.h:104
static int get(const T *msg)
Definition: proto_impl.h:341
static int get(const T *msg)
Definition: proto_impl.h:335
int operator()(const uint8_t *data, size_t size, ParseContext *context, T *obj)
Definition: proto_impl.h:287
int operator()(const uint8_t *data, size_t size, ParseContext *context, T *obj)
Definition: proto_impl.h:271
int operator()(uint8_t *data, int element_size, T *obj)
Definition: proto_impl.h:66
int Copy(Iterator begin, Iterator end, uint8_t *data)
Definition: proto_impl.h:50
int operator()(uint8_t *data, int element_size, T *obj)
Definition: proto_impl.h:59