comimpl.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. #include "com.h"
  4. #pragma clang diagnostic push
  5. #pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection"
  6. #ifndef COMIMPL_H_INCLUDED
  7. #define COMIMPL_H_INCLUDED
  8. #include <cstring>
  9. __IID_DEF(IUnknown, 0, 0, 0, C0, 00, 00, 00, 00, 00, 00, 46);
  10. class ComObject : public virtual IUnknown
  11. {
  12. private:
  13. unsigned int _refCount;
  14. public:
  15. virtual ULONG AddRef()
  16. {
  17. _refCount++;
  18. return _refCount;
  19. }
  20. virtual ULONG Release()
  21. {
  22. _refCount--;
  23. ULONG rv = _refCount;
  24. if(_refCount == 0)
  25. delete(this);
  26. return rv;
  27. }
  28. ComObject()
  29. {
  30. _refCount = 1;
  31. }
  32. virtual ~ComObject()
  33. {
  34. }
  35. virtual ::HRESULT STDMETHODCALLTYPE QueryInterfaceImpl(REFIID riid, void **ppvObject) = 0;
  36. virtual ::HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid,
  37. void **ppvObject)
  38. {
  39. if(0 == memcmp(riid, &IID_IUnknown, sizeof(GUID)))
  40. *ppvObject = (IUnknown*)this;
  41. else
  42. {
  43. auto rv = QueryInterfaceImpl(riid, ppvObject);
  44. if(rv != S_OK)
  45. return rv;
  46. }
  47. _refCount++;
  48. return S_OK;
  49. }
  50. };
  51. #define FORWARD_IUNKNOWN() \
  52. virtual ULONG Release() override \
  53. { \
  54. return ComObject::Release(); \
  55. } \
  56. virtual ULONG AddRef() override \
  57. { \
  58. return ComObject::AddRef(); \
  59. } \
  60. virtual HRESULT QueryInterface(REFIID riid, void **ppvObject) override \
  61. { \
  62. return ComObject::QueryInterface(riid, ppvObject); \
  63. }
  64. #define BEGIN_INTERFACE_MAP() public: virtual HRESULT STDMETHODCALLTYPE QueryInterfaceImpl(REFIID riid, void **ppvObject) override {
  65. #define INTERFACE_MAP_ENTRY(TInterface, IID) if(0 == memcmp(riid, &IID, sizeof(GUID))) { TInterface* casted = this; *ppvObject = casted; return S_OK; }
  66. #define END_INTERFACE_MAP() return E_NOINTERFACE; }
  67. #define INHERIT_INTERFACE_MAP(TBase) if(TBase::QueryInterfaceImpl(riid, ppvObject) == S_OK) return S_OK;
  68. class ComUnknownObject : public ComObject
  69. {
  70. public:
  71. FORWARD_IUNKNOWN()
  72. virtual ::HRESULT STDMETHODCALLTYPE QueryInterfaceImpl(REFIID riid, void **ppvObject) override
  73. {
  74. return E_NOINTERFACE;
  75. };
  76. virtual ~ComUnknownObject(){}
  77. };
  78. template<class TInterface, GUID const* TIID> class ComSingleObject : public ComObject, public virtual TInterface
  79. {
  80. BEGIN_INTERFACE_MAP()
  81. INTERFACE_MAP_ENTRY(TInterface, *TIID)
  82. END_INTERFACE_MAP()
  83. public:
  84. virtual ~ComSingleObject(){}
  85. };
  86. template<class TInterface>
  87. class ComPtr
  88. {
  89. private:
  90. TInterface* _obj;
  91. public:
  92. ComPtr()
  93. {
  94. _obj = 0;
  95. }
  96. ComPtr(TInterface* pObj)
  97. {
  98. _obj = 0;
  99. if (pObj)
  100. {
  101. _obj = pObj;
  102. _obj->AddRef();
  103. }
  104. }
  105. ComPtr(const ComPtr& ptr)
  106. {
  107. _obj = 0;
  108. if (ptr._obj)
  109. {
  110. _obj = ptr._obj;
  111. _obj->AddRef();
  112. }
  113. }
  114. ComPtr& operator=(ComPtr other)
  115. {
  116. if(_obj != NULL)
  117. _obj->Release();
  118. _obj = other._obj;
  119. if(_obj != NULL)
  120. _obj->AddRef();
  121. return *this;
  122. }
  123. ~ComPtr()
  124. {
  125. if (_obj)
  126. {
  127. _obj->Release();
  128. _obj = 0;
  129. }
  130. }
  131. TInterface* getRaw()
  132. {
  133. return _obj;
  134. }
  135. TInterface* getRetainedReference()
  136. {
  137. if(_obj == NULL)
  138. return NULL;
  139. _obj->AddRef();
  140. return _obj;
  141. }
  142. TInterface** getPPV()
  143. {
  144. return &_obj;
  145. }
  146. operator TInterface*() const
  147. {
  148. return _obj;
  149. }
  150. TInterface& operator*() const
  151. {
  152. return *_obj;
  153. }
  154. TInterface** operator&()
  155. {
  156. return &_obj;
  157. }
  158. TInterface* operator->() const
  159. {
  160. return _obj;
  161. }
  162. };
  163. #endif // COMIMPL_H_INCLUDED
  164. #pragma clang diagnostic pop