comparison DEPENDENCIES/generic/include/boost/spirit/home/phoenix/scope/dynamic.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
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_DYNAMIC_HPP
9 #define PHOENIX_SCOPE_DYNAMIC_HPP
10
11 #include <boost/spirit/home/phoenix/core/limits.hpp>
12 #include <boost/fusion/include/at.hpp>
13 #include <boost/fusion/include/vector.hpp>
14 #include <boost/spirit/home/phoenix/core/actor.hpp>
15
16 #include <boost/preprocessor/cat.hpp>
17 #include <boost/preprocessor/punctuation/comma_if.hpp>
18 #include <boost/preprocessor/seq/for_each_i.hpp>
19 #include <boost/preprocessor/tuple/elem.hpp>
20 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
21 #include <boost/preprocessor/repetition/enum_params.hpp>
22 #include <boost/preprocessor/cat.hpp>
23 #include <boost/preprocessor/inc.hpp>
24
25 #include <boost/noncopyable.hpp>
26 #include <boost/assert.hpp>
27
28 #define PHOENIX_DYNAMIC_MEMBER(z, n, data) \
29 typedef actor<dynamic_member<n, self_type> > \
30 BOOST_PP_CAT(member, BOOST_PP_INC(n));
31
32 namespace boost { namespace phoenix
33 {
34 template <typename DynamicScope>
35 struct dynamic_frame : noncopyable
36 {
37 typedef typename DynamicScope::tuple_type tuple_type;
38
39 dynamic_frame(DynamicScope const& scope)
40 : tuple()
41 , save(scope.frame)
42 , scope(scope)
43 {
44 scope.frame = this;
45 }
46
47 template <typename Tuple>
48 dynamic_frame(DynamicScope const& scope, Tuple const& init)
49 : tuple(init)
50 , save(scope.frame)
51 , scope(scope)
52 {
53 scope.frame = this;
54 }
55
56 ~dynamic_frame()
57 {
58 scope.frame = save;
59 }
60
61 tuple_type& data() { return tuple; }
62 tuple_type const& data() const { return tuple; }
63
64 private:
65
66 tuple_type tuple;
67 dynamic_frame* save;
68 DynamicScope const& scope;
69 };
70
71 template <int N, typename DynamicScope>
72 struct dynamic_member
73 {
74 typedef mpl::false_ no_nullary;
75 typedef typename DynamicScope::tuple_type tuple_type;
76
77 dynamic_member(DynamicScope const& scope)
78 : scope(scope) {}
79
80 template <typename Env>
81 struct result
82 {
83 typedef typename
84 fusion::result_of::at_c<tuple_type, N>::type
85 type;
86 };
87
88 template <typename Env>
89 typename result<Env>::type
90 eval(Env const& /*env*/) const
91 {
92 BOOST_ASSERT(scope.frame != 0);
93 return fusion::at_c<N>(scope.frame->data());
94 }
95
96 private:
97
98 DynamicScope const& scope;
99 };
100
101 template <BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(PHOENIX_DYNAMIC_LIMIT, typename T, void_)>
102 struct dynamic : noncopyable
103 {
104 typedef fusion::vector<BOOST_PP_ENUM_PARAMS(PHOENIX_DYNAMIC_LIMIT, T)> tuple_type;
105 typedef dynamic<BOOST_PP_ENUM_PARAMS(PHOENIX_DYNAMIC_LIMIT, T)> self_type;
106 typedef dynamic_frame<self_type> dynamic_frame_type;
107
108 dynamic()
109 : frame(0) {}
110
111 BOOST_PP_REPEAT(PHOENIX_DYNAMIC_LIMIT, PHOENIX_DYNAMIC_MEMBER, _)
112
113 private:
114
115 template <int N, typename DynamicScope>
116 friend struct dynamic_member;
117
118 template <typename DynamicScope>
119 friend struct dynamic_frame;
120
121 mutable dynamic_frame_type* frame;
122 };
123 }}
124
125 #if defined(BOOST_MSVC)
126 # pragma warning(push)
127 # pragma warning(disable:4355)
128 #endif
129
130 /*
131 PHOENIX_DYNAMIC macro(name, type-name sequence)
132 Example:
133
134 PHOENIX_DYNAMIC(
135 my_dynamic,
136 (int, num)
137 (std::string, message)
138 (double, real)
139 );
140
141 which expands to:
142
143 struct my_dynamic : ::boost::phoenix::dynamic<int, std::string, double>
144 {
145 my_dynamic() : num(*this), message(*this), real(*this) {}
146
147 member1 num;
148 member2 message;
149 member3 real;
150 };
151
152 PHOENIX_DYNAMIC takes the input (containing a binary sequence)
153 and converts that sequence to a unary sequence of
154 binary tuples and passes it on to PHOENIX_DYNAMIC_I.
155
156 Thanks to Paul Mensonides for the PP macro help
157 */
158
159 #define PHOENIX_DYNAMIC(name, bseq) \
160 PHOENIX_DYNAMIC_I(name, BOOST_PP_CAT(PHOENIX_DYNAMIC_X bseq, 0)) \
161
162 #define PHOENIX_DYNAMIC_X(x, y) ((x, y)) PHOENIX_DYNAMIC_Y
163 #define PHOENIX_DYNAMIC_Y(x, y) ((x, y)) PHOENIX_DYNAMIC_X
164 #define PHOENIX_DYNAMIC_X0
165 #define PHOENIX_DYNAMIC_Y0
166
167 // PHOENIX_DYNAMIC_I generates the overarching structure and uses
168 // SEQ_FOR_EACH_I to generate the "linear" substructures.
169
170 #define PHOENIX_DYNAMIC_I(name, seq) \
171 struct name : \
172 ::boost::phoenix::dynamic< \
173 BOOST_PP_SEQ_FOR_EACH_I(PHOENIX_DYNAMIC_A, ~, seq)> { \
174 name() : BOOST_PP_SEQ_FOR_EACH_I(PHOENIX_DYNAMIC_B, ~, seq) {} \
175 BOOST_PP_SEQ_FOR_EACH_I(PHOENIX_DYNAMIC_C, ~, seq) \
176 } \
177
178 #define PHOENIX_DYNAMIC_A(r, _, i, xy) \
179 BOOST_PP_COMMA_IF(i) BOOST_PP_TUPLE_ELEM(2, 0, xy) \
180
181 #define PHOENIX_DYNAMIC_B(r, _, i, xy) \
182 BOOST_PP_COMMA_IF(i) BOOST_PP_TUPLE_ELEM(2, 1, xy)(*this) \
183
184 #define PHOENIX_DYNAMIC_C(r, _, i, xy) \
185 BOOST_PP_CAT(member, BOOST_PP_INC(i)) BOOST_PP_TUPLE_ELEM(2, 1, xy); \
186
187 #undef PHOENIX_DYNAMIC_MEMBER
188
189 #if defined(BOOST_MSVC)
190 # pragma warning(pop)
191 #endif
192
193 #endif