]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | /*============================================================================= |
2 | Copyright (c) 2001-2007 Joel de Guzman | |
3 | Copyright (c) 2014 John Fletcher | |
4 | ||
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 | #include <iostream> | |
9 | #include <cmath> | |
10 | #include <algorithm> | |
11 | #include <vector> | |
12 | ||
13 | #include <typeinfo> | |
14 | ||
15 | #include <boost/detail/lightweight_test.hpp> | |
16 | #include <boost/phoenix/core.hpp> | |
17 | #include <boost/phoenix/operator.hpp> | |
18 | #include <boost/phoenix/function.hpp> | |
19 | //#include <boost/phoenix/bind.hpp> | |
20 | #include <boost/phoenix/scope.hpp> | |
21 | ||
22 | namespace boost { namespace phoenix | |
23 | { | |
24 | struct for_each_impl | |
25 | { | |
26 | template <typename Sig> | |
27 | struct result; | |
28 | ||
29 | template <typename This, typename C, typename F> | |
30 | struct result<This(C,F)> | |
31 | { | |
32 | typedef void type; | |
33 | }; | |
34 | ||
35 | template <typename C, typename F> | |
36 | void operator()(C& c, F f) const | |
37 | { | |
38 | std::for_each(c.begin(), c.end(), f); | |
39 | } | |
40 | }; | |
41 | ||
42 | function<for_each_impl> const for_each = for_each_impl(); | |
43 | ||
44 | struct push_back_impl | |
45 | { | |
46 | template <typename Sig> | |
47 | struct result; | |
48 | ||
49 | template <typename This, typename C, typename T> | |
50 | struct result<This(C,T)> | |
51 | { | |
52 | typedef void type; | |
53 | }; | |
54 | ||
55 | template <typename C, typename T> | |
56 | void operator()(C& c, T& x) const | |
57 | { | |
58 | c.push_back(x); | |
59 | } | |
60 | }; | |
61 | ||
62 | function<push_back_impl> const push_back = push_back_impl(); | |
63 | }} | |
64 | ||
65 | struct zzz {}; | |
66 | ||
67 | int | |
68 | main() | |
69 | { | |
70 | using boost::phoenix::lambda; | |
71 | using boost::phoenix::let; | |
72 | using boost::phoenix::ref; | |
73 | using boost::phoenix::val; | |
74 | using boost::phoenix::arg_names::_1; | |
75 | using boost::phoenix::arg_names::_2; | |
76 | using boost::phoenix::local_names::_a; | |
77 | using boost::phoenix::local_names::_b; | |
78 | using boost::phoenix::placeholders::arg1; | |
79 | ||
80 | { | |
81 | int x = 1; | |
82 | int y = lambda[_1]()(x); | |
83 | BOOST_TEST(x == y); | |
84 | } | |
85 | ||
86 | { | |
87 | int x = 1, y = 10; | |
88 | BOOST_TEST( | |
89 | (_1 + lambda[_1 + 2])(x)(y) == 1+10+2 | |
90 | ); | |
91 | BOOST_TEST( | |
92 | (_1 + lambda[-_1])(x)(y) == 1+-10 | |
93 | ); | |
94 | } | |
95 | ||
96 | { | |
97 | int x = 1, y = 10, z = 13; | |
98 | BOOST_TEST( | |
99 | lambda(_a = _1, _b = _2) | |
100 | [ | |
101 | _1 + _a + _b | |
102 | ] | |
103 | (x, z)(y) == x + y + z | |
104 | ); | |
105 | } | |
106 | ||
107 | { | |
108 | int x = 4; | |
109 | int y = 5; | |
110 | lambda(_a = _1)[_a = 555](x)(); | |
111 | BOOST_TEST(x == 555); | |
112 | (void)y; | |
113 | } | |
114 | ||
115 | { | |
116 | int x = 1; | |
117 | long x2 = 2; | |
118 | short x3 = 3; | |
119 | char const* y = "hello"; | |
120 | zzz z; | |
121 | ||
122 | BOOST_TEST(lambda[_1](x)(y) == y); | |
123 | BOOST_TEST(lambda(_a = _1)[_a](x)(y) == x); | |
124 | BOOST_TEST(lambda(_a = _1)[lambda[_a]](x)(y)(z) == x); | |
125 | BOOST_TEST(lambda(_a = _1)[lambda[_a + _1]](x)(y)(x) == 2); | |
126 | BOOST_TEST(lambda(_a = _1)[lambda(_b = _1)[_a + _b + _1]](x)(x2)(x3) == 6); | |
127 | } | |
128 | ||
129 | { | |
130 | int x = 1, y = 10; | |
131 | BOOST_TEST( | |
132 | (_1 + lambda(_a = _1)[_a + _1 + 2])(x)(y) == 1+1+10+2 | |
133 | ); | |
134 | } | |
135 | ||
136 | { | |
137 | int x = 1, y = 10; | |
138 | BOOST_TEST( | |
139 | ( | |
140 | _1 + | |
141 | lambda(_a = _1) | |
142 | [ | |
143 | _a + lambda[_a + 2] | |
144 | ] | |
145 | ) | |
146 | (x)(y)(y) == 1+1+1+2 | |
147 | ); | |
148 | } | |
149 | ||
150 | { | |
151 | using boost::phoenix::for_each; | |
152 | #if (!defined(BOOST_MSVC) || (BOOST_MSVC < 1800)) | |
153 | int init[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; | |
154 | std::vector<int> v(init, init+10); | |
155 | ||
156 | int x = 0; | |
157 | for_each(_1, lambda(_a = _2)[_a += _1])(v, x); | |
158 | BOOST_TEST(x == 55); | |
159 | #endif | |
160 | } | |
161 | ||
162 | { | |
163 | using boost::phoenix::for_each; | |
164 | using boost::phoenix::push_back; | |
165 | ||
166 | #if (!defined(BOOST_MSVC) || (BOOST_MSVC < 1800)) | |
167 | int x = 10; | |
168 | std::vector<std::vector<int> > v(10); | |
169 | for_each(_1, lambda(_a = _2)[push_back(_1, _a)])(v, x); | |
170 | ||
171 | int y = 0; | |
172 | for_each(arg1, lambda[ref(y) += _1[0]])(v); | |
173 | BOOST_TEST(y == 100); | |
174 | #endif | |
175 | } | |
176 | ||
177 | { | |
178 | int x = 1, y = 10, z = 13; | |
179 | ||
180 | BOOST_TEST( | |
181 | lambda(_a = _1, _b = _2) | |
182 | [ | |
183 | _1 + _a + _b | |
184 | ] | |
185 | (x, z)(y) == x + y + z | |
186 | ); | |
187 | } | |
188 | ||
189 | { | |
190 | { | |
191 | // $$$ Fixme. This should not be failing $$$ | |
192 | //int x = (let(_a = lambda[val(1)])[_a])()(); | |
193 | //BOOST_TEST(x == 1); | |
194 | } | |
195 | ||
196 | { | |
197 | // int x = (let(_a = lambda[val(1)])[bind(_a)])(); | |
198 | // BOOST_TEST(x == 1); | |
199 | // Take this out too, I am not sure about this. | |
200 | } | |
201 | } | |
202 | ||
203 | { | |
204 | int i = 0; | |
205 | int j = 2; | |
206 | BOOST_TEST(lambda[let(_a = _1)[_a = _2]]()(i, j) == j); | |
207 | BOOST_TEST(i == j); | |
208 | } | |
209 | ||
210 | return boost::report_errors(); | |
211 | } | |
212 |