facade.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-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_FACADE_HPP
  14. #define RANGES_V3_VIEW_FACADE_HPP
  15. #include <type_traits>
  16. #include <utility>
  17. #include <meta/meta.hpp>
  18. #include <concepts/concepts.hpp>
  19. #include <range/v3/range_fwd.hpp>
  20. #include <range/v3/iterator/basic_iterator.hpp>
  21. #include <range/v3/iterator/default_sentinel.hpp>
  22. #include <range/v3/iterator/traits.hpp>
  23. #include <range/v3/view/interface.hpp>
  24. #include <range/v3/detail/prologue.hpp>
  25. namespace ranges
  26. {
  27. /// \cond
  28. namespace detail
  29. {
  30. template<typename Derived>
  31. using begin_cursor_t = detail::decay_t<decltype(
  32. range_access::begin_cursor(std::declval<Derived &>()))>;
  33. template<typename Derived>
  34. using end_cursor_t = detail::decay_t<decltype(
  35. range_access::end_cursor(std::declval<Derived &>()))>;
  36. template<typename Derived>
  37. using facade_iterator_t = basic_iterator<begin_cursor_t<Derived>>;
  38. template<typename Derived>
  39. using facade_sentinel_t =
  40. meta::if_c<same_as<begin_cursor_t<Derived>, end_cursor_t<Derived>>,
  41. facade_iterator_t<Derived>, end_cursor_t<Derived>>;
  42. } // namespace detail
  43. /// \endcond
  44. /// \addtogroup group-views
  45. /// @{
  46. /// \brief A utility for constructing a view from a (derived) type that
  47. /// implements begin and end cursors.
  48. /// \tparam Derived A type that derives from `view_facade` and implements
  49. /// begin and end cursors. This type is permitted to be incomplete.
  50. /// \tparam Cardinality The cardinality of this view: `finite`, `infinite`,
  51. /// or `unknown`. See `ranges::cardinality`.
  52. template<typename Derived, cardinality Cardinality>
  53. struct view_facade : view_interface<Derived, Cardinality>
  54. {
  55. protected:
  56. friend range_access;
  57. struct view_as_cursor : Derived
  58. {
  59. view_as_cursor() = default;
  60. explicit view_as_cursor(Derived const * derived)
  61. : Derived(*derived)
  62. {}
  63. explicit operator bool() = delete;
  64. explicit operator bool() const = delete;
  65. };
  66. // Default implementations
  67. constexpr view_as_cursor begin_cursor() const
  68. {
  69. return view_as_cursor{static_cast<Derived const *>(this)};
  70. }
  71. constexpr default_sentinel_t end_cursor() const
  72. {
  73. return {};
  74. }
  75. public:
  76. /// Let `d` be `static_cast<Derived &>(*this)`. Let `b` be
  77. /// `std::as_const(d).begin_cursor()` if that expression is well-formed;
  78. /// otherwise, let `b` be `d.begin_cursor()`. Let `B` be the type of
  79. /// `b`.
  80. /// \return `ranges::basic_iterator<B>(b)`
  81. template(typename D = Derived)(
  82. requires same_as<D, Derived>)
  83. constexpr auto begin() -> detail::facade_iterator_t<D>
  84. {
  85. return detail::facade_iterator_t<D>{
  86. range_access::begin_cursor(*static_cast<Derived *>(this))};
  87. }
  88. /// \overload
  89. template(typename D = Derived)(
  90. requires same_as<D, Derived>)
  91. constexpr auto begin() const -> detail::facade_iterator_t<D const>
  92. {
  93. return detail::facade_iterator_t<D const>{
  94. range_access::begin_cursor(*static_cast<Derived const *>(this))};
  95. }
  96. /// Let `d` be `static_cast<Derived &>(*this)`. Let `e` be
  97. /// `std::as_const(d).end_cursor()` if that expression is well-formed;
  98. /// otherwise, let `e` be `d.end_cursor()`. Let `E` be the type of
  99. /// `e`.
  100. /// \return `ranges::basic_iterator<E>(e)` if `E` is the same
  101. /// as `B` computed above for `begin()`; otherwise, return `e`.
  102. template(typename D = Derived)(
  103. requires same_as<D, Derived>)
  104. constexpr auto end() -> detail::facade_sentinel_t<D>
  105. {
  106. return static_cast<detail::facade_sentinel_t<D>>(
  107. range_access::end_cursor(*static_cast<Derived *>(this)));
  108. }
  109. /// \overload
  110. template(typename D = Derived)(
  111. requires same_as<D, Derived>)
  112. constexpr auto end() const -> detail::facade_sentinel_t<D const>
  113. {
  114. return static_cast<detail::facade_sentinel_t<D const>>(
  115. range_access::end_cursor(*static_cast<Derived const *>(this)));
  116. }
  117. };
  118. /// @}
  119. } // namespace ranges
  120. #include <range/v3/detail/epilogue.hpp>
  121. #endif