common_view.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 <__config>
  12. #include <__iterator/common_iterator.h>
  13. #include <__iterator/iterator_traits.h>
  14. #include <__ranges/access.h>
  15. #include <__ranges/all.h>
  16. #include <__ranges/concepts.h>
  17. #include <__ranges/enable_borrowed_range.h>
  18. #include <__ranges/range_adaptor.h>
  19. #include <__ranges/size.h>
  20. #include <__ranges/view_interface.h>
  21. #include <__utility/forward.h>
  22. #include <__utility/move.h>
  23. #include <concepts>
  24. #include <type_traits>
  25. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  26. # pragma GCC system_header
  27. #endif
  28. _LIBCPP_BEGIN_NAMESPACE_STD
  29. #if !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  30. namespace ranges {
  31. template<view _View>
  32. requires (!common_range<_View> && copyable<iterator_t<_View>>)
  33. class common_view : public view_interface<common_view<_View>> {
  34. _View __base_ = _View();
  35. public:
  36. _LIBCPP_HIDE_FROM_ABI
  37. common_view() requires default_initializable<_View> = default;
  38. _LIBCPP_HIDE_FROM_ABI
  39. constexpr explicit common_view(_View __v) : __base_(std::move(__v)) { }
  40. _LIBCPP_HIDE_FROM_ABI
  41. constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
  42. _LIBCPP_HIDE_FROM_ABI
  43. constexpr _View base() && { return std::move(__base_); }
  44. _LIBCPP_HIDE_FROM_ABI
  45. constexpr auto begin() {
  46. if constexpr (random_access_range<_View> && sized_range<_View>)
  47. return ranges::begin(__base_);
  48. else
  49. return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::begin(__base_));
  50. }
  51. _LIBCPP_HIDE_FROM_ABI
  52. constexpr auto begin() const requires range<const _View> {
  53. if constexpr (random_access_range<const _View> && sized_range<const _View>)
  54. return ranges::begin(__base_);
  55. else
  56. return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::begin(__base_));
  57. }
  58. _LIBCPP_HIDE_FROM_ABI
  59. constexpr auto end() {
  60. if constexpr (random_access_range<_View> && sized_range<_View>)
  61. return ranges::begin(__base_) + ranges::size(__base_);
  62. else
  63. return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::end(__base_));
  64. }
  65. _LIBCPP_HIDE_FROM_ABI
  66. constexpr auto end() const requires range<const _View> {
  67. if constexpr (random_access_range<const _View> && sized_range<const _View>)
  68. return ranges::begin(__base_) + ranges::size(__base_);
  69. else
  70. return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::end(__base_));
  71. }
  72. _LIBCPP_HIDE_FROM_ABI
  73. constexpr auto size() requires sized_range<_View> {
  74. return ranges::size(__base_);
  75. }
  76. _LIBCPP_HIDE_FROM_ABI
  77. constexpr auto size() const requires sized_range<const _View> {
  78. return ranges::size(__base_);
  79. }
  80. };
  81. template<class _Range>
  82. common_view(_Range&&)
  83. -> common_view<views::all_t<_Range>>;
  84. template<class _View>
  85. inline constexpr bool enable_borrowed_range<common_view<_View>> = enable_borrowed_range<_View>;
  86. namespace views {
  87. namespace __common {
  88. struct __fn : __range_adaptor_closure<__fn> {
  89. template<class _Range>
  90. requires common_range<_Range>
  91. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  92. constexpr auto operator()(_Range&& __range) const
  93. noexcept(noexcept(views::all(std::forward<_Range>(__range))))
  94. -> decltype( views::all(std::forward<_Range>(__range)))
  95. { return views::all(std::forward<_Range>(__range)); }
  96. template<class _Range>
  97. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  98. constexpr auto operator()(_Range&& __range) const
  99. noexcept(noexcept(common_view{std::forward<_Range>(__range)}))
  100. -> decltype( common_view{std::forward<_Range>(__range)})
  101. { return common_view{std::forward<_Range>(__range)}; }
  102. };
  103. } // namespace __common
  104. inline namespace __cpo {
  105. inline constexpr auto common = __common::__fn{};
  106. } // namespace __cpo
  107. } // namespace views
  108. } // namespace ranges
  109. #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
  110. _LIBCPP_END_NAMESPACE_STD
  111. #endif // _LIBCPP___RANGES_COMMON_VIEW_H