Move sourcecode into 'senf/' directory
[senf.git] / senf / Packets / VariantParser.cti
diff --git a/senf/Packets/VariantParser.cti b/senf/Packets/VariantParser.cti
new file mode 100644 (file)
index 0000000..d694cf6
--- /dev/null
@@ -0,0 +1,167 @@
+// $Id$
+//
+// Copyright (C) 2007
+// Fraunhofer Institute for Open Communication Systems (FOKUS)
+// Competence Center NETwork research (NET), St. Augustin, GERMANY
+//     Stefan Bund <g0dil@berlios.de>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the
+// Free Software Foundation, Inc.,
+// 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+
+/** \file
+    \brief VariantParser inline template implementation */
+
+#include "VariantParser.ih"
+
+// Custom includes
+#include "../Utils/senfassert.hh"
+#include <boost/mpl/size.hpp>
+
+#define prefix_ inline
+///////////////////////////////cti.p///////////////////////////////////////
+
+///////////////////////////////////////////////////////////////////////////
+// senf::VariantParser<AuxPolicy,Parsers>
+
+template <class AuxPolicy, class Parsers>
+prefix_ senf::VariantParser<AuxPolicy,Parsers>::
+VariantParser(data_iterator i, state_type s)
+    : PacketParserBase(i,s)
+{}
+
+template <class AuxPolicy, class Parsers>
+prefix_ senf::VariantParser<AuxPolicy,Parsers>::
+VariantParser(AuxPolicy policy, data_iterator i, state_type s)
+    : PacketParserBase(i,s), AuxPolicy(policy)
+{}
+
+template <class AuxPolicy, class Parsers>
+prefix_ senf::PacketParserBase::size_type
+senf::VariantParser<AuxPolicy,Parsers>::bytes()
+    const
+{
+    return detail::VariantBytes< VariantParser, boost::mpl::size<parsers>::value - 1 >
+        ::bytes(*this, variant()) + AuxPolicy::aux_bytes;
+}
+
+template <class AuxPolicy, class Parsers>
+prefix_ void
+senf::VariantParser<AuxPolicy,Parsers>::init()
+{
+    AuxPolicy::aux(0,i(),state());
+    get<0>().init();
+}
+
+template <class AuxPolicy, class Parsers>
+prefix_ unsigned senf::VariantParser<AuxPolicy,Parsers>::variant()
+    const
+{
+    return AuxPolicy::aux(i(),state());
+}
+
+template <class AuxPolicy, class Parsers>
+template <unsigned N>
+prefix_ typename boost::mpl::at<
+    typename senf::VariantParser<AuxPolicy,Parsers>::parsers, 
+    boost::mpl::int_<N> >::type
+senf::VariantParser<AuxPolicy,Parsers>::get()
+    const
+{
+    SENF_ASSERT( variant() == N );
+    return typename boost::mpl::at<parsers, boost::mpl::int_<N> >::type(
+        AuxPolicy::adjust(i(), state()), state() );
+}
+
+/////////////////////////////////////////////////////////////////////////
+// senf::detail::VariantBytes<Variant,N>
+
+template <class Variant, unsigned N>
+prefix_ senf::PacketParserBase::size_type
+senf::detail::VariantBytes<Variant,N>::bytes(Variant const & v, unsigned n)
+{
+    if (n == N)
+        return senf::bytes(v.template get<N>());
+    else
+        return VariantBytes<Variant, N-1>::bytes(v, n);
+}
+
+template <class Variant>
+prefix_ senf::PacketParserBase::size_type
+senf::detail::VariantBytes<Variant,0>::bytes(Variant const & v, unsigned n)
+{
+    return senf::bytes(v.template get<0>());
+}
+
+///////////////////////////////////////////////////////////////////////////
+// senf::detail::VariantKeyTransform<T,Keys>
+
+template <class T, class Keys>
+prefix_ unsigned senf::detail::VariantKeyTransform<T,Keys>::get(input_type v)
+{
+    return VariantKeyTransformCheck<
+        input_type, value_type, Keys, boost::mpl::size<Keys>::type::value-1>::get(v);
+}
+
+template <class T, class Keys>
+prefix_ typename senf::detail::VariantKeyTransform<T,Keys>::input_type
+senf::detail::VariantKeyTransform<T,Keys>::set(unsigned v)
+{
+    return VariantKeyTransformCheck<
+        input_type, value_type, Keys, boost::mpl::size<Keys>::type::value-1>::set(v);
+}
+
+template <class In, class Out, class Keys, unsigned N>
+prefix_ Out senf::detail::VariantKeyTransformCheck<In,Out,Keys,N>::get(In v)
+{
+    if (boost::mpl::at<Keys, boost::mpl::int_<N> >::type::key() == v)
+        return N;
+    else
+        return VariantKeyTransformCheck<In, Out, Keys, N-1>::get(v);
+}
+
+template <class In, class Out, class Keys, unsigned N>
+prefix_ In senf::detail::VariantKeyTransformCheck<In,Out,Keys,N>::set(Out v)
+{
+    if (v == N)
+        return boost::mpl::at<Keys, boost::mpl::int_<N> >::type::key();
+    else
+        return VariantKeyTransformCheck<In, Out, Keys, N-1>::set(v);
+}
+
+template <class In, class Out, class Keys>
+prefix_ Out senf::detail::VariantKeyTransformCheck<In, Out, Keys, 0>::get(In v)
+{
+    return 0;
+}
+
+template <class In, class Out, class Keys>
+prefix_ In senf::detail::VariantKeyTransformCheck<In, Out, Keys, 0>::set(Out v)
+{
+    return boost::mpl::at<Keys, boost::mpl::int_<0> >::type::key();
+}
+
+///////////////////////////////cti.e///////////////////////////////////////
+#undef prefix_
+
+\f
+// Local Variables:
+// mode: c++
+// fill-column: 100
+// comment-column: 40
+// c-file-style: "senf"
+// indent-tabs-mode: nil
+// ispell-local-dictionary: "american"
+// compile-command: "scons -u test"
+// End: