1 #ifndef BOOST_PP_IS_ITERATING
2 ///////////////////////////////////////////////////////////////////////////////
3 // Copyright 2008 Eric Niebler. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // This example contains a full-featured reimplementation of the old,
8 // now-deprecated Boost Lambda Library (BLL) on top of Boost.Proto. It
9 // is necessarily complex to accomodate all the quirks and inconsistencies
10 // of that old library, but it is a good example of how to build a
11 // complete and full-featured EDLS using Proto.
12 #ifndef BOOST_LAMBDA_HPP_EAN_04_19_2008
13 #define BOOST_LAMBDA_HPP_EAN_04_19_2008
18 #include <boost/ref.hpp>
19 #include <boost/assert.hpp>
20 #include <boost/mpl/or.hpp>
21 #include <boost/mpl/int.hpp>
22 #include <boost/mpl/void.hpp>
23 #include <boost/mpl/identity.hpp>
24 #include <boost/mpl/next_prior.hpp>
25 #include <boost/mpl/min_max.hpp>
26 #include <boost/mpl/assert.hpp>
27 #include <boost/preprocessor.hpp>
28 #include <boost/utility/enable_if.hpp>
29 #include <boost/utility/result_of.hpp>
30 #include <boost/fusion/include/vector.hpp>
31 #include <boost/type_traits/add_reference.hpp>
32 #include <boost/type_traits/remove_reference.hpp>
33 #include <boost/type_traits/remove_const.hpp>
34 #include <boost/type_traits/is_same.hpp>
35 #include <boost/proto/proto.hpp>
37 #ifndef BOOST_LAMBDA_MAX_ARITY
38 # define BOOST_LAMBDA_MAX_ARITY 3
42 # pragma warning(push)
43 # pragma warning(disable: 4355) // 'this' : used in base member initializer list
44 # pragma warning(disable: 4065) // switch statement contains 'default' but no 'case' labels
47 namespace boost { namespace lambda
62 template<int I> struct case_ { static const int value = I; };
63 template<typename E> struct catch_ { typedef E exception_type; };
64 struct catch_all_ { typedef catch_all_ exception_type; };
67 template<typename Int>
70 typedef typename Int::tag tag;
71 typedef typename Int::value_type value_type;
72 typedef placeholder<Int> type;
73 typedef placeholder<typename Int::next> next;
74 typedef placeholder<typename Int::prior> prior;
75 static const value_type value = Int::value;
77 friend std::ostream &operator<<(std::ostream &sout, placeholder)
79 return sout << "boost::lambda::_" << (Int::value+1);
83 struct exception_placeholder
86 struct no_exception_type {};
87 no_exception_type const no_exception = {};
89 // Calculate the arity of a lambda expression
92 proto::when<proto::terminal<placeholder<proto::_> >, mpl::next<proto::_value>()>
93 , proto::when<proto::terminal<proto::_>, mpl::int_<0>()>
94 , proto::otherwise<proto::fold<proto::_, mpl::int_<0>(), mpl::max<proto::_state, Arity>()> >
98 // True when a lambda expression can be applied with no arguments and
99 // without an active exception object
102 proto::when<proto::terminal<placeholder<proto::_> >, mpl::false_()>
103 , proto::when<proto::terminal<exception_placeholder>, mpl::false_()>
104 , proto::when<proto::terminal<proto::_>, mpl::true_()>
105 , proto::otherwise<proto::fold<proto::_, mpl::true_(), mpl::and_<proto::_state, IsNullary>()> >
111 struct EvalWhile : proto::transform<EvalWhile>
113 template<typename Expr, typename State, typename Data>
114 struct impl : proto::transform_impl<Expr, State, Data>
116 typedef mpl::void_ result_type;
118 result_type operator()(
119 typename impl::expr_param expr
120 , typename impl::state_param state
121 , typename impl::data_param data
124 while(Eval()(proto::left(expr), state, data))
126 Eval()(proto::right(expr), state, data);
128 return result_type();
133 struct EvalDoWhile : proto::transform<EvalDoWhile>
135 template<typename Expr, typename State, typename Data>
136 struct impl : proto::transform_impl<Expr, State, Data>
138 typedef mpl::void_ result_type;
140 result_type operator()(
141 typename impl::expr_param expr
142 , typename impl::state_param state
143 , typename impl::data_param data
148 Eval()(proto::child_c<0>(expr), state, data);
150 while(Eval()(proto::child_c<1>(expr), state, data));
152 return result_type();
157 struct EvalFor : proto::transform<EvalFor>
159 template<typename Expr, typename State, typename Data>
160 struct impl : proto::transform_impl<Expr, State, Data>
162 typedef mpl::void_ result_type;
164 result_type operator()(
165 typename impl::expr_param expr
166 , typename impl::state_param state
167 , typename impl::data_param data
170 for(Eval()(proto::child_c<0>(expr), state, data)
171 ; Eval()(proto::child_c<1>(expr), state, data)
172 ; Eval()(proto::child_c<2>(expr), state, data))
174 Eval()(proto::child_c<3>(expr), state, data);
176 return result_type();
181 struct EvalIf : proto::transform<EvalIf>
183 template<typename Expr, typename State, typename Data>
184 struct impl : proto::transform_impl<Expr, State, Data>
186 typedef mpl::void_ result_type;
188 result_type operator()(
189 typename impl::expr_param expr
190 , typename impl::state_param state
191 , typename impl::data_param data
194 if(Eval()(proto::left(expr), state, data))
196 Eval()(proto::right(expr), state, data);
198 return result_type();
203 struct EvalIfElse : proto::transform<EvalIfElse>
205 template<typename Expr, typename State, typename Data>
206 struct impl : proto::transform_impl<Expr, State, Data>
208 typedef mpl::void_ result_type;
210 result_type operator()(
211 typename impl::expr_param expr
212 , typename impl::state_param state
213 , typename impl::data_param data
216 if(Eval()(proto::child_c<0>(expr), state, data))
218 Eval()(proto::child_c<1>(expr), state, data);
222 Eval()(proto::child_c<2>(expr), state, data);
224 return result_type();
229 struct EvalException : proto::transform<EvalException>
231 template<typename Expr, typename State, typename Data>
232 struct impl : proto::transform_impl<Expr, State, Data>
234 typedef typename remove_const<typename impl::state>::type result_type;
235 BOOST_MPL_ASSERT_NOT((is_same<result_type, no_exception_type>));
236 BOOST_MPL_ASSERT_NOT((is_same<result_type, tag::catch_all_>));
238 typename impl::state_param operator()(
239 typename impl::expr_param
240 , typename impl::state_param state
241 , typename impl::data_param
249 struct EvalSwitch : proto::transform<EvalSwitch>
251 template<typename Expr, typename State, typename Data, long Arity, typename BackTag>
254 #define M0(Z, N, DATA) \
255 case proto::tag_of<typename proto::result_of::child_c<Expr, N>::type>::type::value: \
256 Eval()(proto::child_c<N>(expr), state, data); \
260 #define M1(Z, N, DATA) \
261 template<typename Expr, typename State, typename Data, typename BackTag> \
262 struct impl2<Expr, State, Data, N, BackTag> \
263 : proto::transform_impl<Expr, State, Data> \
265 typedef void result_type; \
268 typename impl2::expr_param expr \
269 , typename impl2::state_param state \
270 , typename impl2::data_param data \
273 switch(Eval()(proto::child_c<0>(expr), state, data)) \
275 BOOST_PP_REPEAT_FROM_TO_ ## Z(1, N, M0, ~) \
282 template<typename Expr, typename State, typename Data> \
283 struct impl2<Expr, State, Data, N, tag::default_> \
284 : proto::transform_impl<Expr, State, Data> \
286 typedef void result_type; \
289 typename impl2::expr_param expr \
290 , typename impl2::state_param state \
291 , typename impl2::data_param data \
294 switch(Eval()(proto::child_c<0>(expr), state, data)) \
296 BOOST_PP_REPEAT_FROM_TO_ ## Z(1, BOOST_PP_DEC(N), M0, ~) \
298 Eval()(proto::child_c<BOOST_PP_DEC(N)>(expr), state, data); \
304 BOOST_PP_REPEAT_FROM_TO(2, BOOST_PP_INC(BOOST_PROTO_MAX_ARITY), M1, ~)
308 template<typename Expr, typename State, typename Data>
314 , proto::arity_of<Expr>::value
315 , typename proto::tag_of<
316 typename proto::result_of::child_c<
318 , proto::arity_of<Expr>::value-1
327 BOOST_PROTO_CALLABLE()
328 typedef void result_type;
329 template<typename Expr>
330 void operator()(Expr const &e) const
336 struct unwrap_ref : proto::callable
338 template<typename Sig>
341 template<typename This, typename T>
342 struct result<This(reference_wrapper<T>)>
347 template<typename This, typename T>
348 struct result<This(T &)>
353 T &operator()(reference_wrapper<T> const &ref) const
362 anytype(T &) { BOOST_ASSERT(false); }
364 operator T &() const { BOOST_ASSERT(false); throw; }
371 BOOST_PROTO_CALLABLE()
372 typedef anytype result_type;
373 template<typename State>
374 anytype operator()(State const &) const
376 BOOST_MPL_ASSERT_NOT((is_same<State, no_exception_type>));
383 template<typename Tag>
385 : proto::otherwise<proto::_default<Eval> >
389 struct case_<tag::catch_<E> >
390 : proto::otherwise<Eval(proto::_child)>
394 struct case_<tag::case_<I> >
395 : proto::otherwise<Eval(proto::_child)>
399 template<> struct Cases::case_<tag::while_> : proto::otherwise<EvalWhile> {};
400 template<> struct Cases::case_<tag::for_> : proto::otherwise<EvalFor> {};
401 template<> struct Cases::case_<tag::if_> : proto::otherwise<EvalIf> {};
402 template<> struct Cases::case_<tag::if_else_> : proto::otherwise<EvalIfElse> {};
403 template<> struct Cases::case_<tag::do_while_> : proto::otherwise<EvalDoWhile> {};
404 template<> struct Cases::case_<tag::switch_> : proto::otherwise<EvalSwitch> {};
405 template<> struct Cases::case_<tag::protect> : proto::otherwise<proto::_child> {};
406 template<> struct Cases::case_<tag::default_> : proto::otherwise<Eval(proto::_child)> {};
407 template<> struct Cases::case_<tag::catch_all_> : proto::otherwise<Eval(proto::_child)> {};
410 struct Cases::case_<proto::tag::terminal>
413 proto::terminal<placeholder<proto::_> >
414 , proto::functional::at(proto::_data, proto::_value)
417 proto::terminal<exception_placeholder>
421 proto::terminal<reference_wrapper<proto::_> >
422 , unwrap_ref(proto::_value)
424 , proto::otherwise<proto::_default<Eval> >
429 struct Cases::case_<proto::tag::function>
432 proto::function<proto::terminal<rethrow_fun> >
433 , rethrow_fun(proto::_state)
435 , proto::otherwise<proto::_default<Eval> >
440 : proto::switch_<Cases>
443 // Use a grammar to disable Proto's assignment operator overloads.
444 // We'll define our own because we want (x+=_1) to store x by
445 // reference. (In all other cases, variables are stored by value
446 // within lambda expressions.)
448 : proto::switch_<struct AssignOps>
453 template<typename Tag> struct case_ : proto::_ {};
456 template<> struct AssignOps::case_<proto::tag::shift_left_assign> : proto::not_<proto::_> {};
457 template<> struct AssignOps::case_<proto::tag::shift_right_assign> : proto::not_<proto::_> {};
458 template<> struct AssignOps::case_<proto::tag::multiplies_assign> : proto::not_<proto::_> {};
459 template<> struct AssignOps::case_<proto::tag::divides_assign> : proto::not_<proto::_> {};
460 template<> struct AssignOps::case_<proto::tag::modulus_assign> : proto::not_<proto::_> {};
461 template<> struct AssignOps::case_<proto::tag::plus_assign> : proto::not_<proto::_> {};
462 template<> struct AssignOps::case_<proto::tag::minus_assign> : proto::not_<proto::_> {};
463 template<> struct AssignOps::case_<proto::tag::bitwise_and_assign> : proto::not_<proto::_> {};
464 template<> struct AssignOps::case_<proto::tag::bitwise_or_assign> : proto::not_<proto::_> {};
465 template<> struct AssignOps::case_<proto::tag::bitwise_xor_assign> : proto::not_<proto::_> {};
469 template<typename Expr>
473 using exprns_::llexpr;
475 // Wrap expressions in lambda::llexpr<>.
477 : proto::pod_generator<llexpr>
480 // The domain for the lambda library.
482 : proto::domain<Generator, Grammar, proto::default_domain>
484 // Make all terminals and children held by value instead of by reference.
485 // Proto::domain<>::as_expr<> holds everything it can by value; the only
486 // exceptions are function types, abstract types, and iostreams.
489 : proto_base_domain::as_expr<T>
492 // The exception is arrays, which should still be held by reference
493 template<typename T, std::size_t N>
494 struct as_child<T[N]>
495 : proto_base_domain::as_child<T[N]>
499 template<typename Sig>
502 template<typename This>
503 struct llresult<This()>
505 result_of<IsNullary(This &)>::type::value
506 , result_of<Eval(This &, no_exception_type const &, fusion::vector0<> &)>
507 , mpl::identity<void>
511 #define M0(Z, N, DATA) \
512 template<typename This BOOST_PP_ENUM_TRAILING_PARAMS_Z(Z, N, typename A)> \
513 struct llresult<This(BOOST_PP_ENUM_PARAMS_Z(Z, N, A))> \
517 , no_exception_type const & \
518 , BOOST_PP_CAT(fusion::vector, N)<BOOST_PP_ENUM_PARAMS_Z(Z, N, A)> & \
523 BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_LAMBDA_MAX_ARITY), M0, ~)
526 template<typename Expr>
529 BOOST_PROTO_BASIC_EXTENDS(Expr, llexpr<Expr>, lldomain)
530 BOOST_PROTO_EXTENDS_ASSIGN()
531 BOOST_PROTO_EXTENDS_SUBSCRIPT()
533 template<typename Sig>
538 typename result<llexpr const()>::type
541 fusion::vector0<> args;
542 return Eval()(*this, no_exception, args);
545 #define M1(Z, N, _) ((0)(1))
547 #define M2(R, PRODUCT) M3(R, BOOST_PP_SEQ_SIZE(PRODUCT), PRODUCT)
549 #define M3(R, SIZE, PRODUCT) \
550 template<BOOST_PP_ENUM_PARAMS(SIZE, typename A)> \
551 typename result<llexpr const(BOOST_PP_SEQ_FOR_EACH_I_R(R, M5, ~, PRODUCT))>::type \
552 operator ()(BOOST_PP_SEQ_FOR_EACH_I_R(R, M4, ~, PRODUCT)) const \
554 BOOST_MPL_ASSERT_RELATION(result_of<Arity(Expr const &)>::type::value, <=, SIZE); \
555 BOOST_PP_CAT(fusion::vector, SIZE)<BOOST_PP_SEQ_FOR_EACH_I_R(R, M5, ~, PRODUCT)> args \
556 (BOOST_PP_SEQ_FOR_EACH_I_R(R, M6, ~, PRODUCT)); \
557 return Eval()(*this, no_exception, args); \
561 #define M4(R, _, I, ELEM) \
562 BOOST_PP_COMMA_IF(I) BOOST_PP_CAT(A, I) BOOST_PP_CAT(C, ELEM) &BOOST_PP_CAT(a, I) \
565 #define M5(R, _, I, ELEM) \
566 BOOST_PP_COMMA_IF(I) BOOST_PP_CAT(A, I) BOOST_PP_CAT(C, ELEM)& \
569 #define M6(R, _, I, ELEM) \
570 BOOST_PP_COMMA_IF(I) BOOST_PP_CAT(a, I) \
577 #define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_LAMBDA_MAX_ARITY, "./lambda.hpp"))
578 #include BOOST_PP_ITERATE()
590 typedef llexpr<proto::terminal<placeholder<mpl::int_<0> > >::type> placeholder1_type;
591 typedef llexpr<proto::terminal<placeholder<mpl::int_<1> > >::type> placeholder2_type;
592 typedef llexpr<proto::terminal<placeholder<mpl::int_<2> > >::type> placeholder3_type;
594 placeholder1_type const _1 = {{{}}};
595 placeholder2_type const _2 = {{{}}};
596 placeholder3_type const _3 = {{{}}};
598 placeholder1_type const free1 = {{{}}};
599 placeholder2_type const free2 = {{{}}};
600 placeholder3_type const free3 = {{{}}};
602 typedef llexpr<proto::terminal<exception_placeholder>::type> placeholderE_type;
603 placeholderE_type const _e = {{{}}};
607 template<typename Sig>
610 template<typename This, typename T>
611 struct result<This(T &)>
613 typedef llexpr<typename proto::terminal<T &>::type> type;
616 template<typename This, typename T>
617 struct result<This(llexpr<T> &)>
619 typedef boost::reference_wrapper<llexpr<T> > type;
622 template<typename This, typename T>
623 struct result<This(llexpr<T> const &)>
625 typedef boost::reference_wrapper<llexpr<T> const> type;
629 typename result<byref(T &)>::type operator()(T &t) const
631 typename result<byref(T &)>::type that = {{t}};
636 typename result<byref(T const &)>::type operator()(T const &t) const
638 typename result<byref(T const &)>::type that = {{t}};
643 boost::reference_wrapper<llexpr<T> > operator()(llexpr<T> &t) const
645 return boost::ref(t);
649 boost::reference_wrapper<llexpr<T> const> operator()(llexpr<T> const &t) const
651 return boost::ref(t);
657 // Ugh, the assign operators (and only the assign operators) store
658 // their left terminals by reference. That requires this special handling.
659 #define BOOST_LAMBDA_DEFINE_ASSIGN_OP(OP, TAG) \
660 template<typename T, typename U> \
661 typename proto::result_of::make_expr< \
664 , typename boost::result_of<byref(T &)>::type \
667 operator OP(T &t, U &u) \
669 return proto::make_expr<TAG, lldomain>(byref()(t), boost::ref(u)); \
671 template<typename T, typename U> \
672 typename proto::result_of::make_expr< \
675 , typename boost::result_of<byref(T &)>::type \
678 operator OP(T &t, U const &u) \
680 return proto::make_expr<TAG, lldomain>(byref()(t), boost::ref(u)); \
684 BOOST_LAMBDA_DEFINE_ASSIGN_OP(<<=, boost::proto::tag::shift_left_assign)
685 BOOST_LAMBDA_DEFINE_ASSIGN_OP(>>=, boost::proto::tag::shift_right_assign)
686 BOOST_LAMBDA_DEFINE_ASSIGN_OP(*= , boost::proto::tag::multiplies_assign)
687 BOOST_LAMBDA_DEFINE_ASSIGN_OP(/= , boost::proto::tag::divides_assign)
688 BOOST_LAMBDA_DEFINE_ASSIGN_OP(%= , boost::proto::tag::modulus_assign)
689 BOOST_LAMBDA_DEFINE_ASSIGN_OP(+= , boost::proto::tag::plus_assign)
690 BOOST_LAMBDA_DEFINE_ASSIGN_OP(-= , boost::proto::tag::minus_assign)
691 BOOST_LAMBDA_DEFINE_ASSIGN_OP(&= , boost::proto::tag::bitwise_and_assign)
692 BOOST_LAMBDA_DEFINE_ASSIGN_OP(|= , boost::proto::tag::bitwise_or_assign)
693 BOOST_LAMBDA_DEFINE_ASSIGN_OP(^= , boost::proto::tag::bitwise_xor_assign)
699 typedef llexpr<typename proto::terminal<T &>::type> type;
703 llexpr<typename proto::terminal<T &>::type> const
706 llexpr<typename proto::terminal<T &>::type> that = {{t}};
712 : proto::result_of::make_expr<
720 typename constant_type<T>::type const
723 typename constant_type<T>::type that = {{t}};
728 struct constant_ref_type
730 typedef llexpr<typename proto::terminal<T const &>::type> type;
734 llexpr<typename proto::terminal<T const &>::type> const
735 constant_ref(T const &t)
737 llexpr<typename proto::terminal<T const &>::type> that = {{t}};
741 template<typename Cond>
742 struct while_generator
744 explicit while_generator(Cond const &c)
748 template<typename Body>
749 typename proto::result_of::make_expr<
755 operator[](Body const &body) const
757 return proto::make_expr<tag::while_, lldomain>(
758 boost::ref(this->cond)
767 template<typename Expr>
768 while_generator<Expr> while_(Expr const &expr)
770 return while_generator<Expr>(expr);
773 template<typename Expr>
774 struct else_generator
776 typedef typename proto::result_of::left<Expr const &>::type condition_type;
777 typedef typename proto::result_of::right<Expr const &>::type body1_type;
779 explicit else_generator(Expr const &expr)
783 template<typename Body2>
784 typename proto::result_of::make_expr<
791 operator[](Body2 const &body2) const
793 return proto::make_expr<tag::if_else_, lldomain>(
794 boost::ref(proto::left(this->if_))
795 , boost::ref(proto::right(this->if_))
804 template<typename Expr>
805 struct with_else : Expr
808 with_else(T const &expr)
813 else_generator<Expr> else_;
816 template<typename Cond>
819 explicit if_generator(Cond const &c)
823 template<typename Body>
825 typename proto::result_of::make_expr<
832 operator[](Body const &body) const
834 return proto::make_expr<tag::if_, lldomain>(
835 boost::ref(this->cond)
844 template<typename Expr>
845 if_generator<Expr> if_(Expr const &expr)
847 return if_generator<Expr>(expr);
850 template<typename Init, typename Cond, typename Oper>
853 explicit for_generator(Init const &i, Cond const &c, Oper const &o)
859 template<typename Body>
860 typename proto::result_of::make_expr<
868 operator[](Body const &body) const
870 return proto::make_expr<tag::for_, lldomain>(
871 boost::ref(this->init)
872 , boost::ref(this->cond)
873 , boost::ref(this->oper)
884 template<typename Init, typename Cond, typename Oper>
885 for_generator<Init, Cond, Oper> for_(Init const &i, Cond const &c, Oper const &o)
887 return for_generator<Init, Cond, Oper>(i, c, o);
890 template<typename Body>
891 struct do_while_generator
893 explicit do_while_generator(Body const &b)
897 template<typename Cond>
898 typename proto::result_of::make_expr<
904 operator()(Cond const &cond) const
906 return proto::make_expr<tag::do_while_, lldomain>(
907 boost::ref(this->body)
916 template<typename Body>
919 explicit do_body(Body const &body)
923 do_while_generator<Body> while_;
928 template<typename Body>
929 do_body<Body> operator[](Body const &body) const
931 return do_body<Body>(body);
935 do_generator const do_ = {};
939 typedef void result_type;
940 void operator()() const {}
943 typedef llexpr<proto::function<llexpr<proto::terminal<noop_fun>::type> >::type> noop_type;
944 noop_type const noop = {{{{{}}}}};
946 template<typename Init, typename Cond, typename Oper>
947 typename proto::result_of::make_expr<
955 for_loop(Init const &init, Cond const &cond, Oper const &oper)
957 return proto::make_expr<tag::for_, lldomain>(
965 template<typename Init, typename Cond, typename Oper, typename Body>
966 typename proto::result_of::make_expr<
974 for_loop(Init const &init, Cond const &cond, Oper const &oper, Body const &body)
976 return proto::make_expr<tag::for_>(
984 template<typename Cond, typename Body>
985 typename proto::result_of::make_expr<
991 while_loop(Cond const &cond, Body const &body)
993 return proto::make_expr<tag::while_, lldomain>(
999 template<typename Cond>
1000 typename proto::result_of::make_expr<
1006 while_loop(Cond const &cond)
1008 return proto::make_expr<tag::while_, lldomain>(
1014 template<typename Cond, typename Body>
1015 typename proto::result_of::make_expr<
1021 do_while_loop(Cond const &cond, Body const &body)
1023 return proto::make_expr<tag::do_while_, lldomain>(
1029 template<typename Cond>
1030 typename proto::result_of::make_expr<
1036 do_while_loop(Cond const &cond)
1038 return proto::make_expr<tag::do_while_, lldomain>(
1044 template<typename Cond, typename Body1>
1045 typename proto::result_of::make_expr<
1051 if_then(Cond const &cond, Body1 const &body1)
1053 return proto::make_expr<tag::if_, lldomain>(
1059 template<typename Cond, typename Body1, typename Body2>
1060 typename proto::result_of::make_expr<
1067 if_then_else(Cond const &cond, Body1 const &body1, Body2 const &body2)
1069 return proto::make_expr<tag::if_else_, lldomain>(
1076 template<typename Cond, typename Body1, typename Body2>
1077 typename proto::result_of::make_expr<
1078 proto::tag::if_else_
1084 if_then_else_return(Cond const &cond, Body1 const &body1, Body2 const &body2)
1086 return proto::make_expr<proto::tag::if_else_, lldomain>(
1093 template<typename T>
1094 T const &make_const(T const &t)
1099 #define M1(N, typename_A, A_const_ref, A_const_ref_a, ref_a) \
1100 template<typename_A(N)> \
1101 typename proto::result_of::make_expr< \
1102 proto::tag::function \
1106 bind(A_const_ref_a(N)) \
1108 return proto::make_expr<proto::tag::function, lldomain>(ref_a(N)); \
1111 template<typename Ret, typename_A(N)> \
1112 typename proto::result_of::make_expr< \
1113 proto::tag::function \
1117 bind(A_const_ref_a(N)) \
1119 return proto::make_expr<proto::tag::function, lldomain>(ref_a(N)); \
1122 BOOST_PROTO_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_PROTO_MAX_ARITY), M1)
1125 template<typename Ret, typename Expr>
1126 Expr const &ret(Expr const &expr)
1131 template<typename Expr>
1132 Expr const &const_parameters(Expr const &expr)
1137 template<typename Expr>
1138 Expr const &break_const(Expr const &expr)
1143 template<typename Lambda>
1144 proto::unexpr<Lambda> const
1145 unlambda(Lambda const &lambda)
1147 return proto::unexpr<Lambda>(lambda);
1150 template<typename Lambda>
1151 typename proto::result_of::make_expr<
1156 protect(Lambda const &lambda)
1158 return proto::make_expr<tag::protect, lldomain>(boost::ref(lambda));
1161 template<typename T>
1162 T const std_functor(T const &t)
1167 template<typename T>
1168 struct ll_static_cast_fun
1170 typedef T result_type;
1172 template<typename U>
1173 T operator()(U &u) const
1175 return static_cast<T>(u);
1178 template<typename U>
1179 T operator()(U const &u) const
1181 return static_cast<T>(u);
1185 template<typename T, typename U>
1186 typename proto::result_of::make_expr<
1187 proto::tag::function
1189 , ll_static_cast_fun<T>
1192 ll_static_cast(U const &u)
1194 ll_static_cast_fun<T> fun;
1195 return proto::make_expr<proto::tag::function, lldomain>(fun, boost::ref(u));
1198 template<typename T>
1199 struct ll_const_cast_fun
1201 typedef T result_type;
1203 template<typename U>
1204 T operator()(U &u) const
1206 return const_cast<T>(u);
1209 template<typename U>
1210 T operator()(U const &u) const
1212 return const_cast<T>(u);
1216 template<typename T, typename U>
1217 typename proto::result_of::make_expr<
1218 proto::tag::function
1220 , ll_const_cast_fun<T>
1223 ll_const_cast(U const &u)
1225 ll_const_cast_fun<T> fun;
1226 return proto::make_expr<proto::tag::function, lldomain>(fun, boost::ref(u));
1229 template<typename T>
1230 struct ll_dynamic_cast_fun
1232 typedef T result_type;
1234 template<typename U>
1235 T operator()(U &u) const
1237 return dynamic_cast<T>(u);
1240 template<typename U>
1241 T operator()(U const &u) const
1243 return dynamic_cast<T>(u);
1247 template<typename T, typename U>
1248 typename proto::result_of::make_expr<
1249 proto::tag::function
1251 , ll_dynamic_cast_fun<T>
1254 ll_dynamic_cast(U const &u)
1256 ll_dynamic_cast_fun<T> fun;
1257 return proto::make_expr<proto::tag::function, lldomain>(fun, boost::ref(u));
1260 template<typename T>
1261 struct ll_reinterpret_cast_fun
1263 typedef T result_type;
1265 template<typename U>
1266 T operator()(U &u) const
1268 return reinterpret_cast<T>(u);
1271 template<typename U>
1272 T operator()(U const &u) const
1274 return reinterpret_cast<T>(u);
1278 template<typename T, typename U>
1279 typename proto::result_of::make_expr<
1280 proto::tag::function
1282 , ll_reinterpret_cast_fun<T>
1285 ll_reinterpret_cast(U const &u)
1287 ll_reinterpret_cast_fun<T> fun;
1288 return proto::make_expr<proto::tag::function, lldomain>(fun, boost::ref(u));
1291 struct ll_sizeof_fun
1293 typedef std::size_t result_type;
1295 template<typename U>
1296 std::size_t operator()(U const &) const
1302 template<typename U>
1303 typename proto::result_of::make_expr<
1304 proto::tag::function
1309 ll_sizeof(U const &u)
1312 return proto::make_expr<proto::tag::function, lldomain>(fun, boost::ref(u));
1315 struct ll_typeid_fun
1317 typedef std::type_info const &result_type;
1319 template<typename U>
1320 std::type_info const &operator()(U const &) const
1326 template<typename U>
1327 typename proto::result_of::make_expr<
1328 proto::tag::function
1333 ll_typeid(U const &u)
1336 return proto::make_expr<proto::tag::function, lldomain>(fun, boost::ref(u));
1339 template<typename T>
1342 typedef T result_type;
1344 T operator()() const
1349 #define M0(Z, N, DATA) \
1350 template<BOOST_PP_ENUM_PARAMS_Z(Z, N, typename A)> \
1351 T operator()(BOOST_PP_ENUM_BINARY_PARAMS_Z(Z, N, A, const &a)) const \
1353 return T(BOOST_PP_ENUM_PARAMS_Z(Z, N, a)); \
1356 BOOST_PP_REPEAT_FROM_TO(1, BOOST_PROTO_MAX_ARITY, M0, ~)
1360 template<typename T>
1363 typedef T *result_type;
1365 T *operator()() const
1370 #define M0(Z, N, DATA) \
1371 template<BOOST_PP_ENUM_PARAMS_Z(Z, N, typename A)> \
1372 T *operator()(BOOST_PP_ENUM_BINARY_PARAMS_Z(Z, N, A, const &a)) const \
1374 return new T(BOOST_PP_ENUM_PARAMS_Z(Z, N, a)); \
1377 BOOST_PP_REPEAT_FROM_TO(1, BOOST_PROTO_MAX_ARITY, M0, ~)
1383 typedef void result_type;
1385 template<typename T>
1386 void operator()(T const &t) const
1391 template<typename T>
1392 void operator()(T *const &t) const
1400 typedef void result_type;
1401 template<typename T>
1402 void operator()(T *t) const
1408 template<typename T>
1411 typedef T *result_type;
1412 T *operator()(std::size_t n) const
1420 typedef void result_type;
1421 template<typename T>
1422 void operator()(T *t) const
1428 template<typename T>
1429 struct type2type {};
1431 struct try_catch_nil {};
1433 template<typename Head, typename Tail>
1434 struct try_catch_cons : Tail
1436 typedef typename Head::proto_tag::exception_type exception_type;
1438 try_catch_cons(Head const &head, Tail const &tail)
1443 template<typename State, typename Data>
1444 typename result_of<Tail const(State const &, Data &)>::type
1445 operator()(State const &state, Data &data) const
1447 return this->invoke(state, data, type2type<exception_type>());
1451 // catch(Exception const &)
1452 template<typename State, typename Data, typename Exception>
1453 typename result_of<Tail const(State const &, Data &)>::type
1454 invoke(State const &state, Data &data, type2type<Exception>) const
1456 typedef typename result_of<Tail const(State const &, Data &)>::type result_type;
1459 return static_cast<result_type>(this->Tail::operator()(state, data));
1461 catch(Exception const &e)
1463 return static_cast<result_type>(Eval()(this->head, e, data));
1468 template<typename State, typename Data>
1469 typename result_of<Tail const(State const &, Data &)>::type
1470 invoke(State const &state, Data &data, type2type<tag::catch_all_>) const
1472 typedef typename result_of<Tail const(State const &, Data &)>::type result_type;
1475 return static_cast<result_type>(this->Tail::operator()(state, data));
1479 return static_cast<result_type>(Eval()(this->head, tag::catch_all_(), data));
1486 template<typename Head>
1487 struct try_catch_cons<Head, try_catch_nil> : proto::callable
1489 try_catch_cons(Head const &head, try_catch_nil const &)
1493 template<typename Sig>
1496 template<typename This, typename State, typename Data>
1497 struct result<This(State, Data)>
1498 : result_of<Eval(Head const &, State, Data)>
1501 template<typename State, typename Data>
1502 typename result_of<Eval(Head const &, State, Data)>::type
1503 operator()(State const &state, Data &data) const
1505 return Eval()(this->head, state, data);
1512 struct try_catch_fun : proto::callable
1514 template<typename Sig>
1517 template<typename This, typename Fun, typename State, typename Data>
1518 struct result<This(Fun, State, Data)>
1519 : result_of<Fun(State, Data)>
1522 template<typename Fun, typename State, typename Data>
1523 typename result_of<Fun(State const &, Data &)>::type
1524 operator()(Fun const &fun, State const &state, Data &data) const
1526 return fun(state, data);
1531 struct Cases::case_<tag::try_>
1537 , try_catch_cons<proto::_, proto::_state>(proto::_, proto::_state)
1545 template<typename E, typename Expr>
1546 typename proto::result_of::make_expr<tag::catch_<E>, lldomain, Expr const &>::type const
1547 catch_exception(Expr const &expr)
1549 return proto::make_expr<tag::catch_<E>, lldomain>(boost::ref(expr));
1552 template<typename E>
1553 typename proto::result_of::make_expr<tag::catch_<E>, lldomain, noop_type const &>::type const
1556 return proto::make_expr<tag::catch_<E>, lldomain>(boost::ref(noop));
1559 template<typename Expr>
1560 typename proto::result_of::make_expr<
1565 catch_all(Expr const &expr)
1567 return proto::make_expr<tag::catch_all_, lldomain>(boost::ref(expr));
1571 proto::result_of::make_expr<tag::catch_all_, lldomain, noop_type const &>::type const
1574 return proto::make_expr<tag::catch_all_, lldomain>(boost::ref(noop));
1577 #define M1(N, typename_A, A_const_ref, A_const_ref_a, ref_a) \
1578 template<typename_A(N)> \
1579 typename proto::result_of::make_expr< \
1584 try_catch(A_const_ref_a(N)) \
1586 return proto::make_expr<tag::try_, lldomain>(ref_a(N)); \
1589 BOOST_PROTO_REPEAT_FROM_TO(2, BOOST_PP_INC(BOOST_PROTO_MAX_ARITY), M1)
1592 template<typename Expr>
1593 typename proto::result_of::make_expr<
1594 proto::tag::function
1599 throw_exception(Expr const &expr)
1602 return proto::make_expr<proto::tag::function, lldomain>(fun, boost::ref(expr));
1606 proto::result_of::make_expr<proto::tag::function, lldomain, rethrow_fun>::type const
1609 return proto::make_expr<proto::tag::function, lldomain>(rethrow_fun());
1612 struct make_void_fun
1614 typedef void result_type;
1615 template<typename T>
1616 void operator()(T const &) const
1620 template<typename Expr>
1621 typename proto::result_of::make_expr<
1622 proto::tag::function
1627 make_void(Expr const &expr)
1630 return proto::make_expr<proto::tag::function, lldomain>(fun, boost::ref(expr));
1633 #define M1(N, typename_A, A_const_ref, A_const_ref_a, ref_a) \
1634 template<typename_A(N)> \
1635 typename proto::result_of::make_expr< \
1640 switch_statement(A_const_ref_a(N)) \
1642 return proto::make_expr<tag::switch_, lldomain>(ref_a(N)); \
1645 BOOST_PROTO_REPEAT_FROM_TO(2, BOOST_PP_INC(BOOST_PROTO_MAX_ARITY), M1)
1648 template<int I, typename Expr>
1649 typename proto::result_of::make_expr<tag::case_<I>, lldomain, Expr const &>::type const
1650 case_statement(Expr const &expr)
1652 return proto::make_expr<tag::case_<I>, lldomain>(boost::ref(expr));
1656 typename proto::result_of::make_expr<tag::case_<I>, lldomain, noop_type const &>::type const
1659 return proto::make_expr<tag::case_<I>, lldomain>(boost::ref(noop));
1662 template<typename Expr>
1663 typename proto::result_of::make_expr<tag::default_, lldomain, Expr const &>::type const
1664 default_statement(Expr const &expr)
1666 return proto::make_expr<tag::default_, lldomain>(boost::ref(expr));
1670 proto::result_of::make_expr<tag::default_, lldomain, noop_type const &>::type const
1673 return proto::make_expr<tag::default_, lldomain>(boost::ref(noop));
1680 template<typename Sig>
1683 template<typename This, typename Begin, typename End, typename Fun>
1684 struct result<This(Begin, End, Fun)>
1685 : remove_const<typename remove_reference<Fun>::type>
1688 template<typename InIter, typename Fun>
1689 Fun operator()(InIter begin, InIter end, Fun fun) const
1691 return std::for_each(begin, end, fun);
1700 template<typename Expr>
1701 struct result_of<lambda::llexpr<Expr>()>
1702 : lambda::llexpr<Expr>::template result<lambda::llexpr<Expr>()>
1705 template<typename Expr>
1706 struct result_of<lambda::llexpr<Expr> const()>
1707 : lambda::llexpr<Expr>::template result<lambda::llexpr<Expr> const()>
1712 # pragma warning(pop)
1719 BOOST_PP_SEQ_FOR_EACH_PRODUCT(
1721 BOOST_PP_REPEAT(BOOST_PP_ITERATION(), M1, ~)