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.

130 lines
3.7KB

  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_CONST_HPP
  14. #define RANGES_V3_VIEW_CONST_HPP
  15. #include <type_traits>
  16. #include <utility>
  17. #include <range/v3/range_fwd.hpp>
  18. #include <range/v3/range/access.hpp>
  19. #include <range/v3/range/concepts.hpp>
  20. #include <range/v3/range/primitives.hpp>
  21. #include <range/v3/utility/common_type.hpp>
  22. #include <range/v3/utility/move.hpp>
  23. #include <range/v3/utility/static_const.hpp>
  24. #include <range/v3/view/adaptor.hpp>
  25. #include <range/v3/view/all.hpp>
  26. #include <range/v3/view/view.hpp>
  27. namespace ranges
  28. {
  29. /// \addtogroup group-views
  30. /// @{
  31. template<typename Rng>
  32. struct const_view : view_adaptor<const_view<Rng>, Rng>
  33. {
  34. private:
  35. friend range_access;
  36. template<bool Const>
  37. struct adaptor : adaptor_base
  38. {
  39. using CRng = meta::const_if_c<Const, Rng>;
  40. using value_ = range_value_t<CRng>;
  41. using reference_ =
  42. common_reference_t<value_ const &&, range_reference_t<CRng>>;
  43. using rvalue_reference_ =
  44. common_reference_t<value_ const &&, range_rvalue_reference_t<CRng>>;
  45. adaptor() = default;
  46. CPP_template(bool Other)( //
  47. requires Const && (!Other)) constexpr adaptor(adaptor<Other>)
  48. {}
  49. reference_ read(iterator_t<CRng> const & it) const
  50. {
  51. return *it;
  52. }
  53. rvalue_reference_ iter_move(iterator_t<CRng> const & it) const
  54. noexcept(noexcept(rvalue_reference_(ranges::iter_move(it))))
  55. {
  56. return ranges::iter_move(it);
  57. }
  58. };
  59. adaptor<simple_view<Rng>()> begin_adaptor()
  60. {
  61. return {};
  62. }
  63. CPP_member
  64. auto begin_adaptor() const -> CPP_ret(adaptor<true>)( //
  65. requires range<Rng const>)
  66. {
  67. return {};
  68. }
  69. adaptor<simple_view<Rng>()> end_adaptor()
  70. {
  71. return {};
  72. }
  73. CPP_member
  74. auto end_adaptor() const -> CPP_ret(adaptor<true>)( //
  75. requires range<Rng const>)
  76. {
  77. return {};
  78. }
  79. public:
  80. const_view() = default;
  81. explicit const_view(Rng rng)
  82. : const_view::view_adaptor{std::move(rng)}
  83. {}
  84. CPP_member
  85. constexpr auto CPP_fun(size)()(requires sized_range<Rng>)
  86. {
  87. return ranges::size(this->base());
  88. }
  89. CPP_member
  90. constexpr auto CPP_fun(size)()(const requires sized_range<Rng const>)
  91. {
  92. return ranges::size(this->base());
  93. }
  94. };
  95. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  96. template<typename Rng>
  97. const_view(Rng &&)->const_view<views::all_t<Rng>>;
  98. #endif
  99. namespace views
  100. {
  101. struct const_fn
  102. {
  103. template<typename Rng>
  104. auto operator()(Rng && rng) const -> CPP_ret(const_view<all_t<Rng>>)( //
  105. requires viewable_range<Rng> && input_range<Rng>)
  106. {
  107. return const_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
  108. }
  109. };
  110. /// \relates const_fn
  111. /// \ingroup group-views
  112. RANGES_INLINE_VARIABLE(view<const_fn>, const_)
  113. } // namespace views
  114. /// @}
  115. } // namespace ranges
  116. #include <range/v3/detail/satisfy_boost_range.hpp>
  117. RANGES_SATISFY_BOOST_RANGE(::ranges::const_view)
  118. #endif