remove_if.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/sequence/io/out.hpp>
  10. #include <boost/fusion/container/generation/make_vector.hpp>
  11. #include <boost/fusion/sequence/comparison/equal_to.hpp>
  12. #include <boost/fusion/algorithm/transformation/remove_if.hpp>
  13. #include <boost/type_traits/is_class.hpp>
  14. #include <boost/type_traits/is_same.hpp>
  15. #include <boost/mpl/not.hpp>
  16. #include <boost/mpl/vector.hpp>
  17. struct X
  18. {
  19. operator char const*() const
  20. {
  21. return "<X-object>";
  22. }
  23. };
  24. struct Y
  25. {
  26. operator char const*() const
  27. {
  28. return "<Y-object>";
  29. }
  30. };
  31. int
  32. main()
  33. {
  34. using namespace boost::fusion;
  35. using boost::mpl::vector;
  36. using boost::mpl::_;
  37. using boost::mpl::not_;
  38. using boost::is_class;
  39. using boost::is_same;
  40. namespace fusion = boost::fusion;
  41. std::cout << tuple_open('[');
  42. std::cout << tuple_close(']');
  43. std::cout << tuple_delimiter(", ");
  44. /// Testing remove_if
  45. X x; Y y;
  46. typedef fusion::vector<Y, char, long, X, bool, double> vector_type;
  47. vector_type t(y, '@', 987654, x, true, 6.6);
  48. {
  49. std::cout << remove_if<not_<is_class<_> > >(t) << std::endl;
  50. BOOST_TEST((remove_if<not_<is_class<_> > >(t)
  51. == make_vector(y, x)));
  52. }
  53. {
  54. std::cout << remove_if<is_class<_> >(t) << std::endl;
  55. BOOST_TEST((remove_if<is_class<_> >(t)
  56. == make_vector('@', 987654, true, 6.6)));
  57. }
  58. {
  59. typedef vector<Y, char, long, X, bool> mpl_vec;
  60. BOOST_TEST((remove_if<not_<is_class<_> > >(mpl_vec())
  61. == vector<Y, X>()));
  62. BOOST_TEST((remove_if<is_class<_> >(mpl_vec())
  63. == vector<char, long, bool>()));
  64. }
  65. return boost::report_errors();
  66. }