Warning: This file is not a C or C++ file. It does not have highlighting.
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___CXX03___MEMORY_POINTER_TRAITS_H |
11 | #define _LIBCPP___CXX03___MEMORY_POINTER_TRAITS_H |
12 | |
13 | #include <__cxx03/__config> |
14 | #include <__cxx03/__memory/addressof.h> |
15 | #include <__cxx03/__type_traits/conditional.h> |
16 | #include <__cxx03/__type_traits/conjunction.h> |
17 | #include <__cxx03/__type_traits/decay.h> |
18 | #include <__cxx03/__type_traits/is_class.h> |
19 | #include <__cxx03/__type_traits/is_function.h> |
20 | #include <__cxx03/__type_traits/is_void.h> |
21 | #include <__cxx03/__type_traits/void_t.h> |
22 | #include <__cxx03/__utility/declval.h> |
23 | #include <__cxx03/__utility/forward.h> |
24 | #include <__cxx03/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 <__cxx03/__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 | |
46 | template <class _Ptr, bool = __has_element_type<_Ptr>::value> |
47 | struct __pointer_traits_element_type {}; |
48 | |
49 | template <class _Ptr> |
50 | struct __pointer_traits_element_type<_Ptr, true> { |
51 | typedef _LIBCPP_NODEBUG typename _Ptr::element_type type; |
52 | }; |
53 | |
54 | template <template <class, class...> class _Sp, class _Tp, class... _Args> |
55 | struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true> { |
56 | typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::element_type type; |
57 | }; |
58 | |
59 | template <template <class, class...> class _Sp, class _Tp, class... _Args> |
60 | struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false> { |
61 | typedef _LIBCPP_NODEBUG _Tp type; |
62 | }; |
63 | |
64 | template <class _Tp, class = void> |
65 | struct __has_difference_type : false_type {}; |
66 | |
67 | template <class _Tp> |
68 | struct __has_difference_type<_Tp, __void_t<typename _Tp::difference_type> > : true_type {}; |
69 | |
70 | template <class _Ptr, bool = __has_difference_type<_Ptr>::value> |
71 | struct __pointer_traits_difference_type { |
72 | typedef _LIBCPP_NODEBUG ptrdiff_t type; |
73 | }; |
74 | |
75 | template <class _Ptr> |
76 | struct __pointer_traits_difference_type<_Ptr, true> { |
77 | typedef _LIBCPP_NODEBUG typename _Ptr::difference_type type; |
78 | }; |
79 | |
80 | template <class _Tp, class _Up> |
81 | struct __has_rebind { |
82 | private: |
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 | |
90 | public: |
91 | static const bool value = decltype(__test<_Tp>(0))::value; |
92 | }; |
93 | |
94 | template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value> |
95 | struct __pointer_traits_rebind { |
96 | typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up>::other type; |
97 | }; |
98 | |
99 | template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up> |
100 | struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true> { |
101 | typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type; |
102 | }; |
103 | |
104 | template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up> |
105 | struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false> { |
106 | typedef _Sp<_Up, _Args...> type; |
107 | }; |
108 | |
109 | template <class _Ptr, class = void> |
110 | struct __pointer_traits_impl {}; |
111 | |
112 | template <class _Ptr> |
113 | struct __pointer_traits_impl<_Ptr, __void_t<typename __pointer_traits_element_type<_Ptr>::type> > { |
114 | typedef _Ptr pointer; |
115 | typedef typename __pointer_traits_element_type<pointer>::type element_type; |
116 | typedef typename __pointer_traits_difference_type<pointer>::type difference_type; |
117 | |
118 | template <class _Up> |
119 | struct rebind { |
120 | typedef typename __pointer_traits_rebind<pointer, _Up>::type other; |
121 | }; |
122 | |
123 | private: |
124 | struct __nat {}; |
125 | |
126 | public: |
127 | _LIBCPP_HIDE_FROM_ABI static pointer |
128 | pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) { |
129 | return pointer::pointer_to(__r); |
130 | } |
131 | }; |
132 | |
133 | template <class _Ptr> |
134 | struct _LIBCPP_TEMPLATE_VIS pointer_traits : __pointer_traits_impl<_Ptr> {}; |
135 | |
136 | template <class _Tp> |
137 | struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*> { |
138 | typedef _Tp* pointer; |
139 | typedef _Tp element_type; |
140 | typedef ptrdiff_t difference_type; |
141 | |
142 | template <class _Up> |
143 | struct rebind { |
144 | typedef _Up* other; |
145 | }; |
146 | |
147 | private: |
148 | struct __nat {}; |
149 | |
150 | public: |
151 | _LIBCPP_HIDE_FROM_ABI static pointer |
152 | pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) _NOEXCEPT { |
153 | return std::addressof(__r); |
154 | } |
155 | }; |
156 | |
157 | template <class _From, class _To> |
158 | using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>::other; |
159 | |
160 | // to_address |
161 | |
162 | template <class _Pointer, class = void> |
163 | struct __to_address_helper; |
164 | |
165 | template <class _Tp> |
166 | _LIBCPP_HIDE_FROM_ABI _Tp* __to_address(_Tp* __p) _NOEXCEPT { |
167 | static_assert(!is_function<_Tp>::value, "_Tp is a function type"); |
168 | return __p; |
169 | } |
170 | |
171 | template <class _Pointer, class = void> |
172 | struct _HasToAddress : false_type {}; |
173 | |
174 | template <class _Pointer> |
175 | struct _HasToAddress<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>())) > |
176 | : true_type {}; |
177 | |
178 | template <class _Pointer, class = void> |
179 | struct _HasArrow : false_type {}; |
180 | |
181 | template <class _Pointer> |
182 | struct _HasArrow<_Pointer, decltype((void)std::declval<const _Pointer&>().operator->()) > : true_type {}; |
183 | |
184 | template <class _Pointer> |
185 | struct _IsFancyPointer { |
186 | static const bool value = _HasArrow<_Pointer>::value || _HasToAddress<_Pointer>::value; |
187 | }; |
188 | |
189 | // enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers |
190 | template <class _Pointer, __enable_if_t< _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value, int> = 0> |
191 | _LIBCPP_HIDE_FROM_ABI __decay_t<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))> |
192 | __to_address(const _Pointer& __p) _NOEXCEPT { |
193 | return __to_address_helper<_Pointer>::__call(__p); |
194 | } |
195 | |
196 | template <class _Pointer, class> |
197 | struct __to_address_helper { |
198 | _LIBCPP_HIDE_FROM_ABI static decltype(std::__to_address(std::declval<const _Pointer&>().operator->())) |
199 | __call(const _Pointer& __p) _NOEXCEPT { |
200 | return std::__to_address(__p.operator->()); |
201 | } |
202 | }; |
203 | |
204 | template <class _Pointer> |
205 | struct __to_address_helper<_Pointer, |
206 | decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))> { |
207 | _LIBCPP_HIDE_FROM_ABI static decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>())) |
208 | __call(const _Pointer& __p) _NOEXCEPT { |
209 | return pointer_traits<_Pointer>::to_address(__p); |
210 | } |
211 | }; |
212 | |
213 | _LIBCPP_END_NAMESPACE_STD |
214 | |
215 | _LIBCPP_POP_MACROS |
216 | |
217 | #endif // _LIBCPP___CXX03___MEMORY_POINTER_TRAITS_H |
218 |
Warning: This file is not a C or C++ file. It does not have highlighting.