summaryrefslogtreecommitdiffstats
path: root/ComLightLib/client/CComPtr.hpp
blob: 778659169d622b5b4e2291c5c10b321eaaaed78e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once

namespace ComLight
{
	// COM smart pointer, very comparable to CComPtr from ATL
	template <class I>
	class CComPtr
	{
		I* p;

		void callAddRef() const
		{
			if( nullptr == p )
				return;
			p->AddRef();
		}

	public:

		// Construct with nullptr
		CComPtr() : p( nullptr ) { }

		// Release the pointer
		void release()
		{
			if( nullptr == p )
				return;
			p->Release();
			p = nullptr;
		}

		~CComPtr()
		{
			release();
		}

		// Attach without AddRef()
		void attach( I* raw )
		{
			release();
			p = raw;
		}

		// Detach without Release(), set this pointer to nullptr
		I* detach()
		{
			I* const result = p;
			p = nullptr;
			return result;
		}

		// Detach without Release() and place to the specified address, set this pointer to nullptr
		template<class Other>
		void detach( Other** pp )
		{
			// If the argument points to a non-empty object, release the old instance: would leak memory otherwise.
			if( nullptr != *pp )
				( *pp )->Release();
			( *pp ) = detach();
		}

		// Set and AddRef()
		void assign( I* raw )
		{
			release();
			attach( raw );
			callAddRef();
		}

		void swap( CComPtr<I>& that )
		{
			std::swap( p, that.p );
		}

		// Set and AddRef()
		CComPtr( I* raw ) : p( raw )
		{
			callAddRef();
		}

		// Set and AddRef()
		CComPtr( const CComPtr<I>& that ) : CComPtr( that.p ) { }
		// Move constructor
		CComPtr( CComPtr<I>&& that ) : p( that.p ) { that.p = nullptr; }

		// Set and AddRef()
		void operator=( I* raw )
		{
			assign( raw );
		}

		// Set and AddRef()
		void operator=( const CComPtr<I>& that )
		{
			assign( that.p );
		}

		// Move assignment operator, destroys the other one
		void operator=( CComPtr<I>&& that )
		{
			attach( that.detach() );
		}

		operator I*( ) const { return p; }
		I* operator -> () const { return p; }
		I** operator &() { return &p; }

		operator bool() const { return nullptr != p; }
	};
}