Home | History | Annotate | Download | only in include
      1 // <scoped_allocator> -*- C++ -*-
      2 
      3 // Copyright (C) 2011, 2012 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file include/scoped_allocator
     26  *  This is a Standard C++ Library header.
     27  */
     28 
     29 #ifndef _SCOPED_ALLOCATOR
     30 #define _SCOPED_ALLOCATOR 1
     31 
     32 #pragma GCC system_header
     33 
     34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
     35 # include <bits/c++0x_warning.h>
     36 #else
     37 
     38 #include <utility>
     39 #include <tuple>
     40 #include <bits/alloc_traits.h>
     41 
     42 namespace std _GLIBCXX_VISIBILITY(default)
     43 {
     44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     45 
     46   template<template<typename> class _Pred, typename... _Allocs>
     47     struct __any_of;
     48 
     49   template<template<typename> class _Pred, typename _Alloc, typename... _Allocs>
     50     struct __any_of<_Pred, _Alloc, _Allocs...>
     51     : __or_<_Pred<_Alloc>, __any_of<_Pred, _Allocs...>>
     52     { };
     53 
     54   template<template<typename> class _Pred, typename _Alloc>
     55     struct __any_of<_Pred, _Alloc>
     56     : _Pred<_Alloc>
     57     { };
     58 
     59   /**
     60    * @addtogroup allocators
     61    * @{
     62    */
     63 
     64   template<typename _Alloc>
     65     struct __propagate_on_copy
     66     : allocator_traits<_Alloc>::propagate_on_container_copy_assignment
     67     { };
     68   template<typename _Alloc>
     69     struct __propagate_on_move
     70     : allocator_traits<_Alloc>::propagate_on_container_move_assignment
     71     { };
     72   template<typename _Alloc>
     73     struct __propagate_on_swap
     74     : allocator_traits<_Alloc>::propagate_on_container_swap
     75     { };
     76 
     77   
     78   template<typename _Alloc>
     79     inline auto
     80     __do_outermost(_Alloc& __a, _Alloc*) -> decltype(__a.outer_allocator())
     81     { return __a.outer_allocator(); }
     82 
     83   template<typename _Alloc>
     84     inline _Alloc&
     85     __do_outermost(_Alloc& __a, ...)
     86     { return __a; }
     87 
     88   template<typename _Alloc>
     89     inline auto
     90     __outermost(_Alloc& __a) -> decltype(__do_outermost(__a, &__a))
     91     { return __do_outermost(__a, &__a); }
     92 
     93   template<typename _OuterAlloc, typename... _InnerAllocs>
     94     class scoped_allocator_adaptor;
     95 
     96   template<typename...> 
     97     struct __inner_type_impl;
     98 
     99   template<typename _Outer>
    100     struct __inner_type_impl<_Outer>
    101     {
    102       typedef scoped_allocator_adaptor<_Outer> __type;
    103 
    104       __inner_type_impl() = default;
    105       __inner_type_impl(const __inner_type_impl&) = default;
    106       __inner_type_impl(__inner_type_impl&&) = default;
    107       
    108       template<typename _Alloc>
    109       __inner_type_impl(const __inner_type_impl<_Alloc>& __other)
    110       { }
    111       
    112       template<typename _Alloc>
    113       __inner_type_impl(__inner_type_impl<_Alloc>&& __other)
    114       { }
    115       
    116       __type& 
    117       _M_get(__type* __p) noexcept { return *__p; }
    118 
    119       const __type& 
    120       _M_get(const __type* __p) const noexcept { return *__p; }
    121       
    122       tuple<> 
    123       _M_tie() const noexcept { return tuple<>(); }
    124       
    125       bool 
    126       operator==(const __inner_type_impl&) const noexcept
    127       { return true; }
    128     };
    129 
    130   template<typename _Outer, typename _InnerHead, typename... _InnerTail>
    131     struct __inner_type_impl<_Outer, _InnerHead, _InnerTail...>
    132     {
    133       typedef scoped_allocator_adaptor<_InnerHead, _InnerTail...> __type;
    134       
    135       __inner_type_impl() = default;
    136       __inner_type_impl(const __inner_type_impl&) = default;
    137       __inner_type_impl(__inner_type_impl&&) = default;
    138       
    139       template<typename... _Allocs>
    140       __inner_type_impl(const __inner_type_impl<_Allocs...>& __other)
    141       : _M_inner(__other._M_inner) { }
    142       
    143       template<typename... _Allocs>
    144       __inner_type_impl(__inner_type_impl<_Allocs...>&& __other)
    145       : _M_inner(std::move(__other._M_inner)) { }
    146 
    147     template<typename... _Args>
    148       explicit
    149       __inner_type_impl(_Args&&... __args)
    150       : _M_inner(std::forward<_Args>(__args)...) { }
    151 
    152       __type& 
    153       _M_get(void*) noexcept { return _M_inner; }
    154       
    155       const __type& 
    156       _M_get(const void*) const noexcept { return _M_inner; }
    157       
    158       tuple<const _InnerHead&, const _InnerTail&...> 
    159       _M_tie() const noexcept
    160       { return _M_inner._M_tie(); }
    161       
    162       bool 
    163       operator==(const __inner_type_impl& __other) const noexcept
    164       { return _M_inner == __other._M_inner; }
    165       
    166     private:
    167       template<typename...> friend class __inner_type_impl;
    168       template<typename, typename...> friend class scoped_allocator_adaptor;
    169       
    170       __type _M_inner;
    171     };
    172 
    173   /// Primary class template.
    174   template<typename _OuterAlloc, typename... _InnerAllocs>
    175     class scoped_allocator_adaptor
    176     : public _OuterAlloc
    177     {
    178       typedef allocator_traits<_OuterAlloc> __traits;
    179 
    180       typedef __inner_type_impl<_OuterAlloc, _InnerAllocs...> __inner_type;
    181       __inner_type _M_inner;
    182 
    183       template<typename _Outer, typename... _Inner>
    184         friend class scoped_allocator_adaptor;
    185 
    186       template<typename...>
    187         friend class __inner_type_impl;
    188 
    189       tuple<const _OuterAlloc&, const _InnerAllocs&...>
    190       _M_tie() const noexcept
    191       { return std::tuple_cat(std::tie(outer_allocator()), _M_inner._M_tie()); }
    192 
    193       
    194       template<typename _Tp, typename... _Args>
    195         void 
    196         _M_construct(__uses_alloc0, _Tp* __p, _Args&&... __args)
    197         {
    198           auto& __outer = __outermost(*this);
    199 	  typedef typename std::decay<decltype(__outer)>::type __outer_type;
    200           typedef allocator_traits<__outer_type> __o_traits;
    201           __o_traits::construct(__outer, __p, std::forward<_Args>(__args)...);
    202         }
    203 
    204       typedef __uses_alloc1<typename __inner_type::__type> __uses_alloc1_;
    205       typedef __uses_alloc2<typename __inner_type::__type> __uses_alloc2_;
    206 
    207       template<typename _Tp, typename... _Args>
    208         void 
    209         _M_construct(__uses_alloc1_, _Tp* __p, _Args&&... __args)
    210         {
    211           auto& __outer = __outermost(*this);
    212 	  typedef typename std::decay<decltype(__outer)>::type __outer_type;
    213           typedef allocator_traits<__outer_type> __o_traits;
    214           __o_traits::construct(__outer, __p, allocator_arg, inner_allocator(),
    215                                 std::forward<_Args>(__args)...);
    216         }
    217 
    218       template<typename _Tp, typename... _Args>
    219         void 
    220         _M_construct(__uses_alloc2_, _Tp* __p, _Args&&... __args)
    221         {
    222 	  auto& __outer = __outermost(*this);
    223 	  typedef typename std::decay<decltype(__outer)>::type __outer_type;
    224           typedef allocator_traits<__outer_type> __o_traits;
    225           __o_traits::construct(__outer, __p, std::forward<_Args>(__args)...,
    226                                 inner_allocator());
    227         }
    228 
    229       template<typename _Alloc>
    230         static _Alloc
    231         _S_select_on_copy(const _Alloc& __a)
    232         {
    233           typedef allocator_traits<_Alloc> __a_traits;
    234           return __a_traits::select_on_container_copy_construction(__a);
    235         }
    236 
    237       template<std::size_t... _Indices>
    238         scoped_allocator_adaptor(tuple<const _OuterAlloc&,
    239                                        const _InnerAllocs&...> __refs,
    240                                  _Index_tuple<_Indices...>)
    241         : _OuterAlloc(_S_select_on_copy(std::get<0>(__refs))),
    242           _M_inner(_S_select_on_copy(std::get<_Indices+1>(__refs))...)
    243         { }
    244 
    245     public:
    246       typedef _OuterAlloc                       outer_allocator_type;
    247       typedef typename __inner_type::__type     inner_allocator_type;
    248 
    249       typedef typename __traits::value_type             value_type;
    250       typedef typename __traits::size_type              size_type;
    251       typedef typename __traits::difference_type        difference_type;
    252       typedef typename __traits::pointer                pointer;
    253       typedef typename __traits::const_pointer          const_pointer;
    254       typedef typename __traits::void_pointer           void_pointer;
    255       typedef typename __traits::const_void_pointer     const_void_pointer;
    256 
    257       typedef typename conditional<
    258         __any_of<__propagate_on_copy, _OuterAlloc, _InnerAllocs...>::value,
    259         true_type, false_type>::type propagate_on_container_copy_assignment;
    260       typedef typename conditional<
    261         __any_of<__propagate_on_move, _OuterAlloc, _InnerAllocs...>::value,
    262         true_type, false_type>::type propagate_on_container_move_assignment;
    263       typedef typename conditional<
    264         __any_of<__propagate_on_swap, _OuterAlloc, _InnerAllocs...>::value,
    265         true_type, false_type>::type propagate_on_container_swap;
    266 
    267       template <class _Tp>
    268         struct rebind
    269         {
    270           typedef scoped_allocator_adaptor<
    271             typename __traits::template rebind_alloc<_Tp>,
    272             _InnerAllocs...> other;
    273         };
    274 
    275       scoped_allocator_adaptor() : _OuterAlloc(), _M_inner() { }
    276 
    277       template<typename _Outer2>
    278         scoped_allocator_adaptor(_Outer2&& __outer,
    279                                  const _InnerAllocs&... __inner)
    280         : _OuterAlloc(std::forward<_Outer2>(__outer)),
    281           _M_inner(__inner...)
    282         { }
    283 
    284       scoped_allocator_adaptor(const scoped_allocator_adaptor& __other)
    285       : _OuterAlloc(__other.outer_allocator()),
    286 	_M_inner(__other._M_inner)
    287       { }
    288 
    289       scoped_allocator_adaptor(scoped_allocator_adaptor&& __other)
    290       : _OuterAlloc(std::move(__other.outer_allocator())),
    291 	_M_inner(std::move(__other._M_inner))
    292       { }
    293 
    294       template<typename _Outer2>
    295         scoped_allocator_adaptor(
    296             const scoped_allocator_adaptor<_Outer2, _InnerAllocs...>& __other)
    297         : _OuterAlloc(__other.outer_allocator()),
    298           _M_inner(__other._M_inner)
    299         { }
    300 
    301       template<typename _Outer2>
    302         scoped_allocator_adaptor(
    303             scoped_allocator_adaptor<_Outer2, _InnerAllocs...>&& __other)
    304         : _OuterAlloc(std::move(__other.outer_allocator())),
    305           _M_inner(std::move(__other._M_inner))
    306         { }
    307 
    308       inner_allocator_type& inner_allocator() noexcept
    309       { return _M_inner._M_get(this); }
    310 
    311       const inner_allocator_type& inner_allocator() const noexcept
    312       { return _M_inner._M_get(this); }
    313 
    314       outer_allocator_type& outer_allocator() noexcept
    315       { return static_cast<_OuterAlloc&>(*this); }
    316 
    317       const outer_allocator_type& outer_allocator() const noexcept
    318       { return static_cast<const _OuterAlloc&>(*this); }
    319 
    320       pointer allocate(size_type __n)
    321       { return __traits::allocate(outer_allocator(), __n); }
    322 
    323       pointer allocate(size_type __n, const_void_pointer __hint)
    324       { return __traits::allocate(outer_allocator(), __n, __hint); }
    325 
    326       void deallocate(pointer __p, size_type __n)
    327       { return __traits::deallocate(outer_allocator(), __p, __n); }
    328 
    329       size_type max_size() const
    330       { return __traits::max_size(outer_allocator()); }
    331 
    332       template<typename _Tp, typename... _Args>
    333         void construct(_Tp* __p, _Args&&... __args)
    334         {
    335           auto& __inner = inner_allocator();
    336           auto __use_tag
    337             = __use_alloc<_Tp, inner_allocator_type, _Args...>(__inner);
    338           _M_construct(__use_tag, __p, std::forward<_Args>(__args)...);
    339         }
    340 
    341       // TODO: construct pairs
    342 
    343       template<typename _Tp>
    344         void destroy(_Tp* __p)
    345         {
    346           auto& __outer = __outermost(*this);
    347 	  typedef typename std::decay<decltype(__outer)>::type __outer_type;
    348           allocator_traits<__outer_type>::destroy(__outer, __p);
    349         }
    350 
    351       scoped_allocator_adaptor
    352       select_on_container_copy_construction() const
    353       {
    354         typedef typename _Build_index_tuple<sizeof...(_InnerAllocs)>::__type
    355 	    _Indices;
    356         return scoped_allocator_adaptor(_M_tie(), _Indices());
    357       }
    358 
    359       template <typename _OutA1, typename _OutA2, typename... _InA>
    360       friend bool
    361       operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
    362                  const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept;
    363     };
    364 
    365   template <typename _OutA1, typename _OutA2, typename... _InA>
    366     inline bool
    367     operator==(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
    368                const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
    369     {
    370       return __a.outer_allocator() == __b.outer_allocator()
    371           && __a._M_inner == __b._M_inner;
    372     }
    373 
    374   template <typename _OutA1, typename _OutA2, typename... _InA>
    375     inline bool
    376     operator!=(const scoped_allocator_adaptor<_OutA1, _InA...>& __a,
    377                const scoped_allocator_adaptor<_OutA2, _InA...>& __b) noexcept
    378     { return !(__a == __b); }
    379 
    380   /// @}
    381 
    382 _GLIBCXX_END_NAMESPACE_VERSION
    383 } // namespace
    384 
    385 #endif // __GXX_EXPERIMENTAL_CXX0X__
    386 
    387 #endif // _SCOPED_ALLOCATOR
    388