Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2001-2007 Joel de Guzman
|
Chris@16
|
3
|
Chris@16
|
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 ==============================================================================*/
|
Chris@16
|
7 #ifndef PHOENIX_STATEMENT_IF_HPP
|
Chris@16
|
8 #define PHOENIX_STATEMENT_IF_HPP
|
Chris@16
|
9
|
Chris@16
|
10 #include <boost/spirit/home/phoenix/core/composite.hpp>
|
Chris@16
|
11 #include <boost/spirit/home/phoenix/core/as_actor.hpp>
|
Chris@16
|
12 #include <boost/spirit/home/phoenix/core/compose.hpp>
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(BOOST_MSVC)
|
Chris@16
|
15 # pragma warning(push)
|
Chris@16
|
16 # pragma warning(disable:4355)
|
Chris@16
|
17 #endif
|
Chris@16
|
18
|
Chris@16
|
19 namespace boost { namespace phoenix
|
Chris@16
|
20 {
|
Chris@16
|
21 struct if_else_eval
|
Chris@16
|
22 {
|
Chris@16
|
23 template <typename Env, typename Cond, typename Then, typename Else>
|
Chris@16
|
24 struct result
|
Chris@16
|
25 {
|
Chris@16
|
26 typedef void type;
|
Chris@16
|
27 };
|
Chris@16
|
28
|
Chris@16
|
29 template <
|
Chris@16
|
30 typename RT, typename Env
|
Chris@16
|
31 , typename Cond, typename Then, typename Else>
|
Chris@16
|
32 static void
|
Chris@16
|
33 eval(Env const& env, Cond& cond, Then& then, Else& else_)
|
Chris@16
|
34 {
|
Chris@16
|
35 if (cond.eval(env))
|
Chris@16
|
36 then.eval(env);
|
Chris@16
|
37 else
|
Chris@16
|
38 else_.eval(env);
|
Chris@16
|
39 }
|
Chris@16
|
40 };
|
Chris@16
|
41
|
Chris@16
|
42 struct if_eval
|
Chris@16
|
43 {
|
Chris@16
|
44 template <typename Env, typename Cond, typename Then>
|
Chris@16
|
45 struct result
|
Chris@16
|
46 {
|
Chris@16
|
47 typedef void type;
|
Chris@16
|
48 };
|
Chris@16
|
49
|
Chris@16
|
50 template <typename RT, typename Env, typename Cond, typename Then>
|
Chris@16
|
51 static void
|
Chris@16
|
52 eval(Env const& env, Cond& cond, Then& then)
|
Chris@16
|
53 {
|
Chris@16
|
54 if (cond.eval(env))
|
Chris@16
|
55 then.eval(env);
|
Chris@16
|
56 }
|
Chris@16
|
57 };
|
Chris@16
|
58
|
Chris@16
|
59 template <typename Cond, typename Then>
|
Chris@16
|
60 struct if_composite;
|
Chris@16
|
61
|
Chris@16
|
62 template <typename Cond, typename Then>
|
Chris@16
|
63 struct else_gen
|
Chris@16
|
64 {
|
Chris@16
|
65 else_gen(if_composite<Cond, Then> const& source)
|
Chris@16
|
66 : source(source) {}
|
Chris@16
|
67
|
Chris@16
|
68 template <typename Else>
|
Chris@16
|
69 actor<typename as_composite<if_else_eval, Cond, Then, Else>::type>
|
Chris@16
|
70 operator[](Else const& else_) const
|
Chris@16
|
71 {
|
Chris@16
|
72 return compose<if_else_eval>(
|
Chris@16
|
73 fusion::at_c<0>(source) // cond
|
Chris@16
|
74 , fusion::at_c<1>(source) // then
|
Chris@16
|
75 , else_ // else
|
Chris@16
|
76 );
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 if_composite<Cond, Then> const& source;
|
Chris@16
|
80
|
Chris@16
|
81 private:
|
Chris@16
|
82 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
83 else_gen& operator= (else_gen const&);
|
Chris@16
|
84 };
|
Chris@16
|
85
|
Chris@16
|
86 template <typename Cond, typename Then>
|
Chris@16
|
87 struct if_composite : composite<if_eval, fusion::vector<Cond, Then> >
|
Chris@16
|
88 {
|
Chris@16
|
89 if_composite(Cond const& cond, Then const& then)
|
Chris@16
|
90 : composite<if_eval, fusion::vector<Cond, Then> >(cond, then)
|
Chris@16
|
91 , else_(*this) {}
|
Chris@16
|
92
|
Chris@16
|
93 else_gen<Cond, Then> else_;
|
Chris@16
|
94
|
Chris@16
|
95 private:
|
Chris@16
|
96 // silence MSVC warning C4512: assignment operator could not be generated
|
Chris@16
|
97 if_composite& operator= (if_composite const&);
|
Chris@16
|
98 };
|
Chris@16
|
99
|
Chris@16
|
100 template <typename Cond>
|
Chris@16
|
101 struct if_gen
|
Chris@16
|
102 {
|
Chris@16
|
103 if_gen(Cond const& cond)
|
Chris@16
|
104 : cond(cond) {}
|
Chris@16
|
105
|
Chris@16
|
106 template <typename Then>
|
Chris@16
|
107 actor<if_composite<Cond, typename as_actor<Then>::type> >
|
Chris@16
|
108 operator[](Then const& then) const
|
Chris@16
|
109 {
|
Chris@16
|
110 return actor<if_composite<Cond, typename as_actor<Then>::type> >(
|
Chris@16
|
111 cond, as_actor<Then>::convert(then));
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 Cond cond;
|
Chris@16
|
115 };
|
Chris@16
|
116
|
Chris@16
|
117 template <typename Cond>
|
Chris@16
|
118 inline if_gen<typename as_actor<Cond>::type>
|
Chris@16
|
119 if_(Cond const& cond)
|
Chris@16
|
120 {
|
Chris@16
|
121 return if_gen<typename as_actor<Cond>::type>(
|
Chris@16
|
122 as_actor<Cond>::convert(cond));
|
Chris@16
|
123 }
|
Chris@16
|
124 }}
|
Chris@16
|
125
|
Chris@16
|
126 #if defined(BOOST_MSVC)
|
Chris@16
|
127 # pragma warning(pop)
|
Chris@16
|
128 #endif
|
Chris@16
|
129
|
Chris@16
|
130 #endif
|