|
libstdc++
|
00001 // Pointer Traits -*- C++ -*- 00002 00003 // Copyright (C) 2011-2019 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/ptr_traits.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. @headername{memory} 00028 */ 00029 00030 #ifndef _PTR_TRAITS_H 00031 #define _PTR_TRAITS_H 1 00032 00033 #if __cplusplus >= 201103L 00034 00035 #include <bits/move.h> 00036 00037 #if __cplusplus > 201703L 00038 namespace __gnu_debug { struct _Safe_iterator_base; } 00039 #endif 00040 00041 namespace std _GLIBCXX_VISIBILITY(default) 00042 { 00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00044 00045 class __undefined; 00046 00047 // Given Template<T, ...> return T, otherwise invalid. 00048 template<typename _Tp> 00049 struct __get_first_arg 00050 { using type = __undefined; }; 00051 00052 template<template<typename, typename...> class _Template, typename _Tp, 00053 typename... _Types> 00054 struct __get_first_arg<_Template<_Tp, _Types...>> 00055 { using type = _Tp; }; 00056 00057 template<typename _Tp> 00058 using __get_first_arg_t = typename __get_first_arg<_Tp>::type; 00059 00060 // Given Template<T, ...> and U return Template<U, ...>, otherwise invalid. 00061 template<typename _Tp, typename _Up> 00062 struct __replace_first_arg 00063 { }; 00064 00065 template<template<typename, typename...> class _Template, typename _Up, 00066 typename _Tp, typename... _Types> 00067 struct __replace_first_arg<_Template<_Tp, _Types...>, _Up> 00068 { using type = _Template<_Up, _Types...>; }; 00069 00070 template<typename _Tp, typename _Up> 00071 using __replace_first_arg_t = typename __replace_first_arg<_Tp, _Up>::type; 00072 00073 template<typename _Tp> 00074 using __make_not_void 00075 = typename conditional<is_void<_Tp>::value, __undefined, _Tp>::type; 00076 00077 /** 00078 * @brief Uniform interface to all pointer-like types 00079 * @ingroup pointer_abstractions 00080 */ 00081 template<typename _Ptr> 00082 struct pointer_traits 00083 { 00084 private: 00085 template<typename _Tp> 00086 using __element_type = typename _Tp::element_type; 00087 00088 template<typename _Tp> 00089 using __difference_type = typename _Tp::difference_type; 00090 00091 template<typename _Tp, typename _Up, typename = void> 00092 struct __rebind : __replace_first_arg<_Tp, _Up> { }; 00093 00094 template<typename _Tp, typename _Up> 00095 struct __rebind<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>>> 00096 { using type = typename _Tp::template rebind<_Up>; }; 00097 00098 public: 00099 /// The pointer type. 00100 using pointer = _Ptr; 00101 00102 /// The type pointed to. 00103 using element_type 00104 = __detected_or_t<__get_first_arg_t<_Ptr>, __element_type, _Ptr>; 00105 00106 /// The type used to represent the difference between two pointers. 00107 using difference_type 00108 = __detected_or_t<ptrdiff_t, __difference_type, _Ptr>; 00109 00110 /// A pointer to a different type. 00111 template<typename _Up> 00112 using rebind = typename __rebind<_Ptr, _Up>::type; 00113 00114 static _Ptr 00115 pointer_to(__make_not_void<element_type>& __e) 00116 { return _Ptr::pointer_to(__e); } 00117 00118 static_assert(!is_same<element_type, __undefined>::value, 00119 "pointer type defines element_type or is like SomePointer<T, Args>"); 00120 }; 00121 00122 /** 00123 * @brief Partial specialization for built-in pointers. 00124 * @ingroup pointer_abstractions 00125 */ 00126 template<typename _Tp> 00127 struct pointer_traits<_Tp*> 00128 { 00129 /// The pointer type 00130 typedef _Tp* pointer; 00131 /// The type pointed to 00132 typedef _Tp element_type; 00133 /// Type used to represent the difference between two pointers 00134 typedef ptrdiff_t difference_type; 00135 00136 template<typename _Up> 00137 using rebind = _Up*; 00138 00139 /** 00140 * @brief Obtain a pointer to an object 00141 * @param __r A reference to an object of type @c element_type 00142 * @return @c addressof(__r) 00143 */ 00144 static _GLIBCXX20_CONSTEXPR pointer 00145 pointer_to(__make_not_void<element_type>& __r) noexcept 00146 { return std::addressof(__r); } 00147 }; 00148 00149 /// Convenience alias for rebinding pointers. 00150 template<typename _Ptr, typename _Tp> 00151 using __ptr_rebind = typename pointer_traits<_Ptr>::template rebind<_Tp>; 00152 00153 template<typename _Tp> 00154 constexpr _Tp* 00155 __to_address(_Tp* __ptr) noexcept 00156 { 00157 static_assert(!std::is_function<_Tp>::value, "not a function pointer"); 00158 return __ptr; 00159 } 00160 00161 #if __cplusplus <= 201703L 00162 template<typename _Ptr> 00163 constexpr typename std::pointer_traits<_Ptr>::element_type* 00164 __to_address(const _Ptr& __ptr) 00165 { return std::__to_address(__ptr.operator->()); } 00166 #else 00167 template<typename _Ptr> 00168 constexpr auto 00169 __to_address(const _Ptr& __ptr) noexcept 00170 -> decltype(std::pointer_traits<_Ptr>::to_address(__ptr)) 00171 { return std::pointer_traits<_Ptr>::to_address(__ptr); } 00172 00173 template<typename _Ptr, typename... _None> 00174 constexpr auto 00175 __to_address(const _Ptr& __ptr, _None...) noexcept 00176 { 00177 if constexpr (is_base_of_v<__gnu_debug::_Safe_iterator_base, _Ptr>) 00178 return std::__to_address(__ptr.base().operator->()); 00179 else 00180 return std::__to_address(__ptr.operator->()); 00181 } 00182 00183 /** 00184 * @brief Obtain address referenced by a pointer to an object 00185 * @param __ptr A pointer to an object 00186 * @return @c __ptr 00187 * @ingroup pointer_abstractions 00188 */ 00189 template<typename _Tp> 00190 constexpr _Tp* 00191 to_address(_Tp* __ptr) noexcept 00192 { return std::__to_address(__ptr); } 00193 00194 /** 00195 * @brief Obtain address referenced by a pointer to an object 00196 * @param __ptr A pointer to an object 00197 * @return @c pointer_traits<_Ptr>::to_address(__ptr) if that expression is 00198 well-formed, otherwise @c to_address(__ptr.operator->()) 00199 * @ingroup pointer_abstractions 00200 */ 00201 template<typename _Ptr> 00202 constexpr auto 00203 to_address(const _Ptr& __ptr) noexcept 00204 { return std::__to_address(__ptr); } 00205 #endif // C++2a 00206 00207 _GLIBCXX_END_NAMESPACE_VERSION 00208 } // namespace std 00209 00210 #endif 00211 00212 #endif