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