| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
#ifndef _ALLOCATED_PTR_H |
| 31 |
#define _ALLOCATED_PTR_H 1 |
| 32 |
|
| 33 |
#if __cplusplus < 201103L |
| 34 |
# include <bits/c++0xwarning.h> |
| 35 |
#else |
| 36 |
# include <type_traits> |
| 37 |
# include <bits/ptr_traits.h> |
| 38 |
# include <bits/alloc_traits.h> |
| 39 |
|
| 40 |
namespace std _GLIBCXX_VISIBILITY(default) |
| 41 |
{ |
| 42 |
_GLIBCXX_BEGIN_NAMESPACE_VERSION |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
template<typename _Alloc> |
| 47 |
struct __allocated_ptr |
| 48 |
{ |
| 49 |
using pointer = typename allocator_traits<_Alloc>::pointer; |
| 50 |
using value_type = typename allocator_traits<_Alloc>::value_type; |
| 51 |
|
| 52 |
|
| 53 |
__allocated_ptr(_Alloc& __a, pointer __ptr) noexcept |
| 54 |
: _M_alloc(std::__addressof(__a)), _M_ptr(__ptr) |
| 55 |
{ } |
| 56 |
|
| 57 |
|
| 58 |
template<typename _Ptr, |
| 59 |
typename _Req = _Require<is_same<_Ptr, value_type*>>> |
| 60 |
__allocated_ptr(_Alloc& __a, _Ptr __ptr) |
| 61 |
: _M_alloc(std::__addressof(__a)), |
| 62 |
_M_ptr(pointer_traits<pointer>::pointer_to(*__ptr)) |
| 63 |
{ } |
| 64 |
|
| 65 |
|
| 66 |
__allocated_ptr(__allocated_ptr&& __gd) noexcept |
| 67 |
: _M_alloc(__gd._M_alloc), _M_ptr(__gd._M_ptr) |
| 68 |
{ __gd._M_ptr = nullptr; } |
| 69 |
|
| 70 |
|
| 71 |
~__allocated_ptr() |
| 72 |
{ |
| 73 |
if (_M_ptr != nullptr) |
| 74 |
std::allocator_traits<_Alloc>::deallocate(*_M_alloc, _M_ptr, 1); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
__allocated_ptr& |
| 79 |
operator=(std::nullptr_t) noexcept |
| 80 |
{ |
| 81 |
_M_ptr = nullptr; |
| 82 |
return *this; |
| 83 |
} |
| 84 |
|
| 85 |
explicit operator bool() const noexcept { return (bool)_M_ptr; } |
| 86 |
|
| 87 |
|
| 88 |
value_type* get() const { return std::__to_address(_M_ptr); } |
| 89 |
|
| 90 |
pointer release() { return std::__exchange(_M_ptr, nullptr); } |
| 91 |
|
| 92 |
private: |
| 93 |
_Alloc* _M_alloc; |
| 94 |
pointer _M_ptr; |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
template<typename _Alloc> |
| 99 |
inline __allocated_ptr<_Alloc> |
| 100 |
__allocate_guarded(_Alloc& __a) |
| 101 |
{ |
| 102 |
return { __a, std::allocator_traits<_Alloc>::allocate(__a, 1) }; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
template<typename _Alloc> |
| 107 |
struct __allocated_obj : __allocated_ptr<_Alloc> |
| 108 |
{ |
| 109 |
using value_type = typename __allocated_ptr<_Alloc>::value_type; |
| 110 |
|
| 111 |
__allocated_obj(__allocated_obj<_Alloc>&&) = default; |
| 112 |
|
| 113 |
|
| 114 |
__allocated_obj(__allocated_ptr<_Alloc>&& __ptr) |
| 115 |
: __allocated_ptr<_Alloc>(std::move(__ptr)) |
| 116 |
{ ::new ((void*)this->get()) value_type; } |
| 117 |
|
| 118 |
|
| 119 |
~__allocated_obj() |
| 120 |
{ |
| 121 |
if (static_cast<bool>(*this)) |
| 122 |
this->get()->~value_type(); |
| 123 |
} |
| 124 |
|
| 125 |
using __allocated_ptr<_Alloc>::operator=; |
| 126 |
|
| 127 |
value_type& operator*() const { return *this->get(); } |
| 128 |
value_type* operator->() const { return this->get(); } |
| 129 |
}; |
| 130 |
|
| 131 |
|
| 132 |
template<typename _Alloc> |
| 133 |
inline __allocated_obj<_Alloc> |
| 134 |
__allocate_guarded_obj(_Alloc& __a) |
| 135 |
{ |
| 136 |
return { std::__allocate_guarded(__a) }; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
_GLIBCXX_END_NAMESPACE_VERSION |
| 141 |
} |
| 142 |
|
| 143 |
#endif |
| 144 |
#endif |