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