[X86] Pre-commit test for D157513
[llvm-project.git] / libcxx / include / __ranges / common_view.h
blobf4cd18074904da9ff004a3b92172067cc52f504f
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___RANGES_COMMON_VIEW_H
11 #define _LIBCPP___RANGES_COMMON_VIEW_H
13 #include <__concepts/constructible.h>
14 #include <__concepts/copyable.h>
15 #include <__config>
16 #include <__iterator/common_iterator.h>
17 #include <__iterator/iterator_traits.h>
18 #include <__ranges/access.h>
19 #include <__ranges/all.h>
20 #include <__ranges/concepts.h>
21 #include <__ranges/enable_borrowed_range.h>
22 #include <__ranges/range_adaptor.h>
23 #include <__ranges/size.h>
24 #include <__ranges/view_interface.h>
25 #include <__utility/forward.h>
26 #include <__utility/move.h>
28 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29 # pragma GCC system_header
30 #endif
32 _LIBCPP_PUSH_MACROS
33 #include <__undef_macros>
35 _LIBCPP_BEGIN_NAMESPACE_STD
37 #if _LIBCPP_STD_VER >= 20
39 namespace ranges {
41 template<view _View>
42 requires (!common_range<_View> && copyable<iterator_t<_View>>)
43 class common_view : public view_interface<common_view<_View>> {
44 _View __base_ = _View();
46 public:
47 _LIBCPP_HIDE_FROM_ABI
48 common_view() requires default_initializable<_View> = default;
50 _LIBCPP_HIDE_FROM_ABI
51 constexpr explicit common_view(_View __v) : __base_(std::move(__v)) { }
53 _LIBCPP_HIDE_FROM_ABI
54 constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
56 _LIBCPP_HIDE_FROM_ABI
57 constexpr _View base() && { return std::move(__base_); }
59 _LIBCPP_HIDE_FROM_ABI
60 constexpr auto begin() {
61 if constexpr (random_access_range<_View> && sized_range<_View>)
62 return ranges::begin(__base_);
63 else
64 return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::begin(__base_));
67 _LIBCPP_HIDE_FROM_ABI
68 constexpr auto begin() const requires range<const _View> {
69 if constexpr (random_access_range<const _View> && sized_range<const _View>)
70 return ranges::begin(__base_);
71 else
72 return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::begin(__base_));
75 _LIBCPP_HIDE_FROM_ABI
76 constexpr auto end() {
77 if constexpr (random_access_range<_View> && sized_range<_View>)
78 return ranges::begin(__base_) + ranges::size(__base_);
79 else
80 return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::end(__base_));
83 _LIBCPP_HIDE_FROM_ABI
84 constexpr auto end() const requires range<const _View> {
85 if constexpr (random_access_range<const _View> && sized_range<const _View>)
86 return ranges::begin(__base_) + ranges::size(__base_);
87 else
88 return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::end(__base_));
91 _LIBCPP_HIDE_FROM_ABI
92 constexpr auto size() requires sized_range<_View> {
93 return ranges::size(__base_);
96 _LIBCPP_HIDE_FROM_ABI
97 constexpr auto size() const requires sized_range<const _View> {
98 return ranges::size(__base_);
102 template<class _Range>
103 common_view(_Range&&)
104 -> common_view<views::all_t<_Range>>;
106 template<class _View>
107 inline constexpr bool enable_borrowed_range<common_view<_View>> = enable_borrowed_range<_View>;
109 namespace views {
110 namespace __common {
111 struct __fn : __range_adaptor_closure<__fn> {
112 template<class _Range>
113 requires common_range<_Range>
114 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
115 constexpr auto operator()(_Range&& __range) const
116 noexcept(noexcept(views::all(std::forward<_Range>(__range))))
117 -> decltype( views::all(std::forward<_Range>(__range)))
118 { return views::all(std::forward<_Range>(__range)); }
120 template<class _Range>
121 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
122 constexpr auto operator()(_Range&& __range) const
123 noexcept(noexcept(common_view{std::forward<_Range>(__range)}))
124 -> decltype( common_view{std::forward<_Range>(__range)})
125 { return common_view{std::forward<_Range>(__range)}; }
127 } // namespace __common
129 inline namespace __cpo {
130 inline constexpr auto common = __common::__fn{};
131 } // namespace __cpo
132 } // namespace views
133 } // namespace ranges
135 #endif // _LIBCPP_STD_VER >= 20
137 _LIBCPP_END_NAMESPACE_STD
139 _LIBCPP_POP_MACROS
141 #endif // _LIBCPP___RANGES_COMMON_VIEW_H