| 58 | |
| 59 | template <class T, T* (*AddObjectReference)(T*), void (*FreeObjectReference)(T*)> |
| 60 | class DbgCoreRefCountObject |
| 61 | { |
| 62 | void AddRefInternal() { m_refs.fetch_add(1); } |
| 63 | |
| 64 | void ReleaseInternal() |
| 65 | { |
| 66 | if (m_refs.fetch_sub(1) == 1) |
| 67 | { |
| 68 | if (!m_registeredRef) |
| 69 | delete this; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | public: |
| 74 | std::atomic<int> m_refs; |
| 75 | bool m_registeredRef = false; |
| 76 | T* m_object; |
| 77 | DbgCoreRefCountObject() : m_refs(0), m_object(nullptr) {} |
| 78 | virtual ~DbgCoreRefCountObject() {} |
| 79 | |
| 80 | T* GetObject() const { return m_object; } |
| 81 | |
| 82 | static T* GetObject(DbgCoreRefCountObject* obj) |
| 83 | { |
| 84 | if (!obj) |
| 85 | return nullptr; |
| 86 | return obj->GetObject(); |
| 87 | } |
| 88 | |
| 89 | void AddRef() |
| 90 | { |
| 91 | if (m_object && (m_refs != 0)) |
| 92 | AddObjectReference(m_object); |
| 93 | AddRefInternal(); |
| 94 | } |
| 95 | |
| 96 | void Release() |
| 97 | { |
| 98 | if (m_object) |
| 99 | FreeObjectReference(m_object); |
| 100 | ReleaseInternal(); |
| 101 | } |
| 102 | |
| 103 | void AddRefForRegistration() { m_registeredRef = true; } |
| 104 | |
| 105 | void ReleaseForRegistration() |
| 106 | { |
| 107 | m_object = nullptr; |
| 108 | m_registeredRef = false; |
| 109 | if (m_refs == 0) |
| 110 | delete this; |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | |
| 115 | template <class T> |
nothing calls this directly
no outgoing calls
no test coverage detected