Path: blob/main/contrib/llvm-project/libcxx/include/__memory/pointer_traits.h
35233 views
// -*- C++ -*-1//===----------------------------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//89#ifndef _LIBCPP___MEMORY_POINTER_TRAITS_H10#define _LIBCPP___MEMORY_POINTER_TRAITS_H1112#include <__config>13#include <__memory/addressof.h>14#include <__type_traits/conditional.h>15#include <__type_traits/conjunction.h>16#include <__type_traits/decay.h>17#include <__type_traits/is_class.h>18#include <__type_traits/is_function.h>19#include <__type_traits/is_void.h>20#include <__type_traits/void_t.h>21#include <__utility/declval.h>22#include <__utility/forward.h>23#include <cstddef>2425#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)26# pragma GCC system_header27#endif2829_LIBCPP_PUSH_MACROS30#include <__undef_macros>3132_LIBCPP_BEGIN_NAMESPACE_STD3334// clang-format off35#define _LIBCPP_CLASS_TRAITS_HAS_XXX(NAME, PROPERTY) \36template <class _Tp, class = void> \37struct NAME : false_type {}; \38template <class _Tp> \39struct NAME<_Tp, __void_t<typename _Tp::PROPERTY> > : true_type {}40// clang-format on4142_LIBCPP_CLASS_TRAITS_HAS_XXX(__has_pointer, pointer);43_LIBCPP_CLASS_TRAITS_HAS_XXX(__has_element_type, element_type);4445template <class _Ptr, bool = __has_element_type<_Ptr>::value>46struct __pointer_traits_element_type {};4748template <class _Ptr>49struct __pointer_traits_element_type<_Ptr, true> {50typedef _LIBCPP_NODEBUG typename _Ptr::element_type type;51};5253template <template <class, class...> class _Sp, class _Tp, class... _Args>54struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, true> {55typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::element_type type;56};5758template <template <class, class...> class _Sp, class _Tp, class... _Args>59struct __pointer_traits_element_type<_Sp<_Tp, _Args...>, false> {60typedef _LIBCPP_NODEBUG _Tp type;61};6263template <class _Tp, class = void>64struct __has_difference_type : false_type {};6566template <class _Tp>67struct __has_difference_type<_Tp, __void_t<typename _Tp::difference_type> > : true_type {};6869template <class _Ptr, bool = __has_difference_type<_Ptr>::value>70struct __pointer_traits_difference_type {71typedef _LIBCPP_NODEBUG ptrdiff_t type;72};7374template <class _Ptr>75struct __pointer_traits_difference_type<_Ptr, true> {76typedef _LIBCPP_NODEBUG typename _Ptr::difference_type type;77};7879template <class _Tp, class _Up>80struct __has_rebind {81private:82template <class _Xp>83static false_type __test(...);84_LIBCPP_SUPPRESS_DEPRECATED_PUSH85template <class _Xp>86static true_type __test(typename _Xp::template rebind<_Up>* = 0);87_LIBCPP_SUPPRESS_DEPRECATED_POP8889public:90static const bool value = decltype(__test<_Tp>(0))::value;91};9293template <class _Tp, class _Up, bool = __has_rebind<_Tp, _Up>::value>94struct __pointer_traits_rebind {95#ifndef _LIBCPP_CXX03_LANG96typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up> type;97#else98typedef _LIBCPP_NODEBUG typename _Tp::template rebind<_Up>::other type;99#endif100};101102template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>103struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, true> {104#ifndef _LIBCPP_CXX03_LANG105typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up> type;106#else107typedef _LIBCPP_NODEBUG typename _Sp<_Tp, _Args...>::template rebind<_Up>::other type;108#endif109};110111template <template <class, class...> class _Sp, class _Tp, class... _Args, class _Up>112struct __pointer_traits_rebind<_Sp<_Tp, _Args...>, _Up, false> {113typedef _Sp<_Up, _Args...> type;114};115116template <class _Ptr, class = void>117struct __pointer_traits_impl {};118119template <class _Ptr>120struct __pointer_traits_impl<_Ptr, __void_t<typename __pointer_traits_element_type<_Ptr>::type> > {121typedef _Ptr pointer;122typedef typename __pointer_traits_element_type<pointer>::type element_type;123typedef typename __pointer_traits_difference_type<pointer>::type difference_type;124125#ifndef _LIBCPP_CXX03_LANG126template <class _Up>127using rebind = typename __pointer_traits_rebind<pointer, _Up>::type;128#else129template <class _Up>130struct rebind {131typedef typename __pointer_traits_rebind<pointer, _Up>::type other;132};133#endif // _LIBCPP_CXX03_LANG134135private:136struct __nat {};137138public:139_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer140pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) {141return pointer::pointer_to(__r);142}143};144145template <class _Ptr>146struct _LIBCPP_TEMPLATE_VIS pointer_traits : __pointer_traits_impl<_Ptr> {};147148template <class _Tp>149struct _LIBCPP_TEMPLATE_VIS pointer_traits<_Tp*> {150typedef _Tp* pointer;151typedef _Tp element_type;152typedef ptrdiff_t difference_type;153154#ifndef _LIBCPP_CXX03_LANG155template <class _Up>156using rebind = _Up*;157#else158template <class _Up>159struct rebind {160typedef _Up* other;161};162#endif163164private:165struct __nat {};166167public:168_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 static pointer169pointer_to(__conditional_t<is_void<element_type>::value, __nat, element_type>& __r) _NOEXCEPT {170return std::addressof(__r);171}172};173174#ifndef _LIBCPP_CXX03_LANG175template <class _From, class _To>176using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>;177#else178template <class _From, class _To>179using __rebind_pointer_t = typename pointer_traits<_From>::template rebind<_To>::other;180#endif181182// to_address183184template <class _Pointer, class = void>185struct __to_address_helper;186187template <class _Tp>188_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _Tp* __to_address(_Tp* __p) _NOEXCEPT {189static_assert(!is_function<_Tp>::value, "_Tp is a function type");190return __p;191}192193template <class _Pointer, class = void>194struct _HasToAddress : false_type {};195196template <class _Pointer>197struct _HasToAddress<_Pointer, decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>())) >198: true_type {};199200template <class _Pointer, class = void>201struct _HasArrow : false_type {};202203template <class _Pointer>204struct _HasArrow<_Pointer, decltype((void)std::declval<const _Pointer&>().operator->()) > : true_type {};205206template <class _Pointer>207struct _IsFancyPointer {208static const bool value = _HasArrow<_Pointer>::value || _HasToAddress<_Pointer>::value;209};210211// enable_if is needed here to avoid instantiating checks for fancy pointers on raw pointers212template <class _Pointer, __enable_if_t< _And<is_class<_Pointer>, _IsFancyPointer<_Pointer> >::value, int> = 0>213_LIBCPP_HIDE_FROM_ABI214_LIBCPP_CONSTEXPR __decay_t<decltype(__to_address_helper<_Pointer>::__call(std::declval<const _Pointer&>()))>215__to_address(const _Pointer& __p) _NOEXCEPT {216return __to_address_helper<_Pointer>::__call(__p);217}218219template <class _Pointer, class>220struct __to_address_helper {221_LIBCPP_HIDE_FROM_ABI222_LIBCPP_CONSTEXPR static decltype(std::__to_address(std::declval<const _Pointer&>().operator->()))223__call(const _Pointer& __p) _NOEXCEPT {224return std::__to_address(__p.operator->());225}226};227228template <class _Pointer>229struct __to_address_helper<_Pointer,230decltype((void)pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))> {231_LIBCPP_HIDE_FROM_ABI232_LIBCPP_CONSTEXPR static decltype(pointer_traits<_Pointer>::to_address(std::declval<const _Pointer&>()))233__call(const _Pointer& __p) _NOEXCEPT {234return pointer_traits<_Pointer>::to_address(__p);235}236};237238#if _LIBCPP_STD_VER >= 20239template <class _Tp>240inline _LIBCPP_HIDE_FROM_ABI constexpr auto to_address(_Tp* __p) noexcept {241return std::__to_address(__p);242}243244template <class _Pointer>245inline _LIBCPP_HIDE_FROM_ABI constexpr auto246to_address(const _Pointer& __p) noexcept -> decltype(std::__to_address(__p)) {247return std::__to_address(__p);248}249#endif250251#if _LIBCPP_STD_VER >= 23252253template <class _Tp>254struct __pointer_of {};255256template <class _Tp>257requires(__has_pointer<_Tp>::value)258struct __pointer_of<_Tp> {259using type = typename _Tp::pointer;260};261262template <class _Tp>263requires(!__has_pointer<_Tp>::value && __has_element_type<_Tp>::value)264struct __pointer_of<_Tp> {265using type = typename _Tp::element_type*;266};267268template <class _Tp>269requires(!__has_pointer<_Tp>::value && !__has_element_type<_Tp>::value &&270__has_element_type<pointer_traits<_Tp>>::value)271struct __pointer_of<_Tp> {272using type = typename pointer_traits<_Tp>::element_type*;273};274275template <typename _Tp>276using __pointer_of_t = typename __pointer_of<_Tp>::type;277278template <class _Tp, class _Up>279struct __pointer_of_or {280using type _LIBCPP_NODEBUG = _Up;281};282283template <class _Tp, class _Up>284requires requires { typename __pointer_of_t<_Tp>; }285struct __pointer_of_or<_Tp, _Up> {286using type _LIBCPP_NODEBUG = __pointer_of_t<_Tp>;287};288289template <typename _Tp, typename _Up>290using __pointer_of_or_t = typename __pointer_of_or<_Tp, _Up>::type;291292template <class _Smart>293concept __resettable_smart_pointer = requires(_Smart __s) { __s.reset(); };294295template <class _Smart, class _Pointer, class... _Args>296concept __resettable_smart_pointer_with_args = requires(_Smart __s, _Pointer __p, _Args... __args) {297__s.reset(static_cast<__pointer_of_or_t<_Smart, _Pointer>>(__p), std::forward<_Args>(__args)...);298};299300#endif301302_LIBCPP_END_NAMESPACE_STD303304_LIBCPP_POP_MACROS305306#endif // _LIBCPP___MEMORY_POINTER_TRAITS_H307308309