• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/container/vector/vector_iterator.hpp>
11 #include <boost/fusion/sequence/io/out.hpp>
12 #include <boost/fusion/sequence/comparison/equal_to.hpp>
13 #include <boost/fusion/view/filter_view/filter_view.hpp>
14 #include <boost/fusion/container/generation/make_vector.hpp>
15 #include <boost/fusion/sequence/intrinsic/size.hpp>
16 #include <boost/fusion/container/map.hpp>
17 #include <boost/fusion/sequence/intrinsic/has_key.hpp>
18 #include <boost/fusion/sequence/intrinsic/begin.hpp>
19 #include <boost/fusion/iterator/key_of.hpp>
20 #include <boost/fusion/iterator/value_of_data.hpp>
21 #include <boost/fusion/iterator/deref_data.hpp>
22 #include <boost/type_traits/is_class.hpp>
23 #include <boost/type_traits/is_same.hpp>
24 #include <boost/mpl/arg.hpp>
25 #include <boost/mpl/not.hpp>
26 #include <boost/mpl/vector_c.hpp>
27 #include <boost/mpl/less.hpp>
28 #include <boost/mpl/bool.hpp>
29 #include <boost/mpl/assert.hpp>
30 
31 struct X
32 {
operator char const*X33     operator char const*() const
34     {
35         return "<X-object>";
36     }
37 };
38 
39 struct Y
40 {
operator char const*Y41     operator char const*() const
42     {
43         return "<Y-object>";
44     }
45 };
46 
47 struct reject_all
48 {
49     template<typename T>
50     struct apply : boost::mpl::false_
51     {};
52 };
53 
54 int
main()55 main()
56 {
57     using namespace boost::fusion;
58 
59     using boost::mpl::int_;
60     using boost::mpl::_;
61     using boost::mpl::not_;
62     using boost::mpl::less;
63     using boost::mpl::vector_c;
64     using boost::is_class;
65     using boost::is_same;
66 
67     std::cout << tuple_open('[');
68     std::cout << tuple_close(']');
69     std::cout << tuple_delimiter(", ");
70 
71     {
72         typedef vector<Y, char, long, X, bool, double> vector_type;
73 
74         X x; Y y;
75         vector_type v(y, '@', 987654, x, true, 6.6);
76         typedef filter_view<vector_type const, not_<is_class<_> > > filter_view_type;
77         filter_view_type view(v);
78         std::cout << view << std::endl;
79         BOOST_TEST((view == make_vector('@', 987654, true, 6.6)));
80         BOOST_STATIC_ASSERT(boost::fusion::result_of::size<filter_view_type>::value == 4);
81     }
82 
83     //cschmidt: This is illegal C++. ADL instantiates less<_, int_<3> > - which
84     //leads to compile errors.
85     /*{
86         // $$$ JDG $$$ For some obscure reason, EDG based compilers
87         // (e.g. comeau 4.3.3, intel) have problems with this.
88         // vc7.1 and g++ are ok. The errors from comeau are useless.
89 
90 #ifndef __EDG_VERSION__
91         typedef vector_c<int, 5, 1, 2, 3, 6, 0, -1> vector_type;
92         typedef filter_view<vector_type const, less<_, int_<3> > > filter_view_type;
93         vector_type v;
94         filter_view_type view(v);
95         std::cout << view << std::endl;
96         BOOST_TEST((view == make_vector(1, 2, 0, -1)));
97         BOOST_STATIC_ASSERT(boost::fusion::result_of::size<filter_view_type>::value == 4);
98 #endif
99     }*/
100 
101     {
102         // Previous filtering out all values caused problems as begin<seq> was not equal to end<seq>
103         // Picked up by Andreas Pokorny
104         typedef vector<int> vec;
105         typedef filter_view<vec, reject_all> filter_view_type;
106 
107         BOOST_MPL_ASSERT((boost::fusion::result_of::equal_to<boost::fusion::result_of::begin<filter_view_type>::type, boost::fusion::result_of::end<filter_view_type>::type>));
108     }
109 
110     {
111         typedef map<pair<void, int>, pair<double, std::string> > map_type;
112         map_type m(make_pair<void>(0), make_pair<double>("Bond"));
113 
114         typedef filter_view<map_type const, is_same<_, pair<double, std::string> > > filter_view_type;
115         filter_view_type f(m);
116 
117         BOOST_MPL_ASSERT((boost::fusion::result_of::has_key<filter_view_type, double>::type));
118         BOOST_MPL_ASSERT_NOT((boost::fusion::result_of::has_key<filter_view_type, void>::type));
119 
120         BOOST_MPL_ASSERT((is_same<boost::fusion::result_of::key_of<boost::fusion::result_of::begin<filter_view_type>::type>::type, double>));
121         BOOST_MPL_ASSERT((is_same<boost::fusion::result_of::value_of_data<boost::fusion::result_of::begin<filter_view_type>::type>::type, std::string>));
122 
123         std::cout << deref_data(begin(f)) << std::endl;
124         BOOST_TEST((deref_data(begin(f)) == "Bond"));
125     }
126 
127     return boost::report_errors();
128 }
129 
130