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