libstdc++
|
00001 // <scoped_allocator> -*- C++ -*- 00002 00003 // Copyright (C) 2011 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 include/scoped_allocator 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 #ifndef _SCOPED_ALLOCATOR 00030 #define _SCOPED_ALLOCATOR 1 00031 00032 #pragma GCC system_header 00033 00034 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00035 # include <bits/c++0x_warning.h> 00036 #else 00037 00038 #include <utility> 00039 #include <tuple> 00040 #include <bits/alloc_traits.h> 00041 00042 namespace std _GLIBCXX_VISIBILITY(default) 00043 { 00044 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00045 00046 template<template<typename> class _Pred, typename... _Allocs> 00047 struct __any_of; 00048 00049 template<template<typename> class _Pred, typename _Alloc, typename... _Allocs> 00050 struct __any_of<_Pred, _Alloc, _Allocs...> 00051 : __or_<_Pred<_Alloc>, __any_of<_Pred, _Allocs...>> 00052 { }; 00053 00054 template<template<typename> class _Pred, typename _Alloc> 00055 struct __any_of<_Pred, _Alloc> 00056 : _Pred<_Alloc> 00057 { }; 00058 00059 /** 00060 * @addtogroup allocators 00061 * @{ 00062 */ 00063 00064 template<typename _Alloc> 00065 struct __propagate_on_copy 00066 : allocator_traits<_Alloc>::propagate_on_container_copy_assignment 00067 { }; 00068 template<typename _Alloc> 00069 struct __propagate_on_move 00070 : allocator_traits<_Alloc>::propagate_on_container_move_assignment 00071 { }; 00072 template<typename _Alloc> 00073 struct __propagate_on_swap 00074 : allocator_traits<_Alloc>::propagate_on_container_swap 00075 { }; 00076 00077 00078 template<typename _Alloc> 00079 inline auto 00080 __do_outermost(_Alloc& __a, _Alloc&) -> decltype(__a.outer_allocator()) 00081 { return __a.outer_allocator(); } 00082 00083 template<typename _Alloc> 00084 inline _Alloc& 00085 __do_outermost(_Alloc& __a, ...) 00086 { return __a; } 00087 00088 template<typename _Alloc> 00089 inline auto 00090 __outermost(_Alloc& __a) -> decltype(__do_outermost(__a, __a)) 00091 { return __do_outermost(__a, __a); } 00092 00093 template<typename _OuterAlloc, typename... _InnerAllocs> 00094 class scoped_allocator_adaptor; 00095 00096 template<typename...> 00097 struct __inner_type_impl; 00098 00099 template<typename _Outer> 00100 struct __inner_type_impl<_Outer> 00101 { 00102 typedef scoped_allocator_adaptor<_Outer> __type; 00103 00104 __inner_type_impl() = default; 00105 __inner_type_impl(const __inner_type_impl&) = default; 00106 __inner_type_impl(__inner_type_impl&&) = default; 00107 00108 template<typename _Alloc> 00109 __inner_type_impl(const __inner_type_impl<_Alloc>& __other) 00110 { } 00111 00112 template<typename _Alloc> 00113 __inner_type_impl(__inner_type_impl<_Alloc>&& __other) 00114 { } 00115 00116 __type& 00117 _M_get(__type* __p) noexcept { return *__p; } 00118 00119 const __type& 00120 _M_get(const __type* __p) const noexcept { return *__p; } 00121 00122 tuple<> 00123 _M_tie() const noexcept { return tuple<>(); } 00124 00125 bool 00126 operator==(const __inner_type_impl&) const noexcept 00127 { return true; } 00128 }; 00129 00130 template<typename _Outer, typename _InnerHead, typename... _InnerTail> 00131 struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...> 00132 { 00133 typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type; 00134 00135 __inner_type_impl() = default; 00136 __inner_type_impl(const __inner_type_impl&) = default; 00137 __inner_type_impl(__inner_type_impl&&) = default; 00138 00139 template<typename... _Allocs> 00140 __inner_type_impl(const __inner_type_impl<_Allocs...>& __other) 00141 : _M_inner(__other._M_inner) { } 00142 00143 template<typename... _Allocs> 00144 __inner_type_impl(__inner_type_impl<_Allocs...>&& __other) 00145 : _M_inner(std::move(__other._M_inner)) { } 00146 00147 template<typename... _Args> 00148 explicit 00149 __inner_type_impl(_Args&&... __args) 00150 : _M_inner(std::forward<_Args>(__args)...) { } 00151 00152 __type& 00153 _M_get(void*) noexcept { return _M_inner; } 00154 00155 const __type& 00156 _M_get(const void*) const noexcept { return _M_inner; } 00157 00158 tuple<const _InnerHead&, const _InnerTail&...> 00159 _M_tie() const noexcept 00160 { return _M_inner._M_tie(); } 00161 00162 bool 00163 operator==(const __inner_type_impl& __other) const noexcept 00164 { return _M_inner == __other._M_inner; } 00165 00166 private: 00167 template<typename...> friend class __inner_type_impl; 00168 template<typename, typename...> friend class scoped_allocator_adaptor; 00169 00170 __type _M_inner; 00171 }; 00172 00173 /// Primary class template. 00174 template<typename _OuterAlloc, typename... _InnerAllocs> 00175 class scoped_allocator_adaptor 00176 : public _OuterAlloc 00177 { 00178 typedef allocator_traits<_OuterAlloc> __traits; 00179 00180 typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type; 00181 __inner_type _M_inner; 00182 00183 template<typename _Outer, typename... _Inner> 00184 friend class scoped_allocator_adaptor; 00185 00186 template<typename...> 00187 friend class __inner_type_impl; 00188 00189 tuple<const _OuterAlloc&, const _InnerAllocs&...> 00190 _M_tie() const noexcept 00191 { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); } 00192 00193 00194 template<typename _Tp, typename... _Args> 00195 void 00196 _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args) 00197 { 00198 auto& __outer = __outermost(*this); 00199 typedef typename std::decay<decltype(__outer)>::type __outer_type; 00200 typedef allocator_traits<__outer_type> __o_traits; 00201 __o_traits::construct(__outer, __p, std::forward<_Args>(__args)...); 00202 } 00203 00204 typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_; 00205 typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_; 00206 00207 template<typename _Tp, typename... _Args> 00208 void 00209 _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args) 00210 { 00211 auto& __outer = __outermost(*this); 00212 typedef typename std::decay<decltype(__outer)>::type __outer_type; 00213 typedef allocator_traits<__outer_type> __o_traits; 00214 __o_traits::construct(__outer, __p, allocator_arg, inner_allocator(), 00215 std::forward<_Args>(__args)...); 00216 } 00217 00218 template<typename _Tp, typename... _Args> 00219 void 00220 _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args) 00221 { 00222 auto& __outer = __outermost(*this); 00223 typedef typename std::decay<decltype(__outer)>::type __outer_type; 00224 typedef allocator_traits<__outer_type> __o_traits; 00225 __o_traits::construct(__outer, __p, std::forward<_Args>(__args)..., 00226 inner_allocator()); 00227 } 00228 00229 template<typename _Alloc> 00230 static _Alloc 00231 _S_select_on_copy(const _Alloc& __a) 00232 { 00233 typedef allocator_traits<_Alloc> __a_traits; 00234 return __a_traits::select_on_container_copy_construction(__a); 00235 } 00236 00237 template<std::size_t... _Indices> 00238 scoped_allocator_adaptor(tuple<const _OuterAlloc&, 00239 const _InnerAllocs&...> __refs, 00240 _Index_tuple<_Indices...>) 00241 : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))), 00242 _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...) 00243 { } 00244 00245 public: 00246 typedef _OuterAlloc outer_allocator_type; 00247 typedef typename __inner_type::__type inner_allocator_type; 00248 00249 typedef typename __traits::value_type value_type; 00250 typedef typename __traits::size_type size_type; 00251 typedef typename __traits::difference_type difference_type; 00252 typedef typename __traits::pointer pointer; 00253 typedef typename __traits::const_pointer const_pointer; 00254 typedef typename __traits::void_pointer void_pointer; 00255 typedef typename __traits::const_void_pointer const_void_pointer; 00256 00257 typedef typename conditional< 00258 __any_of<__propagate_on_copy, _OuterAlloc, _InnerAllocs...>::value, 00259 true_type, false_type>::type propagate_on_container_copy_assignment; 00260 typedef typename conditional< 00261 __any_of<__propagate_on_move, _OuterAlloc, _InnerAllocs...>::value, 00262 true_type, false_type>::type propagate_on_container_move_assignment; 00263 typedef typename conditional< 00264 __any_of<__propagate_on_swap, _OuterAlloc, _InnerAllocs...>::value, 00265 true_type, false_type>::type propagate_on_container_swap; 00266 00267 template <class _Tp> 00268 struct rebind 00269 { 00270 typedef scoped_allocator_adaptor< 00271 typename __traits::template rebind_alloc<_Tp>, 00272 _InnerAllocs...> other; 00273 }; 00274 00275 scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { } 00276 00277 template<typename _Outer2> 00278 scoped_allocator_adaptor(_Outer2&& __outer, 00279 const _InnerAllocs&... __inner) 00280 : _OuterAlloc(std::forward<_Outer2>(__outer)), 00281 _M_inner(__inner...) 00282 { } 00283 00284 scoped_allocator_adaptor(const scoped_allocator_adaptor& __other) 00285 : _OuterAlloc(__other.outer_allocator()), 00286 _M_inner(__other._M_inner) 00287 { } 00288 00289 scoped_allocator_adaptor(scoped_allocator_adaptor&& __other) 00290 : _OuterAlloc(std::move(__other.outer_allocator())), 00291 _M_inner(std::move(__other._M_inner)) 00292 { } 00293 00294 template<typename _Outer2> 00295 scoped_allocator_adaptor( 00296 const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other) 00297 : _OuterAlloc(__other.outer_allocator()), 00298 _M_inner(__other._M_inner) 00299 { } 00300 00301 template<typename _Outer2> 00302 scoped_allocator_adaptor( 00303 scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other) 00304 : _OuterAlloc(std::move(__other.outer_allocator())), 00305 _M_inner(std::move(__other._M_inner)) 00306 { } 00307 00308 inner_allocator_type& inner_allocator() noexcept 00309 { return _M_inner._M_get(this); } 00310 00311 const inner_allocator_type& inner_allocator() const noexcept 00312 { return _M_inner._M_get(this); } 00313 00314 outer_allocator_type& outer_allocator() noexcept 00315 { return static_cast<_OuterAlloc&>(*this); } 00316 00317 const outer_allocator_type& outer_allocator() const noexcept 00318 { return static_cast<const _OuterAlloc&>(*this); } 00319 00320 pointer allocate(size_type __n) 00321 { return __traits::allocate(outer_allocator(), __n); } 00322 00323 pointer allocate(size_type __n, const_void_pointer __hint) 00324 { return __traits::allocate(outer_allocator(), __n, __hint); } 00325 00326 void deallocate(pointer __p, size_type __n) 00327 { return __traits::deallocate(outer_allocator(), __p, __n); } 00328 00329 size_type max_size() const 00330 { return __traits::max_size(outer_allocator()); } 00331 00332 template<typename _Tp, typename... _Args> 00333 void construct(_Tp* __p, _Args&&... __args) 00334 { 00335 auto& __inner = inner_allocator(); 00336 auto __use_tag 00337 = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner); 00338 _M_construct(__use_tag, __p, std::forward<_Args>(__args)...); 00339 } 00340 00341 // TODO: construct pairs 00342 00343 template<typename _Tp> 00344 void destroy(_Tp* __p) 00345 { 00346 auto& __outer = __outermost(*this); 00347 typedef typename std::decay<decltype(__outer)>::type __outer_type; 00348 allocator_traits<__outer_type>::destroy(__outer, __p); 00349 } 00350 00351 scoped_allocator_adaptor 00352 select_on_container_copy_construction() const 00353 { 00354 typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type 00355 _Indices; 00356 return scoped_allocator_adaptor(_M_tie(), _Indices()); 00357 } 00358 00359 template <typename _OutA1, typename _OutA2, typename... _InA> 00360 friend bool 00361 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a, 00362 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept; 00363 }; 00364 00365 template <typename _OutA1, typename _OutA2, typename... _InA> 00366 inline bool 00367 operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a, 00368 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept 00369 { 00370 return __a.outer_allocator() == __b.outer_allocator() 00371 && __a._M_inner == __b._M_inner; 00372 } 00373 00374 template <typename _OutA1, typename _OutA2, typename... _InA> 00375 inline bool 00376 operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a, 00377 const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept 00378 { return !(__a == __b); } 00379 00380 /// @} 00381 00382 _GLIBCXX_END_NAMESPACE_VERSION 00383 } // namespace 00384 00385 #endif // __GXX_EXPERIMENTAL_CXX0X__ 00386 00387 #endif // _SCOPED_ALLOCATOR