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