Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / phoenix / scope / dynamic.hpp
1 /*==============================================================================
2     Copyright (c) 2001-2010 Joel de Guzman
3     Copyright (c) 2004 Daniel Wallin
4     Copyright (c) 2010 Thomas Heller
5
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 BOOST_PHOENIX_SCOPE_DYNAMIC_HPP
10 #define BOOST_PHOENIX_SCOPE_DYNAMIC_HPP
11
12 #include <boost/phoenix/core/limits.hpp>
13 #include <boost/assert.hpp>
14 #include <boost/noncopyable.hpp>
15 #include <boost/fusion/sequence/intrinsic/at.hpp>
16 #include <boost/phoenix/core/expression.hpp>
17 #include <boost/phoenix/core/meta_grammar.hpp>
18 #include <boost/phoenix/core/call.hpp>
19 #include <boost/phoenix/support/iterate.hpp>
20 #include <boost/preprocessor/seq/for_each.hpp>
21 #include <boost/preprocessor/seq/fold_left.hpp>
22 #include <boost/preprocessor/punctuation/comma.hpp>
23 #include <boost/type_traits/remove_pointer.hpp>
24
25 #define BOOST_PHOENIX_DYNAMIC_TEMPLATE_PARAMS(R, DATA, I, ELEM)                 \
26       BOOST_PP_COMMA_IF(I) BOOST_PP_TUPLE_ELEM(2, 0, ELEM)                      \
27 /**/
28
29 #define BOOST_PHOENIX_DYNAMIC_CTOR_INIT(R, DATA, I, ELEM)                       \
30     BOOST_PP_COMMA_IF(I) BOOST_PP_TUPLE_ELEM(2, 1, ELEM)(init<I>(this))         \
31 /**/
32
33 #define BOOST_PHOENIX_DYNAMIC_MEMBER(R, DATA, I, ELEM)                          \
34     BOOST_PP_CAT(member, BOOST_PP_INC(I)) BOOST_PP_TUPLE_ELEM(2, 1, ELEM);      \
35 /**/
36
37 #define BOOST_PHOENIX_DYNAMIC_FILLER_0(X, Y)                                    \
38     ((X, Y)) BOOST_PHOENIX_DYNAMIC_FILLER_1                                     \
39 /**/
40
41 #define BOOST_PHOENIX_DYNAMIC_FILLER_1(X, Y)                                    \
42     ((X, Y)) BOOST_PHOENIX_DYNAMIC_FILLER_0                                     \
43 /**/
44
45 #define BOOST_PHOENIX_DYNAMIC_FILLER_0_END
46 #define BOOST_PHOENIX_DYNAMIC_FILLER_1_END
47
48 #define BOOST_PHOENIX_DYNAMIC_BASE(NAME, MEMBER)                                \
49 struct NAME                                                                     \
50     : ::boost::phoenix::dynamic<                                                \
51         BOOST_PP_SEQ_FOR_EACH_I(                                                \
52                 BOOST_PHOENIX_DYNAMIC_TEMPLATE_PARAMS                           \
53               , _                                                               \
54               , MEMBER)                                                         \
55     >                                                                           \
56 {                                                                               \
57     NAME()                                                                      \
58         : BOOST_PP_SEQ_FOR_EACH_I(BOOST_PHOENIX_DYNAMIC_CTOR_INIT, _, MEMBER)   \
59     {}                                                                          \
60                                                                                 \
61     BOOST_PP_SEQ_FOR_EACH_I(BOOST_PHOENIX_DYNAMIC_MEMBER, _, MEMBER)            \
62 }                                                                               \
63 /**/
64
65 #define BOOST_PHOENIX_DYNAMIC(NAME, MEMBER)                                     \
66     BOOST_PHOENIX_DYNAMIC_BASE(                                                 \
67         NAME                                                                    \
68       , BOOST_PP_CAT(BOOST_PHOENIX_DYNAMIC_FILLER_0 MEMBER,_END)                \
69     )                                                                           \
70 /**/
71
72 BOOST_PHOENIX_DEFINE_EXPRESSION(
73     (boost)(phoenix)(dynamic_member)
74   , (proto::terminal<proto::_>)
75     (proto::terminal<proto::_>)
76 )
77
78 namespace boost { namespace phoenix
79 {
80     template <typename DynamicScope>
81     struct dynamic_frame : noncopyable
82     {
83         typedef typename DynamicScope::tuple_type tuple_type;
84
85         dynamic_frame(DynamicScope const& s)
86             : tuple()
87             , save(s.frame)
88             , scope(s)
89         {
90             scope.frame = this;
91         }
92
93         template <typename Tuple>
94         dynamic_frame(DynamicScope const& s, Tuple const& init)
95             : tuple(init)
96             , save(s.frame)
97             , scope(s)
98         {
99             scope.frame = this;
100         }
101
102         ~dynamic_frame()
103         {
104             scope.frame = save;
105         }
106
107         tuple_type& data() { return tuple; }
108         tuple_type const& data() const { return tuple; }
109
110         private:
111             tuple_type tuple;
112             dynamic_frame *save;
113             DynamicScope const& scope;
114     };
115
116     struct dynamic_member_eval
117     {
118         template <typename Sig>
119         struct result;
120
121         template <typename This, typename N, typename Scope, typename Context>
122         struct result<This(N, Scope, Context)>
123         {
124             typedef
125                 typename boost::remove_pointer<
126                     typename proto::detail::uncvref<
127                         typename proto::result_of::value<Scope>::type
128                     >::type
129                 >::type
130                 scope_type;
131             typedef 
132                 typename scope_type::dynamic_frame_type::tuple_type
133                 tuple_type;
134
135             typedef
136                 typename fusion::result_of::at_c<
137                     tuple_type
138                   , proto::detail::uncvref<
139                         typename proto::result_of::value<N>::type
140                     >::type::value
141                 >::type
142                 type;
143
144         };
145
146         template <typename N, typename Scope, typename Context>
147         typename result<dynamic_member_eval(N, Scope, Context)>::type
148         operator()(N, Scope s, Context const &) const
149         {
150             return
151                 fusion::at_c<
152                     proto::detail::uncvref<
153                         typename proto::result_of::value<N>::type
154                     >::type::value
155                 >(
156                     proto::value(s)->frame->data()
157                 );
158         }
159     };
160
161     template <typename Dummy>
162     struct default_actions::when<rule::dynamic_member, Dummy>
163         : call<dynamic_member_eval>
164     {};
165     
166     template <
167         BOOST_PHOENIX_typename_A_void(BOOST_PHOENIX_DYNAMIC_LIMIT)
168       , typename Dummy = void
169     >
170     struct dynamic;
171     
172     // Bring in the rest ...
173     #include <boost/phoenix/scope/detail/dynamic.hpp>
174 }}
175
176 #endif