diff options
Diffstat (limited to 'zen/com_ptr.h')
-rw-r--r-- | zen/com_ptr.h | 30 |
1 files changed, 14 insertions, 16 deletions
diff --git a/zen/com_ptr.h b/zen/com_ptr.h index 380f4536..de8dbe64 100644 --- a/zen/com_ptr.h +++ b/zen/com_ptr.h @@ -8,6 +8,7 @@ #define SMART_COM_PTR_H #include <algorithm> +#include "win.h" //includes "windows.h" -> always include before other headers that also might include "windows.h"! #include <Objbase.h> namespace zen @@ -16,7 +17,7 @@ namespace zen ComPtr: RAII class handling COM objects Example: - -------- +-------- ComPtr<IUPnPDeviceFinder> devFinder; if (FAILED(::CoCreateInstance(CLSID_UPnPDeviceFinder, NULL, @@ -33,16 +34,17 @@ template <class T> class ComPtr { public: - ComPtr() : ptr(NULL) {} + ComPtr() : ptr(nullptr) {} ComPtr(const ComPtr& other) : ptr(other.ptr) { if (ptr) ptr->AddRef(); } - ComPtr( ComPtr&& other) : ptr(other.release()) {} - - ComPtr& operator=(const ComPtr& other) { ComPtr(other).swap(*this); return *this; } - ComPtr& operator=( ComPtr&& other) { swap(other); return *this; } + ComPtr( ComPtr&& other) : ptr(other.ptr) { other.ptr = nullptr; } ~ComPtr() { if (ptr) ptr->Release(); } + ComPtr& operator=(ComPtr other) { swap(other); return *this; } //unifying assignment: no need for r-value reference assignment! + + void swap(ComPtr& rhs) { std::swap(ptr, rhs.ptr); } //throw() + T** init() //get pointer for use with ::CoCreateInstance() { ComPtr<T>().swap(*this); @@ -51,24 +53,23 @@ public: T* get() const { return ptr; } + T* operator->() const { return ptr; } + T& operator* () const { return *ptr; } + T* release() //throw() { T* tmp = ptr; - ptr = NULL; + ptr = nullptr; return tmp; } - void swap(ComPtr& rhs) { std::swap(ptr, rhs.ptr); } //throw() - - T* operator->() const { return ptr; } - private: T* ptr; struct ConversionToBool { int dummy; }; public: //use member pointer as implicit conversion to bool (C++ Templates - Vandevoorde/Josuttis; chapter 20) - operator int ConversionToBool::* () const { return ptr != NULL ? &ConversionToBool::dummy : NULL; } + operator int ConversionToBool::* () const { return ptr != nullptr ? &ConversionToBool::dummy : nullptr; } }; @@ -96,10 +97,7 @@ ComPtr<S> com_dynamic_cast(const ComPtr<T>& other); //throw() - - - -//################# Inline Implementation ############################# +//################# implementation ############################# //we cannot specialize std::swap() for a class template and are not allowed to overload it => offer swap in own namespace template <class T> inline |