libstdc++
scoped_allocator
Go to the documentation of this file.
00001 // <scoped_allocator> -*- 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 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 #if __cplusplus < 201103L
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   // TODO: make recursive (see note in 20.12.4/1)
00089   template<typename _Alloc>
00090     inline auto
00091     __outermost(_Alloc& __a) -> decltype(__do_outermost(__a, &__a))
00092     { return __do_outermost(__a, &__a); }
00093 
00094   template<typename _OuterAlloc, typename... _InnerAllocs>
00095     class scoped_allocator_adaptor;
00096 
00097   template<typename...> 
00098     struct __inner_type_impl;
00099 
00100   template<typename _Outer>
00101     struct __inner_type_impl<_Outer>
00102     {
00103       typedef scoped_allocator_adaptor<_Outer> __type;
00104 
00105       __inner_type_impl() = default;
00106       __inner_type_impl(const __inner_type_impl&) = default;
00107       __inner_type_impl(__inner_type_impl&&) = default;
00108       __inner_type_impl& operator=(const __inner_type_impl&) = default;
00109       __inner_type_impl& operator=(__inner_type_impl&&) = default;
00110       
00111       template<typename _Alloc>
00112       __inner_type_impl(const __inner_type_impl<_Alloc>& __other)
00113       { }
00114       
00115       template<typename _Alloc>
00116       __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
00117       { }
00118       
00119       __type& 
00120       _M_get(__type* __p) noexcept { return *__p; }
00121 
00122       const __type& 
00123       _M_get(const __type* __p) const noexcept { return *__p; }
00124       
00125       tuple<> 
00126       _M_tie() const noexcept { return tuple<>(); }
00127       
00128       bool 
00129       operator==(const __inner_type_impl&) const noexcept
00130       { return true; }
00131     };
00132 
00133   template<typename _Outer, typename _InnerHead, typename... _InnerTail>
00134     struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
00135     {
00136       typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
00137       
00138       __inner_type_impl() = default;
00139       __inner_type_impl(const __inner_type_impl&) = default;
00140       __inner_type_impl(__inner_type_impl&&) = default;
00141       __inner_type_impl& operator=(const __inner_type_impl&) = default;
00142       __inner_type_impl& operator=(__inner_type_impl&&) = default;
00143       
00144       template<typename... _Allocs>
00145       __inner_type_impl(const __inner_type_impl<_Allocs...>& __other)
00146       : _M_inner(__other._M_inner) { }
00147       
00148       template<typename... _Allocs>
00149       __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
00150       : _M_inner(std::move(__other._M_inner)) { }
00151 
00152     template<typename... _Args>
00153       explicit
00154       __inner_type_impl(_Args&&... __args)
00155       : _M_inner(std::forward<_Args>(__args)...) { }
00156 
00157       __type& 
00158       _M_get(void*) noexcept { return _M_inner; }
00159       
00160       const __type& 
00161       _M_get(const void*) const noexcept { return _M_inner; }
00162       
00163       tuple<const _InnerHead&, const _InnerTail&...> 
00164       _M_tie() const noexcept
00165       { return _M_inner._M_tie(); }
00166       
00167       bool 
00168       operator==(const __inner_type_impl& __other) const noexcept
00169       { return _M_inner == __other._M_inner; }
00170       
00171     private:
00172       template<typename...> friend class __inner_type_impl;
00173       template<typename, typename...> friend class scoped_allocator_adaptor;
00174       
00175       __type _M_inner;
00176     };
00177 
00178   /// Primary class template.
00179   template<typename _OuterAlloc, typename... _InnerAllocs>
00180     class scoped_allocator_adaptor
00181     : public _OuterAlloc
00182     {
00183       typedef allocator_traits<_OuterAlloc> __traits;
00184 
00185       typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
00186       __inner_type _M_inner;
00187 
00188       template<typename _Outer, typename... _Inner>
00189         friend class scoped_allocator_adaptor;
00190 
00191       template<typename...>
00192         friend class __inner_type_impl;
00193 
00194       tuple<const _OuterAlloc&, const _InnerAllocs&...>
00195       _M_tie() const noexcept
00196       { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
00197 
00198       template<typename _Alloc>
00199         using __outermost_type = typename
00200           std::decay<decltype(__outermost(std::declval<_Alloc&>()))>::type;
00201 
00202       template<typename _Alloc>
00203         using __outermost_alloc_traits
00204           = allocator_traits<__outermost_type<_Alloc>>;
00205       
00206       template<typename _Tp, typename... _Args>
00207         void 
00208         _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
00209         {
00210           typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
00211           _O_traits::construct(__outermost(*this), __p,
00212                                std::forward<_Args>(__args)...);
00213         }
00214 
00215       typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
00216       typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
00217 
00218       template<typename _Tp, typename... _Args>
00219         void 
00220         _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
00221         {
00222           typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
00223           _O_traits::construct(__outermost(*this), __p,
00224                                allocator_arg, inner_allocator(),
00225                                std::forward<_Args>(__args)...);
00226         }
00227 
00228       template<typename _Tp, typename... _Args>
00229         void 
00230         _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
00231         {
00232           typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
00233           _O_traits::construct(__outermost(*this), __p,
00234                                std::forward<_Args>(__args)...,
00235                                inner_allocator());
00236         }
00237 
00238       template<typename _Alloc>
00239         static _Alloc
00240         _S_select_on_copy(const _Alloc& __a)
00241         {
00242           typedef allocator_traits<_Alloc> __a_traits;
00243           return __a_traits::select_on_container_copy_construction(__a);
00244         }
00245 
00246       template<std::size_t... _Indices>
00247         scoped_allocator_adaptor(tuple<const _OuterAlloc&,
00248                                        const _InnerAllocs&...> __refs,
00249                                  _Index_tuple<_Indices...>)
00250         : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
00251           _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
00252         { }
00253 
00254     public:
00255       typedef _OuterAlloc                       outer_allocator_type;
00256       typedef typename __inner_type::__type     inner_allocator_type;
00257 
00258       typedef typename __traits::value_type             value_type;
00259       typedef typename __traits::size_type              size_type;
00260       typedef typename __traits::difference_type        difference_type;
00261       typedef typename __traits::pointer                pointer;
00262       typedef typename __traits::const_pointer          const_pointer;
00263       typedef typename __traits::void_pointer           void_pointer;
00264       typedef typename __traits::const_void_pointer     const_void_pointer;
00265 
00266       typedef typename conditional<
00267         __any_of<__propagate_on_copy, _OuterAlloc, _InnerAllocs...>::value,
00268         true_type, false_type>::type propagate_on_container_copy_assignment;
00269       typedef typename conditional<
00270         __any_of<__propagate_on_move, _OuterAlloc, _InnerAllocs...>::value,
00271         true_type, false_type>::type propagate_on_container_move_assignment;
00272       typedef typename conditional<
00273         __any_of<__propagate_on_swap, _OuterAlloc, _InnerAllocs...>::value,
00274         true_type, false_type>::type propagate_on_container_swap;
00275 
00276       template <class _Tp>
00277         struct rebind
00278         {
00279           typedef scoped_allocator_adaptor<
00280             typename __traits::template rebind_alloc<_Tp>,
00281             _InnerAllocs...> other;
00282         };
00283 
00284       scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
00285 
00286       template<typename _Outer2>
00287         scoped_allocator_adaptor(_Outer2&& __outer,
00288                                  const _InnerAllocs&... __inner)
00289         : _OuterAlloc(std::forward<_Outer2>(__outer)),
00290           _M_inner(__inner...)
00291         { }
00292 
00293       scoped_allocator_adaptor(const scoped_allocator_adaptor& __other)
00294       : _OuterAlloc(__other.outer_allocator()),
00295         _M_inner(__other._M_inner)
00296       { }
00297 
00298       scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
00299       : _OuterAlloc(std::move(__other.outer_allocator())),
00300         _M_inner(std::move(__other._M_inner))
00301       { }
00302 
00303       template<typename _Outer2>
00304         scoped_allocator_adaptor(
00305             const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
00306         : _OuterAlloc(__other.outer_allocator()),
00307           _M_inner(__other._M_inner)
00308         { }
00309 
00310       template<typename _Outer2>
00311         scoped_allocator_adaptor(
00312             scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
00313         : _OuterAlloc(std::move(__other.outer_allocator())),
00314           _M_inner(std::move(__other._M_inner))
00315         { }
00316 
00317       scoped_allocator_adaptor&
00318       operator=(const scoped_allocator_adaptor&) = default;
00319 
00320       scoped_allocator_adaptor&
00321       operator=(scoped_allocator_adaptor&&) = default;
00322 
00323       inner_allocator_type& inner_allocator() noexcept
00324       { return _M_inner._M_get(this); }
00325 
00326       const inner_allocator_type& inner_allocator() const noexcept
00327       { return _M_inner._M_get(this); }
00328 
00329       outer_allocator_type& outer_allocator() noexcept
00330       { return static_cast<_OuterAlloc&>(*this); }
00331 
00332       const outer_allocator_type& outer_allocator() const noexcept
00333       { return static_cast<const _OuterAlloc&>(*this); }
00334 
00335       pointer allocate(size_type __n)
00336       { return __traits::allocate(outer_allocator(), __n); }
00337 
00338       pointer allocate(size_type __n, const_void_pointer __hint)
00339       { return __traits::allocate(outer_allocator(), __n, __hint); }
00340 
00341       void deallocate(pointer __p, size_type __n)
00342       { return __traits::deallocate(outer_allocator(), __p, __n); }
00343 
00344       size_type max_size() const
00345       { return __traits::max_size(outer_allocator()); }
00346 
00347       template<typename _Tp, typename... _Args>
00348         void construct(_Tp* __p, _Args&&... __args)
00349         {
00350           auto& __inner = inner_allocator();
00351           auto __use_tag
00352             = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
00353           _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
00354         }
00355 
00356       template<typename _T1, typename _T2, typename... _Args1,
00357                typename... _Args2>
00358         void
00359         construct(pair<_T1, _T2>* __p, piecewise_construct_t,
00360                   tuple<_Args1...> __x, tuple<_Args2...> __y)
00361         {
00362           // _GLIBCXX_RESOLVE_LIB_DEFECTS
00363           // 2203.  wrong argument types for piecewise construction
00364           auto& __inner = inner_allocator();
00365           auto __x_use_tag
00366             = __use_alloc<_T1, inner_allocator_type, _Args1...>(__inner);
00367           auto __y_use_tag
00368             = __use_alloc<_T2, inner_allocator_type, _Args2...>(__inner);
00369           typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
00370           _O_traits::construct(__outermost(*this), __p, piecewise_construct,
00371                                _M_construct_p(__x_use_tag, __x),
00372                                _M_construct_p(__y_use_tag, __y));
00373         }
00374 
00375       template<typename _T1, typename _T2>
00376         void
00377         construct(pair<_T1, _T2>* __p)
00378         { construct(__p, piecewise_construct, tuple<>(), tuple<>()); }
00379 
00380       template<typename _T1, typename _T2, typename _Up, typename _Vp>
00381         void
00382         construct(pair<_T1, _T2>* __p, _Up&& __u, _Vp&& __v)
00383         {
00384           construct(__p, piecewise_construct,
00385                     std::forward_as_tuple(std::forward<_Up>(__u)),
00386                     std::forward_as_tuple(std::forward<_Vp>(__v)));
00387         }
00388 
00389       template<typename _T1, typename _T2, typename _Up, typename _Vp>
00390         void
00391         construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x)
00392         {
00393           construct(__p, piecewise_construct,
00394                     std::forward_as_tuple(__x.first),
00395                     std::forward_as_tuple(__x.second));
00396         }
00397 
00398       template<typename _T1, typename _T2, typename _Up, typename _Vp>
00399         void
00400         construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x)
00401         {
00402           construct(__p, piecewise_construct,
00403                     std::forward_as_tuple(std::forward<_Up>(__x.first)),
00404                     std::forward_as_tuple(std::forward<_Vp>(__x.second)));
00405         }
00406 
00407       template<typename _Tp>
00408         void destroy(_Tp* __p)
00409         {
00410           typedef __outermost_alloc_traits<scoped_allocator_adaptor> _O_traits;
00411           _O_traits::destroy(__outermost(*this), __p);
00412         }
00413 
00414       scoped_allocator_adaptor
00415       select_on_container_copy_construction() const
00416       {
00417         typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
00418             _Indices;
00419         return scoped_allocator_adaptor(_M_tie(), _Indices());
00420       }
00421 
00422       template <typename _OutA1, typename _OutA2, typename... _InA>
00423       friend bool
00424       operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
00425                  const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
00426 
00427     private:
00428       template<typename _Tuple>
00429         _Tuple&&
00430         _M_construct_p(__uses_alloc0, _Tuple& __t)
00431         { return std::move(__t); }
00432 
00433       template<typename... _Args>
00434         std::tuple<allocator_arg_t, inner_allocator_type&, _Args...>
00435         _M_construct_p(__uses_alloc1_, std::tuple<_Args...>& __t)
00436         {
00437           typedef std::tuple<allocator_arg_t, inner_allocator_type&> _Tuple;
00438           return std::tuple_cat(_Tuple(allocator_arg, inner_allocator()),
00439                                 std::move(__t));
00440         }
00441 
00442       template<typename... _Args>
00443         std::tuple<_Args..., inner_allocator_type&>
00444         _M_construct_p(__uses_alloc2_, std::tuple<_Args...>& __t)
00445         {
00446           typedef std::tuple<inner_allocator_type&> _Tuple;
00447           return std::tuple_cat(std::move(__t), _Tuple(inner_allocator()));
00448         }
00449     };
00450 
00451   template <typename _OutA1, typename _OutA2, typename... _InA>
00452     inline bool
00453     operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
00454                const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
00455     {
00456       return __a.outer_allocator() == __b.outer_allocator()
00457           && __a._M_inner == __b._M_inner;
00458     }
00459 
00460   template <typename _OutA1, typename _OutA2, typename... _InA>
00461     inline bool
00462     operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
00463                const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
00464     { return !(__a == __b); }
00465 
00466   /// @}
00467 
00468 _GLIBCXX_END_NAMESPACE_VERSION
00469 } // namespace
00470 
00471 #endif // C++11
00472 
00473 #endif // _SCOPED_ALLOCATOR