• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*=============================================================================
2     Copyright (c) 2001-2011 Joel de Guzman
3     Copyright (c) 2018 Kohei Takahashi
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/core/lightweight_test.hpp>
9 #include <boost/fusion/container/vector/vector.hpp>
10 #include <boost/fusion/adapted/mpl.hpp>
11 #include <boost/fusion/sequence/io/out.hpp>
12 #include <boost/fusion/sequence/comparison/equal_to.hpp>
13 #include <boost/fusion/algorithm/iteration/for_each.hpp>
14 #include <boost/mpl/vector_c.hpp>
15 
16 struct print
17 {
18     template <typename T>
operator ()print19     void operator()(T const& v) const
20     {
21         std::cout << "[ " << v << " ] ";
22     }
23 };
24 
25 struct increment
26 {
27     template <typename T>
operator ()increment28     void operator()(T& v) const
29     {
30         ++v;
31     }
32 };
33 
34 struct mutable_increment : increment
35 {
36     template <typename T>
operator ()mutable_increment37     void operator()(T& v)
38     {
39         return increment::operator()(v);
40     }
41 };
42 
43 int
main()44 main()
45 {
46     using namespace boost::fusion;
47     using boost::mpl::vector_c;
48     namespace fusion = boost::fusion;
49 
50     {
51         typedef vector<int, char, double, char const*> vector_type;
52         vector_type v(1, 'x', 3.3, "Ruby");
53         for_each(v, print());
54         std::cout << std::endl;
55     }
56 
57     {
58         char const ruby[] = "Ruby";
59         typedef vector<int, char, double, char const*> vector_type;
60         vector_type v(1, 'x', 3.3, ruby);
61         for_each(v, increment());
62         BOOST_TEST_EQ(v, vector_type(2, 'y', 4.3, ruby + 1));
63         std::cout << v << std::endl;
64     }
65 
66     {
67         char const ruby[] = "Ruby";
68         typedef vector<int, char, double, char const*> vector_type;
69         vector_type v(1, 'x', 3.3, ruby);
70         for_each(v, mutable_increment());
71         BOOST_TEST_EQ(v, vector_type(2, 'y', 4.3, ruby + 1));
72         std::cout << v << std::endl;
73     }
74 
75     {
76         typedef vector_c<int, 2, 3, 4, 5, 6> mpl_vec;
77         fusion::for_each(mpl_vec(), print());
78         std::cout << std::endl;
79     }
80 
81     return boost::report_errors();
82 }
83 
84