ComPtr.hpp 2.9 KB

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