1*c2c66affSColin Finck #ifndef APITESTS_UNKNOWNBASE_H 2*c2c66affSColin Finck #define APITESTS_UNKNOWNBASE_H 3*c2c66affSColin Finck 4*c2c66affSColin Finck template<typename Interface> 5*c2c66affSColin Finck class CUnknownBase : public Interface 6*c2c66affSColin Finck { 7*c2c66affSColin Finck LONG m_lRef; 8*c2c66affSColin Finck bool m_AutoDelete; 9*c2c66affSColin Finck protected: 10*c2c66affSColin Finck virtual const QITAB* GetQITab() = 0; 11*c2c66affSColin Finck public: 12*c2c66affSColin Finck 13*c2c66affSColin Finck CUnknownBase(bool autoDelete = true, LONG initialRef = 0) m_lRef(initialRef)14*c2c66affSColin Finck : m_lRef(initialRef), 15*c2c66affSColin Finck m_AutoDelete(autoDelete) 16*c2c66affSColin Finck { 17*c2c66affSColin Finck } 18*c2c66affSColin Finck AddRef()19*c2c66affSColin Finck ULONG STDMETHODCALLTYPE AddRef () 20*c2c66affSColin Finck { 21*c2c66affSColin Finck return InterlockedIncrement( &m_lRef ); 22*c2c66affSColin Finck } 23*c2c66affSColin Finck Release()24*c2c66affSColin Finck ULONG STDMETHODCALLTYPE Release() 25*c2c66affSColin Finck { 26*c2c66affSColin Finck long newref = InterlockedDecrement( &m_lRef ); 27*c2c66affSColin Finck if (m_AutoDelete && newref<=0) 28*c2c66affSColin Finck { 29*c2c66affSColin Finck delete this; 30*c2c66affSColin Finck } 31*c2c66affSColin Finck return newref; 32*c2c66affSColin Finck } 33*c2c66affSColin Finck QueryInterface(REFIID riid,void ** ppv)34*c2c66affSColin Finck HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, void** ppv) 35*c2c66affSColin Finck { 36*c2c66affSColin Finck return QISearch(this, GetQITab(), riid, ppv); 37*c2c66affSColin Finck } 38*c2c66affSColin Finck ~CUnknownBase()39*c2c66affSColin Finck virtual ~CUnknownBase() {} 40*c2c66affSColin Finck GetRef()41*c2c66affSColin Finck LONG GetRef() const 42*c2c66affSColin Finck { 43*c2c66affSColin Finck return m_lRef; 44*c2c66affSColin Finck } 45*c2c66affSColin Finck }; 46*c2c66affSColin Finck 47*c2c66affSColin Finck #endif // APITESTS_UNKNOWNBASE_H 48