summaryrefslogtreecommitdiff
path: root/zen/com_ptr.h
diff options
context:
space:
mode:
authorDaniel Wilhelm <daniel@wili.li>2014-04-18 17:15:39 +0200
committerDaniel Wilhelm <daniel@wili.li>2014-04-18 17:15:39 +0200
commitd2854834e18443876c8f75e0a7f3b88d1d549fc4 (patch)
treee967b628081e50abc7c34cd264e6586271c7e728 /zen/com_ptr.h
parent4.1 (diff)
downloadFreeFileSync-d2854834e18443876c8f75e0a7f3b88d1d549fc4.tar.gz
FreeFileSync-d2854834e18443876c8f75e0a7f3b88d1d549fc4.tar.bz2
FreeFileSync-d2854834e18443876c8f75e0a7f3b88d1d549fc4.zip
4.2
Diffstat (limited to 'zen/com_ptr.h')
-rw-r--r--zen/com_ptr.h30
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
bgstack15