map.cpp 7.3 KB

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