1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
3 Copyright (c) 2007 Dan Marsden
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 <boost/detail/lightweight_test.hpp>
9 #include <boost/fusion/container/vector/vector.hpp>
10 #include <boost/fusion/adapted/mpl.hpp>
11 #include <boost/fusion/sequence/io/out.hpp>
12 #include <boost/fusion/sequence/intrinsic/at.hpp>
13 #include <boost/fusion/container/generation/make_vector.hpp>
14 #include <boost/fusion/algorithm/iteration/fold.hpp>
15 #include <boost/fusion/algorithm/iteration/accumulate.hpp>
16 #include <boost/type_traits/is_same.hpp>
17 #include <boost/mpl/if.hpp>
18 #include <boost/mpl/next.hpp>
19 #include <boost/mpl/int.hpp>
20 #include <boost/mpl/vector.hpp>
21
22 #include <boost/type_traits/remove_const.hpp>
23 #include <boost/type_traits/remove_reference.hpp>
24 #include <boost/type_traits/is_reference.hpp>
25
26 #include <string>
27
28 using boost::mpl::if_;
29 using boost::mpl::int_;
30 using boost::is_same;
31
32 struct add_ints_only
33 {
34 template<typename T>
35 struct result;
36
37 template <typename State, typename T>
38 struct result<add_ints_only(State, T)>
39 {
40 typedef typename boost::remove_const<
41 typename boost::remove_reference<State>::type>::type type;
42 };
43
44 template <typename State, typename T>
45 State
operator ()add_ints_only46 operator()(State const& state, T const& /*x*/) const
47 {
48 return state;
49 }
50
51 int
operator ()add_ints_only52 operator()(int state, int x) const
53 {
54 return x + state;
55 }
56 };
57
58 struct count_ints
59 {
60 template<typename T>
61 struct result;
62
63 template <typename CountT, typename T>
64 struct result<count_ints(CountT, T)>
65 {
66 typedef typename boost::remove_const<
67 typename boost::remove_reference<CountT>::type>::type state;
68 typedef typename boost::remove_const<
69 typename boost::remove_reference<T>::type>::type elem;
70
71 typedef typename
72 if_<
73 is_same<elem, int>
74 , typename boost::mpl::next<state>::type
75 , state
76 >::type
77 type;
78 };
79
80 template <typename CountT, typename T>
81 typename result<count_ints(CountT, T)>::type
operator ()count_ints82 operator()(CountT const&, T const&) const
83 {
84 typedef typename result<count_ints(CountT, T)>::type result_;
85 return result_();
86 }
87 };
88
89 struct appender
90 {
91 typedef std::string result_type;
92
operator ()appender93 std::string operator()(std::string const& str, char c) const
94 {
95 return str + c;
96 }
97 };
98
99 struct lvalue_adder
100 {
101 template<typename Sig>
102 struct result;
103
104 template<typename T0, typename T1>
105 struct result<lvalue_adder(T0, T1&)>
106 {
107 // Second argument still needs to support rvalues - see definition of fusion::fold
108 typedef T1 type;
109 };
110
111 template<typename T0, typename T1>
operator ()lvalue_adder112 T1 operator()(T0 const& lhs, T1& rhs) const
113 {
114 return lhs + rhs;
115 }
116 };
117
add(int lhs,int rhs)118 int add(int lhs, int rhs)
119 {
120 return lhs + rhs;
121 }
122
123 struct functor
124 {
125 template<typename T>
126 int
operator ()functor127 operator() (int hitherho, T const& cur) const
128 {
129 return int(hitherho + cur);
130 }
131 };
132
133 struct visitor
134 {
135 typedef int result_type;
136
operator ()visitor137 int operator()(int sum, long&)
138 {
139 return sum;
140 }
141 };
142
143 int
main()144 main()
145 {
146 using namespace boost::fusion;
147 namespace fusion = boost::fusion;
148
149 {
150 typedef vector<int, char, int, double> vector_type;
151 vector_type v(12345, 'x', 678910, 3.36);
152 int result = fold(v, 0, add_ints_only());
153 std::cout << result << std::endl;
154 BOOST_TEST(result == 12345+678910);
155 }
156
157 {
158 typedef vector<int> vector_type;
159 vector_type v(12345);
160
161 int n = fusion::fold(v, int_<0>(), count_ints());
162 std::cout << n << std::endl;
163 BOOST_TEST(n == 1);
164 }
165
166 {
167 typedef vector<int, char, int, double, int> vector_type;
168 vector_type v(12345, 'x', 678910, 3.36, 8756);
169
170 int n = fusion::fold(v, int_<0>(), count_ints());
171 std::cout << n << std::endl;
172 BOOST_TEST(n == 3);
173 }
174
175 {
176 typedef boost::mpl::vector<int, char, int, double, int> mpl_vec;
177 int n = fusion::fold(mpl_vec(), int_<0>(), count_ints());
178 std::cout << n << std::endl;
179 BOOST_TEST(n == 3);
180 }
181
182 {
183 BOOST_TEST(fusion::fold(fusion::make_vector('a','b','c','d','e'), std::string(""), appender())
184 == "abcde");
185 }
186
187 {
188 vector<int, int> vec(1,2);
189 BOOST_TEST(fusion::fold(vec, 0, lvalue_adder()) == 3);
190 }
191
192 {
193 vector<int, int> vec(1,2);
194 BOOST_TEST(fusion::fold(vec, 0, add) == 3);
195 }
196
197 {
198 typedef vector<int, char, int, double> vector_type;
199 vector_type v(12345, 'x', 678910, 3.36);
200 int result = accumulate(v, 0, add_ints_only());
201 std::cout << result << std::endl;
202 BOOST_TEST(result == 12345+678910);
203 }
204
205 {
206 typedef vector<int> vector_type;
207 vector_type v(12345);
208
209 int n = fusion::accumulate(v, int_<0>(), count_ints());
210 std::cout << n << std::endl;
211 BOOST_TEST(n == 1);
212 }
213
214 {
215 typedef vector<int, char, int, double, int> vector_type;
216 vector_type v(12345, 'x', 678910, 3.36, 8756);
217
218 int n = fusion::accumulate(v, int_<0>(), count_ints());
219 std::cout << n << std::endl;
220 BOOST_TEST(n == 3);
221 }
222
223 {
224 typedef boost::mpl::vector<int, char, int, double, int> mpl_vec;
225 int n = fusion::accumulate(mpl_vec(), int_<0>(), count_ints());
226 std::cout << n << std::endl;
227 BOOST_TEST(n == 3);
228 }
229
230 {
231 BOOST_TEST(fusion::accumulate(fusion::make_vector('a','b','c','d','e'), std::string(""), appender())
232 == "abcde");
233 }
234
235 {
236 vector<int, int> vec(1,2);
237 BOOST_TEST(fusion::accumulate(vec, 0, add) == 3);
238 }
239
240 {
241 #if defined(BOOST_RESULT_OF_USE_DECLTYPE)
242 {
243 boost::fusion::vector<int, double, long> container{1, 2, 3};
244 functor f;
245 boost::fusion::fold(container, 0, f);
246 }
247 #endif
248
249 {
250 boost::fusion::vector<long> vec;
251 visitor v;
252 boost::fusion::fold(vec, 0, v);
253 }
254 }
255
256 return boost::report_errors();
257 }
258