No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

134 líneas
4.5KB

  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. namespace ranges
  25. {
  26. /// \cond
  27. namespace detail
  28. {
  29. template<typename Derived>
  30. using begin_cursor_t = detail::decay_t<decltype(
  31. range_access::begin_cursor(std::declval<Derived &>()))>;
  32. template<typename Derived>
  33. using end_cursor_t = detail::decay_t<decltype(
  34. range_access::end_cursor(std::declval<Derived &>()))>;
  35. template<typename Derived>
  36. using facade_iterator_t = basic_iterator<begin_cursor_t<Derived>>;
  37. template<typename Derived>
  38. using facade_sentinel_t =
  39. meta::if_c<same_as<begin_cursor_t<Derived>, end_cursor_t<Derived>>,
  40. facade_iterator_t<Derived>, end_cursor_t<Derived>>;
  41. } // namespace detail
  42. /// \endcond
  43. /// \addtogroup group-views
  44. /// @{
  45. /// \brief A utility for constructing a view from a (derived) type that
  46. /// implements begin and end cursors.
  47. /// \tparam Derived A type that derives from `view_facade` and implements
  48. /// begin and end cursors. This type is permitted to be incomplete.
  49. /// \tparam Cardinality The cardinality of this view: `finite`, `infinite`,
  50. /// or `unknown`. See `ranges::cardinality`.
  51. template<typename Derived, cardinality Cardinality>
  52. struct view_facade : view_interface<Derived, Cardinality>
  53. {
  54. protected:
  55. friend range_access;
  56. using view_interface<Derived, Cardinality>::derived;
  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{&derived()};
  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. constexpr auto begin() -> CPP_ret(detail::facade_iterator_t<D>)( //
  83. requires same_as<D, Derived>)
  84. {
  85. return detail::facade_iterator_t<D>{range_access::begin_cursor(derived())};
  86. }
  87. /// \overload
  88. template<typename D = Derived>
  89. constexpr auto begin() const -> CPP_ret(detail::facade_iterator_t<D const>)( //
  90. requires same_as<D, Derived>)
  91. {
  92. return detail::facade_iterator_t<D const>{
  93. range_access::begin_cursor(derived())};
  94. }
  95. /// Let `d` be `static_cast<Derived &>(*this)`. Let `e` be
  96. /// `std::as_const(d).end_cursor()` if that expression is well-formed;
  97. /// otherwise, let `e` be `d.end_cursor()`. Let `E` be the type of
  98. /// `e`.
  99. /// \return `ranges::basic_iterator<E>(e)` if `E` is the same
  100. /// as `B` computed above for `begin()`; otherwise, return `e`.
  101. template<typename D = Derived>
  102. constexpr auto end() -> CPP_ret(detail::facade_sentinel_t<D>)( //
  103. requires same_as<D, Derived>)
  104. {
  105. return static_cast<detail::facade_sentinel_t<D>>(
  106. range_access::end_cursor(derived()));
  107. }
  108. /// \overload
  109. template<typename D = Derived>
  110. constexpr auto end() const -> CPP_ret(detail::facade_sentinel_t<D const>)( //
  111. requires same_as<D, Derived>)
  112. {
  113. return static_cast<detail::facade_sentinel_t<D const>>(
  114. range_access::end_cursor(derived()));
  115. }
  116. };
  117. /// @}
  118. } // namespace ranges
  119. #endif