| 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 | template<typename _Alloc> |
| 46 | struct __allocated_ptr |
| 47 | { |
| 48 | using pointer = typename allocator_traits<_Alloc>::pointer; |
| 49 | using value_type = typename allocator_traits<_Alloc>::value_type; |
| 50 | |
| 51 | |
| 52 | __allocated_ptr(_Alloc& __a, pointer __ptr) noexcept |
| 53 | : _M_alloc(std::__addressof(__a)), _M_ptr(__ptr) |
| 54 | { } |
| 55 | |
| 56 | |
| 57 | template<typename _Ptr, |
| 58 | typename _Req = _Require<is_same<_Ptr, value_type*>>> |
| 59 | __allocated_ptr(_Alloc& __a, _Ptr __ptr) |
| 60 | : _M_alloc(std::__addressof(__a)), |
| 61 | _M_ptr(pointer_traits<pointer>::pointer_to(*__ptr)) |
| 62 | { } |
| 63 | |
| 64 | |
| 65 | __allocated_ptr(__allocated_ptr&& __gd) noexcept |
| 66 | : _M_alloc(__gd._M_alloc), _M_ptr(__gd._M_ptr) |
| 67 | { __gd._M_ptr = nullptr; } |
| 68 | |
| 69 | |
| 70 | ~__allocated_ptr() |
| 71 | { |
| 72 | if (_M_ptr != nullptr) |
| 73 | std::allocator_traits<_Alloc>::deallocate(*_M_alloc, _M_ptr, 1); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | __allocated_ptr& |
| 78 | operator=(std::nullptr_t) noexcept |
| 79 | { |
| 80 | _M_ptr = nullptr; |
| 81 | return *this; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | value_type* get() { return _S_raw_ptr(_M_ptr); } |
| 86 | |
| 87 | private: |
| 88 | static value_type* _S_raw_ptr(value_type* __ptr) { return __ptr; } |
| 89 | |
| 90 | template<typename _Ptr> |
| 91 | static auto |
| 92 | _S_raw_ptr(_Ptr __ptr) -> decltype(_S_raw_ptr(__ptr.operator->())) |
| 93 | { return _S_raw_ptr(__ptr.operator->()); } |
| 94 | |
| 95 | _Alloc* _M_alloc; |
| 96 | pointer _M_ptr; |
| 97 | }; |
| 98 | |
| 99 | |
| 100 | template<typename _Alloc> |
| 101 | __allocated_ptr<_Alloc> |
| 102 | __allocate_guarded(_Alloc& __a) |
| 103 | { |
| 104 | return { __a, std::allocator_traits<_Alloc>::allocate(__a, 1) }; |
| 105 | } |
| 106 | |
| 107 | _GLIBCXX_END_NAMESPACE_VERSION |
| 108 | } |
| 109 | |
| 110 | #endif |
| 111 | #endif |
| 112 | |