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