| 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(__BORLANDC__, BOOST_TESTED_AT(0x564)) | 
|---|
| 15 | #  pragma warn -8091 // supress 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 > | 
|---|
| 28 | typename boost::range_result_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 |         { | 
|---|
| 44 |                 typedef std::vector<int>       data_t; | 
|---|
| 45 |                 typedef data_t::iterator       iterator; | 
|---|
| 46 |                 typedef data_t::const_iterator const_iterator; | 
|---|
| 47 |                 typedef data_t::size_type      size_type; | 
|---|
| 48 |  | 
|---|
| 49 |                 data_t vec; | 
|---|
| 50 |  | 
|---|
| 51 |                 void push_back( int i ) | 
|---|
| 52 |                 { vec.push_back(i); } | 
|---|
| 53 |         }; | 
|---|
| 54 |  | 
|---|
| 55 |         // | 
|---|
| 56 |         // The required functions. No type-traits need | 
|---|
| 57 |         // to be defined because X defines the proper set of | 
|---|
| 58 |         // nested types. | 
|---|
| 59 |         // | 
|---|
| 60 |         inline X::iterator boost_range_begin( X& x ) | 
|---|
| 61 |         { | 
|---|
| 62 |                 return x.vec.begin(); | 
|---|
| 63 |         } | 
|---|
| 64 |  | 
|---|
| 65 |  | 
|---|
| 66 |         inline X::const_iterator boost_range_begin( const X& x ) | 
|---|
| 67 |         { | 
|---|
| 68 |                 return x.vec.begin(); | 
|---|
| 69 |         } | 
|---|
| 70 |  | 
|---|
| 71 |  | 
|---|
| 72 |         inline X::iterator boost_range_end( X& x ) | 
|---|
| 73 |         { | 
|---|
| 74 |                 return x.vec.end(); | 
|---|
| 75 |         } | 
|---|
| 76 |  | 
|---|
| 77 |         inline X::const_iterator boost_range_end( const X& x ) | 
|---|
| 78 |         { | 
|---|
| 79 |                 return x.vec.end(); | 
|---|
| 80 |         } | 
|---|
| 81 |  | 
|---|
| 82 |         inline X::size_type boost_range_size( const X& x ) | 
|---|
| 83 |         { | 
|---|
| 84 |                 return x.vec.size(); | 
|---|
| 85 |         } | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | void check_extension() | 
|---|
| 89 | { | 
|---|
| 90 |         Foo::X x; | 
|---|
| 91 |         x.push_back(3); | 
|---|
| 92 |         const Foo::X x2; | 
|---|
| 93 |  | 
|---|
| 94 |         foo_algo( x ); | 
|---|
| 95 |         foo_algo( x2 ); | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | using boost::unit_test::test_suite; | 
|---|
| 99 |  | 
|---|
| 100 | test_suite* init_unit_test_suite( int argc, char* argv[] ) | 
|---|
| 101 | { | 
|---|
| 102 |     test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" ); | 
|---|
| 103 |  | 
|---|
| 104 |     test->add( BOOST_TEST_CASE( &check_extension ) ); | 
|---|
| 105 |  | 
|---|
| 106 |     return test; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 |  | 
|---|
| 111 |  | 
|---|
| 112 |  | 
|---|