Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/spirit/fusion/test/fold_tests.cpp @ 12

Last change on this file since 12 was 12, checked in by landauf, 18 years ago

added boost

File size: 2.7 KB
Line 
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
18using boost::mpl::if_;
19using boost::mpl::int_;
20using boost::is_same;
21
22struct 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
44struct 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
67int
68main()
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
Note: See TracBrowser for help on using the repository browser.