microcom.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ///////////////////////////////////////////////////////////////////////////////
  2. // //
  3. // microcom.h //
  4. // Copyright (C) Microsoft Corporation. All rights reserved. //
  5. // This file is distributed under the University of Illinois Open Source //
  6. // License. See LICENSE.TXT for details. //
  7. // //
  8. // Provides support for basic COM-like constructs. //
  9. // //
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #ifndef __DXC_MICROCOM__
  12. #define __DXC_MICROCOM__
  13. template <typename TIface>
  14. class CComInterfaceArray {
  15. private:
  16. TIface **m_pData;
  17. unsigned m_length;
  18. public:
  19. CComInterfaceArray() : m_pData(nullptr), m_length(0) { }
  20. ~CComInterfaceArray() {
  21. clear();
  22. }
  23. bool empty() const { return m_length == 0; }
  24. unsigned size() const { return m_length; }
  25. TIface ***data_ref() { return &m_pData; }
  26. unsigned *size_ref() { return &m_length; }
  27. TIface **begin() {
  28. return m_pData;
  29. }
  30. TIface **end() {
  31. return m_pData + m_length;
  32. }
  33. void clear() {
  34. if (m_length) {
  35. for (unsigned i = 0; i < m_length; ++i) {
  36. if (m_pData[i] != nullptr) {
  37. m_pData[i]->Release();
  38. m_pData[i] = nullptr;
  39. }
  40. }
  41. m_length = 0;
  42. }
  43. if (m_pData) {
  44. CoTaskMemFree(m_pData);
  45. m_pData = nullptr;
  46. }
  47. }
  48. HRESULT alloc(unsigned count) {
  49. clear();
  50. m_pData = (TIface**)CoTaskMemAlloc(sizeof(TIface*) * count);
  51. if (m_pData == nullptr)
  52. return E_OUTOFMEMORY;
  53. m_length = count;
  54. ZeroMemory(m_pData, sizeof(TIface*) * count);
  55. return S_OK;
  56. }
  57. TIface **get_address_of(unsigned index) {
  58. return &(m_pData[index]);
  59. }
  60. TIface **release() {
  61. TIface **result = m_pData;
  62. m_pData = nullptr;
  63. m_length = 0;
  64. return result;
  65. }
  66. void release(TIface ***pValues, unsigned *length) {
  67. *pValues = m_pData;
  68. m_pData = nullptr;
  69. *length = m_length;
  70. m_length = 0;
  71. }
  72. };
  73. #define DXC_MICROCOM_REF_FIELD(m_dwRef) volatile ULONG m_dwRef = 0;
  74. #define DXC_MICROCOM_ADDREF_IMPL(m_dwRef) \
  75. ULONG STDMETHODCALLTYPE AddRef() {\
  76. return InterlockedIncrement(&m_dwRef); \
  77. }
  78. #define DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef) \
  79. DXC_MICROCOM_ADDREF_IMPL(m_dwRef) \
  80. ULONG STDMETHODCALLTYPE Release() { \
  81. ULONG result = InterlockedDecrement(&m_dwRef); \
  82. if (result == 0) delete this; \
  83. return result; \
  84. }
  85. template <typename T, typename... Args>
  86. inline T *CreateOnMalloc(IMalloc * pMalloc, Args&&... args) {
  87. void *P = pMalloc->Alloc(sizeof(T)); \
  88. if (P) new (P)T(pMalloc, std::forward<Args>(args)...); \
  89. return (T *)P; \
  90. }
  91. template<typename T>
  92. void DxcCallDestructor(T *obj) {
  93. obj->~T();
  94. }
  95. // The "TM" version keep an IMalloc field that, if not null, indicate
  96. // ownership of 'this' and of any allocations used during release.
  97. #define DXC_MICROCOM_TM_REF_FIELDS() \
  98. volatile ULONG m_dwRef = 0;\
  99. CComPtr<IMalloc> m_pMalloc;
  100. #define DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL() \
  101. DXC_MICROCOM_ADDREF_IMPL(m_dwRef) \
  102. ULONG STDMETHODCALLTYPE Release() { \
  103. ULONG result = InterlockedDecrement(&m_dwRef); \
  104. if (result == 0) { \
  105. CComPtr<IMalloc> pTmp(m_pMalloc); \
  106. DxcThreadMalloc M(pTmp); \
  107. DxcCallDestructor(this); \
  108. pTmp->Free(this); \
  109. } \
  110. return result; \
  111. }
  112. #define DXC_MICROCOM_TM_CTOR(T) \
  113. T(IMalloc *pMalloc) : m_dwRef(0), m_pMalloc(pMalloc) { } \
  114. static T* Alloc(IMalloc *pMalloc) { \
  115. void *P = pMalloc->Alloc(sizeof(T)); \
  116. try { if (P) new (P)T(pMalloc); } catch (...) { operator delete(P); throw; } \
  117. return (T *)P; \
  118. }
  119. /// <summary>
  120. /// Provides a QueryInterface implementation for a class that supports
  121. /// any number of interfaces in addition to IUnknown.
  122. /// </summary>
  123. /// <remarks>
  124. /// This implementation will also report the instance as not supporting
  125. /// marshaling. This will help catch marshaling problems early or avoid
  126. /// them altogether.
  127. /// </remarks>
  128. template<typename... Ts, typename TObject>
  129. HRESULT DoBasicQueryInterface(TObject* self, REFIID iid, void** ppvObject) {
  130. if (ppvObject == nullptr) return E_POINTER;
  131. // Support INoMarshal to void GIT shenanigans.
  132. if (IsEqualIID(iid, __uuidof(IUnknown)) ||
  133. IsEqualIID(iid, __uuidof(INoMarshal))) {
  134. *ppvObject = reinterpret_cast<IUnknown*>(self);
  135. reinterpret_cast<IUnknown*>(self)->AddRef();
  136. return S_OK;
  137. }
  138. return DoBasicQueryInterface_recurse<TObject, Ts...>(self, iid, ppvObject);
  139. }
  140. template<typename TObject>
  141. HRESULT DoBasicQueryInterface_recurse(TObject* self, REFIID iid, void** ppvObject) {
  142. return E_NOINTERFACE;
  143. }
  144. template<typename TObject, typename TInterface, typename... Ts>
  145. HRESULT DoBasicQueryInterface_recurse(TObject* self, REFIID iid, void** ppvObject) {
  146. if (ppvObject == nullptr) return E_POINTER;
  147. if (IsEqualIID(iid, __uuidof(TInterface))) {
  148. *(TInterface**)ppvObject = self;
  149. self->AddRef();
  150. return S_OK;
  151. }
  152. return DoBasicQueryInterface_recurse<TObject, Ts...>(self, iid, ppvObject);
  153. }
  154. template <typename T>
  155. HRESULT AssignToOut(T value, _Out_ T* pResult) {
  156. if (pResult == nullptr)
  157. return E_POINTER;
  158. *pResult = value;
  159. return S_OK;
  160. }
  161. template <typename T>
  162. HRESULT AssignToOut(nullptr_t value, _Out_ T* pResult) {
  163. if (pResult == nullptr)
  164. return E_POINTER;
  165. *pResult = value;
  166. return S_OK;
  167. }
  168. template <typename T>
  169. HRESULT ZeroMemoryToOut(_Out_ T* pResult) {
  170. if (pResult == nullptr)
  171. return E_POINTER;
  172. ZeroMemory(pResult, sizeof(*pResult));
  173. return S_OK;
  174. }
  175. template <typename T>
  176. void AssignToOutOpt(T value, _Out_opt_ T* pResult) {
  177. if (pResult != nullptr)
  178. *pResult = value;
  179. }
  180. template <typename T>
  181. void AssignToOutOpt(nullptr_t value, _Out_opt_ T* pResult) {
  182. if (pResult != nullptr)
  183. *pResult = value;
  184. }
  185. #endif