1 /*=============================================================================
2     Copyright (c) 2001-2011 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/container/map/map.hpp>
9 #include <boost/fusion/container/generation/make_map.hpp>
10 #include <boost/fusion/sequence/intrinsic/at_key.hpp>
11 #include <boost/fusion/sequence/intrinsic/value_at_key.hpp>
12 #include <boost/fusion/sequence/intrinsic/has_key.hpp>
13 #include <boost/fusion/sequence/intrinsic/begin.hpp>
14 #include <boost/fusion/sequence/io/out.hpp>
15 #include <boost/fusion/iterator/key_of.hpp>
16 #include <boost/fusion/iterator/deref_data.hpp>
17 #include <boost/fusion/iterator/value_of_data.hpp>
18 #include <boost/fusion/iterator/next.hpp>
19 #include <boost/fusion/support/pair.hpp>
20 #include <boost/fusion/support/category_of.hpp>
21 #include <boost/static_assert.hpp>
22 #include <boost/mpl/assert.hpp>
23 #include <boost/mpl/at.hpp>
24 #include <boost/typeof/typeof.hpp>
25 #include <iostream>
26 #include <string>
27 
28 
29 struct copy_all
30 {
copy_allcopy_all31     copy_all() {}
copy_allcopy_all32     copy_all(copy_all const&) {}
33 
34     template <typename T>
copy_allcopy_all35     copy_all(T const& x)
36     {
37         foo(x); // should fail!
38     }
39 };
40 
41 struct abstract
42 {
43     virtual void foo() = 0;
44 };
45 
46 int
main()47 main()
48 {
49     using namespace boost::fusion;
50     using namespace boost;
51     namespace fusion = boost::fusion;
52     using boost::fusion::pair;
53     using boost::fusion::make_pair;
54 
55     std::cout << tuple_open('[');
56     std::cout << tuple_close(']');
57     std::cout << tuple_delimiter(", ");
58 
59     {
60         typedef map<
61             pair<int, char>
62           , pair<double, std::string>
63           , pair<abstract, int> >
64         map_type;
65 
66         BOOST_MPL_ASSERT((traits::is_associative<map_type>));
67         BOOST_MPL_ASSERT((traits::is_random_access<map_type>));
68 
69         map_type m(
70             make_pair<int>('X')
71           , make_pair<double>("Men")
72           , make_pair<abstract>(2));
73 
74         std::cout << at_key<int>(m) << std::endl;
75         std::cout << at_key<double>(m) << std::endl;
76         std::cout << at_key<abstract>(m) << std::endl;
77 
78         BOOST_TEST(at_key<int>(m) == 'X');
79         BOOST_TEST(at_key<double>(m) == "Men");
80         BOOST_TEST(at_key<abstract>(m) == 2);
81 
82         BOOST_STATIC_ASSERT((
83             boost::is_same<boost::fusion::result_of::value_at_key<map_type, int>::type, char>::value));
84         BOOST_STATIC_ASSERT((
85             boost::is_same<boost::fusion::result_of::value_at_key<map_type, double>::type, std::string>::value));
86         BOOST_STATIC_ASSERT((
87             boost::is_same<boost::fusion::result_of::value_at_key<map_type, abstract>::type, int>::value));
88 
89         std::cout << m << std::endl;
90 
91         BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, int>::value));
92         BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, double>::value));
93         BOOST_STATIC_ASSERT((boost::fusion::result_of::has_key<map_type, abstract>::value));
94         BOOST_STATIC_ASSERT((!boost::fusion::result_of::has_key<map_type, std::string>::value));
95 
96         std::cout << deref_data(begin(m)) << std::endl;
97         std::cout << deref_data(fusion::next(begin(m))) << std::endl;
98 
99         BOOST_TEST(deref_data(begin(m)) == 'X');
100         BOOST_TEST(deref_data(fusion::next(begin(m))) == "Men");
101         BOOST_TEST(deref_data(fusion::next(next(begin(m)))) == 2);
102 
103         BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::begin<map_type>::type>::type, int>::value));
104         BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, double>::value));
105         BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type>::type, abstract>::value));
106         BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<map_type>::type>::type, char>::value));
107         BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type, std::string>::value));
108         BOOST_STATIC_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<map_type>::type>::type>::type>::type, int>::value));
109 
110         // Test random access interface.
111         pair<int, char> a = at_c<0>(m); (void) a;
112         pair<double, std::string> b = at_c<1>(m);
113         pair<abstract, int> c = at_c<2>(m);
114         (void)c;
115     }
116 
117     // iterators & random access interface.
118     {
119         typedef pair<boost::mpl::int_<0>, std::string> pair0;
120         typedef pair<boost::mpl::int_<1>, std::string> pair1;
121         typedef pair<boost::mpl::int_<2>, std::string> pair2;
122         typedef pair<boost::mpl::int_<3>, std::string> pair3;
123         typedef pair<boost::mpl::int_<4>, std::string> pair4;
124 
125         typedef map< pair0, pair1, pair2, pair3, pair4 > map_type;
126         map_type m( pair0("zero"), pair1("one"), pair2("two"), pair3("three"), pair4("four") );
127         BOOST_AUTO( it0, begin(m) );
128         BOOST_TEST((deref(it0) == pair0("zero")));
129         BOOST_AUTO( it1, fusion::next(it0) );
130         BOOST_TEST((deref(it1) == pair1("one")));
131         BOOST_AUTO( it2, fusion::next(it1) );
132         BOOST_TEST((deref(it2) == pair2("two")));
133         BOOST_AUTO( it3, fusion::next(it2) );
134         BOOST_TEST((deref(it3) == pair3("three")));
135         BOOST_AUTO( it4, fusion::next(it3) );
136         BOOST_TEST((deref(it4) == pair4("four")));
137 
138         BOOST_TEST((deref(fusion::advance_c<4>(it0)) == deref(it4)));
139 
140         // Bi-directional
141         BOOST_TEST((deref(fusion::prior(it4)) == deref(it3) ));
142         BOOST_TEST((deref(fusion::prior(it3)) == deref(it2) ));
143         BOOST_TEST((deref(fusion::prior(it2)) == deref(it1) ));
144         BOOST_TEST((deref(fusion::prior(it1)) == deref(it0) ));
145     }
146 
147     {
148         std::cout << make_map<char, int>('X', 123) << std::endl;
149         BOOST_TEST(at_key<char>(make_map<char, int>('X', 123)) == 'X');
150         BOOST_TEST(at_key<int>(make_map<char, int>('X', 123)) == 123);
151     }
152 
153     {
154         // test for copy construction of fusion pairs
155         // make sure that the correct constructor is called
156         pair<int, copy_all> p1;
157         pair<int, copy_all> p2 = p1;
158         (void)p2;
159     }
160 
161     {
162         // compile test only
163         // make sure result_of::deref_data returns a reference
164         typedef map<pair<float, int> > map_type;
165         typedef boost::fusion::result_of::begin<map_type>::type i_type;
166         typedef boost::fusion::result_of::deref_data<i_type>::type r_type;
167         BOOST_STATIC_ASSERT((boost::is_same<r_type, int&>::value));
168     }
169 
170     {
171         // compile test only
172         // make sure result_of::deref_data is const correct
173         typedef map<pair<float, int> > const map_type;
174         typedef boost::fusion::result_of::begin<map_type>::type i_type;
175         typedef boost::fusion::result_of::deref_data<i_type>::type r_type;
176         BOOST_STATIC_ASSERT((boost::is_same<r_type, int const&>::value));
177     }
178 
179     return boost::report_errors();
180 }
181 
182