2013-12-25 21:40:33 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Hugh Bailey <obs.jim@gmail.com>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2019-11-27 16:31:54 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <Unknwn.h>
|
|
|
|
#endif
|
|
|
|
|
2014-05-03 22:54:38 -07:00
|
|
|
/* Oh no I have my own com pointer class, the world is ending, how dare you
|
|
|
|
* write your own! */
|
|
|
|
|
2015-02-08 15:41:47 -08:00
|
|
|
template<class T> class ComPtr {
|
|
|
|
|
|
|
|
protected:
|
2013-09-30 19:37:13 -07:00
|
|
|
T *ptr;
|
|
|
|
|
|
|
|
inline void Kill()
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
ptr->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void Replace(T *p)
|
|
|
|
{
|
|
|
|
if (ptr != p) {
|
2019-06-22 22:13:45 -07:00
|
|
|
if (p)
|
|
|
|
p->AddRef();
|
|
|
|
if (ptr)
|
|
|
|
ptr->Release();
|
2013-09-30 19:37:13 -07:00
|
|
|
ptr = p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-06-22 22:13:45 -07:00
|
|
|
inline ComPtr() : ptr(nullptr) {}
|
|
|
|
inline ComPtr(T *p) : ptr(p)
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
ptr->AddRef();
|
|
|
|
}
|
|
|
|
inline ComPtr(const ComPtr<T> &c) : ptr(c.ptr)
|
|
|
|
{
|
|
|
|
if (ptr)
|
|
|
|
ptr->AddRef();
|
|
|
|
}
|
|
|
|
inline ComPtr(ComPtr<T> &&c) : ptr(c.ptr) { c.ptr = nullptr; }
|
|
|
|
inline ~ComPtr() { Kill(); }
|
2013-09-30 19:37:13 -07:00
|
|
|
|
|
|
|
inline void Clear()
|
|
|
|
{
|
|
|
|
if (ptr) {
|
|
|
|
ptr->Release();
|
2015-03-20 22:07:40 -07:00
|
|
|
ptr = nullptr;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-08 15:41:47 -08:00
|
|
|
inline ComPtr<T> &operator=(T *p)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
Replace(p);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-08 15:41:47 -08:00
|
|
|
inline ComPtr<T> &operator=(const ComPtr<T> &c)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
|
|
|
Replace(c.ptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-08 15:41:47 -08:00
|
|
|
inline ComPtr<T> &operator=(ComPtr<T> &&c)
|
2013-09-30 19:37:13 -07:00
|
|
|
{
|
2019-03-31 19:09:50 -07:00
|
|
|
if (&ptr != &c.ptr) {
|
2013-09-30 19:37:13 -07:00
|
|
|
Kill();
|
|
|
|
ptr = c.ptr;
|
2015-03-20 22:07:40 -07:00
|
|
|
c.ptr = nullptr;
|
2013-09-30 19:37:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2015-02-08 15:41:47 -08:00
|
|
|
inline T *Detach()
|
|
|
|
{
|
|
|
|
T *out = ptr;
|
|
|
|
ptr = nullptr;
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void CopyTo(T **out)
|
|
|
|
{
|
|
|
|
if (out) {
|
2019-06-22 22:13:45 -07:00
|
|
|
if (ptr)
|
|
|
|
ptr->AddRef();
|
2015-02-08 15:41:47 -08:00
|
|
|
*out = ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ULONG Release()
|
|
|
|
{
|
|
|
|
ULONG ref;
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
if (!ptr)
|
|
|
|
return 0;
|
2015-02-08 15:41:47 -08:00
|
|
|
ref = ptr->Release();
|
|
|
|
ptr = nullptr;
|
|
|
|
return ref;
|
|
|
|
}
|
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
inline T **Assign()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
return &ptr;
|
|
|
|
}
|
|
|
|
inline void Set(T *p)
|
|
|
|
{
|
|
|
|
Kill();
|
|
|
|
ptr = p;
|
|
|
|
}
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
inline T *Get() const { return ptr; }
|
2014-03-29 17:19:31 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
inline T **operator&() { return Assign(); }
|
2014-05-03 22:54:38 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
inline operator T *() const { return ptr; }
|
|
|
|
inline T *operator->() const { return ptr; }
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
inline bool operator==(T *p) const { return ptr == p; }
|
|
|
|
inline bool operator!=(T *p) const { return ptr != p; }
|
2013-09-30 19:37:13 -07:00
|
|
|
|
2019-06-22 22:13:45 -07:00
|
|
|
inline bool operator!() const { return !ptr; }
|
2013-09-30 19:37:13 -07:00
|
|
|
};
|
2015-02-08 15:41:47 -08:00
|
|
|
|
2015-03-20 21:19:02 -07:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2015-02-08 15:41:47 -08:00
|
|
|
template<class T> class ComQIPtr : public ComPtr<T> {
|
|
|
|
|
|
|
|
public:
|
|
|
|
inline ComQIPtr(IUnknown *unk)
|
|
|
|
{
|
|
|
|
this->ptr = nullptr;
|
2019-06-22 22:13:45 -07:00
|
|
|
unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
|
2015-02-08 15:41:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
inline ComPtr<T> &operator=(IUnknown *unk)
|
|
|
|
{
|
|
|
|
ComPtr<T>::Clear();
|
2019-06-22 22:13:45 -07:00
|
|
|
unk->QueryInterface(__uuidof(T), (void **)&this->ptr);
|
2015-02-08 15:41:47 -08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
2015-03-20 21:19:02 -07:00
|
|
|
|
|
|
|
#endif
|