microcom.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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;
  74. #define DXC_MICROCOM_ADDREF_RELEASE_IMPL(m_dwRef) \
  75. bool HasSingleRef() { return 1 == m_dwRef; } \
  76. ULONG STDMETHODCALLTYPE AddRef() {\
  77. return InterlockedIncrement(&m_dwRef); \
  78. } \
  79. ULONG STDMETHODCALLTYPE Release() { \
  80. ULONG result = InterlockedDecrement(&m_dwRef); \
  81. if (result == 0) delete this; \
  82. return result; \
  83. }
  84. /// <summary>
  85. /// Provides a QueryInterface implementation for a class that supports
  86. /// any number of interfaces in addition to IUnknown.
  87. /// </summary>
  88. /// <remarks>
  89. /// This implementation will also report the instance as not supporting
  90. /// marshaling. This will help catch marshaling problems early or avoid
  91. /// them altogether.
  92. /// </remarks>
  93. template<typename... Ts, typename TObject>
  94. HRESULT DoBasicQueryInterface(TObject* self, REFIID iid, void** ppvObject) {
  95. if (ppvObject == nullptr) return E_POINTER;
  96. // Support INoMarshal to void GIT shenanigans.
  97. if (IsEqualIID(iid, __uuidof(IUnknown)) ||
  98. IsEqualIID(iid, __uuidof(INoMarshal))) {
  99. *ppvObject = reinterpret_cast<IUnknown*>(self);
  100. reinterpret_cast<IUnknown*>(self)->AddRef();
  101. return S_OK;
  102. }
  103. return DoBasicQueryInterface_recurse<TObject, Ts...>(self, iid, ppvObject);
  104. }
  105. template<typename TObject>
  106. HRESULT DoBasicQueryInterface_recurse(TObject* self, REFIID iid, void** ppvObject) {
  107. return E_NOINTERFACE;
  108. }
  109. template<typename TObject, typename TInterface, typename... Ts>
  110. HRESULT DoBasicQueryInterface_recurse(TObject* self, REFIID iid, void** ppvObject) {
  111. if (ppvObject == nullptr) return E_POINTER;
  112. if (IsEqualIID(iid, __uuidof(TInterface))) {
  113. *(TInterface**)ppvObject = self;
  114. self->AddRef();
  115. return S_OK;
  116. }
  117. return DoBasicQueryInterface_recurse<TObject, Ts...>(self, iid, ppvObject);
  118. }
  119. template <typename T>
  120. HRESULT AssignToOut(T value, _Out_ T* pResult) {
  121. if (pResult == nullptr)
  122. return E_POINTER;
  123. *pResult = value;
  124. return S_OK;
  125. }
  126. template <typename T>
  127. HRESULT AssignToOut(nullptr_t value, _Out_ T* pResult) {
  128. if (pResult == nullptr)
  129. return E_POINTER;
  130. *pResult = value;
  131. return S_OK;
  132. }
  133. template <typename T>
  134. HRESULT ZeroMemoryToOut(_Out_ T* pResult) {
  135. if (pResult == nullptr)
  136. return E_POINTER;
  137. ZeroMemory(pResult, sizeof(*pResult));
  138. return S_OK;
  139. }
  140. template <typename T>
  141. void AssignToOutOpt(T value, _Out_opt_ T* pResult) {
  142. if (pResult != nullptr)
  143. *pResult = value;
  144. }
  145. template <typename T>
  146. void AssignToOutOpt(nullptr_t value, _Out_opt_ T* pResult) {
  147. if (pResult != nullptr)
  148. *pResult = value;
  149. }
  150. #endif