Quellcode durchsuchen

libobs: Update to latest version of ComPtr.hpp

jp9000 vor 10 Jahren
Ursprung
Commit
0c30dca526
1 geänderte Dateien mit 54 neuen und 11 gelöschten Zeilen
  1. 54 11
      libobs/util/windows/ComPtr.hpp

+ 54 - 11
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<typename T> class ComPtr {
+template<class T> class ComPtr {
+
+protected:
 	T *ptr;
 
 	inline void Kill()
@@ -38,11 +40,11 @@ template<typename T> 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<T> &c) : ptr(c.ptr) {if (ptr) ptr->AddRef();}
+	inline ComPtr(ComPtr<T> &&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<T> &operator=(T *p)
 	{
 		Replace(p);
 		return *this;
 	}
 
-	inline ComPtr &operator=(const ComPtr &c)
+	inline ComPtr<T> &operator=(const ComPtr<T> &c)
 	{
 		Replace(c.ptr);
 		return *this;
 	}
 
-	inline ComPtr &operator=(ComPtr &&c)
+	inline ComPtr<T> &operator=(ComPtr<T> &&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 T> class ComQIPtr : public ComPtr<T> {
+
+public:
+	inline ComQIPtr(IUnknown *unk)
+	{
+		this->ptr = nullptr;
+		unk->QueryInterface(__uuidof(T), (void**)&this->ptr);
+	}
+
+	inline ComPtr<T> &operator=(IUnknown *unk)
+	{
+		ComPtr<T>::Clear();
+		unk->QueryInterface(__uuidof(T), (void**)&this->ptr);
+		return *this;
+	}
+};