a7129f9afe6ac34e7228e6448f45f94ca27d8c15
[senf.git] / senf / Packets / VariantParser.cti
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief VariantParser inline template implementation */
25
26 #include "VariantParser.ih"
27
28 // Custom includes
29 #include <senf/Utils/senfassert.hh>
30 #include <boost/mpl/size.hpp>
31
32 #define prefix_ inline
33 //-/////////////////////////////////////////////////////////////////////////////////////////////////
34
35 //-/////////////////////////////////////////////////////////////////////////////////////////////////
36 // senf::VariantParser<AuxPolicy,Parsers>
37
38 template <class AuxPolicy, class Parsers>
39 prefix_ senf::VariantParser<AuxPolicy,Parsers>::
40 VariantParser(data_iterator i, state_type s)
41     : PacketParserBase(i,s)
42 {}
43
44 template <class AuxPolicy, class Parsers>
45 prefix_ senf::VariantParser<AuxPolicy,Parsers>::
46 VariantParser(AuxPolicy policy, data_iterator i, state_type s)
47     : PacketParserBase(i,s), AuxPolicy(policy)
48 {}
49
50 template <class AuxPolicy, class Parsers>
51 prefix_ senf::PacketParserBase::size_type
52 senf::VariantParser<AuxPolicy,Parsers>::bytes()
53     const
54 {
55     return detail::VariantBytes< VariantParser, boost::mpl::size<parsers>::value - 1 >
56         ::bytes(*this, variant()) + AuxPolicy::aux_bytes;
57 }
58
59 template <class AuxPolicy, class Parsers>
60 prefix_ void
61 senf::VariantParser<AuxPolicy,Parsers>::init()
62 {
63     AuxPolicy::aux(0,i(),state());
64     get<0>().init();
65 }
66
67 template <class AuxPolicy, class Parsers>
68 prefix_ unsigned senf::VariantParser<AuxPolicy,Parsers>::variant()
69     const
70 {
71     return AuxPolicy::aux(i(),state());
72 }
73
74 template <class AuxPolicy, class Parsers>
75 template <unsigned N>
76 prefix_ typename boost::mpl::at<
77     typename senf::VariantParser<AuxPolicy,Parsers>::parsers,
78     boost::mpl::int_<N> >::type
79 senf::VariantParser<AuxPolicy,Parsers>::get()
80     const
81 {
82     SENF_ASSERT( variant() == N, "Access to non-active variant member" );
83     return typename boost::mpl::at<parsers, boost::mpl::int_<N> >::type(
84         AuxPolicy::adjust(i(), state()), state() );
85 }
86
87 //-/////////////////////////////////////////////////////////////////////////////////////////////////
88 // senf::detail::VariantBytes<Variant,N>
89
90 template <class Variant, unsigned N>
91 prefix_ senf::PacketParserBase::size_type
92 senf::detail::VariantBytes<Variant,N>::bytes(Variant const & v, unsigned n)
93 {
94     if (n == N)
95         return senf::bytes(v.template get<N>());
96     else
97         return VariantBytes<Variant, N-1>::bytes(v, n);
98 }
99
100 template <class Variant>
101 prefix_ senf::PacketParserBase::size_type
102 senf::detail::VariantBytes<Variant,0>::bytes(Variant const & v, unsigned n)
103 {
104     return senf::bytes(v.template get<0>());
105 }
106
107 //-/////////////////////////////////////////////////////////////////////////////////////////////////
108 // senf::detail::VariantKeyTransform<T,Keys>
109
110 template <class T, class Keys>
111 prefix_ unsigned senf::detail::VariantKeyTransform<T,Keys>::get(input_type v)
112 {
113     return VariantKeyTransformCheck<
114         input_type, value_type, Keys, boost::mpl::size<Keys>::type::value-1>::get(v);
115 }
116
117 template <class T, class Keys>
118 prefix_ typename senf::detail::VariantKeyTransform<T,Keys>::input_type
119 senf::detail::VariantKeyTransform<T,Keys>::set(unsigned v)
120 {
121     return VariantKeyTransformCheck<
122         input_type, value_type, Keys, boost::mpl::size<Keys>::type::value-1>::set(v);
123 }
124
125 template <class In, class Out, class Keys, unsigned N>
126 prefix_ Out senf::detail::VariantKeyTransformCheck<In,Out,Keys,N>::get(In v)
127 {
128     if (boost::mpl::at<Keys, boost::mpl::int_<N> >::type::key() == v)
129         return N;
130     else
131         return VariantKeyTransformCheck<In, Out, Keys, N-1>::get(v);
132 }
133
134 template <class In, class Out, class Keys, unsigned N>
135 prefix_ In senf::detail::VariantKeyTransformCheck<In,Out,Keys,N>::set(Out v)
136 {
137     if (v == N)
138         return boost::mpl::at<Keys, boost::mpl::int_<N> >::type::key();
139     else
140         return VariantKeyTransformCheck<In, Out, Keys, N-1>::set(v);
141 }
142
143 template <class In, class Out, class Keys>
144 prefix_ Out senf::detail::VariantKeyTransformCheck<In, Out, Keys, 0>::get(In v)
145 {
146     return 0;
147 }
148
149 template <class In, class Out, class Keys>
150 prefix_ In senf::detail::VariantKeyTransformCheck<In, Out, Keys, 0>::set(Out v)
151 {
152     return boost::mpl::at<Keys, boost::mpl::int_<0> >::type::key();
153 }
154
155 //-/////////////////////////////////////////////////////////////////////////////////////////////////
156 #undef prefix_
157
158 \f
159 // Local Variables:
160 // mode: c++
161 // fill-column: 100
162 // comment-column: 40
163 // c-file-style: "senf"
164 // indent-tabs-mode: nil
165 // ispell-local-dictionary: "american"
166 // compile-command: "scons -u test"
167 // End: