1 /*=============================================================================
2 Copyright (c) 2010 Christopher Schmidt
3
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/sequence.hpp>
9 #include <boost/fusion/support.hpp>
10 #include <boost/fusion/container/list.hpp>
11 #include <boost/fusion/container/vector.hpp>
12 #include <boost/fusion/container/generation/make_vector.hpp>
13 #include <boost/fusion/adapted/adt/adapt_assoc_adt.hpp>
14 #include <boost/mpl/assert.hpp>
15 #include <boost/mpl/not.hpp>
16 #include <boost/mpl/front.hpp>
17 #include <boost/mpl/is_sequence.hpp>
18 #include <boost/type_traits/is_same.hpp>
19 #include <boost/static_assert.hpp>
20 #include <iostream>
21 #include <string>
22
23 namespace ns
24 {
25 struct x_member;
26 struct y_member;
27 struct z_member;
28
29 struct non_member;
30
31 template<typename X, typename Y, typename Z>
32 class point
33 {
34 public:
35
point()36 point() : x(0), y(0), z(0) {}
point(X in_x,Y in_y,Z in_z)37 point(X in_x, Y in_y, Z in_z) : x(in_x), y(in_y), z(in_z) {}
38
get_x() const39 X get_x() const { return x; }
get_y() const40 Y get_y() const { return y; }
get_z() const41 Z get_z() const { return z; }
set_x(X x_)42 void set_x(X x_) { x = x_; }
set_y(Y y_)43 void set_y(Y y_) { y = y_; }
set_z(Z z_)44 void set_z(Z z_) { z = z_; }
45
46 private:
47
48 X x;
49 Y y;
50 Z z;
51 };
52 }
53
54 #if BOOST_PP_VARIADICS
55 BOOST_FUSION_ADAPT_ASSOC_TPL_ADT(
56 (X)(Y)(Z),
57 (ns::point)(X)(Y)(Z),
58 (X, X, obj.get_x(), obj.set_x(val), ns::x_member)
59 (Y, Y, obj.get_y(), obj.set_y(val), ns::y_member)
60 (obj.get_z(), obj.set_z(val), ns::z_member)
61 )
62
63 #else // BOOST_PP_VARIADICS
64 BOOST_FUSION_ADAPT_ASSOC_TPL_ADT(
65 (X)(Y)(Z),
66 (ns::point)(X)(Y)(Z),
67 (X, X, obj.get_x(), obj.set_x(val), ns::x_member)
68 (Y, Y, obj.get_y(), obj.set_y(val), ns::y_member)
69 (auto, auto, obj.get_z(), obj.set_z(val), ns::z_member)
70 )
71
72 #endif
73
74 template <typename TypeToConstruct>
75 class empty_adt_templated_factory {
76
operator ()()77 TypeToConstruct operator()() {
78 return TypeToConstruct();
79 }
80
81 };
82
83 BOOST_FUSION_ADAPT_ASSOC_TPL_ADT(
84 (TypeToConstruct),
85 (empty_adt_templated_factory)(TypeToConstruct),
86 )
87
88 int
main()89 main()
90 {
91 using namespace boost::fusion;
92
93 typedef ns::point<int,int,long> point;
94
95 std::cout << tuple_open('[');
96 std::cout << tuple_close(']');
97 std::cout << tuple_delimiter(", ");
98
99 {
100 BOOST_MPL_ASSERT_NOT((traits::is_view<point>));
101 BOOST_STATIC_ASSERT(!traits::is_view<point>::value);
102 point p(123, 456, 789);
103
104 std::cout << at_c<0>(p) << std::endl;
105 std::cout << at_c<1>(p) << std::endl;
106 std::cout << at_c<2>(p) << std::endl;
107 std::cout << p << std::endl;
108 BOOST_TEST(p == make_vector(123, 456, 789));
109
110 at_c<0>(p) = 6;
111 at_c<1>(p) = 9;
112 at_c<2>(p) = 12;
113 BOOST_TEST(p == make_vector(6, 9, 12));
114
115 BOOST_STATIC_ASSERT(boost::fusion::result_of::size<point>::value == 3);
116 BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<point>::value);
117
118 BOOST_TEST(front(p) == 6);
119 BOOST_TEST(back(p) == 12);
120 }
121
122 {
123 boost::fusion::vector<int, float, long> v1(4, 2.f, 2);
124 point v2(5, 3, 3);
125 boost::fusion::vector<long, double, long> v3(5, 4., 4);
126 BOOST_TEST(v1 < v2);
127 BOOST_TEST(v1 <= v2);
128 BOOST_TEST(v2 > v1);
129 BOOST_TEST(v2 >= v1);
130 BOOST_TEST(v2 < v3);
131 BOOST_TEST(v2 <= v3);
132 BOOST_TEST(v3 > v2);
133 BOOST_TEST(v3 >= v2);
134 }
135
136 {
137 // conversion from point to vector
138 point p(5, 3, 3);
139 boost::fusion::vector<int, long, int> v(p);
140 v = p;
141 }
142
143 {
144 // conversion from point to list
145 point p(5, 3, 3);
146 boost::fusion::list<int, long, int> l(p);
147 l = p;
148 }
149
150 {
151 BOOST_MPL_ASSERT((boost::mpl::is_sequence<point>));
152 BOOST_MPL_ASSERT((boost::is_same<
153 boost::fusion::result_of::value_at_c<point,0>::type
154 , boost::mpl::front<point>::type>));
155 }
156
157 {
158 // assoc stuff
159 BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<point, ns::x_member>));
160 BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<point, ns::y_member>));
161 BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<point, ns::z_member>));
162 BOOST_MPL_ASSERT((boost::mpl::not_<boost::fusion::result_of::has_key<point, ns::non_member> >));
163
164 BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<point, ns::x_member>::type, int>));
165 BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<point, ns::y_member>::type, int>));
166 BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_key<point, ns::z_member>::type, long>));
167
168 point p(5, 3, 1);
169
170 BOOST_TEST(at_key<ns::x_member>(p) == 5);
171 BOOST_TEST(at_key<ns::y_member>(p) == 3);
172 BOOST_TEST(at_key<ns::z_member>(p) == 1);
173 }
174
175 return boost::report_errors();
176 }
177
178