1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
//[ VirtualMember
// Copyright 2008 Eric Niebler. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// This example demonstrates how to use BOOST_PROTO_EXTENDS_MEMBERS()
// to add "virtual" data members to expressions within a domain. For
// instance, with Phoenix you can create a lambda expression such as
//
// if_(_1 > 0)[ std::cout << _2 ].else_[ std::cout << _3 ]
//
// In the above expression, "else_" is a so-called virtual data member
// of the expression "if_(_1 > 0)[ std::cout << _2 ]". This example
// shows how to implement the ".else_" syntax with Proto.
//
// ****WARNING****WARNING****WARNING****WARNING****WARNING****WARNING****
// * The virtual data member feature is experimental and can change at *
// * any time. Use it at your own risk. *
// **********************************************************************
#if defined(_MSC_VER) && _MSC_VER == 1310
#error "Sorry, this example doesn\'t work with MSVC 7.1"
#endif
#include <iostream>
#include <boost/config.hpp>
#include <boost/detail/workaround.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/min_max.hpp>
#include <boost/mpl/next_prior.hpp>
#include <boost/fusion/include/at.hpp>
#include <boost/fusion/include/vector.hpp>
#include <boost/typeof/std/ostream.hpp>
#include <boost/proto/proto.hpp>
namespace mpl = boost::mpl;
namespace proto = boost::proto;
namespace fusion = boost::fusion;
using proto::_;
namespace mini_lambda
{
// A callable PolymorphicFunctionObject that wraps
// fusion::at()
struct at : proto::callable
{
template<class Sig>
struct result;
template<class This, class Vector, class N>
struct result<This(Vector, N)>
: fusion::result_of::at<
typename boost::remove_reference<Vector>::type
, typename boost::remove_reference<N>::type
>
{};
template<class Vector, class N>
typename fusion::result_of::at<Vector const, N>::type
operator()(Vector const &vector, N) const
{
return fusion::at<N>(vector);
}
};
// An MPL IntegralConstant
template<class N>
struct placeholder
{
typedef N type;
typedef typename N::tag tag;
typedef typename N::next next;
typedef typename N::prior prior;
typedef typename N::value_type value_type;
static const value_type value = N::value;
};
// Some keyword types for our lambda EDSL
namespace keyword
{
struct if_ {};
struct else_ {};
struct do_ {};
struct while_ {};
struct try_ {};
struct catch_ {};
}
// Forward declaration for the mini-lambda grammar
struct eval_if_else;
// Forward declaration for the mini-lambda expression wrapper
template<class E>
struct expression;
// The grammar for mini-lambda expressions with transforms for
// evaluating the lambda expression.
struct grammar
: proto::or_<
// When evaluating a placeholder, use the placeholder
// to index into the "data" parameter, which is a fusion
// vector containing the arguments to the lambda expression.
proto::when<
proto::terminal<placeholder<_> >
, at(proto::_data, proto::_value)
>
// When evaluating if/then/else expressions of the form
// "if_( E0 )[ E1 ].else_[ E2 ]", pass E0, E1 and E2 to
// eval_if_else along with the "data" parameter. Note the
// use of proto::member<> to match binary expressions like
// "X.Y" where "Y" is a virtual data member.
, proto::when<
proto::subscript<
proto::member<
proto::subscript<
proto::function<
proto::terminal<keyword::if_>
, grammar
>
, grammar
>
, proto::terminal<keyword::else_>
>
, grammar
>
, eval_if_else(
proto::_right(proto::_left(proto::_left(proto::_left)))
, proto::_right(proto::_left(proto::_left))
, proto::_right
, proto::_data
)
>
, proto::otherwise<
proto::_default<grammar>
>
>
{};
// A callable PolymorphicFunctionObject that evaluates
// if/then/else expressions.
struct eval_if_else : proto::callable
{
typedef void result_type;
template<typename If, typename Then, typename Else, typename Args>
void operator()(If const &if_, Then const &then_, Else const &else_, Args const &args) const
{
if(grammar()(if_, 0, args))
{
grammar()(then_, 0, args);
}
else
{
grammar()(else_, 0, args);
}
}
};
// Define the mini-lambda domain, in which all expressions are
// wrapped in mini_lambda::expression.
struct domain
: proto::domain<proto::pod_generator<expression> >
{};
// A simple transform for computing the arity of
// a lambda expression.
struct arity_of
: proto::or_<
proto::when<
proto::terminal< placeholder<_> >
, mpl::next<proto::_value>()
>
, proto::when<
proto::terminal<_>
, mpl::int_<0>()
>
, proto::otherwise<
proto::fold<
_
, mpl::int_<0>()
, mpl::max<arity_of, proto::_state>()
>
>
>
{};
// Here is the mini-lambda expression wrapper. It serves two purposes:
// 1) To define operator() overloads that evaluate the lambda expression, and
// 2) To define virtual data members like "else_" so that we can write
// expressions like "if_(X)[Y].else_[Z]".
template<class E>
struct expression
{
BOOST_PROTO_BASIC_EXTENDS(E, expression<E>, domain)
BOOST_PROTO_EXTENDS_ASSIGN()
BOOST_PROTO_EXTENDS_SUBSCRIPT()
// Use BOOST_PROTO_EXTENDS_MEMBERS() to define "virtual"
// data members that all expressions in the mini-lambda
// domain will have. They can be used to create expressions
// like "if_(x)[y].else_[z]" and "do_[y].while_(z)".
BOOST_PROTO_EXTENDS_MEMBERS(
((keyword::else_, else_))
((keyword::while_, while_))
((keyword::catch_, catch_))
)
// Calculate the arity of this lambda expression
static int const arity = boost::result_of<arity_of(E)>::type::value;
// Define overloads of operator() that evaluate the lambda
// expression for up to 3 arguments.
// Don't try to compute the return type of the lambda if
// it isn't nullary.
typename mpl::eval_if_c<
0 != arity
, mpl::identity<void>
, boost::result_of<grammar(
E const &
, int const &
, fusion::vector<> &
)>
>::type
operator()() const
{
BOOST_MPL_ASSERT_RELATION(arity, ==, 0);
fusion::vector<> args;
return grammar()(proto_base(), 0, args);
}
#define BOOST_PROTO_LOCAL_MACRO( \
N, typename_A, A_const_ref, A_const_ref_a, a \
) \
template<typename_A(N)> \
typename boost::result_of<grammar( \
E const & \
, int const & \
, fusion::vector<A_const_ref(N)> & \
)>::type \
operator ()(A_const_ref_a(N)) const \
{ \
BOOST_MPL_ASSERT_RELATION(arity, <=, N); \
fusion::vector<A_const_ref(N)> args(a(N)); \
return grammar()(proto_base(), 0, args); \
}
// Repeats BOOST_PROTO_LOCAL_MACRO macro for N=1 to 3
// inclusive (because there are only 3 placeholders)
#define BOOST_PROTO_LOCAL_a BOOST_PROTO_a
#define BOOST_PROTO_LOCAL_LIMITS (1, 3)
#include BOOST_PROTO_LOCAL_ITERATE()
};
namespace placeholders
{
typedef placeholder<mpl::int_<0> > _1_t;
typedef placeholder<mpl::int_<1> > _2_t;
typedef placeholder<mpl::int_<2> > _3_t;
// Define some placeholders
expression<proto::terminal<_1_t>::type> const _1 = {{{}}};
expression<proto::terminal<_2_t>::type> const _2 = {{{}}};
expression<proto::terminal<_3_t>::type> const _3 = {{{}}};
// Define the if_() statement
template<typename E>
typename proto::result_of::make_expr<proto::tag::function, domain
, keyword::if_
, E const &
>::type const
if_(E const &e)
{
return proto::make_expr<proto::tag::function, domain>(
keyword::if_()
, boost::ref(e)
);
}
}
using placeholders::if_;
}
int main()
{
using namespace mini_lambda::placeholders;
// OK, we can create if/then/else lambda expressions
// and evaluate them.
if_(_1 > 0)
[
std::cout << _2 << '\n'
]
.else_
[
std::cout << _3 << '\n'
]
(-42, "positive", "non-positive");
// Even though all expressions in the mini-lambda
// domain have members named else_, while_, and catch_,
// they all occupy the same byte in the expression.
BOOST_MPL_ASSERT_RELATION(sizeof(_1), ==, 2);
return 0;
}
//]
|