d95e9715a00325aa8d065dfb28fe48a5846db943
[senf.git] / 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 <boost/mpl/size.hpp>
30
31 #define prefix_ inline
32 ///////////////////////////////cti.p///////////////////////////////////////
33
34 ///////////////////////////////////////////////////////////////////////////
35 // senf::VariantParser<VariantPolicy,SENF_PARSE_VARIANT_TPL_ARGS(P)>
36
37 template <class VariantPolicy, SENF_PARSE_VARIANT_TPL_ARGS(class P)>
38 prefix_ senf::VariantParser<VariantPolicy,SENF_PARSE_VARIANT_TPL_ARGS(P)>::
39 VariantParser(data_iterator i, state_type s)
40     : PacketParserBase(i,s)
41 {}
42
43 template <class VariantPolicy, SENF_PARSE_VARIANT_TPL_ARGS(class P)>
44 prefix_ senf::VariantParser<VariantPolicy,SENF_PARSE_VARIANT_TPL_ARGS(P)>::
45 VariantParser(VariantPolicy policy, data_iterator i, state_type s)
46     : PacketParserBase(i,s), VariantPolicy(policy)
47 {}
48
49 template <class VariantPolicy, SENF_PARSE_VARIANT_TPL_ARGS(class P)>
50 prefix_ senf::PacketParserBase::size_type
51 senf::VariantParser<VariantPolicy,SENF_PARSE_VARIANT_TPL_ARGS(P)>::bytes()
52     const
53 {
54     return detail::VariantBytes< VariantParser, boost::mpl::size<parsers>::value - 1 >
55         ::bytes(*this, variant()) + VariantPolicy::bytes(i(),state());
56 }
57
58 template <class VariantPolicy, SENF_PARSE_VARIANT_TPL_ARGS(class P)>
59 prefix_ void
60 senf::VariantParser<VariantPolicy,SENF_PARSE_VARIANT_TPL_ARGS(P)>::init()
61 {
62     VariantPolicy::variant(0,i(),state());
63     get<0>().init();
64 }
65
66 template <class VariantPolicy, SENF_PARSE_VARIANT_TPL_ARGS(class P)>
67 prefix_ unsigned senf::VariantParser<VariantPolicy,SENF_PARSE_VARIANT_TPL_ARGS(P)>::variant()
68     const
69 {
70     return VariantPolicy::variant(i(),state());
71 }
72
73 template <class VariantPolicy, SENF_PARSE_VARIANT_TPL_ARGS(class P)>
74 template <unsigned N>
75 prefix_ typename boost::mpl::at<
76     typename senf::VariantParser<VariantPolicy,SENF_PARSE_VARIANT_TPL_ARGS(P)>::parsers, 
77     boost::mpl::int_<N> >::type
78 senf::VariantParser<VariantPolicy,SENF_PARSE_VARIANT_TPL_ARGS(P)>::get()
79     const
80 {
81     BOOST_ASSERT( variant() == N );
82     return typename boost::mpl::at<parsers, boost::mpl::int_<N> >::type(
83         VariantPolicy::begin(i(), state()), state() );
84 }
85
86 /////////////////////////////////////////////////////////////////////////
87 // senf::detail::VariantBytes<Variant,N>
88
89 template <class Variant, unsigned N>
90 prefix_ senf::PacketParserBase::size_type
91 senf::detail::VariantBytes<Variant,N>::bytes(Variant const & v, unsigned n)
92 {
93     if (n == N)
94         return senf::bytes(v.template get<N>());
95     else
96         return VariantBytes<Variant, N-1>::bytes(v, n);
97 }
98
99 template <class Variant>
100 prefix_ senf::PacketParserBase::size_type
101 senf::detail::VariantBytes<Variant,0>::bytes(Variant const & v, unsigned n)
102 {
103     return senf::bytes(v.template get<0>());
104 }
105
106 ///////////////////////////////cti.e///////////////////////////////////////
107 #undef prefix_
108
109 \f
110 // Local Variables:
111 // mode: c++
112 // fill-column: 100
113 // comment-column: 40
114 // c-file-style: "senf"
115 // indent-tabs-mode: nil
116 // ispell-local-dictionary: "american"
117 // compile-command: "scons -u test"
118 // End: