• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/sequence/io/out.hpp>
10 #include <boost/fusion/view/single_view/single_view.hpp>
11 #include <boost/fusion/sequence/intrinsic/at.hpp>
12 #include <boost/fusion/sequence/intrinsic/begin.hpp>
13 #include <boost/fusion/sequence/intrinsic/end.hpp>
14 #include <boost/fusion/sequence/intrinsic/size.hpp>
15 #include <boost/fusion/sequence/intrinsic/front.hpp>
16 #include <boost/fusion/sequence/intrinsic/back.hpp>
17 #include <boost/fusion/sequence/intrinsic/empty.hpp>
18 #include <boost/fusion/sequence/intrinsic/value_at.hpp>
19 #include <boost/fusion/iterator/next.hpp>
20 #include <boost/fusion/iterator/prior.hpp>
21 #include <boost/fusion/iterator/deref.hpp>
22 #include <boost/fusion/iterator/advance.hpp>
23 #include <boost/fusion/iterator/distance.hpp>
24 #include <boost/mpl/int.hpp>
25 #include <boost/mpl/assert.hpp>
26 #include <boost/type_traits/is_same.hpp>
27 
28 struct X {};
29 
30 template <typename OS>
operator <<(OS & os,X const &)31 OS& operator<<(OS& os, X const&)
32 {
33     os << "<X-object>";
34     return os;
35 }
36 
foo()37 void foo() {}
38 
39 int
main()40 main()
41 {
42     using namespace boost::fusion;
43 
44     std::cout << tuple_open('[');
45     std::cout << tuple_close(']');
46     std::cout << tuple_delimiter(", ");
47 
48     {
49         single_view<int> view1(3);
50         std::cout << view1 << std::endl;
51 
52 #ifdef FUSION_TEST_FAIL
53         // single_view is immutable
54         *begin(view1) += 4;
55 #endif
56         std::cout << view1 << std::endl;
57         BOOST_TEST(*begin(view1) == 3);
58         BOOST_TEST(at<boost::mpl::int_<0> >(view1) == 3);
59         BOOST_TEST(view1.val == 3);
60         BOOST_TEST(3 == front(view1));
61         BOOST_TEST(3 == back(view1));
62         BOOST_TEST(!empty(view1));
63         BOOST_TEST(next(begin(view1)) == end(view1));
64         BOOST_TEST(prior(end(view1)) == begin(view1));
65         BOOST_TEST(!(next(begin(view1)) != end(view1)));
66         BOOST_TEST(!(prior(end(view1)) != begin(view1)));
67         BOOST_TEST(1 == distance(begin(view1), end(view1)));
68         BOOST_TEST(0 == distance(end(view1), end(view1)));
69         BOOST_TEST(0 == distance(begin(view1), begin(view1)));
70         BOOST_TEST(end(view1) == advance<boost::mpl::int_<1> >(begin(view1)));
71         BOOST_TEST(begin(view1) == advance<boost::mpl::int_<0> >(begin(view1)));
72         BOOST_TEST(end(view1) == advance<boost::mpl::int_<0> >(end(view1)));
73         BOOST_TEST(begin(view1) == advance<boost::mpl::int_<-1> >(end(view1)));
74         BOOST_TEST(end(view1) == advance_c<1>(begin(view1)));
75         BOOST_TEST(begin(view1) == advance_c<0>(begin(view1)));
76         BOOST_TEST(end(view1) == advance_c<0>(end(view1)));
77         BOOST_TEST(begin(view1) == advance_c<-1>(end(view1)));
78         BOOST_TEST(1 == size(view1));
79         BOOST_MPL_ASSERT((boost::is_same<int, boost::fusion::result_of::value_at<single_view<int>, boost::mpl::int_<0> >::type>));
80 
81         single_view<X> view2;
82         std::cout << view2 << std::endl;
83     }
84 
85     {
86         std::cout << make_single_view(1) << std::endl;
87         std::cout << make_single_view("Hello, World") << std::endl;
88         std::cout << make_single_view(&foo) << std::endl;
89     }
90 
91     return boost::report_errors();
92 }
93 
94