1 /*=============================================================================
2 Copyright (c) 2001-2007 Joel de Guzman
3 Copyright (c) 2004 Daniel Wallin
4 Copyright (c) 2011 Thomas Heller
6 Distributed under the Boost Software License, Version 1.0. (See accompanying
7 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
9 #ifndef PHOENIX_SCOPE_DETAIL_LOCAL_VARIABLE_HPP
10 #define PHOENIX_SCOPE_DETAIL_LOCAL_VARIABLE_HPP
12 #include <boost/mpl/int.hpp>
13 #include <boost/mpl/bool.hpp>
14 #include <boost/mpl/eval_if.hpp>
15 #include <boost/mpl/identity.hpp>
16 #include <boost/fusion/include/at.hpp>
17 #include <boost/fusion/include/value_at.hpp>
18 #include <boost/preprocessor/enum.hpp>
19 #include <boost/preprocessor/repeat.hpp>
20 #include <boost/type_traits/remove_reference.hpp>
21 #include <boost/type_traits/is_reference.hpp>
23 #define BOOST_PHOENIX_MAP_LOCAL_TEMPLATE_PARAM(z, n, data) \
24 typename T##n = unused<n>
26 #define BOOST_PHOENIX_MAP_LOCAL_DISPATCH(z, n, data) \
27 typedef char(&result##n)[n+2]; \
28 static result##n get(T##n*);
30 namespace boost { namespace phoenix
32 template <typename Env, typename OuterEnv, typename Locals, typename Map>
33 struct scoped_environment;
37 template <typename Key>
45 template <typename Locals, typename Context>
46 struct initialize_locals;
48 template <typename Context>
49 struct initialize_locals<vector0<>, Context>
51 typedef vector0<> type;
55 typename boost::phoenix::result_of::eval< \
62 template <BOOST_PHOENIX_typename_A(N), typename Context> \
63 struct initialize_locals< \
64 BOOST_PP_CAT(vector, N)< \
71 BOOST_PP_CAT(vector, N)< \
72 BOOST_PP_ENUM(N, M1, _) \
77 BOOST_PP_REPEAT_FROM_TO(1, BOOST_PHOENIX_LIMIT, M0, _)
81 template <typename Context>
83 initialize_locals(vector0<> const &, Context const &)
89 eval(locals. BOOST_PP_CAT(a, N), ctx) \
93 template <BOOST_PHOENIX_typename_A(N), typename Context> \
94 BOOST_PP_CAT(vector, N)<BOOST_PP_ENUM(N, M1, _)> \
96 BOOST_PP_CAT(vector, N)<BOOST_PHOENIX_A(N)> const & locals \
97 , Context const & ctx \
100 BOOST_PP_CAT(vector, N)<BOOST_PP_ENUM(N, M1, _)> vars \
101 = {BOOST_PP_ENUM(N, M2, _)}; \
105 BOOST_PP_REPEAT_FROM_TO(1, BOOST_PHOENIX_LIMIT, M0, _)
115 BOOST_PHOENIX_LOCAL_LIMIT
116 , BOOST_PHOENIX_MAP_LOCAL_TEMPLATE_PARAM
120 struct map_local_index_to_tuple
122 typedef char(¬_found)[1];
123 static not_found get(...);
125 BOOST_PP_REPEAT(BOOST_PHOENIX_LOCAL_LIMIT, BOOST_PHOENIX_MAP_LOCAL_DISPATCH, _)
129 T* generate_pointer();
131 template <typename Map, typename Tag>
134 BOOST_STATIC_CONSTANT(int,
136 static_cast<int>((sizeof(Map::get(generate_pointer<Tag>()))) / sizeof(char)) - 2
139 // if value == -1, Tag is not found
140 typedef mpl::int_<value> type;
144 template <typename Local, typename Env>
147 template <typename Local, typename Env>
151 apply_local<Local, typename Env::outer_env_type>::type
155 template <typename Locals, int Index>
156 struct get_local_or_void
160 Index < Locals::size_value
161 , fusion::result_of::at_c<Locals, Index>
162 , mpl::identity<fusion::void_>
167 template <typename Local, typename Env, int Index>
168 struct get_local_from_index
173 , outer_local<Local, Env>
174 , get_local_or_void<typename Env::locals_type, Index>
179 template <typename Local, typename Env>
182 static const int index_value = get_index<typename Env::map_type, Local>::value;
185 get_local_from_index<Local, Env, index_value>::type
189 template <typename Local, typename Env>
192 // $$$ TODO: static assert that Env is a scoped_environment $$$
193 typedef typename get_local<Local, Env>::type type;
196 template <typename Key>
199 template <typename RT, int Index, typename Env>
201 get(Env const& env, mpl::false_)
203 return RT(fusion::at_c<Index>(env.locals));
206 template <typename RT, int Index, typename Env>
208 get(Env const& env, mpl::true_)
210 static const int index_value = get_index<typename Env::outer_env_type::map_type, detail::local<Key> >::value;
212 return get<RT, index_value>(
214 , mpl::bool_<index_value == -1>());
217 template <typename RT, int Index, typename Env>
221 return get<RT, Index>(
223 , mpl::bool_<Index == -1>());
229 #undef BOOST_PHOENIX_MAP_LOCAL_TEMPLATE_PARAM
230 #undef BOOST_PHOENIX_MAP_LOCAL_DISPATCH