extension_mechanism.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #include <boost/detail/workaround.hpp>
  11. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
  12. # pragma warn -8091 // suppress warning in Boost.Test
  13. # pragma warn -8057 // unused argument argc/argv in Boost.Test
  14. #endif
  15. #include <boost/range.hpp>
  16. #include <boost/test/test_tools.hpp>
  17. #include <boost/test/unit_test.hpp>
  18. #include <vector>
  19. //
  20. // Generic range algorithm
  21. //
  22. template< class Rng >
  23. typename boost::range_iterator<Rng>::type foo_algo( Rng& r )
  24. {
  25. //
  26. // This will only compile for Rng = UDT if the qualified calls
  27. // find boost_range_XXX via ADL.
  28. //
  29. return boost::size(r) == 0u ? boost::begin(r) : boost::end(r);
  30. }
  31. namespace Foo
  32. {
  33. //
  34. // Our sample UDT
  35. //
  36. struct X
  37. {
  38. X() : vec() { }
  39. typedef std::vector<int> data_t;
  40. typedef data_t::iterator iterator;
  41. typedef data_t::const_iterator const_iterator;
  42. data_t vec;
  43. void push_back( int i )
  44. { vec.push_back(i); }
  45. };
  46. //
  47. // The required functions. No type-traits need
  48. // to be defined because X defines the proper set of
  49. // nested types.
  50. //
  51. inline X::iterator range_begin( X& x )
  52. {
  53. return x.vec.begin();
  54. }
  55. inline X::const_iterator range_begin( const X& x )
  56. {
  57. return x.vec.begin();
  58. }
  59. inline X::iterator range_end( X& x )
  60. {
  61. return x.vec.end();
  62. }
  63. inline X::const_iterator range_end( const X& x )
  64. {
  65. return x.vec.end();
  66. }
  67. }
  68. void check_extension()
  69. {
  70. Foo::X x;
  71. x.push_back(3);
  72. const Foo::X x2;
  73. foo_algo( x );
  74. foo_algo( x2 );
  75. }
  76. using boost::unit_test::test_suite;
  77. test_suite* init_unit_test_suite( int argc, char* argv[] )
  78. {
  79. test_suite* test = BOOST_TEST_SUITE( "Range Test Suite" );
  80. test->add( BOOST_TEST_CASE( &check_extension ) );
  81. return test;
  82. }