segmented_find_if.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2011 Eric Niebler
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/fusion/container/vector/vector.hpp>
  9. #include <boost/fusion/algorithm/query/find_if.hpp>
  10. #include <boost/fusion/container/generation/make_vector.hpp>
  11. #include <boost/mpl/placeholders.hpp>
  12. #include <boost/type_traits/is_same.hpp>
  13. #include "../sequence/tree.hpp"
  14. struct not_there {};
  15. template<typename Tree>
  16. void
  17. process_tree(Tree const &tree)
  18. {
  19. using namespace boost;
  20. using mpl::_;
  21. typedef typename boost::fusion::result_of::find_if<Tree const, is_same<_,short> >::type short_iter;
  22. typedef typename boost::fusion::result_of::find_if<Tree const, is_same<_,float> >::type float_iter;
  23. typedef typename boost::fusion::result_of::find_if<Tree const, is_same<_,not_there> >::type not_there_iter;
  24. // find_if of a segmented data structure returns generic
  25. // segmented iterators
  26. short_iter si = fusion::find_if<is_same<_,short> >(tree);
  27. float_iter fi = fusion::find_if<is_same<_,float> >(tree);
  28. // they behave like ordinary Fusion iterators ...
  29. BOOST_TEST((*si == short('d')));
  30. BOOST_TEST((*fi == float(1)));
  31. // Searching for something that's not there should return the end iterator.
  32. not_there_iter nti = fusion::find_if<is_same<_,not_there> >(tree);
  33. BOOST_TEST((nti == fusion::end(tree)));
  34. }
  35. int
  36. main()
  37. {
  38. using namespace boost::fusion;
  39. process_tree(
  40. make_tree(
  41. make_vector(double(0),'B')
  42. , make_tree(
  43. make_vector(1,2,long(3))
  44. , make_tree(make_vector('a','b','c'))
  45. , make_tree(make_vector(short('d'),'e','f'))
  46. )
  47. , make_tree(
  48. make_vector(4,5,6)
  49. , make_tree(make_vector(float(1),'h','i'))
  50. , make_tree(make_vector('j','k','l'))
  51. )
  52. )
  53. );
  54. return boost::report_errors();
  55. }