LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - proto_impl.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 131 133 98.5 %
Date: 2026-11-30 02:00:48 Functions: 963 987 97.6 %
Legend: Lines: hit not hit

          Line data    Source code
       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     2415319 :     void operator()(const uint8_t *data, int element_size, U *obj) {
      20     2415319 :         int value = get_value(data, element_size);
      21     2415332 :         T::set(obj, value);
      22     2415323 :     }
      23             : };
      24             : 
      25             : template <>
      26             : struct ApplySetter<void> {
      27     1007450 :     void operator()(const uint8_t *data, int element_size, void *obj) {
      28     1007450 :     }
      29             : };
      30             : 
      31             : template <typename T>
      32             : struct ApplyGetter {
      33             :     template <typename U>
      34     3017601 :     void operator()(uint8_t *data, int element_size, U *obj) {
      35     3017601 :         uint64_t value = T::get(obj);
      36     3017674 :         put_value(data, element_size, value);
      37     3017660 :     }
      38             : };
      39             : template <>
      40             : struct ApplyGetter<void> {
      41             :     template <typename U>
      42     1123941 :     void operator()(uint8_t *data, int element_size, U *obj) {
      43     1123941 :         memset(data, 0, element_size);
      44     1123941 :     }
      45             : };
      46             : 
      47             : template <typename Accessor, typename T>
      48             : struct VariableLengthWriter {
      49             :     template <typename Iterator>
      50     2140863 :     int Copy(Iterator begin, Iterator end, uint8_t *data) {
      51     2140863 :         int count = 0;
      52    17410609 :         for (Iterator iter = begin; iter != end; ++iter) {
      53    15269551 :             put_value(data, sizeof(*iter), *iter);
      54    15269722 :             data += sizeof(*iter);
      55    15269722 :             count+= sizeof(*iter);
      56             :         }
      57     2140882 :         return count;
      58             :     }
      59     2140869 :     int operator()(uint8_t *data, int element_size, T *obj) {
      60     2140869 :         int count = Copy(Accessor::begin(obj), Accessor::end(obj), data);
      61     2140882 :         return count;
      62             :     }
      63             : };
      64             : template <typename T>
      65             : struct VariableLengthWriter<void, T> {
      66      681508 :     int operator()(uint8_t *data, int element_size, T *obj) {
      67      681508 :         return 0;
      68             :     }
      69             : };
      70             : 
      71             : template <typename P, typename C>
      72             : struct ContextPush {
      73     1994321 :     C * operator()(ParseContext *context, P *obj) {
      74     1994321 :         C *child_obj = new C;
      75     1994324 :         context->Push(child_obj);
      76     1994305 :         return child_obj;
      77             :     }
      78             : };
      79             : template <typename P>
      80             : struct ContextPush<P, void> {
      81      661930 :     P *operator()(ParseContext *context, P *obj) {
      82      661930 :         context->ReleaseData();
      83      661905 :         context->Push(obj);
      84      661936 :         return obj;
      85             :     }
      86             : };
      87             : 
      88             : template <>
      89             : struct ContextPush<void, void> {
      90      198934 :     void *operator()(ParseContext *context, void *obj) {
      91      198934 :         return obj;
      92             :     }
      93             : };
      94             : template <typename T>
      95             : struct NoContextPush {
      96      633917 :     T *operator()(ParseContext *context, T *obj) {
      97      633917 :         return obj;
      98             :     }
      99             : };
     100             : 
     101             : template<typename T, typename ChildContextType>
     102             : struct StoreContext {
     103             :     template <typename C>
     104     1798632 :     void operator()(C *obj, ParseObject *context_obj) {
     105     1798632 :         ChildContextType *child_obj =
     106     1798632 :             dynamic_cast<ChildContextType *>(context_obj);
     107     1798632 :         assert(child_obj);
     108     1798632 :         T::insert(obj, child_obj);
     109     1798626 :     }
     110             : };
     111             : 
     112             : template <typename ChildContextType>
     113             : struct StoreContext<void, ChildContextType> {
     114      191356 :     void operator()(void *obj, ParseObject *child_obj) {
     115      191356 :         delete child_obj;
     116      191356 :     }
     117             : };
     118             : 
     119             : template <typename Parent, typename T, typename ChildContextType>
     120             : struct ContextPop {
     121     1989974 :     void operator()(ParseContext *context, T *obj) {
     122             :         StoreContext<typename Parent::ContextStorer, ChildContextType> storer;
     123     1989974 :         storer(obj, context->Pop());
     124     1989982 :     }
     125             : };
     126             : 
     127             : template<typename Parent, typename T>
     128             : struct ContextPop<Parent, T, void> {
     129      658241 :     void operator()(ParseContext *context, void *obj) {
     130      658241 :         context->SwapData(context->Pop());
     131      658223 :     }
     132             : };
     133             : 
     134             : template<typename Parent>
     135             : struct ContextPop<Parent, void, void> {
     136     9533115 :     void operator()(ParseContext *context, void *obj) {
     137     9533115 :     }
     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>
     142             : struct DescendentContextPush {
     143      194987 :     opt_ctx_t *operator()(ParseContext *context, T *obj) {
     144             :         ContextPush<T, opt_ctx_t> pushfn;
     145      389964 :         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     8087456 :     T *operator()(ParseContext *context, T *obj) {
     155     8087456 :         return obj;
     156             :     }
     157             : };
     158             : template <typename Child>
     159             : struct DescendentContextSwap {
     160             :     typedef typename Child::ContextType ctx_t;
     161             :     template <typename U>
     162      633941 :         ctx_t *operator()(ParseContext *context, U *obj) {
     163             :             typedef typename Child::ContextSwap swap_t;
     164      633941 :             ctx_t *nobj = swap_t()(obj);
     165             :             if (nobj == NULL) {
     166             :                 // TODO:
     167             :             }
     168      633939 :             if (nobj != obj) {
     169      633939 :                 context->SwapData(nobj);
     170             :             }
     171      633935 :             return nobj;
     172             :         }
     173             : };
     174             : 
     175             : template <class Parent, typename Child>
     176             : struct DescendentParser {
     177             :     template <typename T>
     178     8916367 :     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>,
     194             :                 DescendentContextPush<Child, T, typename Child::ContextType>,
     195             :                 DescendentContextSwap<Child> >::type context_op_t;
     196             : 
     197     8916367 :         ctx_t *child_obj = context_op_t()(context, obj);
     198             : 
     199     8916378 :         int result = Child::Parse(data, size, context, child_obj);
     200     8916380 :         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;
     208             :         ContextPop<Parent, pctx_t, push_ctx_t> popfn;
     209     8899844 :         popfn(context, obj);
     210     8899841 :         return result;
     211             :     }
     212             : };
     213             : 
     214             : template <typename T, typename Context>
     215             : struct SequenceLengthSetter {
     216     1814297 :     int operator()(ParseContext *context, const uint8_t *data, size_t size,
     217             :                    size_t msgsize, Context *obj) {
     218             :         T t;
     219     1814297 :         int value = t(obj, data, size);
     220     1814300 :         context->set_lensize(size);
     221     1814282 :         if ((size_t) value > msgsize) {
     222         161 :             PROTO_DEBUG(TYPE_NAME(Context) << " Sequence length error: "
     223             :                        << value << " > " << msgsize);
     224         161 :             return -1;
     225             :         }
     226     1814121 :         context->set_size(value);
     227     1814123 :         return 0;
     228             :     }
     229             : };
     230             : 
     231             : template <typename Context>
     232             : struct SequenceLengthSetter<int, Context> {
     233      342727 :     int operator()(ParseContext *context, const uint8_t *data, size_t size,
     234             :                    size_t msgsize, Context *obj) {
     235      342727 :         int value = get_value(data, size);
     236      342727 :         context->set_lensize(size);
     237      342708 :         if ((size_t) value > msgsize) {
     238           2 :             PROTO_DEBUG(TYPE_NAME(Context) << " Sequence length error: "
     239             :                     << value << " > " << msgsize);
     240           2 :             return -1;
     241             :         }
     242      342706 :         context->set_size(value);
     243      342712 :         return 0;
     244             :     }
     245             : };
     246             : 
     247             : template <typename Context>
     248             : struct SequenceLengthSetter<void, Context> {
     249     3617720 :     int operator()(ParseContext *context, const uint8_t *data, size_t size,
     250             :                    size_t msgsize, Context *obj) {
     251     3617720 :         return 0;
     252             :     }
     253             : };
     254             : 
     255             : template<typename T>
     256             : struct SequenceLengthAddCallback {
     257     1589366 :     void operator()(EncodeContext::CallbackType cb, EncodeContext *context,
     258             :                     uint8_t *data, int arg) {
     259     1589366 :         context->AddCallback(cb, data, arg);
     260     1589110 :     }
     261             : };
     262             : template<>
     263             : struct SequenceLengthAddCallback<void> {
     264     5714845 :     void operator()(EncodeContext::CallbackType cb, EncodeContext *context,
     265             :                     uint8_t *data, int arg) {
     266     5714845 :     }
     267             : };
     268             : 
     269             : template<typename Accessor, typename T>
     270             : struct VariableLengthSetter {
     271     1554563 :     int operator()(const uint8_t *data, size_t size, ParseContext *context,
     272             :                    T *obj) {
     273     1554563 :         int element_size = context->size();
     274             :         // If element size is unknown, read till the end of buffer
     275     1554556 :         if (element_size < 0) element_size = size;
     276     1549647 :         else if (size < (size_t) element_size) {
     277           0 :             PROTO_DEBUG(TYPE_NAME(T) << " Variable Length Setter failed "
     278             :                        << size << " < " << element_size);
     279           0 :             return -1;
     280             :         }
     281     1554556 :         Accessor::set(obj, data, element_size);
     282     1554593 :         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>
     294             : struct LengthSizeSetter {
     295      633793 :     int operator()(const uint8_t *data, size_t size, ParseContext *context,
     296             :                    T *obj) {
     297      633793 :         return SizeSetter::get(obj);
     298             :     }
     299             : };
     300             : 
     301             : template<typename Derived, typename T>
     302             : struct FixedLengthSetter {
     303     3422772 :     int operator()(const uint8_t *data, size_t size, ParseContext *context,
     304             :                    T *obj) {
     305             :         typedef typename Derived::Setter setter_t;
     306             :         detail::ApplySetter<setter_t> setter;
     307     3422772 :         setter(data, Derived::kSize, obj);
     308     3422795 :         return Derived::kSize;
     309             :     }
     310             : };
     311             : 
     312             : template<typename Derived, typename T>
     313             : struct NopSetter {
     314      163507 :     int operator()(const uint8_t *data, size_t size, ParseContext *context,
     315             :                    T *obj) {
     316      163507 :         return Derived::kSize;
     317             :     }
     318             : };
     319             : 
     320             : template <typename Derived>
     321             : struct SizeComparer {
     322     3426966 :     bool operator()(int size) const {
     323     3426966 :         return size >= Derived::kSize;
     324             :     }
     325             : };
     326             : 
     327             : struct NopComparer {
     328     2352030 :     bool operator()(int size) const {
     329     2352030 :         return true;
     330             :     }
     331             : };
     332             : 
     333             : template <typename Setter, typename T>
     334             : struct VarLengthSizeValue {
     335     2141018 :     static int get(const T *msg) {
     336     2141018 :         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>
     346             : struct FixedLengthSizeValue {
     347     4482637 :     static int get(const void *msg) {
     348     4482637 :         return Derived::kSize;
     349             :     }
     350             : };
     351             : 
     352             : template <typename T>
     353             : struct AddCallback {
     354      451979 :     void operator()(EncodeContext *context, uint8_t *data, int arg) {
     355      451979 :         context->AddCallback(&T::Callback, data, arg);
     356      452002 :     }
     357             : };
     358             : template <>
     359             : struct AddCallback<void>{
     360     6851136 :     void operator()(EncodeContext *context, uint8_t *data, int arg) {
     361     6851136 :     }
     362             : };
     363             : 
     364             : template <typename ContextAccessor>
     365             : struct ContextElementType {
     366             :     typedef typename ContextAccessor::ValueType ValueType;
     367             : };
     368             : template <>
     369             : struct ContextElementType<void> {
     370             :     typedef void ValueType;
     371             : };
     372             : 
     373             : template <typename Accessor>
     374             : struct ContextIterator {
     375             :     typedef typename Accessor::ValueType ValueType;
     376             :     template <typename Obj>
     377      970364 :     ContextIterator(const Obj *obj)
     378      970364 :     : iter(accessor.begin(obj)) {
     379             : 
     380      970379 :     }
     381             : 
     382     2335802 :     ValueType * Next() {
     383     2335802 :         ValueType *obj = *iter;
     384     2335796 :         ++iter;
     385     2335788 :         return obj;
     386             :     }
     387             : 
     388             :     template <typename Obj>
     389     3305882 :     bool HasNext(Obj *obj) const {
     390     3305882 :         return (iter != accessor.end(obj));
     391             :     }
     392             : 
     393             : private:
     394             :     Accessor accessor;
     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 {
     407      583111 :     void operator()(EncodeContext *ctx) {
     408      583111 :         ctx->SaveOffset(T()());
     409      583402 :     }
     410             : };
     411             : 
     412             : template <>
     413             : struct SaveOffset<void> {
     414     9473387 :     void operator()(EncodeContext *ctx) { }
     415             : };
     416             : 
     417             : }  // detail
     418             : 
     419             : 
     420             : #endif

Generated by: LCOV version 1.14