find.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/set/set.hpp>
  10. #include <boost/fusion/container/map/map.hpp>
  11. #include <boost/fusion/algorithm/query/find.hpp>
  12. #include <boost/fusion/iterator/deref.hpp>
  13. #include <boost/mpl/vector.hpp>
  14. #include <string>
  15. struct X
  16. {
  17. operator int() const
  18. {
  19. return 12345;
  20. }
  21. };
  22. int
  23. main()
  24. {
  25. using namespace boost::fusion;
  26. using boost::mpl::identity;
  27. {
  28. typedef vector<int, char, int, double> seq_type;
  29. seq_type seq(12345, 'x', 678910, 3.36);
  30. std::cout << *boost::fusion::find<char>(seq) << std::endl;
  31. BOOST_TEST(*boost::fusion::find<char>(seq) == 'x');
  32. std::cout << *boost::fusion::find<int>(seq) << std::endl;
  33. BOOST_TEST(*boost::fusion::find<int>(seq) == 12345);
  34. std::cout << *boost::fusion::find<double>(seq) << std::endl;
  35. BOOST_TEST(*boost::fusion::find<double>(seq) == 3.36);
  36. BOOST_TEST(boost::fusion::find<bool>(seq) == boost::fusion::end(seq));
  37. }
  38. {
  39. typedef set<int, char, double> seq_type;
  40. seq_type seq(12345, 'x', 3.36);
  41. std::cout << *boost::fusion::find<char>(seq) << std::endl;
  42. BOOST_TEST(*boost::fusion::find<char>(seq) == 'x');
  43. BOOST_TEST(boost::fusion::find<bool>(seq) == boost::fusion::end(seq));
  44. }
  45. {
  46. typedef map<
  47. pair<int, char>
  48. , pair<double, std::string> >
  49. map_type;
  50. map_type seq(
  51. make_pair<int>('X')
  52. , make_pair<double>("Men"));
  53. std::cout << *boost::fusion::find<int>(seq) << std::endl;
  54. std::cout << *boost::fusion::find<double>(seq) << std::endl;
  55. BOOST_TEST((*boost::fusion::find<int>(seq)).second == 'X');
  56. BOOST_TEST((*boost::fusion::find<double>(seq)).second == "Men");
  57. BOOST_TEST(boost::fusion::find<bool>(seq) == boost::fusion::end(seq));
  58. }
  59. {
  60. typedef boost::mpl::vector<int, char, X, double> mpl_vec;
  61. BOOST_TEST((*boost::fusion::find<X>(mpl_vec()) == 12345));
  62. BOOST_TEST(boost::fusion::find<bool>(mpl_vec()) == boost::fusion::end(mpl_vec()));
  63. }
  64. return boost::report_errors();
  65. }