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/size.h>
20 #include <__ranges/view_interface.h>
21 #include <concepts>
22 #include <type_traits>
23 
24 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
25 #pragma GCC system_header
26 #endif
27 
28 _LIBCPP_PUSH_MACROS
29 #include <__undef_macros>
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 #if !defined(_LIBCPP_HAS_NO_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_(_VSTD::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 _VSTD::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 ranges
106 
107 #endif // !defined(_LIBCPP_HAS_NO_RANGES)
108 
109 _LIBCPP_END_NAMESPACE_STD
110 
111 _LIBCPP_POP_MACROS
112 
113 #endif // _LIBCPP___RANGES_COMMON_VIEW_H
114