1 /*=============================================================================
2 Copyright (c) 2001-2007 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #include <boost/detail/lightweight_test.hpp>
8 #include <boost/fusion/adapted/struct/adapt_struct.hpp>
9 #include <boost/fusion/sequence/intrinsic/at.hpp>
10 #include <boost/fusion/sequence/intrinsic/size.hpp>
11 #include <boost/fusion/sequence/intrinsic/empty.hpp>
12 #include <boost/fusion/sequence/intrinsic/front.hpp>
13 #include <boost/fusion/sequence/intrinsic/back.hpp>
14 #include <boost/fusion/sequence/intrinsic/value_at.hpp>
15 #include <boost/fusion/sequence/io/out.hpp>
16 #include <boost/fusion/container/vector/vector.hpp>
17 #include <boost/fusion/container/list/list.hpp>
18 #include <boost/fusion/container/generation/make_vector.hpp>
19 #include <boost/fusion/container/vector/convert.hpp>
20 #include <boost/fusion/sequence/comparison/equal_to.hpp>
21 #include <boost/fusion/sequence/comparison/not_equal_to.hpp>
22 #include <boost/fusion/sequence/comparison/less.hpp>
23 #include <boost/fusion/sequence/comparison/less_equal.hpp>
24 #include <boost/fusion/sequence/comparison/greater.hpp>
25 #include <boost/fusion/sequence/comparison/greater_equal.hpp>
26 #include <boost/fusion/mpl.hpp>
27 #include <boost/fusion/support/is_view.hpp>
28 #include <boost/mpl/front.hpp>
29 #include <boost/mpl/is_sequence.hpp>
30 #include <boost/mpl/assert.hpp>
31 #include <boost/static_assert.hpp>
35 namespace namespaced_type
{
45 namespaced_type::integer z
;
48 #if !BOOST_WORKAROUND(__GNUC__,<4)
49 struct point_with_private_attributes
51 friend struct boost::fusion::extension::access
;
59 point_with_private_attributes(int x
, int y
, int z
):x(x
),y(y
),z(z
)
76 // Testing non-constexpr compatible types
81 employee(std::string name
, std::string nickname
)
82 : name(name
), nickname(nickname
)
87 #if BOOST_PP_VARIADICS
89 BOOST_FUSION_ADAPT_STRUCT(
96 # if !BOOST_WORKAROUND(__GNUC__,<4)
97 BOOST_FUSION_ADAPT_STRUCT(
98 ns::point_with_private_attributes
,
106 BOOST_FUSION_ADAPT_STRUCT(s
, m
)
108 BOOST_FUSION_ADAPT_STRUCT(
110 foo_
.x
, // test that adapted members can actually be expressions
114 BOOST_FUSION_ADAPT_STRUCT(
121 #else // BOOST_PP_VARIADICS
123 BOOST_FUSION_ADAPT_STRUCT(
127 (namespaced_type::integer
, z
)
130 # if !BOOST_WORKAROUND(__GNUC__,<4)
131 BOOST_FUSION_ADAPT_STRUCT(
132 ns::point_with_private_attributes
,
140 BOOST_FUSION_ADAPT_STRUCT(s
, (auto, m
))
142 BOOST_FUSION_ADAPT_STRUCT(
144 (auto, foo_
.x
) // test that adapted members can actually be expressions
145 (BOOST_FUSION_ADAPT_AUTO
, y
) // Mixing auto & BOOST_FUSION_ADAPT_AUTO
146 // to test backward compatibility
149 BOOST_FUSION_ADAPT_STRUCT(
152 (BOOST_FUSION_ADAPT_AUTO
, nickname
)
157 struct empty_struct
{};
158 BOOST_FUSION_ADAPT_STRUCT(empty_struct
,)
163 using namespace boost::fusion
;
164 using namespace boost
;
167 std::cout
<< tuple_open('[');
168 std::cout
<< tuple_close(']');
169 std::cout
<< tuple_delimiter(", ");
172 BOOST_MPL_ASSERT_NOT((traits::is_view
<point
>));
173 BOOST_STATIC_ASSERT(!traits::is_view
<point
>::value
);
174 point p
= {123, 456, 789};
176 std::cout
<< at_c
<0>(p
) << std::endl
;
177 std::cout
<< at_c
<1>(p
) << std::endl
;
178 std::cout
<< at_c
<2>(p
) << std::endl
;
179 std::cout
<< p
<< std::endl
;
180 BOOST_TEST(p
== make_vector(123, 456, 789));
185 BOOST_TEST(p
== make_vector(6, 9, 12));
187 BOOST_STATIC_ASSERT(boost::fusion::result_of::size
<point
>::value
== 3);
188 BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty
<point
>::value
);
190 BOOST_TEST(front(p
) == 6);
191 BOOST_TEST(back(p
) == 12);
195 vector
<int, float, int> v1(4, 2.f
, 2);
196 point v2
= {5, 3, 3};
197 vector
<long, double, int> v3(5, 4., 4);
199 BOOST_TEST(v1
<= v2
);
201 BOOST_TEST(v2
>= v1
);
203 BOOST_TEST(v2
<= v3
);
205 BOOST_TEST(v3
>= v2
);
209 // conversion from point to vector
211 vector
<int, long, int> v(p
);
216 // conversion from point to list
218 list
<int, long, int> l(p
);
223 using namespace boost::fusion
;
224 using boost::is_same
;
226 typedef boost::fusion::result_of::begin
<s
>::type b
;
227 typedef boost::fusion::result_of::end
<s
>::type e
;
229 BOOST_MPL_ASSERT((is_same
<boost::fusion::result_of::next
<b
>::type
, e
>));
233 BOOST_MPL_ASSERT((mpl::is_sequence
<ns::point
>));
234 BOOST_MPL_ASSERT((boost::is_same
<
235 boost::fusion::result_of::value_at_c
<ns::point
,0>::type
236 , mpl::front
<ns::point
>::type
>));
239 #if !BOOST_WORKAROUND(__GNUC__,<4)
241 ns::point_with_private_attributes
p(123, 456, 789);
243 std::cout
<< at_c
<0>(p
) << std::endl
;
244 std::cout
<< at_c
<1>(p
) << std::endl
;
245 std::cout
<< at_c
<2>(p
) << std::endl
;
246 std::cout
<< p
<< std::endl
;
247 BOOST_TEST(p
== make_vector(123, 456, 789));
252 fusion::vector
<int, float> v1(4, 2.f
);
253 ns::bar v2
= {{5}, 3};
255 BOOST_TEST(v1
<= v2
);
257 BOOST_TEST(v2
>= v1
);
261 ns::employee
emp("John Doe", "jdoe");
262 std::cout
<< at_c
<0>(emp
) << std::endl
;
263 std::cout
<< at_c
<1>(emp
) << std::endl
;
265 fusion::vector
<std::string
, std::string
> v1("John Doe", "jdoe");
266 BOOST_TEST(emp
== v1
);
269 return boost::report_errors();