]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/fusion/test/sequence/map.cpp
1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
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>
32 copy_all(copy_all
const&) {}
37 foo(x
); // should fail!
43 virtual void foo() = 0;
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
;
55 std::cout
<< tuple_open('[');
56 std::cout
<< tuple_close(']');
57 std::cout
<< tuple_delimiter(", ");
62 , pair
<double, std::string
>
63 , pair
<abstract
, int> >
66 BOOST_MPL_ASSERT((traits::is_associative
<map_type
>));
67 BOOST_MPL_ASSERT((traits::is_random_access
<map_type
>));
71 , make_pair
<double>("Men")
72 , make_pair
<abstract
>(2));
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
;
78 BOOST_TEST(at_key
<int>(m
) == 'X');
79 BOOST_TEST(at_key
<double>(m
) == "Men");
80 BOOST_TEST(at_key
<abstract
>(m
) == 2);
83 boost::is_same
<boost::fusion::result_of::value_at_key
<map_type
, int>::type
, char>::value
));
85 boost::is_same
<boost::fusion::result_of::value_at_key
<map_type
, double>::type
, std::string
>::value
));
87 boost::is_same
<boost::fusion::result_of::value_at_key
<map_type
, abstract
>::type
, int>::value
));
89 std::cout
<< m
<< std::endl
;
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
));
96 std::cout
<< deref_data(begin(m
)) << std::endl
;
97 std::cout
<< deref_data(fusion::next(begin(m
))) << std::endl
;
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);
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
));
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
);
117 // iterators & random access interface.
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
;
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")));
138 BOOST_TEST((deref(fusion::advance_c
<4>(it0
)) == deref(it4
)));
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
) ));
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);
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
;
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
));
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
));
179 return boost::report_errors();