PageRenderTime 46ms CodeModel.GetById 12ms app.highlight 29ms RepoModel.GetById 2ms app.codeStats 0ms

/src/contrib/boost/spirit/home/phoenix/scope/lambda.hpp

http://pythonocc.googlecode.com/
C++ Header | 176 lines | 144 code | 24 blank | 8 comment | 0 complexity | 9b37270d3d2f179d61bb0a9cd817b1f4 MD5 | raw file
  1/*=============================================================================
  2    Copyright (c) 2001-2007 Joel de Guzman
  3    Copyright (c) 2004 Daniel Wallin
  4
  5    Distributed under the Boost Software License, Version 1.0. (See accompanying 
  6    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7==============================================================================*/
  8#ifndef PHOENIX_SCOPE_LAMBDA_HPP
  9#define PHOENIX_SCOPE_LAMBDA_HPP
 10
 11#include <boost/spirit/home/phoenix/core/limits.hpp>
 12#include <boost/spirit/home/phoenix/core/composite.hpp>
 13#include <boost/spirit/home/phoenix/scope/scoped_environment.hpp>
 14#include <boost/spirit/home/phoenix/scope/detail/local_variable.hpp>
 15#include <boost/spirit/home/phoenix/detail/local_reference.hpp>
 16#include <boost/spirit/home/phoenix/core/actor.hpp>
 17#include <boost/fusion/include/transform.hpp>
 18#include <boost/fusion/include/as_vector.hpp>
 19
 20namespace boost { namespace phoenix
 21{
 22    template <typename Base, typename OuterEnv, typename Locals, typename Map>
 23    struct lambda_eval : Base
 24    {
 25        template <typename Env>
 26        struct result
 27        {
 28            typedef typename Base::template
 29                result<scoped_environment<Env, OuterEnv, Locals, Map> >::type
 30            result_type;
 31
 32            typedef typename 
 33                detail::unwrap_local_reference<result_type>::type 
 34            type;
 35        };
 36
 37        lambda_eval(
 38            Base const& base
 39          , OuterEnv const& outer_env
 40          , Locals const& locals)
 41            : Base(base)
 42            , outer_env(outer_env)
 43            , locals(locals) {}
 44
 45        template <typename Env>
 46        typename result<Env>::type
 47        eval(Env const& env) const
 48        {
 49            typedef typename result<Env>::type RT;
 50            return RT(Base::eval(
 51                scoped_environment<Env, OuterEnv, Locals, Map>(
 52                    env, outer_env, locals)));
 53        }
 54
 55        OuterEnv outer_env;
 56        mutable Locals locals;
 57    };
 58    
 59    template <typename Base, typename Vars, typename Map>
 60    struct lambda_actor
 61    {
 62        typedef typename
 63            mpl::fold<
 64                Vars
 65              , mpl::false_
 66              , detail::compute_no_nullary
 67            >::type
 68        no_nullary;
 69
 70        template <typename Env>
 71        struct result
 72        {
 73            typedef typename 
 74                fusion::result_of::as_vector<
 75                    typename fusion::result_of::transform<
 76                        Vars
 77                      , detail::initialize_local<Env>
 78                    >::type
 79                >::type 
 80            locals_type;
 81
 82            typedef actor<lambda_eval<Base, Env, locals_type, Map> > type;
 83        };
 84
 85        lambda_actor(Base const& f, Vars const& vars)
 86            : f(f), vars(vars) {}
 87
 88        template <typename Env>
 89        typename result<Env>::type
 90        eval(Env const& env) const
 91        {
 92            typedef typename result<Env>::type result_type;
 93            
 94            return result_type(
 95                f, env, fusion::as_vector(
 96                    fusion::transform(
 97                        vars
 98                      , detail::initialize_local<Env>(env)
 99                    )));
100        }
101
102        Base f;
103        Vars vars;
104    };
105    
106    template <typename Vars, typename Map>
107    struct lambda_actor_gen
108    {
109        template <typename Base>
110        actor<lambda_actor<Base, Vars, Map> > const
111        operator[](actor<Base> const& f) const
112        {
113            return lambda_actor<Base, Vars, Map>(f, vars);
114        }
115
116        lambda_actor_gen(Vars const& vars)
117            : vars(vars) {}
118
119        Vars vars;
120    };
121
122    template <typename Key>
123    struct local_variable; // forward
124    struct assign_eval; // forward
125
126    struct lambda_gen 
127        : lambda_actor_gen<
128            fusion::vector<>
129          , detail::map_local_index_to_tuple<> >
130    {
131        typedef 
132            lambda_actor_gen<
133                fusion::vector<>
134              , detail::map_local_index_to_tuple<> >
135        base_type;
136
137        lambda_gen()
138            : base_type(fusion::vector<>())
139        {
140        }
141
142        template <typename K0, typename V0>
143        lambda_actor_gen<
144            fusion::vector<V0>
145          , detail::map_local_index_to_tuple<K0>
146        >
147        operator()(
148            actor<composite<assign_eval, fusion::vector<local_variable<K0>, V0> > > const& a0
149        ) const
150        {
151            return fusion::vector<V0>(fusion::at_c<1>(a0));
152        }
153    
154        template <typename K0, typename K1, typename V0, typename V1>
155        lambda_actor_gen<
156            fusion::vector<V0, V1>
157          , detail::map_local_index_to_tuple<K0, K1>
158        >
159        operator()(
160            actor<composite<assign_eval, fusion::vector<local_variable<K0>, V0> > > const& a0
161          , actor<composite<assign_eval, fusion::vector<local_variable<K1>, V1> > > const& a1
162        ) const
163        {
164            return fusion::vector<V0, V1>(fusion::at_c<1>(a0), fusion::at_c<1>(a1));
165        }
166        
167        // Bring in the rest...
168        #define PHOENIX_LOCAL_GEN_NAME lambda_actor_gen
169        #include <boost/spirit/home/phoenix/scope/detail/local_gen.hpp>
170        #undef PHOENIX_LOCAL_GEN_NAME
171    };
172
173    lambda_gen const lambda = lambda_gen();
174}}
175
176#endif