microcom.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. // The "TM" version keep an IMalloc field that, if not null, indicate
  92. // ownership of 'this' and of any allocations used during release.
  93. #define DXC_MICROCOM_TM_REF_FIELDS() \
  94. volatile ULONG m_dwRef = 0;\
  95. CComPtr<IMalloc> m_pMalloc;
  96. #define DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL() \
  97. DXC_MICROCOM_ADDREF_IMPL(m_dwRef) \
  98. ULONG STDMETHODCALLTYPE Release() { \
  99. ULONG result = InterlockedDecrement(&m_dwRef); \
  100. if (result == 0) { \
  101. CComPtr<IMalloc> pTmp(m_pMalloc); \
  102. DxcThreadMalloc M(pTmp); delete this; \
  103. } \
  104. return result; \
  105. }
  106. #define DXC_MICROCOM_TM_CTOR(T) \
  107. T(IMalloc *pMalloc) : m_dwRef(0), m_pMalloc(pMalloc) { } \
  108. static T* Alloc(IMalloc *pMalloc) { \
  109. void *P = pMalloc->Alloc(sizeof(T)); \
  110. try { if (P) new (P)T(pMalloc); } catch (...) { operator delete(P); throw; } \
  111. return (T *)P; \
  112. }
  113. /// <summary>
  114. /// Provides a QueryInterface implementation for a class that supports
  115. /// any number of interfaces in addition to IUnknown.
  116. /// </summary>
  117. /// <remarks>
  118. /// This implementation will also report the instance as not supporting
  119. /// marshaling. This will help catch marshaling problems early or avoid
  120. /// them altogether.
  121. /// </remarks>
  122. template<typename... Ts, typename TObject>
  123. HRESULT DoBasicQueryInterface(TObject* self, REFIID iid, void** ppvObject) {
  124. if (ppvObject == nullptr) return E_POINTER;
  125. // Support INoMarshal to void GIT shenanigans.
  126. if (IsEqualIID(iid, __uuidof(IUnknown)) ||
  127. IsEqualIID(iid, __uuidof(INoMarshal))) {
  128. *ppvObject = reinterpret_cast<IUnknown*>(self);
  129. reinterpret_cast<IUnknown*>(self)->AddRef();
  130. return S_OK;
  131. }
  132. return DoBasicQueryInterface_recurse<TObject, Ts...>(self, iid, ppvObject);
  133. }
  134. template<typename TObject>
  135. HRESULT DoBasicQueryInterface_recurse(TObject* self, REFIID iid, void** ppvObject) {
  136. return E_NOINTERFACE;
  137. }
  138. template<typename TObject, typename TInterface, typename... Ts>
  139. HRESULT DoBasicQueryInterface_recurse(TObject* self, REFIID iid, void** ppvObject) {
  140. if (ppvObject == nullptr) return E_POINTER;
  141. if (IsEqualIID(iid, __uuidof(TInterface))) {
  142. *(TInterface**)ppvObject = self;
  143. self->AddRef();
  144. return S_OK;
  145. }
  146. return DoBasicQueryInterface_recurse<TObject, Ts...>(self, iid, ppvObject);
  147. }
  148. template <typename T>
  149. HRESULT AssignToOut(T value, _Out_ T* pResult) {
  150. if (pResult == nullptr)
  151. return E_POINTER;
  152. *pResult = value;
  153. return S_OK;
  154. }
  155. template <typename T>
  156. HRESULT AssignToOut(nullptr_t value, _Out_ T* pResult) {
  157. if (pResult == nullptr)
  158. return E_POINTER;
  159. *pResult = value;
  160. return S_OK;
  161. }
  162. template <typename T>
  163. HRESULT ZeroMemoryToOut(_Out_ T* pResult) {
  164. if (pResult == nullptr)
  165. return E_POINTER;
  166. ZeroMemory(pResult, sizeof(*pResult));
  167. return S_OK;
  168. }
  169. template <typename T>
  170. void AssignToOutOpt(T value, _Out_opt_ T* pResult) {
  171. if (pResult != nullptr)
  172. *pResult = value;
  173. }
  174. template <typename T>
  175. void AssignToOutOpt(nullptr_t value, _Out_opt_ T* pResult) {
  176. if (pResult != nullptr)
  177. *pResult = value;
  178. }
  179. #endif