• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Range library
2 //
3 //  Copyright Thorsten Ottosen 2003-2004. 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)
7 //
8 // For more information, see http://www.boost.org/libs/range/
9 //
10 
11 
12 #include <boost/detail/workaround.hpp>
13 
14 #if BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564))
15 #  pragma warn -8091 // suppress warning in Boost.Test
16 #  pragma warn -8057 // unused argument argc/argv in Boost.Test
17 #endif
18 
19 #include <boost/range.hpp>
20 #include <boost/test/test_tools.hpp>
21 #include <boost/test/unit_test.hpp>
22 #include <vector>
23 
24 //
25 // Generic range algorithm
26 //
27 template< class Rng >
foo_algo(Rng & r)28 typename boost::range_iterator<Rng>::type foo_algo( Rng& r )
29 {
30         //
31         // This will only compile for Rng = UDT if the qualified calls
32         // find boost_range_XXX via ADL.
33         //
34         return boost::size(r) == 0u ? boost::begin(r) : boost::end(r);
35 }
36 
37 namespace Foo
38 {
39         //
40         // Our sample UDT
41         //
42         struct X
43         {
XFoo::X44           X() : vec() { }
45 
46                 typedef std::vector<int>       data_t;
47                 typedef data_t::iterator       iterator;
48                 typedef data_t::const_iterator const_iterator;
49 
50                 data_t vec;
51 
push_backFoo::X52                 void push_back( int i )
53                 { vec.push_back(i); }
54         };
55 
56         //
57         // The required functions. No type-traits need
58         // to be defined because X defines the proper set of
59         // nested types.
60         //
range_begin(X & x)61         inline X::iterator range_begin( X& x )
62         {
63                 return x.vec.begin();
64         }
65 
66 
range_begin(const X & x)67         inline X::const_iterator range_begin( const X& x )
68         {
69                 return x.vec.begin();
70         }
71 
72 
range_end(X & x)73         inline X::iterator range_end( X& x )
74         {
75                 return x.vec.end();
76         }
77 
range_end(const X & x)78         inline X::const_iterator range_end( const X& x )
79         {
80                 return x.vec.end();
81         }
82 
83 }
84 
check_extension()85 void check_extension()
86 {
87         Foo::X x;
88         x.push_back(3);
89         const Foo::X x2;
90 
91         foo_algo( x );
92         foo_algo( x2 );
93 }
94 
95 using boost::unit_test::test_suite;
96 
init_unit_test_suite(int argc,char * argv[])97 test_suite* init_unit_test_suite( int argc, char* argv[] )
98 {
99     test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" );
100 
101     test->add( BOOST_TEST_CASE( &check_extension ) );
102 
103     return test;
104 }
105 
106 
107 
108 
109 
110