3 // Copyright Neil Groves 2010. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see http://www.boost.org/libs/range/
11 //[type_erased_example
12 #include <boost/range/adaptor/type_erased.hpp>
13 #include <boost/range/algorithm/copy.hpp>
14 #include <boost/assign.hpp>
15 #include <boost/foreach.hpp>
21 #include <boost/test/test_tools.hpp>
22 #include <boost/test/unit_test.hpp>
26 namespace boost_range_test
28 namespace type_erased_example
32 // The client interface from an OO perspective merely requires a sequence
33 // of integers that can be forward traversed
34 typedef boost::any_range<
36 , boost::forward_traversal_tag
43 void display_integers(const integer_range& rng)
46 std::ostream_iterator<int>(std::cout, ","));
48 std::cout << std::endl;
56 using namespace boost::assign;
57 using namespace boost::adaptors;
59 // Under most conditions one would simply use an appropriate
60 // any_range as a function parameter. The type_erased adaptor
61 // is often superfluous. However because the type_erased
62 // adaptor is applied to a range, we can use default template
63 // arguments that are generated in conjunction with the
64 // range type to which we are applying the adaptor.
66 std::vector<int> input;
69 // Note that this call is to a non-template function
70 server::display_integers(input);
72 std::list<int> input2;
75 // Note that this call is to the same non-tempate function
76 server::display_integers(input2);
79 input2 += 11,12,13,14,15;
81 // Calling using the adaptor looks like this:
82 // Notice that here I have a type_erased that would be a
83 // bidirectional_traversal_tag, but this is convertible
84 // to the forward_traversal_tag equivalent hence this
86 server::display_integers(input2 | type_erased<>());
88 // However we may simply wish to define an adaptor that
89 // takes a range and makes it into an appropriate
90 // forward_traversal any_range...
91 typedef boost::adaptors::type_erased<
93 , boost::forward_traversal_tag
94 > type_erased_forward;
96 // This adaptor can turn other containers with different
97 // value_types and reference_types into the appropriate
100 server::display_integers(input2 | type_erased_forward());
104 //=int main(int argc, const char* argv[])
111 } // namespace type_erased_example
112 } // namespace boost_range_test
113 } // anonymous namespace
115 boost::unit_test::test_suite*
116 init_unit_test_suite(int argc, char* argv[])
118 boost::unit_test::test_suite* test
119 = BOOST_TEST_SUITE( "RangeTestSuite.adaptor.type_erased_example" );
121 test->add( BOOST_TEST_CASE( &boost_range_test::type_erased_example::client::run) );