1
0

ComPtr.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /* Oh no I have my own com pointer class, the world is ending, how dare you
  18. * write your own! */
  19. template<class T> class ComPtr {
  20. protected:
  21. T *ptr;
  22. inline void Kill()
  23. {
  24. if (ptr)
  25. ptr->Release();
  26. }
  27. inline void Replace(T *p)
  28. {
  29. if (ptr != p) {
  30. if (p)
  31. p->AddRef();
  32. if (ptr)
  33. ptr->Release();
  34. ptr = p;
  35. }
  36. }
  37. public:
  38. inline ComPtr() : ptr(nullptr) {}
  39. inline ComPtr(T *p) : ptr(p)
  40. {
  41. if (ptr)
  42. ptr->AddRef();
  43. }
  44. inline ComPtr(const ComPtr<T> &c) : ptr(c.ptr)
  45. {
  46. if (ptr)
  47. ptr->AddRef();
  48. }
  49. inline ComPtr(ComPtr<T> &&c) : ptr(c.ptr) { c.ptr = nullptr; }
  50. inline ~ComPtr() { Kill(); }
  51. inline void Clear()
  52. {
  53. if (ptr) {
  54. ptr->Release();
  55. ptr = nullptr;
  56. }
  57. }
  58. inline ComPtr<T> &operator=(T *p)
  59. {
  60. Replace(p);
  61. return *this;
  62. }
  63. inline ComPtr<T> &operator=(const ComPtr<T> &c)
  64. {
  65. Replace(c.ptr);
  66. return *this;
  67. }
  68. inline ComPtr<T> &operator=(ComPtr<T> &&c)
  69. {
  70. if (&ptr != &c.ptr) {
  71. Kill();
  72. ptr = c.ptr;
  73. c.ptr = nullptr;
  74. }
  75. return *this;
  76. }
  77. inline T *Detach()
  78. {
  79. T *out = ptr;
  80. ptr = nullptr;
  81. return out;
  82. }
  83. inline void CopyTo(T **out)
  84. {
  85. if (out) {
  86. if (ptr)
  87. ptr->AddRef();
  88. *out = ptr;
  89. }
  90. }
  91. inline ULONG Release()
  92. {
  93. ULONG ref;
  94. if (!ptr)
  95. return 0;
  96. ref = ptr->Release();
  97. ptr = nullptr;
  98. return ref;
  99. }
  100. inline T **Assign()
  101. {
  102. Clear();
  103. return &ptr;
  104. }
  105. inline void Set(T *p)
  106. {
  107. Kill();
  108. ptr = p;
  109. }
  110. inline T *Get() const { return ptr; }
  111. inline T **operator&() { return Assign(); }
  112. inline operator T *() const { return ptr; }
  113. inline T *operator->() const { return ptr; }
  114. inline bool operator==(T *p) const { return ptr == p; }
  115. inline bool operator!=(T *p) const { return ptr != p; }
  116. inline bool operator!() const { return !ptr; }
  117. };
  118. #ifdef _WIN32
  119. template<class T> class ComQIPtr : public ComPtr<T> {
  120. public:
  121. inline ComQIPtr(IUnknown *unk)
  122. {
  123. this->ptr = nullptr;
  124. unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
  125. }
  126. inline ComPtr<T> &operator=(IUnknown *unk)
  127. {
  128. ComPtr<T>::Clear();
  129. unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
  130. return *this;
  131. }
  132. };
  133. #endif