example.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Boost.Range library
  2. //
  3. // Copyright Thorsten Ottosen 2003-2008. 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/range.hpp>
  11. #include <iterator> // for std::iterator_traits, std::distance()
  12. namespace Foo
  13. {
  14. //
  15. // Our sample UDT. A 'Pair'
  16. // will work as a range when the stored
  17. // elements are iterators.
  18. //
  19. template< class T >
  20. struct Pair
  21. {
  22. T first, last;
  23. };
  24. } // namespace 'Foo'
  25. namespace boost
  26. {
  27. //
  28. // Specialize metafunctions. We must include the range.hpp header.
  29. // We must open the 'boost' namespace.
  30. //
  31. /*
  32. template< class T >
  33. struct range_value< Foo::Pair<T> >
  34. {
  35. typedef typename std::iterator_traits<T>::value_type type;
  36. };
  37. */
  38. template< class T >
  39. struct range_iterator< Foo::Pair<T> >
  40. {
  41. typedef T type;
  42. };
  43. template< class T >
  44. struct range_const_iterator< Foo::Pair<T> >
  45. {
  46. //
  47. // Remark: this is defined similar to 'range_iterator'
  48. // because the 'Pair' type does not distinguish
  49. // between an iterator and a const_iterator.
  50. //
  51. typedef T type;
  52. };
  53. /*
  54. template< class T >
  55. struct range_difference< Foo::Pair<T> >
  56. {
  57. typedef typename std::iterator_traits<T>::difference_type type;
  58. };
  59. */
  60. template< class T >
  61. struct range_size< Foo::Pair<T> >
  62. {
  63. int static_assertion[ sizeof( std::size_t ) >=
  64. sizeof( typename range_difference< Foo::Pair<T> >::type ) ];
  65. typedef std::size_t type;
  66. };
  67. } // namespace 'boost'
  68. namespace Foo
  69. {
  70. //
  71. // The required functions. These should be defined in
  72. // the same namespace as 'Pair', in this case
  73. // in namespace 'Foo'.
  74. //
  75. template< class T >
  76. inline T boost_range_begin( Pair<T>& x )
  77. {
  78. return x.first;
  79. }
  80. template< class T >
  81. inline T boost_range_begin( const Pair<T>& x )
  82. {
  83. return x.first;
  84. }
  85. template< class T >
  86. inline T boost_range_end( Pair<T>& x )
  87. {
  88. return x.last;
  89. }
  90. template< class T >
  91. inline T boost_range_end( const Pair<T>& x )
  92. {
  93. return x.last;
  94. }
  95. template< class T >
  96. inline typename boost::range_size< Pair<T> >::type
  97. boost_range_size( const Pair<T>& x )
  98. {
  99. return std::distance(x.first,x.last);
  100. }
  101. } // namespace 'Foo'
  102. #include <vector>
  103. int main()
  104. {
  105. typedef std::vector<int>::iterator iter;
  106. std::vector<int> vec;
  107. vec.push_back( 42 );
  108. Foo::Pair<iter> pair = { vec.begin(), vec.end() };
  109. const Foo::Pair<iter>& cpair = pair;
  110. //
  111. // Notice that we call 'begin' etc with qualification.
  112. //
  113. iter i = boost::begin( pair );
  114. iter e = boost::end( pair );
  115. i = boost::begin( cpair );
  116. e = boost::end( cpair );
  117. boost::range_size< Foo::Pair<iter> >::type s = boost::size( pair );
  118. s = boost::size( cpair );
  119. boost::range_const_reverse_iterator< Foo::Pair<iter> >::type
  120. ri = boost::rbegin( cpair ),
  121. re = boost::rend( cpair );
  122. //
  123. // Test metafunctions
  124. //
  125. boost::range_value< Foo::Pair<iter> >::type
  126. v = *boost::begin(pair);
  127. boost::range_difference< Foo::Pair<iter> >::type
  128. d = boost::end(pair) - boost::begin(pair);
  129. }