• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2009 Joel de Guzman
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_named.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 <iostream>
32 #include <string>
33 
34 namespace ns
35 {
36     class point
37     {
38     public:
39 
point()40         point() : x(0), y(0), z(0) {}
point(int in_x,int in_y,int in_z)41         point(int in_x, int in_y, int in_z) : x(in_x), y(in_y), z(in_z) {}
42 
get_x() const43         int get_x() const { return x; }
get_y() const44         int get_y() const { return y; }
get_z() const45         int get_z() const { return z; }
set_x(int x_)46         void set_x(int x_) { x = x_; }
set_y(int y_)47         void set_y(int y_) { y = y_; }
set_z(int z_)48         void set_z(int z_) { z = z_; }
49 
50     private:
51 
52         int x;
53         int y;
54         int z;
55     };
56 }
57 
58 #if BOOST_PP_VARIADICS
59 
60 // this creates a fusion view: boost::fusion::adapted::point
61 BOOST_FUSION_ADAPT_ADT_NAMED(
62     ns::point, point,
63     (int, int, obj.get_x(), obj.set_x(val))
64     (int, int, obj.get_y(), obj.set_y(val))
65     (obj.get_z(), obj.set_z(val))
66 )
67 
68 #else // BOOST_PP_VARIADICS
69 
70 // this creates a fusion view: boost::fusion::adapted::point
71 BOOST_FUSION_ADAPT_ADT_NAMED(
72     ns::point, point,
73     (int, int, obj.get_x(), obj.set_x(val))
74     (int, int, obj.get_y(), obj.set_y(val))
75     (auto, auto, obj.get_z(), obj.set_z(val))
76 )
77 
78 #endif // BOOST_PP_VARIADICS
79 
80 
81 class empty_adt{};
82 BOOST_FUSION_ADAPT_ADT_NAMED(empty_adt,renamed_empty_adt,)
83 
84 int
main()85 main()
86 {
87     using namespace boost::fusion;
88     using namespace boost;
89 
90     std::cout << tuple_open('[');
91     std::cout << tuple_close(']');
92     std::cout << tuple_delimiter(", ");
93 
94     {
95         BOOST_MPL_ASSERT((traits::is_view<adapted::point>));
96         BOOST_STATIC_ASSERT(traits::is_view<adapted::point>::value);
97         ns::point basep(123, 456, 789);
98         adapted::point p(basep);
99 
100         std::cout << at_c<0>(p) << std::endl;
101         std::cout << at_c<1>(p) << std::endl;
102         std::cout << at_c<2>(p) << std::endl;
103         std::cout << p << std::endl;
104         BOOST_TEST(p == make_vector(123, 456, 789));
105 
106         at_c<0>(p) = 6;
107         at_c<1>(p) = 9;
108         at_c<2>(p) = 12;
109         BOOST_TEST(p == make_vector(6, 9, 12));
110 
111         BOOST_STATIC_ASSERT(boost::fusion::result_of::size<adapted::point>::value == 3);
112         BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<adapted::point>::value);
113 
114         BOOST_TEST(front(p) == 6);
115         BOOST_TEST(back(p) == 12);
116     }
117 
118     {
119         fusion::vector<int, float, int> v1(4, 2.f, 2);
120         ns::point basep(5, 3, 3);
121         adapted::point v2(basep);
122 
123         fusion::vector<long, double, int> v3(5, 4., 4);
124         BOOST_TEST(v1 < v2);
125         BOOST_TEST(v1 <= v2);
126         BOOST_TEST(v2 > v1);
127         BOOST_TEST(v2 >= v1);
128         BOOST_TEST(v2 < v3);
129         BOOST_TEST(v2 <= v3);
130         BOOST_TEST(v3 > v2);
131         BOOST_TEST(v3 >= v2);
132     }
133 
134     {
135         // conversion from ns::point to vector
136         ns::point basep(5, 3, 3);
137         adapted::point p(basep);
138 
139         fusion::vector<int, long, int> v(p);
140         v = p;
141     }
142 
143     {
144         // conversion from ns::point to list
145         ns::point basep(5, 3, 3);
146         adapted::point p(basep);
147 
148         fusion::list<int, long, float> l(p);
149         l = p;
150     }
151 
152     {
153         BOOST_MPL_ASSERT((mpl::is_sequence<adapted::point>));
154         BOOST_MPL_ASSERT((boost::is_same<
155             boost::fusion::result_of::value_at_c<adapted::point,0>::type
156           , mpl::front<adapted::point>::type>));
157     }
158 
159     return boost::report_errors();
160 }
161 
162