From 0c30dca526dd1871dc8b0cbd9fb027c435828ad4 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Sun, 8 Feb 2015 15:41:47 -0800 Subject: [PATCH] libobs: Update to latest version of ComPtr.hpp --- libobs/util/windows/ComPtr.hpp | 65 ++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 11 deletions(-) diff --git a/libobs/util/windows/ComPtr.hpp b/libobs/util/windows/ComPtr.hpp index 38911e5c0..215e09073 100644 --- a/libobs/util/windows/ComPtr.hpp +++ b/libobs/util/windows/ComPtr.hpp @@ -19,7 +19,9 @@ /* Oh no I have my own com pointer class, the world is ending, how dare you * write your own! */ -template class ComPtr { +template class ComPtr { + +protected: T *ptr; inline void Kill() @@ -38,11 +40,11 @@ template class ComPtr { } public: - inline ComPtr() : ptr(NULL) {} - inline ComPtr(T *p) : ptr(p) {if (ptr) ptr->AddRef();} - inline ComPtr(const ComPtr &c) : ptr(c.ptr) {if (ptr) ptr->AddRef();} - inline ComPtr(ComPtr &&c) : ptr(c.ptr) {c.ptr = NULL;} - inline ~ComPtr() {Kill();} + inline ComPtr() : ptr(NULL) {} + inline ComPtr(T *p) : ptr(p) {if (ptr) ptr->AddRef();} + inline ComPtr(const ComPtr &c) : ptr(c.ptr) {if (ptr) ptr->AddRef();} + inline ComPtr(ComPtr &&c) : ptr(c.ptr) {c.ptr = NULL;} + inline ~ComPtr() {Kill();} inline void Clear() { @@ -52,19 +54,19 @@ public: } } - inline ComPtr &operator=(T *p) + inline ComPtr &operator=(T *p) { Replace(p); return *this; } - inline ComPtr &operator=(const ComPtr &c) + inline ComPtr &operator=(const ComPtr &c) { Replace(c.ptr); return *this; } - inline ComPtr &operator=(ComPtr &&c) + inline ComPtr &operator=(ComPtr &&c) { if (this != &c) { Kill(); @@ -75,13 +77,37 @@ public: return *this; } + inline T *Detach() + { + T *out = ptr; + ptr = nullptr; + return out; + } + + inline void CopyTo(T **out) + { + if (out) { + if (ptr) ptr->AddRef(); + *out = ptr; + } + } + + inline ULONG Release() + { + ULONG ref; + + if (!ptr) return 0; + ref = ptr->Release(); + ptr = nullptr; + return ref; + } + inline T **Assign() {Clear(); return &ptr;} inline void Set(T *p) {Kill(); ptr = p;} inline T *Get() const {return ptr;} - /* nabbed this one from virtualdub */ - inline T **operator~() {return Assign();} + inline T **operator&() {return Assign();} inline operator T*() const {return ptr;} inline T *operator->() const {return ptr;} @@ -91,3 +117,20 @@ public: inline bool operator!() const {return !ptr;} }; + +template class ComQIPtr : public ComPtr { + +public: + inline ComQIPtr(IUnknown *unk) + { + this->ptr = nullptr; + unk->QueryInterface(__uuidof(T), (void**)&this->ptr); + } + + inline ComPtr &operator=(IUnknown *unk) + { + ComPtr::Clear(); + unk->QueryInterface(__uuidof(T), (void**)&this->ptr); + return *this; + } +};