You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

154 lines
4.2KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_VIEW_REF_HPP
  14. #define RANGES_V3_VIEW_REF_HPP
  15. #include <concepts/concepts.hpp>
  16. #include <range/v3/range_fwd.hpp>
  17. #include <range/v3/range/access.hpp>
  18. #include <range/v3/range/primitives.hpp>
  19. #include <range/v3/range/traits.hpp>
  20. #include <range/v3/utility/addressof.hpp>
  21. #include <range/v3/view/interface.hpp>
  22. #include <range/v3/view/view.hpp>
  23. RANGES_DISABLE_WARNINGS
  24. namespace ranges
  25. {
  26. template<typename Rng>
  27. struct ref_view;
  28. /// \cond
  29. namespace _ref_view_
  30. {
  31. struct adl_hook
  32. {};
  33. template<typename Rng>
  34. constexpr iterator_t<Rng> begin(ref_view<Rng> && rng) noexcept(
  35. noexcept(rng.begin()))
  36. {
  37. return rng.begin();
  38. }
  39. template<typename Rng>
  40. constexpr iterator_t<Rng> begin(ref_view<Rng> const && rng) noexcept(
  41. noexcept(rng.begin()))
  42. {
  43. return rng.begin();
  44. }
  45. template<typename Rng>
  46. constexpr sentinel_t<Rng> end(ref_view<Rng> && rng) noexcept(noexcept(rng.end()))
  47. {
  48. return rng.end();
  49. }
  50. template<typename Rng>
  51. constexpr sentinel_t<Rng> end(ref_view<Rng> const && rng) noexcept(
  52. noexcept(rng.end()))
  53. {
  54. return rng.end();
  55. }
  56. } // namespace _ref_view_
  57. /// \endcond
  58. /// \addtogroup group-views
  59. /// @{
  60. template<typename Rng>
  61. struct ref_view
  62. : view_interface<ref_view<Rng>, range_cardinality<Rng>::value>
  63. , private _ref_view_::adl_hook
  64. {
  65. private:
  66. CPP_assert(range<Rng>);
  67. static_assert(std::is_object<Rng>::value, "");
  68. Rng * rng_ = nullptr; // exposition only
  69. public:
  70. constexpr ref_view() noexcept = default;
  71. constexpr ref_view(Rng & rng) noexcept
  72. : rng_(detail::addressof(rng))
  73. {}
  74. constexpr Rng & base() const noexcept
  75. {
  76. return *rng_;
  77. }
  78. constexpr iterator_t<Rng> begin() const noexcept(noexcept(ranges::begin(*rng_)))
  79. {
  80. return ranges::begin(*rng_);
  81. }
  82. constexpr sentinel_t<Rng> end() const noexcept(noexcept(ranges::end(*rng_)))
  83. {
  84. return ranges::end(*rng_);
  85. }
  86. CPP_member
  87. constexpr auto empty() const noexcept(noexcept(ranges::empty(*rng_)))
  88. -> CPP_ret(bool)( //
  89. requires detail::can_empty_<Rng>)
  90. {
  91. return ranges::empty(*rng_);
  92. }
  93. CPP_member
  94. constexpr auto CPP_fun(size)()(const noexcept(noexcept(ranges::size(*rng_))) //
  95. requires sized_range<Rng>)
  96. {
  97. return ranges::size(*rng_);
  98. }
  99. CPP_member
  100. constexpr auto CPP_fun(data)()(const noexcept(noexcept(ranges::data(*rng_))) //
  101. requires contiguous_range<Rng>)
  102. {
  103. return ranges::data(*rng_);
  104. }
  105. };
  106. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  107. CPP_template(typename R)(requires range<R>) ref_view(R &)->ref_view<R>;
  108. #endif
  109. namespace views
  110. {
  111. struct ref_fn
  112. {
  113. template<typename Rng>
  114. constexpr auto operator()(Rng & rng) const noexcept
  115. -> CPP_ret(ref_view<Rng>)( //
  116. requires range<Rng>)
  117. {
  118. return ref_view<Rng>(rng);
  119. }
  120. template<typename Rng>
  121. void operator()(Rng const && rng) const = delete;
  122. };
  123. /// \relates const_fn
  124. /// \ingroup group-views
  125. RANGES_INLINE_VARIABLE(view<ref_fn>, ref)
  126. } // namespace views
  127. namespace cpp20
  128. {
  129. CPP_template(typename Rng)( //
  130. requires std::is_object<Rng>::value) //
  131. using ref_view = ranges::ref_view<Rng>;
  132. }
  133. } // namespace ranges
  134. RANGES_RE_ENABLE_WARNINGS
  135. #include <range/v3/detail/satisfy_boost_range.hpp>
  136. RANGES_SATISFY_BOOST_RANGE(::ranges::ref_view)
  137. #endif