ComPtr.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2013 Hugh 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>
  54. inline ComPtr(ComPtr<U> &&c) noexcept : ptr(c.Detach())
  55. {
  56. }
  57. inline ~ComPtr() { Kill(); }
  58. inline void Clear()
  59. {
  60. if (ptr) {
  61. ptr->Release();
  62. ptr = nullptr;
  63. }
  64. }
  65. inline ComPtr<T> &operator=(T *p)
  66. {
  67. Replace(p);
  68. return *this;
  69. }
  70. inline ComPtr<T> &operator=(const ComPtr<T> &c)
  71. {
  72. Replace(c.ptr);
  73. return *this;
  74. }
  75. inline ComPtr<T> &operator=(ComPtr<T> &&c) noexcept
  76. {
  77. if (&ptr != &c.ptr) {
  78. Kill();
  79. ptr = c.ptr;
  80. c.ptr = nullptr;
  81. }
  82. return *this;
  83. }
  84. template<class U> inline ComPtr<T> &operator=(ComPtr<U> &&c) noexcept
  85. {
  86. Kill();
  87. ptr = c.Detach();
  88. return *this;
  89. }
  90. inline T *Detach()
  91. {
  92. T *out = ptr;
  93. ptr = nullptr;
  94. return out;
  95. }
  96. inline void CopyTo(T **out)
  97. {
  98. if (out) {
  99. if (ptr)
  100. ptr->AddRef();
  101. *out = ptr;
  102. }
  103. }
  104. inline ULONG Release()
  105. {
  106. ULONG ref;
  107. if (!ptr)
  108. return 0;
  109. ref = ptr->Release();
  110. ptr = nullptr;
  111. return ref;
  112. }
  113. inline T **Assign()
  114. {
  115. Clear();
  116. return &ptr;
  117. }
  118. inline void Set(T *p)
  119. {
  120. Kill();
  121. ptr = p;
  122. }
  123. inline T *Get() const { return ptr; }
  124. inline T **operator&() { return Assign(); }
  125. inline operator T *() const { return ptr; }
  126. inline T *operator->() const { return ptr; }
  127. inline bool operator==(T *p) const { return ptr == p; }
  128. inline bool operator!=(T *p) const { return ptr != p; }
  129. inline bool operator!() const { return !ptr; }
  130. };
  131. #ifdef _WIN32
  132. template<class T> class ComQIPtr : public ComPtr<T> {
  133. public:
  134. inline ComQIPtr(IUnknown *unk)
  135. {
  136. this->ptr = nullptr;
  137. unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
  138. }
  139. inline ComPtr<T> &operator=(IUnknown *unk)
  140. {
  141. ComPtr<T>::Clear();
  142. unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
  143. return *this;
  144. }
  145. };
  146. #endif