VariantParser.cti
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief VariantParser inline template implementation */
16 
17 #include "VariantParser.ih"
18 
19 // Custom includes
20 #include <senf/Utils/senfassert.hh>
21 #include <boost/mpl/size.hpp>
22 
23 #define prefix_ inline
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 // senf::VariantParser<AuxPolicy,Parsers>
28 
29 template <class AuxPolicy, class Parsers>
30 prefix_ senf::VariantParser<AuxPolicy,Parsers>::
31 VariantParser(data_iterator i, state_type s)
32  : PacketParserBase(i,s)
33 {}
34 
35 template <class AuxPolicy, class Parsers>
36 prefix_ senf::VariantParser<AuxPolicy,Parsers>::
37 VariantParser(AuxPolicy policy, data_iterator i, state_type s)
38  : PacketParserBase(i,s), AuxPolicy(policy)
39 {}
40 
41 template <class AuxPolicy, class Parsers>
42 prefix_ senf::PacketParserBase::size_type
43 senf::VariantParser<AuxPolicy,Parsers>::bytes()
44  const
45 {
46  return detail::VariantBytes< VariantParser, boost::mpl::size<parsers>::value - 1 >
47  ::bytes(*this, variant()) + AuxPolicy::aux_bytes;
48 }
49 
50 template <class AuxPolicy, class Parsers>
51 prefix_ void
52 senf::VariantParser<AuxPolicy,Parsers>::init()
53 {
54  AuxPolicy::aux(0,i(),state());
55  get<0>().init();
56 }
57 
58 template <class AuxPolicy, class Parsers>
59 prefix_ unsigned senf::VariantParser<AuxPolicy,Parsers>::variant()
60  const
61 {
62  return AuxPolicy::aux(i(),state());
63 }
64 
65 template <class AuxPolicy, class Parsers>
66 template <unsigned N>
67 prefix_ typename boost::mpl::at<
68  typename senf::VariantParser<AuxPolicy,Parsers>::parsers,
69  boost::mpl::int_<N> >::type
70 senf::VariantParser<AuxPolicy,Parsers>::get()
71  const
72 {
73  SENF_ASSERT( variant() == N, "Access to non-active variant member" );
74  return typename boost::mpl::at<parsers, boost::mpl::int_<N> >::type(
75  AuxPolicy::adjust(i(), state()), state() );
76 }
77 
78 //-/////////////////////////////////////////////////////////////////////////////////////////////////
79 // senf::detail::VariantBytes<Variant,N>
80 
81 template <class Variant, unsigned N>
82 prefix_ senf::PacketParserBase::size_type
83 senf::detail::VariantBytes<Variant,N>::bytes(Variant const & v, unsigned n)
84 {
85  if (n == N)
86  return senf::bytes(v.template get<N>());
87  else
88  return VariantBytes<Variant, N-1>::bytes(v, n);
89 }
90 
91 template <class Variant>
92 prefix_ senf::PacketParserBase::size_type
93 senf::detail::VariantBytes<Variant,0>::bytes(Variant const & v, unsigned n)
94 {
95  return senf::bytes(v.template get<0>());
96 }
97 
98 //-/////////////////////////////////////////////////////////////////////////////////////////////////
99 // senf::detail::VariantKeyTransform<T,Keys>
100 
101 template <class T, class Keys>
102 prefix_ unsigned senf::detail::VariantKeyTransform<T,Keys>::get(input_type v)
103 {
104  return VariantKeyTransformCheck<
105  input_type, value_type, Keys, boost::mpl::size<Keys>::type::value-1>::get(v);
106 }
107 
108 template <class T, class Keys>
109 prefix_ typename senf::detail::VariantKeyTransform<T,Keys>::input_type
110 senf::detail::VariantKeyTransform<T,Keys>::set(unsigned v)
111 {
112  return VariantKeyTransformCheck<
113  input_type, value_type, Keys, boost::mpl::size<Keys>::type::value-1>::set(v);
114 }
115 
116 template <class In, class Out, class Keys, unsigned N>
117 prefix_ Out senf::detail::VariantKeyTransformCheck<In,Out,Keys,N>::get(In v)
118 {
119  if (boost::mpl::at<Keys, boost::mpl::int_<N> >::type::key() == v)
120  return N;
121  else
122  return VariantKeyTransformCheck<In, Out, Keys, N-1>::get(v);
123 }
124 
125 template <class In, class Out, class Keys, unsigned N>
126 prefix_ In senf::detail::VariantKeyTransformCheck<In,Out,Keys,N>::set(Out v)
127 {
128  if (v == N)
129  return boost::mpl::at<Keys, boost::mpl::int_<N> >::type::key();
130  else
131  return VariantKeyTransformCheck<In, Out, Keys, N-1>::set(v);
132 }
133 
134 template <class In, class Out, class Keys>
135 prefix_ Out senf::detail::VariantKeyTransformCheck<In, Out, Keys, 0>::get(In v)
136 {
137  return 0;
138 }
139 
140 template <class In, class Out, class Keys>
141 prefix_ In senf::detail::VariantKeyTransformCheck<In, Out, Keys, 0>::set(Out v)
142 {
143  return boost::mpl::at<Keys, boost::mpl::int_<0> >::type::key();
144 }
145 
146 //-/////////////////////////////////////////////////////////////////////////////////////////////////
147 #undef prefix_
148 
149 
150 // Local Variables:
151 // mode: c++
152 // fill-column: 100
153 // comment-column: 40
154 // c-file-style: "senf"
155 // indent-tabs-mode: nil
156 // ispell-local-dictionary: "american"
157 // compile-command: "scons -u test"
158 // End: