joint_view.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.hpp>
  8. #include <boost/fusion/container/set.hpp>
  9. #include <boost/fusion/container/vector/vector.hpp>
  10. #include <boost/fusion/view/joint_view/joint_view.hpp>
  11. #include <boost/fusion/sequence/io/out.hpp>
  12. #include <boost/fusion/sequence/comparison/equal_to.hpp>
  13. #include <boost/fusion/container/generation/make_vector.hpp>
  14. #include <boost/fusion/sequence/intrinsic/at.hpp>
  15. #include <boost/fusion/sequence/intrinsic/has_key.hpp>
  16. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  17. #include <boost/fusion/iterator/next.hpp>
  18. #include <boost/fusion/iterator/key_of.hpp>
  19. #include <boost/fusion/iterator/value_of.hpp>
  20. #include <boost/fusion/iterator/deref_data.hpp>
  21. #include <boost/mpl/assert.hpp>
  22. #include <string>
  23. struct X
  24. {
  25. operator char const*() const
  26. {
  27. return "<X-object>";
  28. }
  29. };
  30. int
  31. main()
  32. {
  33. using namespace boost::fusion;
  34. namespace fusion = boost::fusion;
  35. std::cout << tuple_open('[');
  36. std::cout << tuple_close(']');
  37. std::cout << tuple_delimiter(", ");
  38. /// Testing joint_view
  39. {
  40. vector<int> t1(3);
  41. vector<X> t2;
  42. typedef joint_view<vector<int>, vector<X> > view_type;
  43. view_type view(t1, t2);
  44. std::cout << view << std::endl;
  45. BOOST_TEST((view == make_vector(3, X())));
  46. }
  47. {
  48. vector<int, char> t1(3, 'x');
  49. vector<X> t2;
  50. typedef joint_view<vector<int, char>, vector<X> > view_type;
  51. view_type view(t1, t2);
  52. std::cout << view << std::endl;
  53. BOOST_TEST((view == make_vector(3, 'x', X())));
  54. *begin(view) = 4;
  55. BOOST_TEST(at_c<0>(t1) == 4);
  56. }
  57. {
  58. vector<int, char> t1(3, 'x');
  59. vector<X, int> t2;
  60. typedef joint_view<vector<int, char>, vector<X, int> > view_type;
  61. view_type view(t1, t2);
  62. std::cout << view << std::endl;
  63. BOOST_TEST((view == make_vector(3, 'x', X(), 0)));
  64. }
  65. {
  66. typedef vector<int> t1_type;
  67. t1_type t1(777);
  68. typedef vector<int, char, double> t2_type;
  69. t2_type t2(1, 'x', 3.3);
  70. {
  71. typedef joint_view<t1_type, t2_type> view_type;
  72. view_type view(t1, t2);
  73. std::cout << view << std::endl;
  74. BOOST_TEST((view == make_vector(777, 1, 'x', 3.3)));
  75. }
  76. {
  77. typedef joint_view<t2_type, t1_type> view_type;
  78. view_type view(t2, t1);
  79. std::cout << view << std::endl;
  80. BOOST_TEST((view == make_vector(1, 'x', 3.3, 777)));
  81. }
  82. {
  83. typedef joint_view<t2_type, t1_type> jv_type;
  84. typedef joint_view<jv_type, jv_type> jv2_type;
  85. jv_type jv(t2, t1);
  86. jv2_type jv2(jv, jv);
  87. std::cout << jv << std::endl;
  88. std::cout << jv2 << std::endl;
  89. BOOST_TEST(jv2
  90. == make_vector(1, 'x', 3.3, 777, 1, 'x', 3.3, 777));
  91. }
  92. {
  93. typedef joint_view<t2_type, t1_type> jt_type;
  94. typedef joint_view<t1_type, t2_type> jv2_type;
  95. typedef joint_view<jt_type, jv2_type> jv3_type;
  96. jt_type jt(t2, t1);
  97. jv2_type jv2(t1, t2);
  98. jv3_type jv3(jt, jv2);
  99. std::cout << jt << std::endl;
  100. std::cout << jv2 << std::endl;
  101. std::cout << jv3 << std::endl;
  102. BOOST_TEST(jv3
  103. == make_vector(1, 'x', 3.3, 777, 777, 1, 'x', 3.3));
  104. }
  105. {
  106. typedef joint_view<vector<>, t1_type> jt_type;
  107. vector<> empty;
  108. jt_type jt(empty, t1);
  109. std::cout << jt << std::endl;
  110. BOOST_TEST(jt == make_vector(777));
  111. }
  112. {
  113. typedef joint_view<t1_type, vector<> > jt_type;
  114. vector<> empty;
  115. jt_type jt(t1, empty);
  116. std::cout << jt << std::endl;
  117. BOOST_TEST(jt == make_vector(777));
  118. }
  119. {
  120. typedef joint_view<vector<>, vector<> > jt_type;
  121. vector<> empty;
  122. jt_type jt(empty, empty);
  123. std::cout << jt << std::endl;
  124. BOOST_TEST(jt == make_vector());
  125. }
  126. }
  127. {
  128. typedef map<pair<void,int> > map_type;
  129. map_type m(make_pair<void>(0));
  130. typedef set<std::string, float> set_type;
  131. set_type s("foo", 1.3f);
  132. typedef joint_view<map_type, set_type> joint_view_type;
  133. joint_view_type j(m,s);
  134. BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<joint_view_type, void>::type));
  135. BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<joint_view_type, std::string>::type));
  136. BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<joint_view_type, float>::type));
  137. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::begin<joint_view_type>::type>::type, void>));
  138. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type, std::string>));
  139. BOOST_MPL_ASSERT((boost::is_same<
  140. boost::fusion::result_of::key_of<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type>::type
  141. , float>));
  142. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<joint_view_type>::type>::type, int>));
  143. BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type, std::string>));
  144. BOOST_MPL_ASSERT((boost::is_same<
  145. boost::fusion::result_of::value_of_data<boost::fusion::result_of::next<boost::fusion::result_of::next<boost::fusion::result_of::begin<joint_view_type>::type>::type>::type>::type
  146. , float>));
  147. std::cout << deref_data(begin(j)) << std::endl;
  148. std::cout << deref_data(fusion::next(begin(j))) << std::endl;
  149. std::cout << deref_data(fusion::next(fusion::next(begin(j)))) << std::endl;
  150. BOOST_TEST((deref_data(begin(j)) == 0));
  151. BOOST_TEST((deref_data(fusion::next(begin(j))) == "foo"));
  152. BOOST_TEST((deref_data(fusion::next(fusion::next(begin(j)))) == 1.3f));
  153. }
  154. return boost::report_errors();
  155. }