size_type.hpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef BOOST_RANGE_SIZE_TYPE_HPP
  11. #define BOOST_RANGE_SIZE_TYPE_HPP
  12. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include <boost/range/config.hpp>
  16. #include <boost/range/difference_type.hpp>
  17. #include <boost/range/concepts.hpp>
  18. #include <boost/range/has_range_iterator.hpp>
  19. #include <boost/utility/enable_if.hpp>
  20. #include <boost/type_traits/make_unsigned.hpp>
  21. #include <boost/type_traits/remove_const.hpp>
  22. #include <cstddef>
  23. #include <utility>
  24. namespace boost
  25. {
  26. namespace detail
  27. {
  28. //////////////////////////////////////////////////////////////////////////
  29. // default
  30. //////////////////////////////////////////////////////////////////////////
  31. template<typename T>
  32. class has_size_type
  33. {
  34. typedef char no_type;
  35. struct yes_type { char dummy[2]; };
  36. template<typename C>
  37. static yes_type test(BOOST_DEDUCED_TYPENAME C::size_type x);
  38. template<typename C>
  39. static no_type test(...);
  40. public:
  41. static const bool value = sizeof(test<T>(0)) == sizeof(yes_type);
  42. };
  43. template<typename C, typename Enabler=void>
  44. struct range_size_
  45. {
  46. typedef BOOST_DEDUCED_TYPENAME make_unsigned<
  47. BOOST_DEDUCED_TYPENAME range_difference<C>::type
  48. >::type type;
  49. };
  50. template<typename C>
  51. struct range_size_<
  52. C,
  53. BOOST_DEDUCED_TYPENAME ::boost::enable_if<has_size_type<C>, void>::type
  54. >
  55. {
  56. typedef BOOST_DEDUCED_TYPENAME C::size_type type;
  57. };
  58. template<typename C, bool B = range_detail::has_type< range_iterator<C> >::value>
  59. struct range_size
  60. { };
  61. template<typename C>
  62. struct range_size<C, true>
  63. : range_size_<C>
  64. { };
  65. }
  66. template< class T >
  67. struct range_size :
  68. detail::range_size<T>
  69. { };
  70. } // namespace boost
  71. #endif