ComPtr.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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) p->AddRef();
  31. if (ptr) ptr->Release();
  32. ptr = p;
  33. }
  34. }
  35. public:
  36. inline ComPtr() : ptr(nullptr) {}
  37. inline ComPtr(T *p) : ptr(p) {if (ptr) ptr->AddRef();}
  38. inline ComPtr(const ComPtr<T> &c) : ptr(c.ptr) {if (ptr) ptr->AddRef();}
  39. inline ComPtr(ComPtr<T> &&c) : ptr(c.ptr) {c.ptr = nullptr;}
  40. inline ~ComPtr() {Kill();}
  41. inline void Clear()
  42. {
  43. if (ptr) {
  44. ptr->Release();
  45. ptr = nullptr;
  46. }
  47. }
  48. inline ComPtr<T> &operator=(T *p)
  49. {
  50. Replace(p);
  51. return *this;
  52. }
  53. inline ComPtr<T> &operator=(const ComPtr<T> &c)
  54. {
  55. Replace(c.ptr);
  56. return *this;
  57. }
  58. inline ComPtr<T> &operator=(ComPtr<T> &&c)
  59. {
  60. if (&ptr != &c.ptr) {
  61. Kill();
  62. ptr = c.ptr;
  63. c.ptr = nullptr;
  64. }
  65. return *this;
  66. }
  67. inline T *Detach()
  68. {
  69. T *out = ptr;
  70. ptr = nullptr;
  71. return out;
  72. }
  73. inline void CopyTo(T **out)
  74. {
  75. if (out) {
  76. if (ptr) ptr->AddRef();
  77. *out = ptr;
  78. }
  79. }
  80. inline ULONG Release()
  81. {
  82. ULONG ref;
  83. if (!ptr) return 0;
  84. ref = ptr->Release();
  85. ptr = nullptr;
  86. return ref;
  87. }
  88. inline T **Assign() {Clear(); return &ptr;}
  89. inline void Set(T *p) {Kill(); ptr = p;}
  90. inline T *Get() const {return ptr;}
  91. inline T **operator&() {return Assign();}
  92. inline operator T*() const {return ptr;}
  93. inline T *operator->() const {return ptr;}
  94. inline bool operator==(T *p) const {return ptr == p;}
  95. inline bool operator!=(T *p) const {return ptr != p;}
  96. inline bool operator!() const {return !ptr;}
  97. };
  98. #ifdef _WIN32
  99. template<class T> class ComQIPtr : public ComPtr<T> {
  100. public:
  101. inline ComQIPtr(IUnknown *unk)
  102. {
  103. this->ptr = nullptr;
  104. unk->QueryInterface(__uuidof(T), (void**)&this->ptr);
  105. }
  106. inline ComPtr<T> &operator=(IUnknown *unk)
  107. {
  108. ComPtr<T>::Clear();
  109. unk->QueryInterface(__uuidof(T), (void**)&this->ptr);
  110. return *this;
  111. }
  112. };
  113. #endif