1 /*=============================================================================
2     Copyright (c) 2001-2011 Joel de Guzman
3 
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/adapted/mpl.hpp>
10 #include <boost/fusion/sequence/io/out.hpp>
11 #include <boost/fusion/container/generation/make_vector.hpp>
12 #include <boost/fusion/sequence/comparison/equal_to.hpp>
13 #include <boost/fusion/view/transform_view/transform_view.hpp>
14 #include <boost/fusion/sequence/intrinsic/begin.hpp>
15 #include <boost/fusion/sequence/intrinsic/at.hpp>
16 #include <boost/fusion/sequence/intrinsic/value_at.hpp>
17 #include <boost/fusion/iterator/next.hpp>
18 #include <boost/fusion/iterator/prior.hpp>
19 #include <boost/fusion/iterator/advance.hpp>
20 #include <boost/fusion/iterator/deref.hpp>
21 #include <boost/fusion/iterator/distance.hpp>
22 
23 #include <boost/mpl/range_c.hpp>
24 #include <boost/mpl/assert.hpp>
25 #include <boost/type_traits/is_same.hpp>
26 
27 struct square
28 {
29     template<typename T>
30     struct result;
31 
32     template <typename T>
33     struct result<square(T)>
34     {
35         typedef int type;
36     };
37 
38     template <typename T>
operator ()square39     int operator()(T x) const
40     {
41         return x * x;
42     }
43 };
44 
45 struct add
46 {
47     template<typename T>
48     struct result;
49 
50     template <typename A, typename B>
51     struct result<add(A,B)>
52     {
53         typedef int type;
54     };
55 
56     template <typename A, typename B>
operator ()add57     int operator()(A a, B b) const
58     {
59         return a + b;
60     }
61 };
62 
63 int
main()64 main()
65 {
66     using namespace boost::fusion;
67 
68     std::cout << tuple_open('[');
69     std::cout << tuple_close(']');
70     std::cout << tuple_delimiter(", ");
71 
72 /// Testing the transform_view
73 
74     {
75         typedef boost::mpl::range_c<int, 5, 9> sequence_type;
76         sequence_type sequence;
77         square sq;
78         typedef transform_view<sequence_type, square> xform_type;
79         xform_type xform(sequence, sq);
80 
81         std::cout << xform << std::endl;
82         BOOST_TEST((xform == make_vector(25, 36, 49, 64)));
83 
84         typedef boost::fusion::result_of::begin<xform_type>::type first_type;
85         first_type first_it(boost::fusion::begin(xform));
86 
87         typedef boost::fusion::result_of::next<first_type>::type next_type;
88         next_type next_it(boost::fusion::next(first_it));
89         BOOST_TEST((*next_it == 36));
90         BOOST_TEST((*boost::fusion::prior(next_it) == 25));
91         BOOST_TEST((boost::fusion::distance(first_it, next_it) == 1));
92 
93         BOOST_TEST((*boost::fusion::advance_c<3>(boost::fusion::begin(xform)) == 64));
94         BOOST_TEST((boost::fusion::at_c<2>(xform) == 49));
95         BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_c<xform_type, 0>::type, int>));
96     }
97 
98     {
99         typedef boost::mpl::range_c<int, 5, 9> sequence1_type;
100         typedef boost::mpl::range_c<int, 10, 14> sequence2_type;
101         sequence1_type sequence1;
102         sequence2_type sequence2;
103         add f;
104         typedef transform_view<sequence1_type, sequence2_type, add> xform_type;
105         xform_type xform(sequence1, sequence2, f);
106 
107         std::cout << xform << std::endl;
108         BOOST_TEST((xform == make_vector(15, 17, 19, 21)));
109         BOOST_TEST((boost::fusion::at_c<2>(xform) == 19));
110         BOOST_MPL_ASSERT((boost::is_same<boost::fusion::result_of::value_at_c<xform_type, 0>::type, int>));
111     }
112 
113     return boost::report_errors();
114 }
115 
116