PageRenderTime 31ms CodeModel.GetById 18ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/spirit/home/phoenix/statement/switch.hpp

http://hadesmem.googlecode.com/
C++ Header | 164 lines | 135 code | 21 blank | 8 comment | 0 complexity | f87deff109e5b264f2fcaee239013235 MD5 | raw file
  1/*=============================================================================
  2    Copyright (c) 2001-2007 Joel de Guzman
  3
  4    Distributed under the Boost Software License, Version 1.0. (See accompanying 
  5    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6==============================================================================*/
  7#ifndef PHOENIX_STATEMENT_SWITCH_HPP
  8#define PHOENIX_STATEMENT_SWITCH_HPP
  9
 10#include <boost/spirit/home/phoenix/core/composite.hpp>
 11#include <boost/spirit/home/phoenix/core/compose.hpp>
 12#include <boost/spirit/home/phoenix/core/nothing.hpp>
 13#include <boost/spirit/home/phoenix/statement/detail/switch_eval.hpp>
 14#include <boost/spirit/home/phoenix/statement/detail/switch.hpp>
 15#include <boost/utility/enable_if.hpp>
 16#include <boost/mpl/not.hpp>
 17
 18namespace boost { namespace phoenix
 19{
 20    template <typename Derived, typename Actor>
 21    struct switch_case_base 
 22    {
 23        typedef Derived derived_t;
 24        typedef Actor actor_t;
 25        typedef typename Actor::no_nullary no_nullary;
 26
 27        template <typename Env>
 28        struct result
 29        {
 30            typedef typename Actor::eval_type::template result<Env>::type type;
 31        };
 32
 33        switch_case_base(Actor const& actor)
 34            : actor(actor) {}
 35
 36        template <typename Env>
 37        typename result<Env>::type
 38        eval(Env const& env) const
 39        {
 40            return actor.eval(env);
 41        }
 42
 43        Actor actor;
 44    };
 45
 46    template <typename Actor, typename K, K Value>
 47    struct switch_case : switch_case_base<switch_case<Actor, K, Value>, Actor>
 48    {
 49        typedef switch_case_base<switch_case<Actor, K, Value>, Actor> base_t;
 50        static K const value = Value;
 51        static bool const is_default = false;
 52
 53        switch_case(Actor const& actor)
 54            : base_t(actor) {}
 55    };
 56
 57    template <typename Actor>
 58    struct default_case : switch_case_base<default_case<Actor>, Actor>
 59    {
 60        typedef switch_case_base<default_case<Actor>, Actor> base_t;
 61        static bool const is_default = true;
 62
 63        default_case(Actor const& actor)
 64            : base_t(actor) {}
 65    };
 66
 67    template <typename Cond>
 68    struct switch_gen
 69    {
 70        switch_gen(Cond const& cond)
 71            : cond(cond) {}
 72
 73        template <typename Cases>
 74        typename lazy_enable_if<
 75            fusion::traits::is_sequence<Cases>
 76          , detail::switch_composite_actor<Cond, Cases>
 77        >::type
 78        operator[](Cases const& cases) const
 79        {
 80            typedef typename
 81                detail::switch_composite<Cond, Cases>
 82            switch_composite;
 83            return switch_composite::eval(cond, cases);
 84        }
 85
 86        template <typename D, typename A>
 87        actor<typename detail::
 88            switch_composite<Cond, fusion::vector<actor<D> > >::type>
 89        operator[](switch_case_base<D, A> const& case_) const
 90        {
 91            typedef typename
 92                detail::switch_composite<Cond, fusion::vector<actor<D> > >
 93            switch_composite;
 94            return switch_composite::eval(cond,
 95                fusion::vector<actor<D> >(static_cast<D const&>(case_)));
 96        }
 97
 98        Cond cond;
 99    };
100
101    template <typename Cond>
102    inline switch_gen<typename as_actor<Cond>::type>
103    switch_(Cond const& cond)
104    {
105        return switch_gen<typename as_actor<Cond>::type>(
106            as_actor<Cond>::convert(cond));
107    }
108
109    template <int N, typename A0>
110    switch_case<typename as_actor<A0>::type, int, N>
111    case_(A0 const& _0)
112    {
113        return switch_case<typename as_actor<A0>::type, int, N>
114            (as_actor<A0>::convert(_0));
115    }
116
117    template <typename A0>
118    default_case<typename as_actor<A0>::type>
119    default_(A0 const& _0)
120    {
121        return default_case<typename as_actor<A0>::type>
122            (as_actor<A0>::convert(_0));
123    }
124
125    template <typename D0, typename A0, typename D1, typename A1>
126    inline typename detail::compose_case_a<D0, D1>::type
127    operator,(
128        switch_case_base<D0, A0> const& _0
129      , switch_case_base<D1, A1> const& _1
130    )
131    {
132        return detail::compose_case_a<D0, D1>::eval(
133            static_cast<D0 const&>(_0)
134          , static_cast<D1 const&>(_1)
135        );
136    }
137
138    template <typename Seq, typename D, typename A>
139    inline typename
140        lazy_enable_if<
141            fusion::traits::is_sequence<Seq>
142          , detail::compose_case_b<Seq, D>
143        >::type
144    operator,(Seq const& seq, switch_case_base<D, A> const& case_)
145    {
146        return detail::compose_case_b<Seq, D>::eval(
147            seq, static_cast<D const&>(case_));
148    }
149
150    // Implementation of routines in detail/switch.hpp that depend on
151    // the completeness of default_case.
152    namespace detail {
153        template <typename Cases>
154        typename ensure_default<Cases>::type
155        ensure_default<Cases>::eval(Cases const& cases, mpl::false_)
156        {
157            actor<default_case<actor<null_actor> > > default_
158              = default_case<actor<null_actor> >(nothing);
159            return fusion::push_front(cases, default_);
160        }
161    }
162}}
163
164#endif