filter_view.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/vector/vector.hpp>
  8. #include <boost/fusion/adapted/mpl.hpp>
  9. #include <boost/fusion/container/vector/vector_iterator.hpp>
  10. #include <boost/fusion/sequence/io/out.hpp>
  11. #include <boost/fusion/sequence/comparison/equal_to.hpp>
  12. #include <boost/fusion/view/filter_view/filter_view.hpp>
  13. #include <boost/fusion/container/generation/make_vector.hpp>
  14. #include <boost/fusion/sequence/intrinsic/size.hpp>
  15. #include <boost/fusion/container/map.hpp>
  16. #include <boost/fusion/sequence/intrinsic/has_key.hpp>
  17. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  18. #include <boost/fusion/iterator/key_of.hpp>
  19. #include <boost/fusion/iterator/value_of_data.hpp>
  20. #include <boost/fusion/iterator/deref_data.hpp>
  21. #include <boost/type_traits/is_class.hpp>
  22. #include <boost/type_traits/is_same.hpp>
  23. #include <boost/mpl/arg.hpp>
  24. #include <boost/mpl/not.hpp>
  25. #include <boost/mpl/vector_c.hpp>
  26. #include <boost/mpl/less.hpp>
  27. #include <boost/mpl/bool.hpp>
  28. #include <boost/mpl/assert.hpp>
  29. struct X
  30. {
  31. operator char const*() const
  32. {
  33. return "<X-object>";
  34. }
  35. };
  36. struct Y
  37. {
  38. operator char const*() const
  39. {
  40. return "<Y-object>";
  41. }
  42. };
  43. struct reject_all
  44. {
  45. template<typename T>
  46. struct apply : boost::mpl::false_
  47. {};
  48. };
  49. int
  50. main()
  51. {
  52. using namespace boost::fusion;
  53. using boost::mpl::int_;
  54. using boost::mpl::_;
  55. using boost::mpl::not_;
  56. using boost::mpl::less;
  57. using boost::mpl::vector_c;
  58. using boost::is_class;
  59. using boost::is_same;
  60. std::cout << tuple_open('[');
  61. std::cout << tuple_close(']');
  62. std::cout << tuple_delimiter(", ");
  63. {
  64. typedef vector<Y, char, long, X, bool, double> vector_type;
  65. X x; Y y;
  66. vector_type v(y, '@', 987654, x, true, 6.6);
  67. typedef filter_view<vector_type const, not_<is_class<_> > > filter_view_type;
  68. filter_view_type view(v);
  69. std::cout << view << std::endl;
  70. BOOST_TEST((view == make_vector('@', 987654, true, 6.6)));
  71. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<filter_view_type>::value == 4);
  72. }
  73. //cschmidt: This is illegal C++. ADL instantiates less<_, int_<3> > - which
  74. //leads to compile errors.
  75. /*{
  76. // $$$ JDG $$$ For some obscure reason, EDG based compilers
  77. // (e.g. comeau 4.3.3, intel) have problems with this.
  78. // vc7.1 and g++ are ok. The errors from comeau are useless.
  79. #ifndef __EDG_VERSION__
  80. typedef vector_c<int, 5, 1, 2, 3, 6, 0, -1> vector_type;
  81. typedef filter_view<vector_type const, less<_, int_<3> > > filter_view_type;
  82. vector_type v;
  83. filter_view_type view(v);
  84. std::cout << view << std::endl;
  85. BOOST_TEST((view == make_vector(1, 2, 0, -1)));
  86. BOOST_STATIC_ASSERT(boost::fusion::result_of::size<filter_view_type>::value == 4);
  87. #endif
  88. }*/
  89. {
  90. // Previous filtering out all values caused problems as begin<seq> was not equal to end<seq>
  91. // Picked up by Andreas Pokorny
  92. typedef vector<int> vec;
  93. typedef filter_view<vec, reject_all> filter_view_type;
  94. BOOST_MPL_ASSERT((boost::fusion::result_of::equal_to<boost::fusion::result_of::begin<filter_view_type>::type, boost::fusion::result_of::end<filter_view_type>::type>));
  95. }
  96. {
  97. typedef map<pair<void, int>, pair<double, std::string> > map_type;
  98. map_type m(make_pair<void>(0), make_pair<double>("Bond"));
  99. typedef filter_view<map_type const, is_same<_, pair<double, std::string> > > filter_view_type;
  100. filter_view_type f(m);
  101. BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<filter_view_type, double>::type));
  102. BOOST_MPL_ASSERT_NOT((boost::fusion::result_of::has_key<filter_view_type, void>::type));
  103. BOOST_MPL_ASSERT((is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::begin<filter_view_type>::type>::type, double>));
  104. BOOST_MPL_ASSERT((is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<filter_view_type>::type>::type, std::string>));
  105. std::cout << deref_data(begin(f)) << std::endl;
  106. BOOST_TEST((deref_data(begin(f)) == "Bond"));
  107. }
  108. return boost::report_errors();
  109. }