size.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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_HPP
  11. #define BOOST_RANGE_SIZE_HPP
  12. #if defined(_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include <boost/range/config.hpp>
  16. #include <boost/range/begin.hpp>
  17. #include <boost/range/end.hpp>
  18. #include <boost/range/size_type.hpp>
  19. #include <boost/range/detail/has_member_size.hpp>
  20. #include <boost/assert.hpp>
  21. #include <boost/cstdint.hpp>
  22. #include <boost/utility.hpp>
  23. namespace boost
  24. {
  25. namespace range_detail
  26. {
  27. template<class SinglePassRange>
  28. inline typename ::boost::enable_if<
  29. has_member_size<SinglePassRange>,
  30. typename range_size<const SinglePassRange>::type
  31. >::type
  32. range_calculate_size(const SinglePassRange& rng)
  33. {
  34. return rng.size();
  35. }
  36. template<class SinglePassRange>
  37. inline typename disable_if<
  38. has_member_size<SinglePassRange>,
  39. typename range_size<const SinglePassRange>::type
  40. >::type
  41. range_calculate_size(const SinglePassRange& rng)
  42. {
  43. return std::distance(boost::begin(rng), boost::end(rng));
  44. }
  45. }
  46. template<class SinglePassRange>
  47. inline typename range_size<const SinglePassRange>::type
  48. size(const SinglePassRange& rng)
  49. {
  50. // Very strange things happen on some compilers that have the range concept
  51. // asserts disabled. This preprocessor condition is clearly redundant on a
  52. // working compiler but is vital for at least some compilers such as clang 4.2
  53. // but only on the Mac!
  54. #if BOOST_RANGE_ENABLE_CONCEPT_ASSERT == 1
  55. BOOST_RANGE_CONCEPT_ASSERT((boost::SinglePassRangeConcept<SinglePassRange>));
  56. #endif
  57. #if !BOOST_WORKAROUND(BOOST_BORLANDC, BOOST_TESTED_AT(0x564)) && \
  58. !BOOST_WORKAROUND(__GNUC__, < 3) \
  59. /**/
  60. using namespace range_detail;
  61. #endif
  62. return range_calculate_size(rng);
  63. }
  64. } // namespace 'boost'
  65. #endif