1
0
mirror of https://github.com/doitsujin/dxvk.git synced 2025-03-07 01:29:15 +01:00
dxvk/src/util/com/com_object.h

73 lines
1.5 KiB
C
Raw Normal View History

2017-10-11 00:41:56 +02:00
#pragma once
#include <atomic>
#include "com_include.h"
2017-10-11 00:41:56 +02:00
namespace dxvk {
/**
* \brief Reference-counted COM object
*
* This can serve as a templated base class for most
* COM objects. It implements AddRef and Release from
* \c IUnknown, and provides methods to increment and
* decrement private references which are not visible
* to the application.
*
* Having two reference counters is sadly necessary
* in order to not break games which steal internal
* references if the refefence count of an object is
+ greater than they expect. DXVK sometimes requires
* holding on to objects which the application wants
* to delete.
*/
2017-10-11 00:41:56 +02:00
template<typename... Base>
class ComObject : public Base... {
public:
virtual ~ComObject() { }
2017-12-12 12:50:52 +01:00
ULONG STDMETHODCALLTYPE AddRef() {
ULONG refCount = m_refCount++;
if (refCount == 0ul)
AddRefPrivate();
return refCount;
2017-10-11 00:41:56 +02:00
}
2017-12-12 12:50:52 +01:00
ULONG STDMETHODCALLTYPE Release() {
2017-10-11 00:41:56 +02:00
ULONG refCount = --m_refCount;
if (refCount == 0ul)
ReleasePrivate();
return refCount;
}
void AddRefPrivate() {
++m_refPrivate;
}
void ReleasePrivate() {
if (--m_refPrivate == 0ul) {
m_refPrivate += 0x80000000;
2017-10-11 00:41:56 +02:00
delete this;
}
2017-10-11 00:41:56 +02:00
}
private:
std::atomic<ULONG> m_refCount = { 0ul };
std::atomic<ULONG> m_refPrivate = { 0ul };
2017-10-11 00:41:56 +02:00
};
template<typename T>
inline void InitReturnPtr(T** ptr) {
if (ptr != nullptr)
*ptr = nullptr;
}
2017-10-11 00:41:56 +02:00
}