libstdc++
|
00001 // Pointer Traits -*- C++ -*- 00002 00003 // Copyright (C) 2011-2018 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 namespace std _GLIBCXX_VISIBILITY(default) 00038 { 00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00040 00041 class __undefined; 00042 00043 // Given Template<T, ...> return T, otherwise invalid. 00044 template<typename _Tp> 00045 struct __get_first_arg 00046 { using type = __undefined; }; 00047 00048 template<template<typename, typename...> class _Template, typename _Tp, 00049 typename... _Types> 00050 struct __get_first_arg<_Template<_Tp, _Types...>> 00051 { using type = _Tp; }; 00052 00053 template<typename _Tp> 00054 using __get_first_arg_t = typename __get_first_arg<_Tp>::type; 00055 00056 // Given Template<T, ...> and U return Template<U, ...>, otherwise invalid. 00057 template<typename _Tp, typename _Up> 00058 struct __replace_first_arg 00059 { }; 00060 00061 template<template<typename, typename...> class _Template, typename _Up, 00062 typename _Tp, typename... _Types> 00063 struct __replace_first_arg<_Template<_Tp, _Types...>, _Up> 00064 { using type = _Template<_Up, _Types...>; }; 00065 00066 template<typename _Tp, typename _Up> 00067 using __replace_first_arg_t = typename __replace_first_arg<_Tp, _Up>::type; 00068 00069 template<typename _Tp> 00070 using __make_not_void 00071 = typename conditional<is_void<_Tp>::value, __undefined, _Tp>::type; 00072 00073 /** 00074 * @brief Uniform interface to all pointer-like types 00075 * @ingroup pointer_abstractions 00076 */ 00077 template<typename _Ptr> 00078 struct pointer_traits 00079 { 00080 private: 00081 template<typename _Tp> 00082 using __element_type = typename _Tp::element_type; 00083 00084 template<typename _Tp> 00085 using __difference_type = typename _Tp::difference_type; 00086 00087 template<typename _Tp, typename _Up, typename = void> 00088 struct __rebind : __replace_first_arg<_Tp, _Up> { }; 00089 00090 template<typename _Tp, typename _Up> 00091 struct __rebind<_Tp, _Up, __void_t<typename _Tp::template rebind<_Up>>> 00092 { using type = typename _Tp::template rebind<_Up>; }; 00093 00094 public: 00095 /// The pointer type. 00096 using pointer = _Ptr; 00097 00098 /// The type pointed to. 00099 using element_type 00100 = __detected_or_t<__get_first_arg_t<_Ptr>, __element_type, _Ptr>; 00101 00102 /// The type used to represent the difference between two pointers. 00103 using difference_type 00104 = __detected_or_t<ptrdiff_t, __difference_type, _Ptr>; 00105 00106 /// A pointer to a different type. 00107 template<typename _Up> 00108 using rebind = typename __rebind<_Ptr, _Up>::type; 00109 00110 static _Ptr 00111 pointer_to(__make_not_void<element_type>& __e) 00112 { return _Ptr::pointer_to(__e); } 00113 00114 static_assert(!is_same<element_type, __undefined>::value, 00115 "pointer type defines element_type or is like SomePointer<T, Args>"); 00116 }; 00117 00118 /** 00119 * @brief Partial specialization for built-in pointers. 00120 * @ingroup pointer_abstractions 00121 */ 00122 template<typename _Tp> 00123 struct pointer_traits<_Tp*> 00124 { 00125 /// The pointer type 00126 typedef _Tp* pointer; 00127 /// The type pointed to 00128 typedef _Tp element_type; 00129 /// Type used to represent the difference between two pointers 00130 typedef ptrdiff_t difference_type; 00131 00132 template<typename _Up> 00133 using rebind = _Up*; 00134 00135 /** 00136 * @brief Obtain a pointer to an object 00137 * @param __r A reference to an object of type @c element_type 00138 * @return @c addressof(__r) 00139 */ 00140 static pointer 00141 pointer_to(__make_not_void<element_type>& __r) noexcept 00142 { return std::addressof(__r); } 00143 }; 00144 00145 /// Convenience alias for rebinding pointers. 00146 template<typename _Ptr, typename _Tp> 00147 using __ptr_rebind = typename pointer_traits<_Ptr>::template rebind<_Tp>; 00148 00149 template<typename _Tp> 00150 constexpr _Tp* 00151 __to_address(_Tp* __ptr) noexcept 00152 { 00153 static_assert(!std::is_function<_Tp>::value, "not a function pointer"); 00154 return __ptr; 00155 } 00156 00157 #if __cplusplus <= 201703L 00158 template<typename _Ptr> 00159 constexpr typename std::pointer_traits<_Ptr>::element_type* 00160 __to_address(const _Ptr& __ptr) 00161 { return std::__to_address(__ptr.operator->()); } 00162 #else 00163 template<typename _Ptr> 00164 constexpr auto 00165 __to_address(const _Ptr& __ptr) noexcept 00166 -> decltype(std::pointer_traits<_Ptr>::to_address(__ptr)) 00167 { return std::pointer_traits<_Ptr>::to_address(__ptr); } 00168 00169 template<typename _Ptr, typename... _None> 00170 constexpr auto 00171 __to_address(const _Ptr& __ptr, _None...) noexcept 00172 { return std::__to_address(__ptr.operator->()); } 00173 00174 /** 00175 * @brief Obtain address referenced by a pointer to an object 00176 * @param __ptr A pointer to an object 00177 * @return @c __ptr 00178 * @ingroup pointer_abstractions 00179 */ 00180 template<typename _Tp> 00181 constexpr _Tp* 00182 to_address(_Tp* __ptr) noexcept 00183 { return std::__to_address(__ptr); } 00184 00185 /** 00186 * @brief Obtain address referenced by a pointer to an object 00187 * @param __ptr A pointer to an object 00188 * @return @c pointer_traits<_Ptr>::to_address(__ptr) if that expression is 00189 well-formed, otherwise @c to_address(__ptr.operator->()) 00190 * @ingroup pointer_abstractions 00191 */ 00192 template<typename _Ptr> 00193 constexpr auto 00194 to_address(const _Ptr& __ptr) noexcept 00195 { return std::__to_address(__ptr); } 00196 #endif // C++2a 00197 00198 _GLIBCXX_END_NAMESPACE_VERSION 00199 } // namespace std 00200 00201 #endif 00202 00203 #endif