ComPtr.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (c) 2023 Lain Bailey <[email protected]>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #ifdef _WIN32
  18. #include <Unknwn.h>
  19. #endif
  20. /* Oh no I have my own com pointer class, the world is ending, how dare you
  21. * write your own! */
  22. template<class T> class ComPtr {
  23. protected:
  24. T *ptr;
  25. inline void Kill()
  26. {
  27. if (ptr)
  28. ptr->Release();
  29. }
  30. inline void Replace(T *p)
  31. {
  32. if (ptr != p) {
  33. if (p)
  34. p->AddRef();
  35. if (ptr)
  36. ptr->Release();
  37. ptr = p;
  38. }
  39. }
  40. public:
  41. inline ComPtr() : ptr(nullptr) {}
  42. inline ComPtr(T *p) : ptr(p)
  43. {
  44. if (ptr)
  45. ptr->AddRef();
  46. }
  47. inline ComPtr(const ComPtr<T> &c) : ptr(c.ptr)
  48. {
  49. if (ptr)
  50. ptr->AddRef();
  51. }
  52. inline ComPtr(ComPtr<T> &&c) noexcept : ptr(c.ptr) { c.ptr = nullptr; }
  53. template<class U> inline ComPtr(ComPtr<U> &&c) noexcept : ptr(c.Detach()) {}
  54. inline ~ComPtr() { Kill(); }
  55. inline void Clear()
  56. {
  57. if (ptr) {
  58. ptr->Release();
  59. ptr = nullptr;
  60. }
  61. }
  62. inline ComPtr<T> &operator=(T *p)
  63. {
  64. Replace(p);
  65. return *this;
  66. }
  67. inline ComPtr<T> &operator=(const ComPtr<T> &c)
  68. {
  69. Replace(c.ptr);
  70. return *this;
  71. }
  72. inline ComPtr<T> &operator=(ComPtr<T> &&c) noexcept
  73. {
  74. if (&ptr != &c.ptr) {
  75. Kill();
  76. ptr = c.ptr;
  77. c.ptr = nullptr;
  78. }
  79. return *this;
  80. }
  81. template<class U> inline ComPtr<T> &operator=(ComPtr<U> &&c) noexcept
  82. {
  83. Kill();
  84. ptr = c.Detach();
  85. return *this;
  86. }
  87. inline T *Detach()
  88. {
  89. T *out = ptr;
  90. ptr = nullptr;
  91. return out;
  92. }
  93. inline void CopyTo(T **out)
  94. {
  95. if (out) {
  96. if (ptr)
  97. ptr->AddRef();
  98. *out = ptr;
  99. }
  100. }
  101. inline ULONG Release()
  102. {
  103. ULONG ref;
  104. if (!ptr)
  105. return 0;
  106. ref = ptr->Release();
  107. ptr = nullptr;
  108. return ref;
  109. }
  110. inline T **Assign()
  111. {
  112. Clear();
  113. return &ptr;
  114. }
  115. inline void Set(T *p)
  116. {
  117. Kill();
  118. ptr = p;
  119. }
  120. inline T *Get() const { return ptr; }
  121. inline T **operator&() { return Assign(); }
  122. inline operator T *() const { return ptr; }
  123. inline T *operator->() const { return ptr; }
  124. inline bool operator==(T *p) const { return ptr == p; }
  125. inline bool operator!=(T *p) const { return ptr != p; }
  126. inline bool operator!() const { return !ptr; }
  127. };
  128. #ifdef _WIN32
  129. template<class T> class ComQIPtr : public ComPtr<T> {
  130. public:
  131. inline ComQIPtr(IUnknown *unk)
  132. {
  133. this->ptr = nullptr;
  134. unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
  135. }
  136. template<class U> inline ComQIPtr(const ComPtr<U> &c)
  137. {
  138. this->ptr = nullptr;
  139. c->QueryInterface(__uuidof(T), (void **)&this->ptr);
  140. }
  141. inline ComPtr<T> &operator=(IUnknown *unk)
  142. {
  143. ComPtr<T>::Clear();
  144. unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
  145. return *this;
  146. }
  147. };
  148. #endif