libstdc++
|
00001 // Pointer Traits -*- C++ -*- 00002 00003 // Copyright (C) 2011-2016 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 { using type = __undefined; }; 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> 00088 using __rebind = typename _Tp::template rebind<_Up>; 00089 00090 public: 00091 /// The pointer type. 00092 using pointer = _Ptr; 00093 00094 /// The type pointed to. 00095 using element_type 00096 = __detected_or_t_<__get_first_arg_t, __element_type, _Ptr>; 00097 00098 /// The type used to represent the difference between two pointers. 00099 using difference_type 00100 = __detected_or_t<ptrdiff_t, __difference_type, _Ptr>; 00101 00102 /// A pointer to a different type. 00103 template<typename _Up> 00104 using rebind 00105 = __detected_or_t_<__replace_first_arg_t, __rebind, _Ptr, _Up>; 00106 00107 static _Ptr 00108 pointer_to(__make_not_void<element_type>& __e) 00109 { return _Ptr::pointer_to(__e); } 00110 00111 static_assert(!is_same<element_type, __undefined>::value, 00112 "pointer type defines element_type or is like SomePointer<T, Args>"); 00113 static_assert(!is_same<rebind<element_type>, __undefined>::value, 00114 "pointer type defines rebind<U> or is like SomePointer<T, Args>"); 00115 }; 00116 00117 /** 00118 * @brief Partial specialization for built-in pointers. 00119 * @ingroup pointer_abstractions 00120 */ 00121 template<typename _Tp> 00122 struct pointer_traits<_Tp*> 00123 { 00124 /// The pointer type 00125 typedef _Tp* pointer; 00126 /// The type pointed to 00127 typedef _Tp element_type; 00128 /// Type used to represent the difference between two pointers 00129 typedef ptrdiff_t difference_type; 00130 00131 template<typename _Up> 00132 using rebind = _Up*; 00133 00134 /** 00135 * @brief Obtain a pointer to an object 00136 * @param __r A reference to an object of type @c element_type 00137 * @return @c addressof(__r) 00138 */ 00139 static pointer 00140 pointer_to(__make_not_void<element_type>& __r) noexcept 00141 { return std::addressof(__r); } 00142 }; 00143 00144 /// Convenience alias for rebinding pointers. 00145 template<typename _Ptr, typename _Tp> 00146 using __ptr_rebind = typename pointer_traits<_Ptr>::template rebind<_Tp>; 00147 00148 _GLIBCXX_END_NAMESPACE_VERSION 00149 } // namespace std 00150 00151 #endif 00152 00153 #endif