• 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/adapted/struct/adapt_struct.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/io/out.hpp>
15 #include <boost/fusion/container/vector/vector.hpp>
16 #include <boost/fusion/container/list/list.hpp>
17 #include <boost/fusion/container/generation/make_vector.hpp>
18 #include <boost/fusion/container/vector/convert.hpp>
19 #include <boost/fusion/sequence/comparison/equal_to.hpp>
20 #include <boost/fusion/sequence/comparison/not_equal_to.hpp>
21 #include <boost/fusion/sequence/comparison/less.hpp>
22 #include <boost/fusion/sequence/comparison/less_equal.hpp>
23 #include <boost/fusion/sequence/comparison/greater.hpp>
24 #include <boost/fusion/sequence/comparison/greater_equal.hpp>
25 #include <boost/fusion/support/is_view.hpp>
26 #include <boost/mpl/assert.hpp>
27 #include <boost/static_assert.hpp>
28 #include <iostream>
29 #include <string>
30 
31 namespace ns
32 {
33     template<typename X, typename Y>
34     struct point
35     {
36         X x;
37         Y y;
38         int z;
39     };
40 }
41 
42 #if BOOST_PP_VARIADICS
43 
44     BOOST_FUSION_ADAPT_TPL_STRUCT(
45         (X)(Y),
46         (ns::point)(X)(Y),
47         x,
48         (auto, y)
49         (int, z)
50     )
51 
52     template<typename M>
53     struct s { M m; };
54     BOOST_FUSION_ADAPT_TPL_STRUCT((M), (s)(M), m)
55 
56 #else // BOOST_PP_VARIADICS
57 
58     BOOST_FUSION_ADAPT_TPL_STRUCT(
59         (X)(Y),
60         (ns::point)(X)(Y),
61         (X, x)
62         (Y, y)
63         (auto, z)
64     )
65 
66     template<typename M>
67     struct s { M m; };
68     BOOST_FUSION_ADAPT_TPL_STRUCT((M), (s)(M), (auto, m))
69 
70 #endif
71 
72 template <typename TypeToConstruct>
73 struct empty_struct_templated_factory {
74 
operator ()empty_struct_templated_factory75   TypeToConstruct operator()() {
76     return TypeToConstruct();
77   }
78 
79 };
80 
81 BOOST_FUSION_ADAPT_TPL_STRUCT(
82     (TypeToConstruct),
83     (empty_struct_templated_factory)(TypeToConstruct),
84 )
85 
86 int
main()87 main()
88 {
89     using namespace boost::fusion;
90 
91     typedef ns::point<int, int> point;
92 
93     std::cout << tuple_open('[');
94     std::cout << tuple_close(']');
95     std::cout << tuple_delimiter(", ");
96 
97     {
98         BOOST_MPL_ASSERT_NOT((traits::is_view<point>));
99         BOOST_STATIC_ASSERT(!traits::is_view<point>::value);
100         point p = {123, 456, 789};
101 
102         std::cout << at_c<0>(p) << std::endl;
103         std::cout << at_c<1>(p) << std::endl;
104         std::cout << at_c<2>(p) << std::endl;
105         std::cout << p << std::endl;
106         BOOST_TEST(p == make_vector(123, 456, 789));
107 
108         at_c<0>(p) = 6;
109         at_c<1>(p) = 9;
110         at_c<2>(p) = 12;
111         BOOST_TEST(p == make_vector(6, 9, 12));
112 
113         BOOST_STATIC_ASSERT(boost::fusion::result_of::size<point>::value == 3);
114         BOOST_STATIC_ASSERT(!boost::fusion::result_of::empty<point>::value);
115 
116         BOOST_TEST(front(p) == 6);
117         BOOST_TEST(back(p) == 12);
118     }
119 
120     {
121         vector<int, float, int> v1(4, 2.f, 2);
122         point v2 = {5, 3, 3};
123         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 point to vector
136         point p = {5, 3, 3};
137         vector<int, long, int> v(p);
138         v = p;
139     }
140 
141     {
142         // conversion from point to list
143         point p = {5, 3, 3};
144         list<int, long> l(p);
145         l = p;
146     }
147 
148     { // begin/end
149         using namespace boost::fusion;
150 
151         typedef boost::fusion::result_of::begin<s<int> >::type b;
152         typedef boost::fusion::result_of::end<s<int> >::type e;
153         // this fails
154         BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::next<b>::type, e>));
155     }
156 
157     return boost::report_errors();
158 }
159 
160