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