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