| 1 | /*============================================================================= |
|---|
| 2 | Copyright (c) 2003 Joel de Guzman |
|---|
| 3 | |
|---|
| 4 | Use, modification and distribution is subject to the Boost Software |
|---|
| 5 | License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 6 | http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 7 | ==============================================================================*/ |
|---|
| 8 | #include <boost/detail/lightweight_test.hpp> |
|---|
| 9 | #include <boost/spirit/fusion/sequence/tuple.hpp> |
|---|
| 10 | #include <boost/spirit/fusion/sequence/io.hpp> |
|---|
| 11 | #include <boost/spirit/fusion/algorithm/fold.hpp> |
|---|
| 12 | #include <boost/type_traits/is_same.hpp> |
|---|
| 13 | #include <boost/mpl/if.hpp> |
|---|
| 14 | #include <boost/mpl/next.hpp> |
|---|
| 15 | #include <boost/mpl/int.hpp> |
|---|
| 16 | #include <boost/mpl/vector.hpp> |
|---|
| 17 | |
|---|
| 18 | using boost::mpl::if_; |
|---|
| 19 | using boost::mpl::int_; |
|---|
| 20 | using boost::is_same; |
|---|
| 21 | |
|---|
| 22 | struct add_ints_only |
|---|
| 23 | { |
|---|
| 24 | template <typename T, typename State> |
|---|
| 25 | struct apply |
|---|
| 26 | { |
|---|
| 27 | typedef State type; |
|---|
| 28 | }; |
|---|
| 29 | |
|---|
| 30 | template <typename T, typename State> |
|---|
| 31 | State const& |
|---|
| 32 | operator()(T const& x, State const& state) const |
|---|
| 33 | { |
|---|
| 34 | return state; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | int |
|---|
| 38 | operator()(int x, int state) const |
|---|
| 39 | { |
|---|
| 40 | return x + state; |
|---|
| 41 | } |
|---|
| 42 | }; |
|---|
| 43 | |
|---|
| 44 | struct count_ints |
|---|
| 45 | { |
|---|
| 46 | template <typename T, typename CountT> |
|---|
| 47 | struct apply |
|---|
| 48 | { |
|---|
| 49 | typedef typename |
|---|
| 50 | if_< |
|---|
| 51 | is_same<T, int> |
|---|
| 52 | , typename boost::mpl::next<CountT>::type |
|---|
| 53 | , CountT |
|---|
| 54 | >::type |
|---|
| 55 | type; |
|---|
| 56 | }; |
|---|
| 57 | |
|---|
| 58 | template <typename T, typename CountT> |
|---|
| 59 | typename apply<T, CountT>::type |
|---|
| 60 | operator()(T const&, CountT const&) const |
|---|
| 61 | { |
|---|
| 62 | typedef typename apply<T, CountT>::type result; |
|---|
| 63 | return result(); |
|---|
| 64 | } |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | int |
|---|
| 68 | main() |
|---|
| 69 | { |
|---|
| 70 | using namespace boost::fusion; |
|---|
| 71 | using boost::mpl::vector; |
|---|
| 72 | namespace fusion = boost::fusion; |
|---|
| 73 | |
|---|
| 74 | /// Testing fold |
|---|
| 75 | |
|---|
| 76 | { |
|---|
| 77 | typedef tuple<int, char, int, double> tuple_type; |
|---|
| 78 | tuple_type t(12345, 'x', 678910, 3.36); |
|---|
| 79 | int result = fold(t, 0, add_ints_only()); |
|---|
| 80 | std::cout << result << std::endl; |
|---|
| 81 | BOOST_TEST(result == 12345+678910); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | { |
|---|
| 85 | typedef tuple<int> tuple_type; |
|---|
| 86 | tuple_type t(12345); |
|---|
| 87 | |
|---|
| 88 | int n = fusion::fold(t, FUSION_INT(0)(), count_ints()); |
|---|
| 89 | std::cout << n << std::endl; |
|---|
| 90 | BOOST_TEST(n == 1); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | { |
|---|
| 94 | typedef tuple<int, char, int, double, int> tuple_type; |
|---|
| 95 | tuple_type t(12345, 'x', 678910, 3.36, 8756); |
|---|
| 96 | |
|---|
| 97 | int n = fusion::fold(t, FUSION_INT(0)(), count_ints()); |
|---|
| 98 | std::cout << n << std::endl; |
|---|
| 99 | BOOST_TEST(n == 3); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | { |
|---|
| 103 | typedef vector<int, char, int, double, int> mpl_vec; |
|---|
| 104 | int n = fusion::fold(mpl_vec(), FUSION_INT(0)(), count_ints()); |
|---|
| 105 | std::cout << n << std::endl; |
|---|
| 106 | BOOST_TEST(n == 3); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | return boost::report_errors(); |
|---|
| 110 | } |
|---|
| 111 | |
|---|