libstdc++
|
00001 // Allocator traits -*- C++ -*- 00002 00003 // Copyright (C) 2011-2015 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 ext/alloc_traits.h 00026 * This file is a GNU extension to the Standard C++ Library. 00027 */ 00028 00029 #ifndef _EXT_ALLOC_TRAITS_H 00030 #define _EXT_ALLOC_TRAITS_H 1 00031 00032 #pragma GCC system_header 00033 00034 #if __cplusplus >= 201103L 00035 # include <bits/move.h> 00036 # include <bits/alloc_traits.h> 00037 #else 00038 # include <bits/allocator.h> // for __alloc_swap 00039 #endif 00040 00041 namespace __gnu_cxx _GLIBCXX_VISIBILITY(default) 00042 { 00043 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00044 00045 #if __cplusplus >= 201103L 00046 template<typename _Alloc> 00047 struct __allocator_always_compares_equal : std::false_type { }; 00048 00049 template<typename _Tp> 00050 struct __allocator_always_compares_equal<std::allocator<_Tp>> 00051 : std::true_type { }; 00052 00053 template<typename, typename> struct array_allocator; 00054 00055 template<typename _Tp, typename _Array> 00056 struct __allocator_always_compares_equal<array_allocator<_Tp, _Array>> 00057 : std::true_type { }; 00058 00059 template<typename> struct bitmap_allocator; 00060 00061 template<typename _Tp> 00062 struct __allocator_always_compares_equal<bitmap_allocator<_Tp>> 00063 : std::true_type { }; 00064 00065 template<typename> struct malloc_allocator; 00066 00067 template<typename _Tp> 00068 struct __allocator_always_compares_equal<malloc_allocator<_Tp>> 00069 : std::true_type { }; 00070 00071 template<typename> struct mt_allocator; 00072 00073 template<typename _Tp> 00074 struct __allocator_always_compares_equal<mt_allocator<_Tp>> 00075 : std::true_type { }; 00076 00077 template<typename> struct new_allocator; 00078 00079 template<typename _Tp> 00080 struct __allocator_always_compares_equal<new_allocator<_Tp>> 00081 : std::true_type { }; 00082 00083 template<typename> struct pool_allocator; 00084 00085 template<typename _Tp> 00086 struct __allocator_always_compares_equal<pool_allocator<_Tp>> 00087 : std::true_type { }; 00088 #endif 00089 00090 /** 00091 * @brief Uniform interface to C++98 and C++0x allocators. 00092 * @ingroup allocators 00093 */ 00094 template<typename _Alloc> 00095 struct __alloc_traits 00096 #if __cplusplus >= 201103L 00097 : std::allocator_traits<_Alloc> 00098 #endif 00099 { 00100 typedef _Alloc allocator_type; 00101 #if __cplusplus >= 201103L 00102 typedef std::allocator_traits<_Alloc> _Base_type; 00103 typedef typename _Base_type::value_type value_type; 00104 typedef typename _Base_type::pointer pointer; 00105 typedef typename _Base_type::const_pointer const_pointer; 00106 typedef typename _Base_type::size_type size_type; 00107 typedef typename _Base_type::difference_type difference_type; 00108 // C++11 allocators do not define reference or const_reference 00109 typedef value_type& reference; 00110 typedef const value_type& const_reference; 00111 using _Base_type::allocate; 00112 using _Base_type::deallocate; 00113 using _Base_type::construct; 00114 using _Base_type::destroy; 00115 using _Base_type::max_size; 00116 00117 private: 00118 template<typename _Ptr> 00119 using __is_custom_pointer 00120 = std::__and_<std::is_same<pointer, _Ptr>, 00121 std::__not_<std::is_pointer<_Ptr>>>; 00122 00123 public: 00124 // overload construct for non-standard pointer types 00125 template<typename _Ptr, typename... _Args> 00126 static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type 00127 construct(_Alloc& __a, _Ptr __p, _Args&&... __args) 00128 { 00129 _Base_type::construct(__a, std::addressof(*__p), 00130 std::forward<_Args>(__args)...); 00131 } 00132 00133 // overload destroy for non-standard pointer types 00134 template<typename _Ptr> 00135 static typename std::enable_if<__is_custom_pointer<_Ptr>::value>::type 00136 destroy(_Alloc& __a, _Ptr __p) 00137 { _Base_type::destroy(__a, std::addressof(*__p)); } 00138 00139 static _Alloc _S_select_on_copy(const _Alloc& __a) 00140 { return _Base_type::select_on_container_copy_construction(__a); } 00141 00142 static void _S_on_swap(_Alloc& __a, _Alloc& __b) 00143 { std::__alloc_on_swap(__a, __b); } 00144 00145 static constexpr bool _S_propagate_on_copy_assign() 00146 { return _Base_type::propagate_on_container_copy_assignment::value; } 00147 00148 static constexpr bool _S_propagate_on_move_assign() 00149 { return _Base_type::propagate_on_container_move_assignment::value; } 00150 00151 static constexpr bool _S_propagate_on_swap() 00152 { return _Base_type::propagate_on_container_swap::value; } 00153 00154 static constexpr bool _S_always_equal() 00155 { return __allocator_always_compares_equal<_Alloc>::value; } 00156 00157 static constexpr bool _S_nothrow_move() 00158 { return _S_propagate_on_move_assign() || _S_always_equal(); } 00159 00160 static constexpr bool _S_nothrow_swap() 00161 { 00162 using std::swap; 00163 return !_S_propagate_on_swap() 00164 || noexcept(swap(std::declval<_Alloc&>(), std::declval<_Alloc&>())); 00165 } 00166 00167 template<typename _Tp> 00168 struct rebind 00169 { typedef typename _Base_type::template rebind_alloc<_Tp> other; }; 00170 #else 00171 00172 typedef typename _Alloc::pointer pointer; 00173 typedef typename _Alloc::const_pointer const_pointer; 00174 typedef typename _Alloc::value_type value_type; 00175 typedef typename _Alloc::reference reference; 00176 typedef typename _Alloc::const_reference const_reference; 00177 typedef typename _Alloc::size_type size_type; 00178 typedef typename _Alloc::difference_type difference_type; 00179 00180 static pointer 00181 allocate(_Alloc& __a, size_type __n) 00182 { return __a.allocate(__n); } 00183 00184 static void deallocate(_Alloc& __a, pointer __p, size_type __n) 00185 { __a.deallocate(__p, __n); } 00186 00187 template<typename _Tp> 00188 static void construct(_Alloc& __a, pointer __p, const _Tp& __arg) 00189 { __a.construct(__p, __arg); } 00190 00191 static void destroy(_Alloc& __a, pointer __p) 00192 { __a.destroy(__p); } 00193 00194 static size_type max_size(const _Alloc& __a) 00195 { return __a.max_size(); } 00196 00197 static const _Alloc& _S_select_on_copy(const _Alloc& __a) { return __a; } 00198 00199 static void _S_on_swap(_Alloc& __a, _Alloc& __b) 00200 { 00201 // _GLIBCXX_RESOLVE_LIB_DEFECTS 00202 // 431. Swapping containers with unequal allocators. 00203 std::__alloc_swap<_Alloc>::_S_do_it(__a, __b); 00204 } 00205 00206 template<typename _Tp> 00207 struct rebind 00208 { typedef typename _Alloc::template rebind<_Tp>::other other; }; 00209 #endif 00210 }; 00211 00212 _GLIBCXX_END_NAMESPACE_VERSION 00213 } // namespace __gnu_cxx 00214 00215 #endif