xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/data.h (revision aa1a8ff2d6dbc51ef058f46f3db5a8bb77967145)
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_DATA_H
11 #define _LIBCPP___RANGES_DATA_H
12 
13 #include <__concepts/class_or_enum.h>
14 #include <__config>
15 #include <__iterator/concepts.h>
16 #include <__iterator/iterator_traits.h>
17 #include <__memory/pointer_traits.h>
18 #include <__ranges/access.h>
19 #include <__type_traits/decay.h>
20 #include <__type_traits/is_object.h>
21 #include <__type_traits/is_pointer.h>
22 #include <__type_traits/is_reference.h>
23 #include <__type_traits/remove_pointer.h>
24 #include <__type_traits/remove_reference.h>
25 #include <__utility/auto_cast.h>
26 
27 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
28 #  pragma GCC system_header
29 #endif
30 
31 _LIBCPP_BEGIN_NAMESPACE_STD
32 
33 #if _LIBCPP_STD_VER >= 20
34 
35 // [range.prim.data]
36 
37 namespace ranges {
38 namespace __data {
39 template <class _Tp>
40 concept __ptr_to_object = is_pointer_v<_Tp> && is_object_v<remove_pointer_t<_Tp>>;
41 
42 template <class _Tp>
43 concept __member_data = __can_borrow<_Tp> && __workaround_52970<_Tp> && requires(_Tp&& __t) {
44   { _LIBCPP_AUTO_CAST(__t.data()) } -> __ptr_to_object;
45 };
46 
47 template <class _Tp>
48 concept __ranges_begin_invocable = !__member_data<_Tp> && __can_borrow<_Tp> && requires(_Tp&& __t) {
49   { ranges::begin(__t) } -> contiguous_iterator;
50 };
51 
52 struct __fn {
53   template <__member_data _Tp>
54   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const noexcept(noexcept(__t.data())) {
55     return __t.data();
56   }
57 
58   template <__ranges_begin_invocable _Tp>
59   _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
60       noexcept(noexcept(std::to_address(ranges::begin(__t)))) {
61     return std::to_address(ranges::begin(__t));
62   }
63 };
64 } // namespace __data
65 
66 inline namespace __cpo {
67 inline constexpr auto data = __data::__fn{};
68 } // namespace __cpo
69 } // namespace ranges
70 
71 // [range.prim.cdata]
72 
73 namespace ranges {
74 namespace __cdata {
75 struct __fn {
76   template <class _Tp>
77     requires is_lvalue_reference_v<_Tp&&>
78   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
79       noexcept(noexcept(ranges::data(static_cast<const remove_reference_t<_Tp>&>(__t))))
80           -> decltype(ranges::data(static_cast<const remove_reference_t<_Tp>&>(__t))) {
81     return ranges::data(static_cast<const remove_reference_t<_Tp>&>(__t));
82   }
83 
84   template <class _Tp>
85     requires is_rvalue_reference_v<_Tp&&>
86   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Tp&& __t) const
87       noexcept(noexcept(ranges::data(static_cast<const _Tp&&>(__t))))
88           -> decltype(ranges::data(static_cast<const _Tp&&>(__t))) {
89     return ranges::data(static_cast<const _Tp&&>(__t));
90   }
91 };
92 } // namespace __cdata
93 
94 inline namespace __cpo {
95 inline constexpr auto cdata = __cdata::__fn{};
96 } // namespace __cpo
97 } // namespace ranges
98 
99 #endif // _LIBCPP_STD_VER >= 20
100 
101 _LIBCPP_END_NAMESPACE_STD
102 
103 #endif // _LIBCPP___RANGES_DATA_H
104