size.hpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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_DETAIL_SIZE_HPP
  11. #define BOOST_RANGE_DETAIL_SIZE_HPP
  12. #include <boost/config.hpp> // BOOST_MSVC
  13. #include <boost/detail/workaround.hpp>
  14. #if BOOST_WORKAROUND(BOOST_MSVC, < 1300)
  15. # include <boost/range/detail/vc6/size.hpp>
  16. #else
  17. # include <boost/range/detail/implementation_help.hpp>
  18. # include <boost/range/detail/size_type.hpp>
  19. # include <boost/range/detail/common.hpp>
  20. # if BOOST_WORKAROUND(BOOST_MSVC, == 1300)
  21. # include <boost/range/detail/remove_extent.hpp>
  22. # endif
  23. # include <iterator>
  24. namespace boost
  25. {
  26. namespace range_detail
  27. {
  28. template< typename T >
  29. struct range_size_;
  30. //////////////////////////////////////////////////////////////////////
  31. // default
  32. //////////////////////////////////////////////////////////////////////
  33. template<>
  34. struct range_size_<std_container_>
  35. {
  36. template< typename C >
  37. static BOOST_RANGE_DEDUCED_TYPENAME C::size_type fun( const C& c )
  38. {
  39. return c.size();
  40. };
  41. };
  42. //////////////////////////////////////////////////////////////////////
  43. // pair
  44. //////////////////////////////////////////////////////////////////////
  45. template<>
  46. struct range_size_<std_pair_>
  47. {
  48. template< typename P >
  49. static BOOST_RANGE_DEDUCED_TYPENAME range_size<P>::type
  50. fun( const P& p )
  51. {
  52. return std::distance( p.first, p.second );
  53. }
  54. };
  55. //////////////////////////////////////////////////////////////////////
  56. // array
  57. //////////////////////////////////////////////////////////////////////
  58. template<>
  59. struct range_size_<array_>
  60. {
  61. #if !BOOST_WORKAROUND(BOOST_MSVC, <= 1300)
  62. template< typename T, std::size_t sz >
  63. static std::size_t fun( T BOOST_RANGE_ARRAY_REF()[sz] )
  64. {
  65. return sz;
  66. }
  67. #else
  68. template<typename T>
  69. static std::size_t fun(T& t)
  70. {
  71. return remove_extent<T>::size;
  72. }
  73. #endif
  74. };
  75. template<>
  76. struct range_size_<char_array_>
  77. {
  78. template< typename T, std::size_t sz >
  79. static std::size_t fun( T BOOST_RANGE_ARRAY_REF()[sz] )
  80. {
  81. return boost::range_detail::array_size( boost_range_array );
  82. }
  83. };
  84. template<>
  85. struct range_size_<wchar_t_array_>
  86. {
  87. template< typename T, std::size_t sz >
  88. static std::size_t fun( T BOOST_RANGE_ARRAY_REF()[sz] )
  89. {
  90. return boost::range_detail::array_size( boost_range_array );
  91. }
  92. };
  93. //////////////////////////////////////////////////////////////////////
  94. // string
  95. //////////////////////////////////////////////////////////////////////
  96. template<>
  97. struct range_size_<char_ptr_>
  98. {
  99. static std::size_t fun( const char* s )
  100. {
  101. return boost::range_detail::str_size( s );
  102. }
  103. };
  104. template<>
  105. struct range_size_<const_char_ptr_>
  106. {
  107. static std::size_t fun( const char* s )
  108. {
  109. return boost::range_detail::str_size( s );
  110. }
  111. };
  112. template<>
  113. struct range_size_<wchar_t_ptr_>
  114. {
  115. static std::size_t fun( const wchar_t* s )
  116. {
  117. return boost::range_detail::str_size( s );
  118. }
  119. };
  120. template<>
  121. struct range_size_<const_wchar_t_ptr_>
  122. {
  123. static std::size_t fun( const wchar_t* s )
  124. {
  125. return boost::range_detail::str_size( s );
  126. }
  127. };
  128. } // namespace 'range_detail'
  129. template< typename C >
  130. BOOST_RANGE_DEDUCED_TYPENAME range_size<C>::type
  131. size( const C& c )
  132. {
  133. return range_detail::range_size_< BOOST_RANGE_DEDUCED_TYPENAME range_detail::range<C>::type >::fun( c );
  134. }
  135. } // namespace 'boost'
  136. # endif
  137. #endif