// -*- C++ -*- //===----------------------------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #ifndef _LIBCPP_SCOPED_ALLOCATOR #define _LIBCPP_SCOPED_ALLOCATOR /* scoped_allocator synopsis namespace std { template class scoped_allocator_adaptor : public OuterAlloc { typedef allocator_traits OuterTraits; // exposition only scoped_allocator_adaptor inner; // exposition only public: typedef OuterAlloc outer_allocator_type; typedef see below inner_allocator_type; typedef typename OuterTraits::value_type value_type; typedef typename OuterTraits::size_type size_type; typedef typename OuterTraits::difference_type difference_type; typedef typename OuterTraits::pointer pointer; typedef typename OuterTraits::const_pointer const_pointer; typedef typename OuterTraits::void_pointer void_pointer; typedef typename OuterTraits::const_void_pointer const_void_pointer; typedef see below propagate_on_container_copy_assignment; typedef see below propagate_on_container_move_assignment; typedef see below propagate_on_container_swap; typedef see below is_always_equal; template struct rebind { typedef scoped_allocator_adaptor< OuterTraits::template rebind_alloc, InnerAllocs...> other; }; scoped_allocator_adaptor(); template scoped_allocator_adaptor(OuterA2&& outerAlloc, const InnerAllocs&... innerAllocs) noexcept; scoped_allocator_adaptor(const scoped_allocator_adaptor& other) noexcept; scoped_allocator_adaptor(scoped_allocator_adaptor&& other) noexcept; template scoped_allocator_adaptor(const scoped_allocator_adaptor& other) noexcept; template scoped_allocator_adaptor(const scoped_allocator_adaptor&& other) noexcept; scoped_allocator_adaptor& operator=(const scoped_allocator_adaptor&) = default; scoped_allocator_adaptor& operator=(scoped_allocator_adaptor&&) = default; ~scoped_allocator_adaptor(); inner_allocator_type& inner_allocator() noexcept; const inner_allocator_type& inner_allocator() const noexcept; outer_allocator_type& outer_allocator() noexcept; const outer_allocator_type& outer_allocator() const noexcept; pointer allocate(size_type n); // [[nodiscard]] in C++20 pointer allocate(size_type n, const_void_pointer hint); // [[nodiscard]] in C++20 void deallocate(pointer p, size_type n) noexcept; size_type max_size() const; template void construct(T* p, Args&& args); template void construct(pair* p, piecewise_construct t, tuple x, tuple y); template void construct(pair* p); template void construct(pair* p, U&& x, V&& y); template void construct(pair* p, const pair& x); template void construct(pair* p, pair&& x); template void destroy(T* p); template void destroy(T* p) noexcept; scoped_allocator_adaptor select_on_container_copy_construction() const noexcept; }; template scoped_allocator_adaptor(OuterAlloc, InnerAllocs...) -> scoped_allocator_adaptor; template bool operator==(const scoped_allocator_adaptor& a, const scoped_allocator_adaptor& b) noexcept; template bool operator!=(const scoped_allocator_adaptor& a, const scoped_allocator_adaptor& b) noexcept; // removed in C++20 } // std */ #include <__assert> // all public C++ headers provide the assertion handler #include <__config> #include <__memory/allocator_traits.h> #include <__memory/uses_allocator_construction.h> #include <__type_traits/common_type.h> #include <__type_traits/enable_if.h> #include <__type_traits/integral_constant.h> #include <__type_traits/is_constructible.h> #include <__type_traits/remove_reference.h> #include <__utility/declval.h> #include <__utility/forward.h> #include <__utility/move.h> #include <__utility/pair.h> #include <__utility/piecewise_construct.h> #include #include #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) # pragma GCC system_header #endif _LIBCPP_PUSH_MACROS #include <__undef_macros> _LIBCPP_BEGIN_NAMESPACE_STD #if !defined(_LIBCPP_CXX03_LANG) // scoped_allocator_adaptor template class scoped_allocator_adaptor; template struct __get_poc_copy_assignment; template struct __get_poc_copy_assignment<_A0> { static const bool value = allocator_traits<_A0>::propagate_on_container_copy_assignment::value; }; template struct __get_poc_copy_assignment<_A0, _Allocs...> { static const bool value = allocator_traits<_A0>::propagate_on_container_copy_assignment::value || __get_poc_copy_assignment<_Allocs...>::value; }; template struct __get_poc_move_assignment; template struct __get_poc_move_assignment<_A0> { static const bool value = allocator_traits<_A0>::propagate_on_container_move_assignment::value; }; template struct __get_poc_move_assignment<_A0, _Allocs...> { static const bool value = allocator_traits<_A0>::propagate_on_container_move_assignment::value || __get_poc_move_assignment<_Allocs...>::value; }; template struct __get_poc_swap; template struct __get_poc_swap<_A0> { static const bool value = allocator_traits<_A0>::propagate_on_container_swap::value; }; template struct __get_poc_swap<_A0, _Allocs...> { static const bool value = allocator_traits<_A0>::propagate_on_container_swap::value || __get_poc_swap<_Allocs...>::value; }; template struct __get_is_always_equal; template struct __get_is_always_equal<_A0> { static const bool value = allocator_traits<_A0>::is_always_equal::value; }; template struct __get_is_always_equal<_A0, _Allocs...> { static const bool value = allocator_traits<_A0>::is_always_equal::value && __get_is_always_equal<_Allocs...>::value; }; template class __scoped_allocator_storage; template class __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...> : public _OuterAlloc { typedef _OuterAlloc outer_allocator_type; protected: typedef scoped_allocator_adaptor<_InnerAllocs...> inner_allocator_type; private: inner_allocator_type __inner_; protected: _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage() _NOEXCEPT {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(_OuterA2&& __outer_alloc, const _InnerAllocs&... __inner_allocs) _NOEXCEPT : outer_allocator_type(std::forward<_OuterA2>(__outer_alloc)), __inner_(__inner_allocs...) {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(const __scoped_allocator_storage<_OuterA2, _InnerAllocs...>& __other) _NOEXCEPT : outer_allocator_type(__other.outer_allocator()), __inner_(__other.inner_allocator()) {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(__scoped_allocator_storage<_OuterA2, _InnerAllocs...>&& __other) _NOEXCEPT : outer_allocator_type(std::move(__other.outer_allocator())), __inner_(std::move(__other.inner_allocator())) {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(_OuterA2&& __o, const inner_allocator_type& __i) _NOEXCEPT : outer_allocator_type(std::forward<_OuterA2>(__o)), __inner_(__i) {} _LIBCPP_HIDE_FROM_ABI inner_allocator_type& inner_allocator() _NOEXCEPT { return __inner_; } _LIBCPP_HIDE_FROM_ABI const inner_allocator_type& inner_allocator() const _NOEXCEPT { return __inner_; } _LIBCPP_HIDE_FROM_ABI outer_allocator_type& outer_allocator() _NOEXCEPT { return static_cast(*this); } _LIBCPP_HIDE_FROM_ABI const outer_allocator_type& outer_allocator() const _NOEXCEPT { return static_cast(*this); } scoped_allocator_adaptor _LIBCPP_HIDE_FROM_ABI select_on_container_copy_construction() const _NOEXCEPT { return scoped_allocator_adaptor( allocator_traits::select_on_container_copy_construction(outer_allocator()), allocator_traits::select_on_container_copy_construction(inner_allocator())); } template friend class __scoped_allocator_storage; }; template class __scoped_allocator_storage<_OuterAlloc> : public _OuterAlloc { typedef _OuterAlloc outer_allocator_type; protected: typedef scoped_allocator_adaptor<_OuterAlloc> inner_allocator_type; _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage() _NOEXCEPT {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(_OuterA2&& __outer_alloc) _NOEXCEPT : outer_allocator_type(std::forward<_OuterA2>(__outer_alloc)) {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(const __scoped_allocator_storage<_OuterA2>& __other) _NOEXCEPT : outer_allocator_type(__other.outer_allocator()) {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI __scoped_allocator_storage(__scoped_allocator_storage<_OuterA2>&& __other) _NOEXCEPT : outer_allocator_type(std::move(__other.outer_allocator())) {} _LIBCPP_HIDE_FROM_ABI inner_allocator_type& inner_allocator() _NOEXCEPT { return static_cast(*this); } _LIBCPP_HIDE_FROM_ABI const inner_allocator_type& inner_allocator() const _NOEXCEPT { return static_cast(*this); } _LIBCPP_HIDE_FROM_ABI outer_allocator_type& outer_allocator() _NOEXCEPT { return static_cast(*this); } _LIBCPP_HIDE_FROM_ABI const outer_allocator_type& outer_allocator() const _NOEXCEPT { return static_cast(*this); } _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor select_on_container_copy_construction() const _NOEXCEPT { return scoped_allocator_adaptor( allocator_traits::select_on_container_copy_construction(outer_allocator())); } __scoped_allocator_storage(const outer_allocator_type& __o, const inner_allocator_type& __i) _NOEXCEPT; template friend class __scoped_allocator_storage; }; // __outermost template decltype(std::declval<_Alloc>().outer_allocator(), true_type()) __has_outer_allocator_test(_Alloc&& __a); template false_type __has_outer_allocator_test(const volatile _Alloc& __a); template struct __has_outer_allocator : public common_type< decltype(std::__has_outer_allocator_test(std::declval<_Alloc&>())) >::type {}; template ::value> struct __outermost { typedef _Alloc type; _LIBCPP_HIDE_FROM_ABI type& operator()(type& __a) const _NOEXCEPT { return __a; } }; template struct __outermost<_Alloc, true> { typedef __libcpp_remove_reference_t< decltype(std::declval<_Alloc>().outer_allocator()) > _OuterAlloc; typedef typename __outermost<_OuterAlloc>::type type; _LIBCPP_HIDE_FROM_ABI type& operator()(_Alloc& __a) const _NOEXCEPT { return __outermost<_OuterAlloc>()(__a.outer_allocator()); } }; template class _LIBCPP_TEMPLATE_VIS scoped_allocator_adaptor<_OuterAlloc, _InnerAllocs...> : public __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...> { typedef __scoped_allocator_storage<_OuterAlloc, _InnerAllocs...> base; typedef allocator_traits<_OuterAlloc> _OuterTraits; public: typedef _OuterAlloc outer_allocator_type; typedef typename base::inner_allocator_type inner_allocator_type; typedef typename _OuterTraits::size_type size_type; typedef typename _OuterTraits::difference_type difference_type; typedef typename _OuterTraits::pointer pointer; typedef typename _OuterTraits::const_pointer const_pointer; typedef typename _OuterTraits::void_pointer void_pointer; typedef typename _OuterTraits::const_void_pointer const_void_pointer; typedef integral_constant< bool, __get_poc_copy_assignment::value > propagate_on_container_copy_assignment; typedef integral_constant< bool, __get_poc_move_assignment::value > propagate_on_container_move_assignment; typedef integral_constant< bool, __get_poc_swap::value > propagate_on_container_swap; typedef integral_constant< bool, __get_is_always_equal::value > is_always_equal; template struct rebind { typedef scoped_allocator_adaptor< typename _OuterTraits::template rebind_alloc<_Tp>, _InnerAllocs... > other; }; _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor() _NOEXCEPT {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor(_OuterA2&& __outer_alloc, const _InnerAllocs&... __inner_allocs) _NOEXCEPT : base(std::forward<_OuterA2>(__outer_alloc), __inner_allocs...) {} // scoped_allocator_adaptor(const scoped_allocator_adaptor& __other) = default; template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor(const scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>& __other) _NOEXCEPT : base(__other) {} template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor(scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>&& __other) _NOEXCEPT : base(std::move(__other)) {} // scoped_allocator_adaptor& operator=(const scoped_allocator_adaptor&) = default; // scoped_allocator_adaptor& operator=(scoped_allocator_adaptor&&) = default; // ~scoped_allocator_adaptor() = default; _LIBCPP_HIDE_FROM_ABI inner_allocator_type& inner_allocator() _NOEXCEPT { return base::inner_allocator(); } _LIBCPP_HIDE_FROM_ABI const inner_allocator_type& inner_allocator() const _NOEXCEPT { return base::inner_allocator(); } _LIBCPP_HIDE_FROM_ABI outer_allocator_type& outer_allocator() _NOEXCEPT { return base::outer_allocator(); } _LIBCPP_HIDE_FROM_ABI const outer_allocator_type& outer_allocator() const _NOEXCEPT { return base::outer_allocator(); } _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI pointer allocate(size_type __n) { return allocator_traits::allocate(outer_allocator(), __n); } _LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI pointer allocate(size_type __n, const_void_pointer __hint) { return allocator_traits::allocate(outer_allocator(), __n, __hint); } _LIBCPP_HIDE_FROM_ABI void deallocate(pointer __p, size_type __n) _NOEXCEPT { allocator_traits::deallocate(outer_allocator(), __p, __n); } _LIBCPP_HIDE_FROM_ABI size_type max_size() const { return allocator_traits::max_size(outer_allocator()); } # if _LIBCPP_STD_VER >= 20 template _LIBCPP_HIDE_FROM_ABI void construct(_Type* __ptr, _Args&&... __args) { using _OM = __outermost; std::apply( [__ptr, this](auto&&... __newargs) { allocator_traits::construct( _OM()(outer_allocator()), __ptr, std::forward(__newargs)...); }, std::uses_allocator_construction_args<_Type>(inner_allocator(), std::forward<_Args>(__args)...)); } # else template _LIBCPP_HIDE_FROM_ABI void construct(_Tp* __p, _Args&&... __args) { __construct(__uses_alloc_ctor<_Tp, inner_allocator_type&, _Args...>(), __p, std::forward<_Args>(__args)...); } template _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, piecewise_construct_t, tuple<_Args1...> __x, tuple<_Args2...> __y) { typedef __outermost _OM; allocator_traits::construct( _OM()(outer_allocator()), __p, piecewise_construct, __transform_tuple(typename __uses_alloc_ctor< _T1, inner_allocator_type&, _Args1... >::type(), std::move(__x), typename __make_tuple_indices::type{}), __transform_tuple(typename __uses_alloc_ctor< _T2, inner_allocator_type&, _Args2... >::type(), std::move(__y), typename __make_tuple_indices::type{})); } template _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p) { construct(__p, piecewise_construct, tuple<>{}, tuple<>{}); } template _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, _Up&& __x, _Vp&& __y) { construct(__p, piecewise_construct, std::forward_as_tuple(std::forward<_Up>(__x)), std::forward_as_tuple(std::forward<_Vp>(__y))); } template _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, const pair<_Up, _Vp>& __x) { construct(__p, piecewise_construct, std::forward_as_tuple(__x.first), std::forward_as_tuple(__x.second)); } template _LIBCPP_HIDE_FROM_ABI void construct(pair<_T1, _T2>* __p, pair<_Up, _Vp>&& __x) { construct(__p, piecewise_construct, std::forward_as_tuple(std::forward<_Up>(__x.first)), std::forward_as_tuple(std::forward<_Vp>(__x.second))); } # endif template _LIBCPP_HIDE_FROM_ABI void destroy(_Tp* __p) { typedef __outermost _OM; allocator_traits::destroy(_OM()(outer_allocator()), __p); } _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor select_on_container_copy_construction() const _NOEXCEPT { return base::select_on_container_copy_construction(); } private: template ::value, int> = 0> _LIBCPP_HIDE_FROM_ABI scoped_allocator_adaptor(_OuterA2&& __o, const inner_allocator_type& __i) _NOEXCEPT : base(std::forward<_OuterA2>(__o), __i) {} template _LIBCPP_HIDE_FROM_ABI void __construct(integral_constant, _Tp* __p, _Args&&... __args) { typedef __outermost _OM; allocator_traits::construct(_OM()(outer_allocator()), __p, std::forward<_Args>(__args)...); } template _LIBCPP_HIDE_FROM_ABI void __construct(integral_constant, _Tp* __p, _Args&&... __args) { typedef __outermost _OM; allocator_traits::construct( _OM()(outer_allocator()), __p, allocator_arg, inner_allocator(), std::forward<_Args>(__args)...); } template _LIBCPP_HIDE_FROM_ABI void __construct(integral_constant, _Tp* __p, _Args&&... __args) { typedef __outermost _OM; allocator_traits::construct( _OM()(outer_allocator()), __p, std::forward<_Args>(__args)..., inner_allocator()); } template _LIBCPP_HIDE_FROM_ABI tuple<_Args&&...> __transform_tuple(integral_constant, tuple<_Args...>&& __t, __tuple_indices<_Idx...>) { return std::forward_as_tuple(std::get<_Idx>(std::move(__t))...); } template _LIBCPP_HIDE_FROM_ABI tuple __transform_tuple(integral_constant, tuple<_Args...>&& __t, __tuple_indices<_Idx...>) { using _Tup = tuple; return _Tup(allocator_arg, inner_allocator(), std::get<_Idx>(std::move(__t))...); } template _LIBCPP_HIDE_FROM_ABI tuple<_Args&&..., inner_allocator_type&> __transform_tuple(integral_constant, tuple<_Args...>&& __t, __tuple_indices<_Idx...>) { using _Tup = tuple<_Args&&..., inner_allocator_type&>; return _Tup(std::get<_Idx>(std::move(__t))..., inner_allocator()); } template friend class __scoped_allocator_storage; }; # if _LIBCPP_STD_VER >= 17 template scoped_allocator_adaptor(_OuterAlloc, _InnerAllocs...) -> scoped_allocator_adaptor<_OuterAlloc, _InnerAllocs...>; # endif template inline _LIBCPP_HIDE_FROM_ABI bool operator==(const scoped_allocator_adaptor<_OuterA1>& __a, const scoped_allocator_adaptor<_OuterA2>& __b) _NOEXCEPT { return __a.outer_allocator() == __b.outer_allocator(); } template inline _LIBCPP_HIDE_FROM_ABI bool operator==(const scoped_allocator_adaptor<_OuterA1, _InnerA0, _InnerAllocs...>& __a, const scoped_allocator_adaptor<_OuterA2, _InnerA0, _InnerAllocs...>& __b) _NOEXCEPT { return __a.outer_allocator() == __b.outer_allocator() && __a.inner_allocator() == __b.inner_allocator(); } # if _LIBCPP_STD_VER <= 17 template inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const scoped_allocator_adaptor<_OuterA1, _InnerAllocs...>& __a, const scoped_allocator_adaptor<_OuterA2, _InnerAllocs...>& __b) _NOEXCEPT { return !(__a == __b); } # endif // _LIBCPP_STD_VER <= 17 #endif // !defined(_LIBCPP_CXX03_LANG) _LIBCPP_END_NAMESPACE_STD _LIBCPP_POP_MACROS #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 # include # include # include # include # include # include # include # include # include # include # include #endif #endif // _LIBCPP_SCOPED_ALLOCATOR