• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2007 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/struct/adapt_struct_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     struct point
37     {
38         int x;
39         int y;
40         int z;
41     };
42 }
43 
44 #if BOOST_PP_VARIADICS
45 
46     // this creates a fusion view: boost::fusion::adapted::point
47     BOOST_FUSION_ADAPT_STRUCT_NAMED(
48         ns::point, point,
49         x,
50         y,
51         z
52     )
53 
54     // this creates a fusion view: ns1::s1
55     struct s { int m; };
56     BOOST_FUSION_ADAPT_STRUCT_NAMED_NS(s, (ns1), s1, m)
57 
58 #else // BOOST_PP_VARIADICS
59 
60     // this creates a fusion view: boost::fusion::adapted::point
61     BOOST_FUSION_ADAPT_STRUCT_NAMED(
62         ns::point, point,
63         (int, x)
64         (BOOST_FUSION_ADAPT_AUTO, y)
65         (auto, z)
66     )
67 
68     // this creates a fusion view: ns1::s1
69     struct s { int m; };
70     BOOST_FUSION_ADAPT_STRUCT_NAMED_NS(s, (ns1), s1, (auto, m))
71 
72 #endif
73 
74 struct empty_struct {};
75 BOOST_FUSION_ADAPT_STRUCT_NAMED(empty_struct, renamed_empty_struct, )
76 BOOST_FUSION_ADAPT_STRUCT_NAMED_NS(empty_struct, (ns1), renamed_empty_struct1, )
77 
78 int
main()79 main()
80 {
81     using namespace boost::fusion;
82     using namespace boost;
83 
84     std::cout << tuple_open('[');
85     std::cout << tuple_close(']');
86     std::cout << tuple_delimiter(", ");
87 
88     {
89         BOOST_MPL_ASSERT((traits::is_view<adapted::point>));
90         BOOST_STATIC_ASSERT(traits::is_view<adapted::point>::value);
91         ns::point basep = {123, 456, 789};
92         adapted::point p(basep);
93 
94         std::cout << at_c<0>(p) << std::endl;
95         std::cout << at_c<1>(p) << std::endl;
96         std::cout << at_c<2>(p) << std::endl;
97         std::cout << p << std::endl;
98         BOOST_TEST(p == make_vector(123, 456, 789));
99 
100         at_c<0>(p) = 6;
101         at_c<1>(p) = 9;
102         at_c<2>(p) = 12;
103         BOOST_TEST(p == make_vector(6, 9, 12));
104 
105         BOOST_STATIC_ASSERT(boost::fusion::result_of::size<adapted::point>::value == 3);
106         BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<adapted::point>::value);
107 
108         BOOST_TEST(front(p) == 6);
109         BOOST_TEST(back(p) == 12);
110     }
111 
112     {
113         fusion::vector<int, float, int> v1(4, 2.f, 2);
114         ns::point p = {5, 3, 3};
115         adapted::point v2(p);
116 
117         fusion::vector<long, double, int> v3(5, 4., 4);
118         BOOST_TEST(v1 < v2);
119         BOOST_TEST(v1 <= v2);
120         BOOST_TEST(v2 > v1);
121         BOOST_TEST(v2 >= v1);
122         BOOST_TEST(v2 < v3);
123         BOOST_TEST(v2 <= v3);
124         BOOST_TEST(v3 > v2);
125         BOOST_TEST(v3 >= v2);
126     }
127 
128     {
129         // conversion from adapted::point to vector
130         ns::point basep = {5, 3, 3};
131         adapted::point p(basep);
132         fusion::vector<int, long, int> v(p);
133         v = p;
134     }
135 
136     {
137         // conversion from adapted::point to list
138         ns::point basep = {5, 3, 3};
139         adapted::point p(basep);
140         fusion::list<int, long, int> l(p);
141         l = p;
142     }
143 
144     { // begin/end
145         using namespace boost::fusion;
146         using boost::is_same;
147 
148         typedef boost::fusion::result_of::begin<ns1::s1>::type b;
149         typedef boost::fusion::result_of::end<ns1::s1>::type e;
150         // this fails
151         BOOST_MPL_ASSERT((is_same<boost::fusion::result_of::next<b>::type, e>));
152     }
153 
154 
155     {
156         BOOST_MPL_ASSERT((mpl::is_sequence<adapted::point>));
157         BOOST_MPL_ASSERT((boost::is_same<
158             boost::fusion::result_of::value_at_c<adapted::point,0>::type
159           , mpl::front<adapted::point>::type>));
160     }
161 
162     return boost::report_errors();
163 }
164 
165