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