218 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03___MEMORY_POINTER_TRAITS_H11#define _LIBCPP___CXX03___MEMORY_POINTER_TRAITS_H12 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_header28#endif29 30_LIBCPP_PUSH_MACROS31#include <__cxx03/__undef_macros>32 33_LIBCPP_BEGIN_NAMESPACE_STD34 35// clang-format off36#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 on42 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_PUSH86 template <class _Xp>87 static true_type __test(typename _Xp::template rebind<_Up>* = 0);88 _LIBCPP_SUPPRESS_DEPRECATED_POP89 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 typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up>::other type;97};98 99template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>100struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true> {101 typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;102};103 104template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>105struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false> {106 typedef _Sp<_Up, _Args...> type;107};108 109template <class _Ptr, class = void>110struct __pointer_traits_impl {};111 112template <class _Ptr>113struct __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 123private:124 struct __nat {};125 126public:127 _LIBCPP_HIDE_FROM_ABI static pointer128 pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) {129 return pointer::pointer_to(__r);130 }131};132 133template <class _Ptr>134struct _LIBCPP_TEMPLATE_VIS pointer_traits : __pointer_traits_impl<_Ptr> {};135 136template <class _Tp>137struct _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 147private:148 struct __nat {};149 150public:151 _LIBCPP_HIDE_FROM_ABI static pointer152 pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) _NOEXCEPT {153 return std::addressof(__r);154 }155};156 157template <class _From, class _To>158using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>::other;159 160// to_address161 162template <class _Pointer, class = void>163struct __to_address_helper;164 165template <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 171template <class _Pointer, class = void>172struct _HasToAddress : false_type {};173 174template <class _Pointer>175struct _HasToAddress<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>())) >176 : true_type {};177 178template <class _Pointer, class = void>179struct _HasArrow : false_type {};180 181template <class _Pointer>182struct _HasArrow<_Pointer, decltype((void)std::declval<const _Pointer&>().operator->()) > : true_type {};183 184template <class _Pointer>185struct _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 pointers190template <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 196template <class _Pointer, class>197struct __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 204template <class _Pointer>205struct __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_STD214 215_LIBCPP_POP_MACROS216 217#endif // _LIBCPP___CXX03___MEMORY_POINTER_TRAITS_H218