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/adapted/adt/adapt_adt.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>
32 #include <iostream>
33 #include <string>
34 
35 namespace ns
36 {
37     template<typename X, typename Y>
38     class point
39     {
40     public:
41 
point()42         point() : x(0), y(0), z(0) {}
point(X x_,Y y_,int z_)43         point(X x_, Y y_, int z_) : x(x_), y(y_), z(z_) {}
44 
get_x() const45         X get_x() const { return x; }
get_y() const46         Y get_y() const { return y; }
get_z() const47         int get_z() const { return z; }
set_x(X x_)48         void set_x(X x_) { x = x_; }
set_y(Y y_)49         void set_y(Y y_) { y = y_; }
set_z(int z_)50         void set_z(int z_) { z = z_; }
51 
52     private:
53 
54         X x;
55         Y y;
56         int z;
57     };
58 }
59 
60 
61 #if BOOST_PP_VARIADICS
62 
63   BOOST_FUSION_ADAPT_TPL_ADT(
64       (X)(Y),
65       (ns::point)(X)(Y),
66       (X, X, obj.get_x(), obj.set_x(val))
67       (Y, Y, obj.get_y(), obj.set_y(val))
68       (obj.get_z(), obj.set_z(val))
69   )
70 
71 #else // BOOST_PP_VARIADICS
72 
73   BOOST_FUSION_ADAPT_TPL_ADT(
74       (X)(Y),
75       (ns::point)(X)(Y),
76       (X, X, obj.get_x(), obj.set_x(val))
77       (Y, Y, obj.get_y(), obj.set_y(val))
78       (auto, auto, obj.get_z(), obj.set_z(val))
79   )
80 #endif
81 
82 template <typename TypeToConstruct>
83 class empty_adt_templated_factory {
84 
operator ()()85   TypeToConstruct operator()() {
86     return TypeToConstruct();
87   }
88 
89 };
90 
91 BOOST_FUSION_ADAPT_TPL_ADT(
92     (TypeToConstruct),
93     (empty_adt_templated_factory)(TypeToConstruct),
94 )
95 
96 int
main()97 main()
98 {
99     using namespace boost::fusion;
100 
101     typedef ns::point<int, int> point;
102     typedef ns::point<std::string, std::string> name;
103 
104     std::cout << tuple_open('[');
105     std::cout << tuple_close(']');
106     std::cout << tuple_delimiter(", ");
107 
108     {
109         BOOST_MPL_ASSERT_NOT((traits::is_view<point>));
110         BOOST_STATIC_ASSERT(!traits::is_view<point>::value);
111         point p(123, 456, 789);
112 
113         std::cout << at_c<0>(p) << std::endl;
114         std::cout << at_c<1>(p) << std::endl;
115         std::cout << at_c<2>(p) << std::endl;
116         std::cout << p << std::endl;
117         BOOST_TEST(p == make_vector(123, 456, 789));
118 
119         at_c<0>(p) = 6;
120         at_c<1>(p) = 9;
121         at_c<2>(p) = 12;
122         BOOST_TEST(p == make_vector(6, 9, 12));
123 
124         BOOST_STATIC_ASSERT(boost::fusion::result_of::size<point>::value == 3);
125         BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<point>::value);
126 
127         BOOST_TEST(front(p) == 6);
128         BOOST_TEST(back(p) == 12);
129     }
130 
131     {
132         boost::fusion::vector<int, float, int> v1(4, 2.f, 2);
133         point v2(5, 3, 3);
134         boost::fusion::vector<long, double, int> v3(5, 4., 4);
135         BOOST_TEST(v1 < v2);
136         BOOST_TEST(v1 <= v2);
137         BOOST_TEST(v2 > v1);
138         BOOST_TEST(v2 >= v1);
139         BOOST_TEST(v2 < v3);
140         BOOST_TEST(v2 <= v3);
141         BOOST_TEST(v3 > v2);
142         BOOST_TEST(v3 >= v2);
143     }
144 
145     {
146         boost::fusion::vector<std::string, std::string, int> v1("Lincoln", "Abraham", 3);
147         name v2("Roosevelt", "Franklin", 3);
148         name v3("Roosevelt", "Theodore", 3);
149         BOOST_TEST(v1 < v2);
150         BOOST_TEST(v1 <= v2);
151         BOOST_TEST(v2 > v1);
152         BOOST_TEST(v2 >= v1);
153         BOOST_TEST(v2 < v3);
154         BOOST_TEST(v2 <= v3);
155         BOOST_TEST(v3 > v2);
156         BOOST_TEST(v3 >= v2);
157     }
158 
159     {
160         // conversion from point to vector
161         point p(5, 3, 3);
162         boost::fusion::vector<int, long, int> v(p);
163         v = p;
164     }
165 
166     {
167         // conversion from point to list
168         point p(5, 3, 3);
169         boost::fusion::list<int, long, int> l(p);
170         l = p;
171     }
172 
173     {
174         BOOST_MPL_ASSERT((boost::mpl::is_sequence<point>));
175         BOOST_MPL_ASSERT((boost::is_same<
176             boost::fusion::result_of::value_at_c<point,0>::type
177           , boost::mpl::front<point>::type>));
178     }
179 
180     return boost::report_errors();
181 }
182 
183