Chris@16
|
1 /*==============================================================================
|
Chris@16
|
2 Copyright (c) 2001-2010 Joel de Guzman
|
Chris@16
|
3 Copyright (c) 2010 Eric Niebler
|
Chris@16
|
4
|
Chris@16
|
5 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
6 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
7 ==============================================================================*/
|
Chris@16
|
8 #ifndef BOOST_PHOENIX_STATEMENT_IF_HPP
|
Chris@16
|
9 #define BOOST_PHOENIX_STATEMENT_IF_HPP
|
Chris@16
|
10
|
Chris@101
|
11 #include <boost/phoenix/config.hpp>
|
Chris@16
|
12 #include <boost/phoenix/core/limits.hpp>
|
Chris@16
|
13 #include <boost/phoenix/core/actor.hpp>
|
Chris@16
|
14 #include <boost/phoenix/core/call.hpp>
|
Chris@16
|
15 #include <boost/phoenix/core/expression.hpp>
|
Chris@16
|
16 #include <boost/phoenix/core/meta_grammar.hpp>
|
Chris@16
|
17 #include <boost/phoenix/core/is_actor.hpp>
|
Chris@16
|
18
|
Chris@16
|
19 #ifdef BOOST_MSVC
|
Chris@16
|
20 #pragma warning(push)
|
Chris@16
|
21 #pragma warning(disable: 4355) // 'this' used in base member initializer list
|
Chris@16
|
22 #endif
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost { namespace phoenix
|
Chris@16
|
25 {
|
Chris@16
|
26 template <typename> struct if_actor;
|
Chris@16
|
27 }}
|
Chris@16
|
28
|
Chris@16
|
29 BOOST_PHOENIX_DEFINE_EXPRESSION_EXT(
|
Chris@16
|
30 if_actor
|
Chris@16
|
31 , (boost)(phoenix)(if_)
|
Chris@16
|
32 , (meta_grammar) // Cond
|
Chris@16
|
33 (meta_grammar) // Then
|
Chris@16
|
34 )
|
Chris@16
|
35
|
Chris@16
|
36 BOOST_PHOENIX_DEFINE_EXPRESSION(
|
Chris@16
|
37 (boost)(phoenix)(if_else_statement)
|
Chris@16
|
38 , (meta_grammar) // Cond
|
Chris@16
|
39 (meta_grammar) // Then
|
Chris@16
|
40 (meta_grammar) // Else
|
Chris@16
|
41 )
|
Chris@16
|
42
|
Chris@16
|
43 namespace boost { namespace phoenix
|
Chris@16
|
44 {
|
Chris@16
|
45 ////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
46 // If-Else statements
|
Chris@16
|
47 ////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
48
|
Chris@16
|
49 // Function for evaluating lambdas like:
|
Chris@16
|
50 // if_( foo )[ bar ]
|
Chris@16
|
51 // and
|
Chris@16
|
52 // if_( foo )[ bar ].else_[ baz ]
|
Chris@16
|
53 struct if_else_eval
|
Chris@16
|
54 {
|
Chris@16
|
55 typedef void result_type;
|
Chris@16
|
56
|
Chris@16
|
57 template<typename Cond, typename Then, typename Context>
|
Chris@16
|
58 result_type
|
Chris@16
|
59 operator()(Cond const & cond, Then const & then, Context const & ctx) const
|
Chris@16
|
60 {
|
Chris@16
|
61 if(boost::phoenix::eval(cond, ctx))
|
Chris@16
|
62 boost::phoenix::eval(then, ctx);
|
Chris@16
|
63 }
|
Chris@16
|
64
|
Chris@16
|
65 template<typename Cond, typename Then, typename Else, typename Context>
|
Chris@16
|
66 result_type
|
Chris@16
|
67 operator()(
|
Chris@16
|
68 Cond const & cond
|
Chris@16
|
69 , Then const & then
|
Chris@16
|
70 , Else const & else_
|
Chris@16
|
71 , Context const & ctx
|
Chris@16
|
72 ) const
|
Chris@16
|
73 {
|
Chris@16
|
74 if(boost::phoenix::eval(cond, ctx))
|
Chris@16
|
75 boost::phoenix::eval(then, ctx);
|
Chris@16
|
76 else
|
Chris@16
|
77 boost::phoenix::eval(else_, ctx);
|
Chris@16
|
78 }
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 template <typename Dummy>
|
Chris@16
|
82 struct default_actions::when<rule::if_, Dummy>
|
Chris@16
|
83 : call<if_else_eval, Dummy>
|
Chris@16
|
84 {};
|
Chris@16
|
85
|
Chris@16
|
86 template <typename Dummy>
|
Chris@16
|
87 struct default_actions::when<rule::if_else_statement, Dummy>
|
Chris@16
|
88 : call<if_else_eval, Dummy>
|
Chris@16
|
89 {};
|
Chris@16
|
90
|
Chris@16
|
91
|
Chris@16
|
92 // Generator for .else_[ expr ] branch.
|
Chris@16
|
93 template<typename Cond, typename Then>
|
Chris@16
|
94 struct else_gen
|
Chris@16
|
95 {
|
Chris@101
|
96 else_gen(Cond const & cond_, Then const & then_)
|
Chris@101
|
97 : cond(cond_)
|
Chris@101
|
98 , then(then_) {}
|
Chris@16
|
99
|
Chris@16
|
100 template<typename Else>
|
Chris@16
|
101 typename expression::if_else_statement<Cond, Then, Else>::type const
|
Chris@16
|
102 operator[](Else const & else_) const
|
Chris@16
|
103 {
|
Chris@16
|
104 return expression::if_else_statement<Cond, Then, Else>::make(cond, then, else_);
|
Chris@16
|
105 }
|
Chris@16
|
106
|
Chris@16
|
107 Cond cond;
|
Chris@16
|
108 Then then;
|
Chris@16
|
109 };
|
Chris@16
|
110
|
Chris@16
|
111 // We subclass actor so we can provide the member else_ (which is an
|
Chris@16
|
112 // else_gen responsible for the .else_[ expr ] branch).
|
Chris@16
|
113 template<typename Expr>
|
Chris@16
|
114 struct if_actor : actor<Expr>
|
Chris@16
|
115 {
|
Chris@16
|
116 typedef actor<Expr> base_type;
|
Chris@16
|
117
|
Chris@16
|
118 if_actor(base_type const & base)
|
Chris@16
|
119 : base_type(base)
|
Chris@16
|
120 , else_(proto::child_c<0>(*this), proto::child_c<1>(*this))
|
Chris@16
|
121 {}
|
Chris@16
|
122
|
Chris@16
|
123 typedef typename proto::result_of::child_c<Expr, 0>::type cond_type;
|
Chris@16
|
124 typedef typename proto::result_of::child_c<Expr, 1>::type then_type;
|
Chris@16
|
125
|
Chris@16
|
126 else_gen<cond_type, then_type> else_;
|
Chris@16
|
127 };
|
Chris@16
|
128
|
Chris@16
|
129 template <typename Expr>
|
Chris@16
|
130 struct is_actor<if_actor<Expr> >
|
Chris@16
|
131 : mpl::true_
|
Chris@16
|
132 {};
|
Chris@16
|
133
|
Chris@16
|
134 // Generator for if( cond )[ then ] branch.
|
Chris@16
|
135 template<typename Cond>
|
Chris@16
|
136 struct if_gen
|
Chris@16
|
137 {
|
Chris@101
|
138 if_gen(Cond const & cond_)
|
Chris@101
|
139 : cond(cond_) {}
|
Chris@16
|
140
|
Chris@16
|
141 template<typename Then>
|
Chris@16
|
142 typename expression::if_<Cond, Then>::type const
|
Chris@16
|
143 operator[](Then const & then) const
|
Chris@16
|
144 {
|
Chris@16
|
145 return expression::if_<Cond, Then>::make(cond, then);
|
Chris@16
|
146 }
|
Chris@16
|
147
|
Chris@16
|
148 Cond cond;
|
Chris@16
|
149 };
|
Chris@16
|
150
|
Chris@16
|
151 template<typename Cond>
|
Chris@16
|
152 inline
|
Chris@16
|
153 if_gen<Cond> const
|
Chris@16
|
154 if_(Cond const & cond)
|
Chris@16
|
155 {
|
Chris@16
|
156 return if_gen<Cond>(cond);
|
Chris@16
|
157 }
|
Chris@16
|
158
|
Chris@16
|
159 }}
|
Chris@16
|
160
|
Chris@16
|
161 #ifdef BOOST_MSVC
|
Chris@16
|
162 #pragma warning(pop)
|
Chris@16
|
163 #endif
|
Chris@16
|
164
|
Chris@16
|
165 #endif
|