xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/common_view.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1fe6060f1SDimitry Andric // -*- C++ -*-
2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
3fe6060f1SDimitry Andric //
4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7fe6060f1SDimitry Andric //
8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
9bdd1243dSDimitry Andric 
10fe6060f1SDimitry Andric #ifndef _LIBCPP___RANGES_COMMON_VIEW_H
11fe6060f1SDimitry Andric #define _LIBCPP___RANGES_COMMON_VIEW_H
12fe6060f1SDimitry Andric 
13bdd1243dSDimitry Andric #include <__concepts/constructible.h>
14bdd1243dSDimitry Andric #include <__concepts/copyable.h>
15fe6060f1SDimitry Andric #include <__config>
16fe6060f1SDimitry Andric #include <__iterator/common_iterator.h>
17fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
18fe6060f1SDimitry Andric #include <__ranges/access.h>
19fe6060f1SDimitry Andric #include <__ranges/all.h>
20fe6060f1SDimitry Andric #include <__ranges/concepts.h>
21fe6060f1SDimitry Andric #include <__ranges/enable_borrowed_range.h>
22349cc55cSDimitry Andric #include <__ranges/range_adaptor.h>
23fe6060f1SDimitry Andric #include <__ranges/size.h>
24fe6060f1SDimitry Andric #include <__ranges/view_interface.h>
25349cc55cSDimitry Andric #include <__utility/forward.h>
26349cc55cSDimitry Andric #include <__utility/move.h>
27fe6060f1SDimitry Andric 
28fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
29fe6060f1SDimitry Andric #  pragma GCC system_header
30fe6060f1SDimitry Andric #endif
31fe6060f1SDimitry Andric 
3206c3fb27SDimitry Andric _LIBCPP_PUSH_MACROS
3306c3fb27SDimitry Andric #include <__undef_macros>
3406c3fb27SDimitry Andric 
35fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
36fe6060f1SDimitry Andric 
3706c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
38fe6060f1SDimitry Andric 
39fe6060f1SDimitry Andric namespace ranges {
40fe6060f1SDimitry Andric 
41fe6060f1SDimitry Andric template <view _View>
42fe6060f1SDimitry Andric   requires(!common_range<_View> && copyable<iterator_t<_View>>)
43fe6060f1SDimitry Andric class common_view : public view_interface<common_view<_View>> {
44fe6060f1SDimitry Andric   _View __base_ = _View();
45fe6060f1SDimitry Andric 
46fe6060f1SDimitry Andric public:
47cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI common_view()
48cb14a3feSDimitry Andric     requires default_initializable<_View>
49cb14a3feSDimitry Andric   = default;
50fe6060f1SDimitry Andric 
common_view(_View __v)51cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr explicit common_view(_View __v) : __base_(std::move(__v)) {}
52fe6060f1SDimitry Andric 
base()53cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
54cb14a3feSDimitry Andric     requires copy_constructible<_View>
55cb14a3feSDimitry Andric   {
56cb14a3feSDimitry Andric     return __base_;
57cb14a3feSDimitry Andric   }
58fe6060f1SDimitry Andric 
base()59cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
60fe6060f1SDimitry Andric 
begin()61cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto begin() {
62fe6060f1SDimitry Andric     if constexpr (random_access_range<_View> && sized_range<_View>)
63fe6060f1SDimitry Andric       return ranges::begin(__base_);
64fe6060f1SDimitry Andric     else
65fe6060f1SDimitry Andric       return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::begin(__base_));
66fe6060f1SDimitry Andric   }
67fe6060f1SDimitry Andric 
begin()68cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
69cb14a3feSDimitry Andric     requires range<const _View>
70cb14a3feSDimitry Andric   {
71fe6060f1SDimitry Andric     if constexpr (random_access_range<const _View> && sized_range<const _View>)
72fe6060f1SDimitry Andric       return ranges::begin(__base_);
73fe6060f1SDimitry Andric     else
74fe6060f1SDimitry Andric       return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::begin(__base_));
75fe6060f1SDimitry Andric   }
76fe6060f1SDimitry Andric 
end()77cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto end() {
78fe6060f1SDimitry Andric     if constexpr (random_access_range<_View> && sized_range<_View>)
79fe6060f1SDimitry Andric       return ranges::begin(__base_) + ranges::size(__base_);
80fe6060f1SDimitry Andric     else
81fe6060f1SDimitry Andric       return common_iterator<iterator_t<_View>, sentinel_t<_View>>(ranges::end(__base_));
82fe6060f1SDimitry Andric   }
83fe6060f1SDimitry Andric 
end()84cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
85cb14a3feSDimitry Andric     requires range<const _View>
86cb14a3feSDimitry Andric   {
87fe6060f1SDimitry Andric     if constexpr (random_access_range<const _View> && sized_range<const _View>)
88fe6060f1SDimitry Andric       return ranges::begin(__base_) + ranges::size(__base_);
89fe6060f1SDimitry Andric     else
90fe6060f1SDimitry Andric       return common_iterator<iterator_t<const _View>, sentinel_t<const _View>>(ranges::end(__base_));
91fe6060f1SDimitry Andric   }
92fe6060f1SDimitry Andric 
size()93cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto size()
94cb14a3feSDimitry Andric     requires sized_range<_View>
95cb14a3feSDimitry Andric   {
96fe6060f1SDimitry Andric     return ranges::size(__base_);
97fe6060f1SDimitry Andric   }
98fe6060f1SDimitry Andric 
size()99cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
100cb14a3feSDimitry Andric     requires sized_range<const _View>
101cb14a3feSDimitry Andric   {
102fe6060f1SDimitry Andric     return ranges::size(__base_);
103fe6060f1SDimitry Andric   }
104fe6060f1SDimitry Andric };
105fe6060f1SDimitry Andric 
106fe6060f1SDimitry Andric template <class _Range>
107cb14a3feSDimitry Andric common_view(_Range&&) -> common_view<views::all_t<_Range>>;
108fe6060f1SDimitry Andric 
109fe6060f1SDimitry Andric template <class _View>
110fe6060f1SDimitry Andric inline constexpr bool enable_borrowed_range<common_view<_View>> = enable_borrowed_range<_View>;
111fe6060f1SDimitry Andric 
112349cc55cSDimitry Andric namespace views {
113349cc55cSDimitry Andric namespace __common {
114349cc55cSDimitry Andric struct __fn : __range_adaptor_closure<__fn> {
115349cc55cSDimitry Andric   template <class _Range>
116349cc55cSDimitry Andric     requires common_range<_Range>
117*0fca6ea1SDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const noexcept(
118*0fca6ea1SDimitry Andric       noexcept(views::all(std::forward<_Range>(__range)))) -> decltype(views::all(std::forward<_Range>(__range))) {
119cb14a3feSDimitry Andric     return views::all(std::forward<_Range>(__range));
120cb14a3feSDimitry Andric   }
121349cc55cSDimitry Andric 
122349cc55cSDimitry Andric   template <class _Range>
123*0fca6ea1SDimitry Andric   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const noexcept(noexcept(common_view{
124*0fca6ea1SDimitry Andric       std::forward<_Range>(__range)})) -> decltype(common_view{std::forward<_Range>(__range)}) {
125cb14a3feSDimitry Andric     return common_view{std::forward<_Range>(__range)};
126cb14a3feSDimitry Andric   }
127349cc55cSDimitry Andric };
1281fd87a68SDimitry Andric } // namespace __common
129349cc55cSDimitry Andric 
130349cc55cSDimitry Andric inline namespace __cpo {
131349cc55cSDimitry Andric inline constexpr auto common = __common::__fn{};
1321fd87a68SDimitry Andric } // namespace __cpo
133349cc55cSDimitry Andric } // namespace views
134fe6060f1SDimitry Andric } // namespace ranges
135fe6060f1SDimitry Andric 
13606c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
137fe6060f1SDimitry Andric 
138fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
139fe6060f1SDimitry Andric 
14006c3fb27SDimitry Andric _LIBCPP_POP_MACROS
14106c3fb27SDimitry Andric 
142fe6060f1SDimitry Andric #endif // _LIBCPP___RANGES_COMMON_VIEW_H
143