xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/ref_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_REF_VIEW_H
11fe6060f1SDimitry Andric #define _LIBCPP___RANGES_REF_VIEW_H
12fe6060f1SDimitry Andric 
13bdd1243dSDimitry Andric #include <__concepts/convertible_to.h>
14bdd1243dSDimitry Andric #include <__concepts/different_from.h>
15fe6060f1SDimitry Andric #include <__config>
16fe6060f1SDimitry Andric #include <__iterator/concepts.h>
17fe6060f1SDimitry Andric #include <__iterator/incrementable_traits.h>
18fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
19fe6060f1SDimitry Andric #include <__memory/addressof.h>
20fe6060f1SDimitry Andric #include <__ranges/access.h>
21fe6060f1SDimitry Andric #include <__ranges/concepts.h>
22fe6060f1SDimitry Andric #include <__ranges/data.h>
23fe6060f1SDimitry Andric #include <__ranges/empty.h>
2404eeddc0SDimitry Andric #include <__ranges/enable_borrowed_range.h>
25fe6060f1SDimitry Andric #include <__ranges/size.h>
26fe6060f1SDimitry Andric #include <__ranges/view_interface.h>
2706c3fb27SDimitry Andric #include <__type_traits/is_object.h>
2806c3fb27SDimitry Andric #include <__utility/declval.h>
29349cc55cSDimitry Andric #include <__utility/forward.h>
30fe6060f1SDimitry Andric 
31fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
32fe6060f1SDimitry Andric #  pragma GCC system_header
33fe6060f1SDimitry Andric #endif
34fe6060f1SDimitry Andric 
35fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
36fe6060f1SDimitry Andric 
3706c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
38fe6060f1SDimitry Andric 
39fe6060f1SDimitry Andric namespace ranges {
40fe6060f1SDimitry Andric template <range _Range>
41fe6060f1SDimitry Andric   requires is_object_v<_Range>
42fe6060f1SDimitry Andric class ref_view : public view_interface<ref_view<_Range>> {
43fe6060f1SDimitry Andric   _Range* __range_;
44fe6060f1SDimitry Andric 
45fe6060f1SDimitry Andric   static void __fun(_Range&);
46*0fca6ea1SDimitry Andric   static void __fun(_Range&&) = delete; // NOLINT(modernize-use-equals-delete) ; This is llvm.org/PR54276
47fe6060f1SDimitry Andric 
48fe6060f1SDimitry Andric public:
49fe6060f1SDimitry Andric   template <class _Tp>
50cb14a3feSDimitry Andric     requires __different_from<_Tp, ref_view> && convertible_to<_Tp, _Range&> && requires { __fun(std::declval<_Tp>()); }
ref_view(_Tp && __t)51cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr ref_view(_Tp&& __t)
52cb14a3feSDimitry Andric       : __range_(std::addressof(static_cast<_Range&>(std::forward<_Tp>(__t)))) {}
53fe6060f1SDimitry Andric 
base()54fe6060f1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr _Range& base() const { return *__range_; }
55fe6060f1SDimitry Andric 
begin()56fe6060f1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Range> begin() const { return ranges::begin(*__range_); }
end()57fe6060f1SDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Range> end() const { return ranges::end(*__range_); }
58fe6060f1SDimitry Andric 
empty()59cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const
60fe6060f1SDimitry Andric     requires requires { ranges::empty(*__range_); }
61cb14a3feSDimitry Andric   {
62cb14a3feSDimitry Andric     return ranges::empty(*__range_);
63cb14a3feSDimitry Andric   }
64fe6060f1SDimitry Andric 
size()65cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
66fe6060f1SDimitry Andric     requires sized_range<_Range>
67cb14a3feSDimitry Andric   {
68cb14a3feSDimitry Andric     return ranges::size(*__range_);
69cb14a3feSDimitry Andric   }
70fe6060f1SDimitry Andric 
data()71cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr auto data() const
72fe6060f1SDimitry Andric     requires contiguous_range<_Range>
73cb14a3feSDimitry Andric   {
74cb14a3feSDimitry Andric     return ranges::data(*__range_);
75cb14a3feSDimitry Andric   }
76fe6060f1SDimitry Andric };
77fe6060f1SDimitry Andric 
78fe6060f1SDimitry Andric template <class _Range>
79fe6060f1SDimitry Andric ref_view(_Range&) -> ref_view<_Range>;
80fe6060f1SDimitry Andric 
8104eeddc0SDimitry Andric template <class _Tp>
8204eeddc0SDimitry Andric inline constexpr bool enable_borrowed_range<ref_view<_Tp>> = true;
83fe6060f1SDimitry Andric } // namespace ranges
84fe6060f1SDimitry Andric 
8506c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
86fe6060f1SDimitry Andric 
87fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
88fe6060f1SDimitry Andric 
89fe6060f1SDimitry Andric #endif // _LIBCPP___RANGES_REF_VIEW_H
90