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_OWNING_VIEW_H
11#define _LIBCPP___RANGES_OWNING_VIEW_H
12
13#include <__concepts/constructible.h>
14#include <__concepts/movable.h>
15#include <__config>
16#include <__ranges/access.h>
17#include <__ranges/concepts.h>
18#include <__ranges/data.h>
19#include <__ranges/empty.h>
20#include <__ranges/enable_borrowed_range.h>
21#include <__ranges/size.h>
22#include <__ranges/view_interface.h>
23#include <__type_traits/remove_cvref.h>
24#include <__utility/move.h>
25
26#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
27# pragma GCC system_header
28#endif
29
30_LIBCPP_PUSH_MACROS
31#include <__undef_macros>
32
33_LIBCPP_BEGIN_NAMESPACE_STD
34
35#if _LIBCPP_STD_VER >= 20
36
37namespace ranges {
38template <range _Rp>
39 requires movable<_Rp> && (!__is_std_initializer_list<remove_cvref_t<_Rp>>)
40class owning_view : public view_interface<owning_view<_Rp>> {
41 _Rp __r_ = _Rp();
42
43public:
44 _LIBCPP_HIDE_FROM_ABI owning_view()
45 requires default_initializable<_Rp>
46 = default;
47 _LIBCPP_HIDE_FROM_ABI constexpr owning_view(_Rp&& __r) : __r_(std::move(__r)) {}
48
49 _LIBCPP_HIDE_FROM_ABI owning_view(owning_view&&) = default;
50 _LIBCPP_HIDE_FROM_ABI owning_view& operator=(owning_view&&) = default;
51
52 _LIBCPP_HIDE_FROM_ABI constexpr _Rp& base() & noexcept { return __r_; }
53 _LIBCPP_HIDE_FROM_ABI constexpr const _Rp& base() const& noexcept { return __r_; }
54 _LIBCPP_HIDE_FROM_ABI constexpr _Rp&& base() && noexcept { return std::move(__r_); }
55 _LIBCPP_HIDE_FROM_ABI constexpr const _Rp&& base() const&& noexcept { return std::move(__r_); }
56
57 _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Rp> begin() { return ranges::begin(__r_); }
58 _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Rp> end() { return ranges::end(__r_); }
59 _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
60 requires range<const _Rp>
61 {
62 return ranges::begin(__r_);
63 }
64 _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
65 requires range<const _Rp>
66 {
67 return ranges::end(__r_);
68 }
69
70 _LIBCPP_HIDE_FROM_ABI constexpr bool empty()
71 requires requires { ranges::empty(__r_); }
72 {
73 return ranges::empty(__r_);
74 }
75 _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const
76 requires requires { ranges::empty(__r_); }
77 {
78 return ranges::empty(__r_);
79 }
80
81 _LIBCPP_HIDE_FROM_ABI constexpr auto size()
82 requires sized_range<_Rp>
83 {
84 return ranges::size(__r_);
85 }
86 _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
87 requires sized_range<const _Rp>
88 {
89 return ranges::size(__r_);
90 }
91
92 _LIBCPP_HIDE_FROM_ABI constexpr auto data()
93 requires contiguous_range<_Rp>
94 {
95 return ranges::data(__r_);
96 }
97 _LIBCPP_HIDE_FROM_ABI constexpr auto data() const
98 requires contiguous_range<const _Rp>
99 {
100 return ranges::data(__r_);
101 }
102};
103_LIBCPP_CTAD_SUPPORTED_FOR_TYPE(owning_view);
104
105template <class _Tp>
106inline constexpr bool enable_borrowed_range<owning_view<_Tp>> = enable_borrowed_range<_Tp>;
107
108} // namespace ranges
109
110#endif // _LIBCPP_STD_VER >= 20
111
112_LIBCPP_END_NAMESPACE_STD
113
114_LIBCPP_POP_MACROS
115
116#endif // _LIBCPP___RANGES_OWNING_VIEW_H
117