1 /*==============================================================================
2 Copyright (c) 2001-2010 Joel de Guzman
3 Copyright (c) 2010 Eric Niebler
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 BOOST_PHOENIX_STATEMENT_IF_HPP
9 #define BOOST_PHOENIX_STATEMENT_IF_HPP
11 #include <boost/phoenix/config.hpp>
12 #include <boost/phoenix/core/limits.hpp>
13 #include <boost/phoenix/core/actor.hpp>
14 #include <boost/phoenix/core/call.hpp>
15 #include <boost/phoenix/core/expression.hpp>
16 #include <boost/phoenix/core/meta_grammar.hpp>
17 #include <boost/phoenix/core/is_actor.hpp>
21 #pragma warning(disable: 4355) // 'this' used in base member initializer list
24 namespace boost { namespace phoenix
26 template <typename> struct if_actor;
29 BOOST_PHOENIX_DEFINE_EXPRESSION_EXT(
31 , (boost)(phoenix)(if_)
32 , (meta_grammar) // Cond
33 (meta_grammar) // Then
36 BOOST_PHOENIX_DEFINE_EXPRESSION(
37 (boost)(phoenix)(if_else_statement)
38 , (meta_grammar) // Cond
39 (meta_grammar) // Then
40 (meta_grammar) // Else
43 namespace boost { namespace phoenix
45 ////////////////////////////////////////////////////////////////////////////
47 ////////////////////////////////////////////////////////////////////////////
49 // Function for evaluating lambdas like:
52 // if_( foo )[ bar ].else_[ baz ]
55 typedef void result_type;
57 template<typename Cond, typename Then, typename Context>
59 operator()(Cond const & cond, Then const & then, Context const & ctx) const
61 if(boost::phoenix::eval(cond, ctx))
62 boost::phoenix::eval(then, ctx);
65 template<typename Cond, typename Then, typename Else, typename Context>
74 if(boost::phoenix::eval(cond, ctx))
75 boost::phoenix::eval(then, ctx);
77 boost::phoenix::eval(else_, ctx);
81 template <typename Dummy>
82 struct default_actions::when<rule::if_, Dummy>
83 : call<if_else_eval, Dummy>
86 template <typename Dummy>
87 struct default_actions::when<rule::if_else_statement, Dummy>
88 : call<if_else_eval, Dummy>
92 // Generator for .else_[ expr ] branch.
93 template<typename Cond, typename Then>
96 else_gen(Cond const & cond_, Then const & then_)
100 template<typename Else>
101 typename expression::if_else_statement<Cond, Then, Else>::type const
102 operator[](Else const & else_) const
104 return expression::if_else_statement<Cond, Then, Else>::make(cond, then, else_);
111 // We subclass actor so we can provide the member else_ (which is an
112 // else_gen responsible for the .else_[ expr ] branch).
113 template<typename Expr>
114 struct if_actor : actor<Expr>
116 typedef actor<Expr> base_type;
118 if_actor(base_type const & base)
120 , else_(proto::child_c<0>(*this), proto::child_c<1>(*this))
123 typedef typename proto::result_of::child_c<Expr, 0>::type cond_type;
124 typedef typename proto::result_of::child_c<Expr, 1>::type then_type;
126 else_gen<cond_type, then_type> else_;
129 template <typename Expr>
130 struct is_actor<if_actor<Expr> >
134 // Generator for if( cond )[ then ] branch.
135 template<typename Cond>
138 if_gen(Cond const & cond_)
141 template<typename Then>
142 typename expression::if_<Cond, Then>::type const
143 operator[](Then const & then) const
145 return expression::if_<Cond, Then>::make(cond, then);
151 template<typename Cond>
154 if_(Cond const & cond)
156 return if_gen<Cond>(cond);