common_view.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___RANGES_COMMON_VIEW_H
  10. #define _LIBCPP___RANGES_COMMON_VIEW_H
  11. #include <__concepts/constructible.h>
  12. #include <__concepts/copyable.h>
  13. #include <__config>
  14. #include <__iterator/common_iterator.h>
  15. #include <__iterator/iterator_traits.h>
  16. #include <__ranges/access.h>
  17. #include <__ranges/all.h>
  18. #include <__ranges/concepts.h>
  19. #include <__ranges/enable_borrowed_range.h>
  20. #include <__ranges/range_adaptor.h>
  21. #include <__ranges/size.h>
  22. #include <__ranges/view_interface.h>
  23. #include <__utility/forward.h>
  24. #include <__utility/move.h>
  25. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  26. # pragma GCC system_header
  27. #endif
  28. _LIBCPP_PUSH_MACROS
  29. #include <__undef_macros>
  30. _LIBCPP_BEGIN_NAMESPACE_STD
  31. #if _LIBCPP_STD_VER >= 20
  32. namespace ranges {
  33. template<view _View>
  34. requires (!common_range<_View> && copyable<iterator_t<_View>>)
  35. class common_view : public view_interface<common_view<_View>> {
  36. _View __base_ = _View();
  37. public:
  38. _LIBCPP_HIDE_FROM_ABI
  39. common_view() requires default_initializable<_View> = default;
  40. _LIBCPP_HIDE_FROM_ABI
  41. constexpr explicit common_view(_View __v) : __base_(std::move(__v)) { }
  42. _LIBCPP_HIDE_FROM_ABI
  43. constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
  44. _LIBCPP_HIDE_FROM_ABI
  45. constexpr _View base() && { return std::move(__base_); }
  46. _LIBCPP_HIDE_FROM_ABI
  47. constexpr auto begin() {
  48. if constexpr (random_access_range<_View> && sized_range<_View>)
  49. return ranges::begin(__base_);
  50. else
  51. return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::begin(__base_));
  52. }
  53. _LIBCPP_HIDE_FROM_ABI
  54. constexpr auto begin() const requires range<const _View> {
  55. if constexpr (random_access_range<const _View> && sized_range<const _View>)
  56. return ranges::begin(__base_);
  57. else
  58. return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::begin(__base_));
  59. }
  60. _LIBCPP_HIDE_FROM_ABI
  61. constexpr auto end() {
  62. if constexpr (random_access_range<_View> && sized_range<_View>)
  63. return ranges::begin(__base_) + ranges::size(__base_);
  64. else
  65. return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::end(__base_));
  66. }
  67. _LIBCPP_HIDE_FROM_ABI
  68. constexpr auto end() const requires range<const _View> {
  69. if constexpr (random_access_range<const _View> && sized_range<const _View>)
  70. return ranges::begin(__base_) + ranges::size(__base_);
  71. else
  72. return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::end(__base_));
  73. }
  74. _LIBCPP_HIDE_FROM_ABI
  75. constexpr auto size() requires sized_range<_View> {
  76. return ranges::size(__base_);
  77. }
  78. _LIBCPP_HIDE_FROM_ABI
  79. constexpr auto size() const requires sized_range<const _View> {
  80. return ranges::size(__base_);
  81. }
  82. };
  83. template<class _Range>
  84. common_view(_Range&&)
  85. -> common_view<views::all_t<_Range>>;
  86. template<class _View>
  87. inline constexpr bool enable_borrowed_range<common_view<_View>> = enable_borrowed_range<_View>;
  88. namespace views {
  89. namespace __common {
  90. struct __fn : __range_adaptor_closure<__fn> {
  91. template<class _Range>
  92. requires common_range<_Range>
  93. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  94. constexpr auto operator()(_Range&& __range) const
  95. noexcept(noexcept(views::all(std::forward<_Range>(__range))))
  96. -> decltype( views::all(std::forward<_Range>(__range)))
  97. { return views::all(std::forward<_Range>(__range)); }
  98. template<class _Range>
  99. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  100. constexpr auto operator()(_Range&& __range) const
  101. noexcept(noexcept(common_view{std::forward<_Range>(__range)}))
  102. -> decltype( common_view{std::forward<_Range>(__range)})
  103. { return common_view{std::forward<_Range>(__range)}; }
  104. };
  105. } // namespace __common
  106. inline namespace __cpo {
  107. inline constexpr auto common = __common::__fn{};
  108. } // namespace __cpo
  109. } // namespace views
  110. } // namespace ranges
  111. #endif // _LIBCPP_STD_VER >= 20
  112. _LIBCPP_END_NAMESPACE_STD
  113. _LIBCPP_POP_MACROS
  114. #endif // _LIBCPP___RANGES_COMMON_VIEW_H