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