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___MEMORY_POINTER_TRAITS_H
11#define _LIBCPP___MEMORY_POINTER_TRAITS_H
12
13#include <__config>
14#include <__memory/addressof.h>
15#include <__type_traits/conditional.h>
16#include <__type_traits/conjunction.h>
17#include <__type_traits/decay.h>
18#include <__type_traits/is_class.h>
19#include <__type_traits/is_function.h>
20#include <__type_traits/is_void.h>
21#include <__type_traits/void_t.h>
22#include <__utility/declval.h>
23#include <__utility/forward.h>
24#include <cstddef>
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// clang-format off
36#define _LIBCPP_CLASS_TRAITS_HAS_XXX(NAME, PROPERTY) \
37 template <class _Tp, class = void> \
38 struct NAME : false_type {}; \
39 template <class _Tp> \
40 struct NAME<_Tp, __void_t<typename _Tp::PROPERTY> > : true_type {}
41// clang-format on
42
43_LIBCPP_CLASS_TRAITS_HAS_XXX(__has_pointer, pointer);
44_LIBCPP_CLASS_TRAITS_HAS_XXX(__has_element_type, element_type);
45
46template <class _Ptr, bool = __has_element_type<_Ptr>::value>
47struct __pointer_traits_element_type {};
48
49template <class _Ptr>
50struct __pointer_traits_element_type<_Ptr, true> {
51 typedef _LIBCPP_NODEBUG typename _Ptr::element_type type;
52};
53
54template <template <class, class...> class _Sp, class _Tp, class... _Args>
55struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true> {
56 typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::element_type type;
57};
58
59template <template <class, class...> class _Sp, class _Tp, class... _Args>
60struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false> {
61 typedef _LIBCPP_NODEBUG _Tp type;
62};
63
64template <class _Tp, class = void>
65struct __has_difference_type : false_type {};
66
67template <class _Tp>
68struct __has_difference_type<_Tp, __void_t<typename _Tp::difference_type> > : true_type {};
69
70template <class _Ptr, bool = __has_difference_type<_Ptr>::value>
71struct __pointer_traits_difference_type {
72 typedef _LIBCPP_NODEBUG ptrdiff_t type;
73};
74
75template <class _Ptr>
76struct __pointer_traits_difference_type<_Ptr, true> {
77 typedef _LIBCPP_NODEBUG typename _Ptr::difference_type type;
78};
79
80template <class _Tp, class _Up>
81struct __has_rebind {
82private:
83 template <class _Xp>
84 static false_type __test(...);
85 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
86 template <class _Xp>
87 static true_type __test(typename _Xp::template rebind<_Up>* = 0);
88 _LIBCPP_SUPPRESS_DEPRECATED_POP
89
90public:
91 static const bool value = decltype(__test<_Tp>(0))::value;
92};
93
94template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>
95struct __pointer_traits_rebind {
96#ifndef _LIBCPP_CXX03_LANG
97 typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up> type;
98#else
99 typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up>::other type;
100#endif
101};
102
103template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>
104struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true> {
105#ifndef _LIBCPP_CXX03_LANG
106 typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up> type;
107#else
108 typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;
109#endif
110};
111
112template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>
113struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false> {
114 typedef _Sp<_Up, _Args...> type;
115};
116
117template <class _Ptr, class = void>
118struct __pointer_traits_impl {};
119
120template <class _Ptr>
121struct __pointer_traits_impl<_Ptr, __void_t<typename __pointer_traits_element_type<_Ptr>::type> > {
122 typedef _Ptr pointer;
123 typedef typename __pointer_traits_element_type<pointer>::type element_type;
124 typedef typename __pointer_traits_difference_type<pointer>::type difference_type;
125
126#ifndef _LIBCPP_CXX03_LANG
127 template <class _Up>
128 using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;
129#else
130 template <class _Up>
131 struct rebind {
132 typedef typename __pointer_traits_rebind<pointer, _Up>::type other;
133 };
134#endif // _LIBCPP_CXX03_LANG
135
136private:
137 struct __nat {};
138
139public:
140 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
141 pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) {
142 return pointer::pointer_to(__r);
143 }
144};
145
146template <class _Ptr>
147struct _LIBCPP_TEMPLATE_VIS pointer_traits : __pointer_traits_impl<_Ptr> {};
148
149template <class _Tp>
150struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*> {
151 typedef _Tp* pointer;
152 typedef _Tp element_type;
153 typedef ptrdiff_t difference_type;
154
155#ifndef _LIBCPP_CXX03_LANG
156 template <class _Up>
157 using rebind = _Up*;
158#else
159 template <class _Up>
160 struct rebind {
161 typedef _Up* other;
162 };
163#endif
164
165private:
166 struct __nat {};
167
168public:
169 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer
170 pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) _NOEXCEPT {
171 return std::addressof(__r);
172 }
173};
174
175#ifndef _LIBCPP_CXX03_LANG
176template <class _From, class _To>
177using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>;
178#else
179template <class _From, class _To>
180using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>::other;
181#endif
182
183// to_address
184
185template <class _Pointer, class = void>
186struct __to_address_helper;
187
188template <class _Tp>
189_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __to_address(_Tp* __p) _NOEXCEPT {
190 static_assert(!is_function<_Tp>::value, "_Tp is a function type");
191 return __p;
192}
193
194template <class _Pointer, class = void>
195struct _HasToAddress : false_type {};
196
197template <class _Pointer>
198struct _HasToAddress<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>())) >
199 : true_type {};
200
201template <class _Pointer, class = void>
202struct _HasArrow : false_type {};
203
204template <class _Pointer>
205struct _HasArrow<_Pointer, decltype((void)std::declval<const _Pointer&>().operator->()) > : true_type {};
206
207template <class _Pointer>
208struct _IsFancyPointer {
209 static const bool value = _HasArrow<_Pointer>::value || _HasToAddress<_Pointer>::value;
210};
211
212// enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers
213template <class _Pointer, __enable_if_t< _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value, int> = 0>
214_LIBCPP_HIDE_FROM_ABI
215_LIBCPP_CONSTEXPR __decay_t<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>
216__to_address(const _Pointer& __p) _NOEXCEPT {
217 return __to_address_helper<_Pointer>::__call(__p);
218}
219
220template <class _Pointer, class>
221struct __to_address_helper {
222 _LIBCPP_HIDE_FROM_ABI
223 _LIBCPP_CONSTEXPR static decltype(std::__to_address(std::declval<const _Pointer&>().operator->()))
224 __call(const _Pointer& __p) _NOEXCEPT {
225 return std::__to_address(__p.operator->());
226 }
227};
228
229template <class _Pointer>
230struct __to_address_helper<_Pointer,
231 decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))> {
232 _LIBCPP_HIDE_FROM_ABI
233 _LIBCPP_CONSTEXPR static decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))
234 __call(const _Pointer& __p) _NOEXCEPT {
235 return pointer_traits<_Pointer>::to_address(__p);
236 }
237};
238
239#if _LIBCPP_STD_VER >= 20
240template <class _Tp>
241inline _LIBCPP_HIDE_FROM_ABI constexpr auto to_address(_Tp* __p) noexcept {
242 return std::__to_address(__p);
243}
244
245template <class _Pointer>
246inline _LIBCPP_HIDE_FROM_ABI constexpr auto
247to_address(const _Pointer& __p) noexcept -> decltype(std::__to_address(__p)) {
248 return std::__to_address(__p);
249}
250#endif
251
252#if _LIBCPP_STD_VER >= 23
253
254template <class _Tp>
255struct __pointer_of {};
256
257template <class _Tp>
258 requires(__has_pointer<_Tp>::value)
259struct __pointer_of<_Tp> {
260 using type = typename _Tp::pointer;
261};
262
263template <class _Tp>
264 requires(!__has_pointer<_Tp>::value && __has_element_type<_Tp>::value)
265struct __pointer_of<_Tp> {
266 using type = typename _Tp::element_type*;
267};
268
269template <class _Tp>
270 requires(!__has_pointer<_Tp>::value && !__has_element_type<_Tp>::value &&
271 __has_element_type<pointer_traits<_Tp>>::value)
272struct __pointer_of<_Tp> {
273 using type = typename pointer_traits<_Tp>::element_type*;
274};
275
276template <typename _Tp>
277using __pointer_of_t = typename __pointer_of<_Tp>::type;
278
279template <class _Tp, class _Up>
280struct __pointer_of_or {
281 using type _LIBCPP_NODEBUG = _Up;
282};
283
284template <class _Tp, class _Up>
285 requires requires { typename __pointer_of_t<_Tp>; }
286struct __pointer_of_or<_Tp, _Up> {
287 using type _LIBCPP_NODEBUG = __pointer_of_t<_Tp>;
288};
289
290template <typename _Tp, typename _Up>
291using __pointer_of_or_t = typename __pointer_of_or<_Tp, _Up>::type;
292
293template <class _Smart>
294concept __resettable_smart_pointer = requires(_Smart __s) { __s.reset(); };
295
296template <class _Smart, class _Pointer, class... _Args>
297concept __resettable_smart_pointer_with_args = requires(_Smart __s, _Pointer __p, _Args... __args) {
298 __s.reset(static_cast<__pointer_of_or_t<_Smart, _Pointer>>(__p), std::forward<_Args>(__args)...);
299};
300
301#endif
302
303_LIBCPP_END_NAMESPACE_STD
304
305_LIBCPP_POP_MACROS
306
307#endif // _LIBCPP___MEMORY_POINTER_TRAITS_H
308