|
1 | 1 | //////////////////////////////////////////////////////////////////////////// |
2 | | -// Module : intrusive_ptr.h |
3 | | -// Created : 30.07.2004 |
4 | | -// Modified : 30.07.2004 |
5 | | -// Author : Dmitriy Iassenev |
6 | | -// Description : Intrusive pointer template |
| 2 | +// Module : intrusive_ptr.h |
| 3 | +// Created : 30.07.2004 |
| 4 | +// Modified : 8.11.2016 by Im-Dex |
| 5 | +// Author : Dmitriy Iassenev |
| 6 | +// Description : Intrusive pointer template |
7 | 7 | //////////////////////////////////////////////////////////////////////////// |
8 | 8 |
|
9 | 9 | #pragma once |
10 | 10 |
|
11 | | -#include "Common/object_type_traits.h" |
12 | | - |
13 | | -#pragma pack(push, 4) |
14 | | - |
15 | 11 | struct intrusive_base |
16 | 12 | { |
17 | | - u32 m_ref_count; |
| 13 | + intrusive_base() XR_NOEXCEPT : m_ref_count(0) {} |
18 | 14 |
|
19 | | - IC intrusive_base() : m_ref_count(0) {} |
20 | 15 | template <typename T> |
21 | | - IC void _release(T* object) |
| 16 | + void release(T* object) XR_NOEXCEPT |
22 | 17 | { |
23 | 18 | try |
24 | 19 | { |
25 | 20 | xr_delete(object); |
26 | 21 | } |
27 | | - catch (...) |
28 | | - { |
29 | | - } |
| 22 | + catch (...) { } |
30 | 23 | } |
| 24 | + |
| 25 | + void acquire() XR_NOEXCEPT { ++m_ref_count; } |
| 26 | + |
| 27 | + bool release() XR_NOEXCEPT { return --m_ref_count == 0; } |
| 28 | + |
| 29 | + bool released() const XR_NOEXCEPT { return m_ref_count == 0; } |
| 30 | + |
| 31 | +private: |
| 32 | + size_t m_ref_count; |
31 | 33 | }; |
32 | 34 |
|
33 | | -template <typename object_type, typename base_type = intrusive_base> |
| 35 | +template <typename ObjectType, typename BaseType = intrusive_base> |
34 | 36 | class intrusive_ptr |
35 | 37 | { |
36 | | -private: |
37 | | - typedef intrusive_ptr<object_type, base_type> self_type; |
38 | | - typedef const object_type* (intrusive_ptr::*unspecified_bool_type)() const; |
| 38 | + using object_type = ObjectType; |
| 39 | + using base_type = BaseType; |
| 40 | + using self_type = intrusive_ptr<object_type, base_type>; |
39 | 41 |
|
40 | | -private: |
41 | | - enum |
42 | | - { |
43 | | - result = object_type_traits::is_base_and_derived<base_type, object_type>::value || |
44 | | - object_type_traits::is_same<base_type, object_type>::value |
45 | | - }; |
| 42 | + static_assert(std::is_base_of_v<BaseType, ObjectType>, |
| 43 | + "ObjectType must be derived from the BaseType"); |
46 | 44 |
|
47 | | -private: |
48 | 45 | object_type* m_object; |
49 | 46 |
|
50 | 47 | protected: |
51 | | - IC void dec(); |
| 48 | + void dec() XR_NOEXCEPT |
| 49 | + { |
| 50 | + if (!m_object) |
| 51 | + return; |
| 52 | + |
| 53 | + if (m_object->release()) |
| 54 | + m_object->release(m_object); |
| 55 | + } |
52 | 56 |
|
53 | 57 | public: |
54 | | - IC intrusive_ptr(); |
55 | | - IC intrusive_ptr(object_type* rhs); |
56 | | - IC intrusive_ptr(self_type const& rhs); |
57 | | - IC ~intrusive_ptr(); |
58 | | - IC self_type& operator=(object_type* rhs); |
59 | | - IC self_type& operator=(self_type const& rhs); |
60 | | - IC object_type& operator*() const; |
61 | | - IC object_type* operator->() const; |
62 | | - IC bool operator!() const; |
63 | | - IC operator unspecified_bool_type() const { return (!m_object ? 0 : &intrusive_ptr::get); } |
64 | | - IC u32 size(); |
65 | | - IC void swap(self_type& rhs); |
66 | | - IC bool equal(const self_type& rhs) const; |
67 | | - IC void set(object_type* rhs); |
68 | | - IC void set(self_type const& rhs); |
69 | | - IC const object_type* get() const; |
70 | | -}; |
| 58 | + intrusive_ptr() XR_NOEXCEPT : m_object(nullptr) {} |
71 | 59 |
|
72 | | -template <typename object_type, typename base_type> |
73 | | -IC bool operator==(intrusive_ptr<object_type, base_type> const& a, intrusive_ptr<object_type, base_type> const& b); |
| 60 | + intrusive_ptr(object_type* rhs) XR_NOEXCEPT : m_object(rhs) |
| 61 | + { |
| 62 | + if (m_object != nullptr) |
| 63 | + m_object->acquire(); |
| 64 | + } |
74 | 65 |
|
75 | | -template <typename object_type, typename base_type> |
76 | | -IC bool operator!=(intrusive_ptr<object_type, base_type> const& a, intrusive_ptr<object_type, base_type> const& b); |
| 66 | + intrusive_ptr(const self_type& rhs) XR_NOEXCEPT : m_object(rhs.m_object) |
| 67 | + { |
| 68 | + if (m_object != nullptr) |
| 69 | + m_object->acquire(); |
| 70 | + } |
77 | 71 |
|
78 | | -template <typename object_type, typename base_type> |
79 | | -IC bool operator<(intrusive_ptr<object_type, base_type> const& a, intrusive_ptr<object_type, base_type> const& b); |
| 72 | + intrusive_ptr(self_type&& rhs) XR_NOEXCEPT : m_object(rhs.m_object) { rhs.m_object = nullptr; } |
80 | 73 |
|
81 | | -template <typename object_type, typename base_type> |
82 | | -IC bool operator>(intrusive_ptr<object_type, base_type> const& a, intrusive_ptr<object_type, base_type> const& b); |
| 74 | + ~intrusive_ptr() XR_NOEXCEPT { dec(); } |
83 | 75 |
|
84 | | -template <typename object_type, typename base_type> |
85 | | -IC void swap(intrusive_ptr<object_type, base_type>& lhs, intrusive_ptr<object_type, base_type>& rhs); |
| 76 | + self_type& operator=(object_type* rhs) XR_NOEXCEPT |
| 77 | + { |
| 78 | + dec(); |
| 79 | + m_object = rhs; |
| 80 | + if (m_object != nullptr) |
| 81 | + m_object->acquire(); |
86 | 82 |
|
87 | | -#include "intrusive_ptr_inline.h" |
| 83 | + return *this; |
| 84 | + } |
| 85 | + |
| 86 | + self_type& operator=(const self_type& rhs) XR_NOEXCEPT |
| 87 | + { |
| 88 | + dec(); |
| 89 | + m_object = rhs.m_object; |
| 90 | + if (m_object != nullptr) |
| 91 | + m_object->acquire(); |
| 92 | + |
| 93 | + return *this; |
| 94 | + } |
| 95 | + |
| 96 | + self_type& operator=(self_type&& rhs) XR_NOEXCEPT |
| 97 | + { |
| 98 | + dec(); |
| 99 | + m_object = rhs.m_object; |
| 100 | + if (m_object != nullptr) |
| 101 | + rhs.m_object = nullptr; |
| 102 | + return *this; |
| 103 | + } |
| 104 | + |
| 105 | + object_type& operator*() const XR_NOEXCEPT |
| 106 | + { |
| 107 | + VERIFY(m_object); |
| 108 | + return *m_object; |
| 109 | + } |
| 110 | + |
| 111 | + object_type* operator->() const XR_NOEXCEPT |
| 112 | + { |
| 113 | + VERIFY(m_object); |
| 114 | + return m_object; |
| 115 | + } |
| 116 | + |
| 117 | + explicit operator bool() const XR_NOEXCEPT { return m_object != nullptr; } |
| 118 | + |
| 119 | + bool operator==(const self_type& rhs) const XR_NOEXCEPT { return m_object == rhs.m_object; } |
| 120 | + |
| 121 | + bool operator!=(const self_type& rhs) const XR_NOEXCEPT { return m_object != rhs.m_object; } |
88 | 122 |
|
89 | | -#pragma pack(pop) |
| 123 | + bool operator<(const self_type& rhs) const XR_NOEXCEPT { return m_object < rhs.m_object; } |
| 124 | + |
| 125 | + bool operator>(const self_type& rhs) const XR_NOEXCEPT { return m_object > rhs.m_object; } |
| 126 | + |
| 127 | + void swap(self_type& rhs) XR_NOEXCEPT |
| 128 | + { |
| 129 | + object_type* tmp = m_object; |
| 130 | + m_object = rhs.m_object; |
| 131 | + rhs.m_object = tmp; |
| 132 | + } |
| 133 | + |
| 134 | + const object_type* get() const XR_NOEXCEPT { return m_object; } |
| 135 | +}; |
| 136 | + |
| 137 | +template <typename object_type, typename base_type> |
| 138 | +void swap(intrusive_ptr<object_type, base_type>& lhs, |
| 139 | + intrusive_ptr<object_type, base_type>& rhs) XR_NOEXCEPT |
| 140 | +{ |
| 141 | + lhs.swap(rhs); |
| 142 | +} |
0 commit comments