switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / VariantParser.cti
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief VariantParser inline template implementation */
30
31 #include "VariantParser.ih"
32
33 // Custom includes
34 #include <senf/Utils/senfassert.hh>
35 #include <boost/mpl/size.hpp>
36
37 #define prefix_ inline
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
41 // senf::VariantParser<AuxPolicy,Parsers>
42
43 template <class AuxPolicy, class Parsers>
44 prefix_ senf::VariantParser<AuxPolicy,Parsers>::
45 VariantParser(data_iterator i, state_type s)
46     : PacketParserBase(i,s)
47 {}
48
49 template <class AuxPolicy, class Parsers>
50 prefix_ senf::VariantParser<AuxPolicy,Parsers>::
51 VariantParser(AuxPolicy policy, data_iterator i, state_type s)
52     : PacketParserBase(i,s), AuxPolicy(policy)
53 {}
54
55 template <class AuxPolicy, class Parsers>
56 prefix_ senf::PacketParserBase::size_type
57 senf::VariantParser<AuxPolicy,Parsers>::bytes()
58     const
59 {
60     return detail::VariantBytes< VariantParser, boost::mpl::size<parsers>::value - 1 >
61         ::bytes(*this, variant()) + AuxPolicy::aux_bytes;
62 }
63
64 template <class AuxPolicy, class Parsers>
65 prefix_ void
66 senf::VariantParser<AuxPolicy,Parsers>::init()
67 {
68     AuxPolicy::aux(0,i(),state());
69     get<0>().init();
70 }
71
72 template <class AuxPolicy, class Parsers>
73 prefix_ unsigned senf::VariantParser<AuxPolicy,Parsers>::variant()
74     const
75 {
76     return AuxPolicy::aux(i(),state());
77 }
78
79 template <class AuxPolicy, class Parsers>
80 template <unsigned N>
81 prefix_ typename boost::mpl::at<
82     typename senf::VariantParser<AuxPolicy,Parsers>::parsers,
83     boost::mpl::int_<N> >::type
84 senf::VariantParser<AuxPolicy,Parsers>::get()
85     const
86 {
87     SENF_ASSERT( variant() == N, "Access to non-active variant member" );
88     return typename boost::mpl::at<parsers, boost::mpl::int_<N> >::type(
89         AuxPolicy::adjust(i(), state()), state() );
90 }
91
92 //-/////////////////////////////////////////////////////////////////////////////////////////////////
93 // senf::detail::VariantBytes<Variant,N>
94
95 template <class Variant, unsigned N>
96 prefix_ senf::PacketParserBase::size_type
97 senf::detail::VariantBytes<Variant,N>::bytes(Variant const & v, unsigned n)
98 {
99     if (n == N)
100         return senf::bytes(v.template get<N>());
101     else
102         return VariantBytes<Variant, N-1>::bytes(v, n);
103 }
104
105 template <class Variant>
106 prefix_ senf::PacketParserBase::size_type
107 senf::detail::VariantBytes<Variant,0>::bytes(Variant const & v, unsigned n)
108 {
109     return senf::bytes(v.template get<0>());
110 }
111
112 //-/////////////////////////////////////////////////////////////////////////////////////////////////
113 // senf::detail::VariantKeyTransform<T,Keys>
114
115 template <class T, class Keys>
116 prefix_ unsigned senf::detail::VariantKeyTransform<T,Keys>::get(input_type v)
117 {
118     return VariantKeyTransformCheck<
119         input_type, value_type, Keys, boost::mpl::size<Keys>::type::value-1>::get(v);
120 }
121
122 template <class T, class Keys>
123 prefix_ typename senf::detail::VariantKeyTransform<T,Keys>::input_type
124 senf::detail::VariantKeyTransform<T,Keys>::set(unsigned v)
125 {
126     return VariantKeyTransformCheck<
127         input_type, value_type, Keys, boost::mpl::size<Keys>::type::value-1>::set(v);
128 }
129
130 template <class In, class Out, class Keys, unsigned N>
131 prefix_ Out senf::detail::VariantKeyTransformCheck<In,Out,Keys,N>::get(In v)
132 {
133     if (boost::mpl::at<Keys, boost::mpl::int_<N> >::type::key() == v)
134         return N;
135     else
136         return VariantKeyTransformCheck<In, Out, Keys, N-1>::get(v);
137 }
138
139 template <class In, class Out, class Keys, unsigned N>
140 prefix_ In senf::detail::VariantKeyTransformCheck<In,Out,Keys,N>::set(Out v)
141 {
142     if (v == N)
143         return boost::mpl::at<Keys, boost::mpl::int_<N> >::type::key();
144     else
145         return VariantKeyTransformCheck<In, Out, Keys, N-1>::set(v);
146 }
147
148 template <class In, class Out, class Keys>
149 prefix_ Out senf::detail::VariantKeyTransformCheck<In, Out, Keys, 0>::get(In v)
150 {
151     return 0;
152 }
153
154 template <class In, class Out, class Keys>
155 prefix_ In senf::detail::VariantKeyTransformCheck<In, Out, Keys, 0>::set(Out v)
156 {
157     return boost::mpl::at<Keys, boost::mpl::int_<0> >::type::key();
158 }
159
160 //-/////////////////////////////////////////////////////////////////////////////////////////////////
161 #undef prefix_
162
163 \f
164 // Local Variables:
165 // mode: c++
166 // fill-column: 100
167 // comment-column: 40
168 // c-file-style: "senf"
169 // indent-tabs-mode: nil
170 // ispell-local-dictionary: "american"
171 // compile-command: "scons -u test"
172 // End: