123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #pragma once
-
- namespace win
- {
- template<typename T>
- class ComPtr;
-
- template<typename T>
- class ComPtrRef
- {
- private:
- ComPtr<T>& m_ComPtr;
-
- ComPtrRef(ComPtr<T>& comPtr) :
- m_ComPtr(comPtr)
- {
- }
-
- friend class ComPtr<T>;
-
- public:
- inline operator T**()
- {
- return m_ComPtr.ReleaseAndGetAddressOf();
- }
-
- inline operator void**()
- {
- return reinterpret_cast<void**>(m_ComPtr.ReleaseAndGetAddressOf());
- }
-
- inline T* operator*() throw ()
- {
- return m_ComPtr;
- }
-
- };
-
- template<typename T>
- class ComPtr
- {
- private:
- T *ptr;
-
- public:
- inline ComPtr(void) : ptr(NULL) {}
- inline ~ComPtr(void) { this->Free(); }
-
- ComPtr(T *ptr)
- {
- if (NULL != (this->ptr = ptr))
- {
- this->ptr->AddRef();
- }
- }
-
- ComPtr(const ComPtr &ptr)
- {
- if (NULL != (this->ptr = ptr.ptr))
- {
- this->ptr->AddRef();
- }
- }
-
- inline bool operator!() const
- {
- return (NULL == this->ptr);
- }
-
- inline operator T*() const { return this->ptr; }
-
- inline T *operator->() const
- {
- //_assert(NULL != this->ptr);
- return this->ptr;
- }
-
- inline T &operator*()
- {
- //_assert(NULL != this->ptr);
- return *this->ptr;
- }
-
- inline ComPtrRef<T> operator&()
- {
- return ComPtrRef<T>(*this);
- }
-
- const ComPtr &operator=(T *ptr)
- {
- if (this->ptr != ptr)
- {
- this->Free();
-
- if (NULL != (this->ptr = ptr))
- {
- this->ptr->AddRef();
- }
- }
-
- return *this;
- }
-
- const ComPtr &operator=(const ComPtr &ptr)
- {
- if (this->ptr != ptr.ptr)
- {
- this->Free();
-
- if (NULL != (this->ptr = ptr.ptr))
- {
- this->ptr->AddRef();
- }
- }
-
- return *this;
- }
-
- void Free(void)
- {
- if (NULL != this->ptr)
- {
- this->ptr->Release();
- this->ptr = NULL;
- }
- }
-
- inline T** ReleaseAndGetAddressOf()
- {
- Free();
- return &ptr;
- }
-
- template<typename U>
- inline HRESULT As(ComPtrRef<U> p) const throw ()
- {
- return ptr->QueryInterface(__uuidof(U), p);
- }
-
- inline bool operator==(std::nullptr_t) const
- {
- return this->ptr == nullptr;
- }
-
- template<typename U>
- inline bool operator==(U* other)
- {
- if (ptr == nullptr || other == nullptr)
- return ptr == other;
-
- ComPtr<IUnknown> meUnknown;
- ComPtr<IUnknown> otherUnknown;
-
- if (FAILED(this->ptr->QueryInterface(__uuidof(IUnknown), &meUnknown)))
- return false;
-
- if (FAILED(other->QueryInterface(__uuidof(IUnknown), &otherUnknown)))
- return false;
-
- return static_cast<IUnknown*>(meUnknown) == static_cast<IUnknown*>(otherUnknown);
- }
-
- template<typename U>
- inline bool operator==(ComPtr<U>& other)
- {
- return *this == static_cast<U*>(other);
- }
-
- inline bool operator!=(std::nullptr_t) const
- {
- return this->ptr != nullptr;
- }
-
- template<typename U>
- inline bool operator!=(U* other)
- {
- return !(*this == other);
- }
-
- template<typename U>
- inline bool operator!=(ComPtr<U>& other)
- {
- return *this != static_cast<U*>(other);
- }
- };
- }
|