Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ComPtr.h 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #pragma once
  2. namespace win
  3. {
  4. template<typename T>
  5. class ComPtr;
  6. template<typename T>
  7. class ComPtrRef
  8. {
  9. private:
  10. ComPtr<T>& m_ComPtr;
  11. ComPtrRef(ComPtr<T>& comPtr) :
  12. m_ComPtr(comPtr)
  13. {
  14. }
  15. friend class ComPtr<T>;
  16. public:
  17. inline operator T**()
  18. {
  19. return m_ComPtr.ReleaseAndGetAddressOf();
  20. }
  21. inline operator void**()
  22. {
  23. return reinterpret_cast<void**>(m_ComPtr.ReleaseAndGetAddressOf());
  24. }
  25. inline T* operator*() throw ()
  26. {
  27. return m_ComPtr;
  28. }
  29. };
  30. template<typename T>
  31. class ComPtr
  32. {
  33. private:
  34. T *ptr;
  35. public:
  36. inline ComPtr(void) : ptr(NULL) {}
  37. inline ~ComPtr(void) { this->Free(); }
  38. ComPtr(T *ptr)
  39. {
  40. if (NULL != (this->ptr = ptr))
  41. {
  42. this->ptr->AddRef();
  43. }
  44. }
  45. ComPtr(const ComPtr &ptr)
  46. {
  47. if (NULL != (this->ptr = ptr.ptr))
  48. {
  49. this->ptr->AddRef();
  50. }
  51. }
  52. inline bool operator!() const
  53. {
  54. return (NULL == this->ptr);
  55. }
  56. inline operator T*() const { return this->ptr; }
  57. inline T *operator->() const
  58. {
  59. //_assert(NULL != this->ptr);
  60. return this->ptr;
  61. }
  62. inline T &operator*()
  63. {
  64. //_assert(NULL != this->ptr);
  65. return *this->ptr;
  66. }
  67. inline ComPtrRef<T> operator&()
  68. {
  69. return ComPtrRef<T>(*this);
  70. }
  71. const ComPtr &operator=(T *ptr)
  72. {
  73. if (this->ptr != ptr)
  74. {
  75. this->Free();
  76. if (NULL != (this->ptr = ptr))
  77. {
  78. this->ptr->AddRef();
  79. }
  80. }
  81. return *this;
  82. }
  83. const ComPtr &operator=(const ComPtr &ptr)
  84. {
  85. if (this->ptr != ptr.ptr)
  86. {
  87. this->Free();
  88. if (NULL != (this->ptr = ptr.ptr))
  89. {
  90. this->ptr->AddRef();
  91. }
  92. }
  93. return *this;
  94. }
  95. void Free(void)
  96. {
  97. if (NULL != this->ptr)
  98. {
  99. this->ptr->Release();
  100. this->ptr = NULL;
  101. }
  102. }
  103. inline T** ReleaseAndGetAddressOf()
  104. {
  105. Free();
  106. return &ptr;
  107. }
  108. template<typename U>
  109. inline HRESULT As(ComPtrRef<U> p) const throw ()
  110. {
  111. return ptr->QueryInterface(__uuidof(U), p);
  112. }
  113. inline bool operator==(std::nullptr_t) const
  114. {
  115. return this->ptr == nullptr;
  116. }
  117. template<typename U>
  118. inline bool operator==(U* other)
  119. {
  120. if (ptr == nullptr || other == nullptr)
  121. return ptr == other;
  122. ComPtr<IUnknown> meUnknown;
  123. ComPtr<IUnknown> otherUnknown;
  124. if (FAILED(this->ptr->QueryInterface(__uuidof(IUnknown), &meUnknown)))
  125. return false;
  126. if (FAILED(other->QueryInterface(__uuidof(IUnknown), &otherUnknown)))
  127. return false;
  128. return static_cast<IUnknown*>(meUnknown) == static_cast<IUnknown*>(otherUnknown);
  129. }
  130. template<typename U>
  131. inline bool operator==(ComPtr<U>& other)
  132. {
  133. return *this == static_cast<U*>(other);
  134. }
  135. inline bool operator!=(std::nullptr_t) const
  136. {
  137. return this->ptr != nullptr;
  138. }
  139. template<typename U>
  140. inline bool operator!=(U* other)
  141. {
  142. return !(*this == other);
  143. }
  144. template<typename U>
  145. inline bool operator!=(ComPtr<U>& other)
  146. {
  147. return *this != static_cast<U*>(other);
  148. }
  149. };
  150. }