30#ifndef _ALLOCATED_PTR_H
31#define _ALLOCATED_PTR_H 1
33#if __cplusplus < 201103L
34# include <bits/c++0xwarning.h>
41namespace std _GLIBCXX_VISIBILITY(default)
43_GLIBCXX_BEGIN_NAMESPACE_VERSION
47 template<
typename _Alloc>
48 struct __allocated_ptr
50 using pointer =
typename allocator_traits<_Alloc>::pointer;
51 using value_type =
typename allocator_traits<_Alloc>::value_type;
54 __allocated_ptr(_Alloc& __a, pointer __ptr) noexcept
59 template<
typename _Ptr,
60 typename _Req = _Require<is_same<_Ptr, value_type*>>>
61 __allocated_ptr(_Alloc& __a, _Ptr __ptr)
63 _M_ptr(pointer_traits<pointer>::pointer_to(*__ptr))
67 __allocated_ptr(__allocated_ptr&& __gd) noexcept
68 : _M_alloc(__gd._M_alloc), _M_ptr(__gd._M_ptr)
69 { __gd._M_ptr =
nullptr; }
74 if (_M_ptr !=
nullptr)
80 operator=(std::nullptr_t)
noexcept
86 explicit operator bool() const noexcept {
return (
bool)_M_ptr; }
89 value_type* get()
const {
return std::__to_address(_M_ptr); }
91 pointer release() {
return std::__exchange(_M_ptr,
nullptr); }
99 template<
typename _Alloc>
100 inline __allocated_ptr<_Alloc>
101 __allocate_guarded(_Alloc& __a)
107 template<
typename _Alloc>
108 struct __allocated_obj : __allocated_ptr<_Alloc>
110 using value_type =
typename __allocated_ptr<_Alloc>::value_type;
112 __allocated_obj(__allocated_obj<_Alloc>&&) =
default;
115 __allocated_obj(__allocated_ptr<_Alloc>&& __ptr)
116 : __allocated_ptr<_Alloc>(std::
move(__ptr))
117 { ::new ((
void*)this->get()) value_type; }
122 if (
static_cast<bool>(*
this))
123 this->get()->~value_type();
126 using __allocated_ptr<_Alloc>::operator=;
128 value_type&
operator*()
const {
return *this->get(); }
129 value_type* operator->()
const {
return this->get(); }
133 template<
typename _Alloc>
134 inline __allocated_obj<_Alloc>
135 __allocate_guarded_obj(_Alloc& __a)
137 return { std::__allocate_guarded(__a) };
165 template<
typename _Alloc>
166 struct _Scoped_allocation
168 using value_type =
typename allocator_traits<_Alloc>::value_type;
169 using pointer =
typename allocator_traits<_Alloc>::pointer;
173 _Scoped_allocation(
const _Alloc& __a,
size_t __n = 1)
174 : _M_a(__a), _M_n(__n), _M_p(_M_a.allocate(__n))
177#if __glibcxx_optional >= 201606L
191 template<
typename... _Args>
193 _Scoped_allocation(
const _Alloc& __a, in_place_t, _Args&&... __args)
194 : _Scoped_allocation(__a, 1)
198 allocator_traits<_Alloc>::construct(_M_a, get(),
204 ~_Scoped_allocation()
206 if (_M_p) [[__unlikely__]]
207 _M_a.deallocate(_M_p, _M_n);
210 _Scoped_allocation(_Scoped_allocation&&) =
delete;
213 get_allocator() const noexcept {
return _M_a; }
215 constexpr value_type*
217 {
return std::__to_address(_M_p); }
221 release() noexcept {
return std::__exchange(_M_p,
nullptr); }
224 [[__no_unique_address__]] _Alloc _M_a;
229#if __glibcxx_optional >= 201606L && __cpp_deduction_guides >= 201606L
230 template<
typename _Alloc,
typename... _Args>
231 _Scoped_allocation(_Alloc, in_place_t, _Args...)
232 -> _Scoped_allocation<_Alloc>;
236_GLIBCXX_END_NAMESPACE_VERSION
constexpr complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
constexpr _Tp && forward(typename std::remove_reference< _Tp >::type &__t) noexcept
Forward an lvalue.
ISO C++ entities toplevel namespace is std.
static constexpr pointer allocate(_Alloc &__a, size_type __n)
Allocate memory.
static constexpr void deallocate(_Alloc &__a, pointer __p, size_type __n)
Deallocate memory.