range.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2009-2020 Vladimir Batov.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. #ifndef BOOST_CONVERT_DETAIL_RANGE_HPP
  5. #define BOOST_CONVERT_DETAIL_RANGE_HPP
  6. #include <boost/convert/detail/has_member.hpp>
  7. #include <boost/convert/detail/char.hpp>
  8. #include <boost/range/iterator.hpp>
  9. namespace boost { namespace cnv
  10. {
  11. namespace detail
  12. {
  13. template<typename T, bool is_class> struct is_range : std::false_type {};
  14. template<typename T> struct is_range<T, /*is_class=*/true>
  15. {
  16. BOOST_DECLARE_HAS_MEMBER(has_begin, begin);
  17. BOOST_DECLARE_HAS_MEMBER( has_end, end);
  18. static bool BOOST_CONSTEXPR_OR_CONST value = has_begin<T>::value && has_end<T>::value;
  19. };
  20. }
  21. template<typename T> struct is_range : detail::is_range<typename boost::remove_const<T>::type, boost::is_class<T>::value> {};
  22. template<typename T, typename enable =void> struct range;
  23. template<typename T, typename enable =void> struct iterator;
  24. template<typename T>
  25. struct iterator<T, typename std::enable_if<is_range<T>::value>::type>
  26. {
  27. using type = typename boost::range_iterator<T>::type;
  28. using const_type = typename boost::range_iterator<T const>::type;
  29. using value_type = typename boost::iterator_value<type>::type;
  30. };
  31. template<typename T>
  32. struct iterator<T*, void>
  33. {
  34. using value_type = typename boost::remove_const<T>::type;
  35. using type = T*;
  36. using const_type = value_type const*;
  37. };
  38. template<typename T>
  39. struct range_base
  40. {
  41. using value_type = typename cnv::iterator<T>::value_type;
  42. using iterator = typename cnv::iterator<T>::type;
  43. using const_iterator = typename cnv::iterator<T>::const_type;
  44. using sentry_type = const_iterator;
  45. iterator begin () { return begin_; }
  46. const_iterator begin () const { return begin_; }
  47. void operator++ () { ++begin_; }
  48. // void operator-- () { --end_; }
  49. protected:
  50. range_base (iterator b, iterator e) : begin_(b), end_(e) {}
  51. iterator begin_;
  52. iterator mutable end_;
  53. };
  54. template<typename T>
  55. struct range<T, typename std::enable_if<is_range<T>::value>::type> : public range_base<T>
  56. {
  57. using this_type = range;
  58. using base_type = range_base<T>;
  59. using iterator = typename base_type::iterator;
  60. using const_iterator = typename base_type::const_iterator;
  61. using sentry_type = const_iterator;
  62. range (T& r) : base_type(r.begin(), r.end()) {}
  63. iterator end () { return base_type::end_; }
  64. const_iterator end () const { return base_type::end_; }
  65. sentry_type sentry () const { return base_type::end_; }
  66. std::size_t size () const { return base_type::end_ - base_type::begin_; }
  67. bool empty () const { return base_type::begin_ == base_type::end_; }
  68. };
  69. template<typename T>
  70. struct range<T*, typename std::enable_if<cnv::is_char<T>::value>::type> : public range_base<T*>
  71. {
  72. using this_type = range;
  73. using base_type = range_base<T*>;
  74. using value_type = typename boost::remove_const<T>::type;
  75. using iterator = T*;
  76. using const_iterator = value_type const*;
  77. struct sentry_type
  78. {
  79. friend bool operator!=(iterator it, sentry_type) { return !!*it; }
  80. };
  81. range (iterator b, iterator e =0) : base_type(b, e) {}
  82. iterator end () { return base_type::end_ ? base_type::end_ : (base_type::end_ = base_type::begin_ + size()); }
  83. const_iterator end () const { return base_type::end_ ? base_type::end_ : (base_type::end_ = base_type::begin_ + size()); }
  84. sentry_type sentry () const { return sentry_type(); }
  85. std::size_t size () const { return std::char_traits<value_type>::length(base_type::begin_); }
  86. bool empty () const { return !*base_type::begin_; }
  87. };
  88. template<typename T>
  89. struct range<T* const, void> : public range<T*>
  90. {
  91. range (T* b, T* e =0) : range<T*>(b, e) {}
  92. };
  93. template <typename T, std::size_t N>
  94. struct range<T [N], void> : public range<T*>
  95. {
  96. range (T* b, T* e =0) : range<T*>(b, e) {}
  97. };
  98. }}
  99. #endif // BOOST_CONVERT_DETAIL_RANGE_HPP