File: ComPtr.h

package info (click to toggle)
jazz2-native 3.5.0-2
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid
  • size: 16,912 kB
  • sloc: cpp: 172,557; xml: 113; python: 36; makefile: 5; sh: 2
file content (369 lines) | stat: -rw-r--r-- 10,835 bytes parent folder | download
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#pragma once

#include "../CommonWindows.h"
#include "../Asserts.h"

#include <type_traits>

// Forward declaration from <unknwn.h>
struct IUnknown;

// Define a helper for the macro below: we just need a function taking a pointer and not returning
// anything to avoid warnings about unused return value of the cast in the macro itself.
namespace Death { namespace Implementation {
	inline void PPV_ARGS_CHECK(void*) {}
}}

/** @brief Used to retrieve an interface pointer, supplying the IID value of the requested interface automatically based on the type of the interface pointer used */
#define DEATH_IID_PPV_ARGS(pType)																				\
	__uuidof(**(pType)),																						\
	(Death::Implementation::PPV_ARGS_CHECK(static_cast<::IUnknown*>(*pType)), reinterpret_cast<void**>(pType))

namespace Death { namespace Containers {
//###==##====#=====--==~--~=~- --- -- -  -  -   -

	/**
		@brief Wraps the interface pointer of the specified type and maintains a reference count
	*/
	template<class T>
	class ComPtr
	{
		template<typename U>
		friend class ComPtr;

	public:
		/** @brief Default constructor */
		constexpr ComPtr(std::nullptr_t = nullptr) noexcept : _pointer(nullptr) {}

		/** @brief Construct from the pointer */
		explicit ComPtr(T* ptr) noexcept : _pointer(ptr) {
			if (_pointer != nullptr) {
				_pointer->AddRef();
			}
		}
		
#if defined(DEATH_TARGET_MSVC) || defined(DOXYGEN_GENERATING_OUTPUT)
		/**
		 * @brief Construct from the pointer of a related type
		 *
		 * @partialsupport Available only on @ref DEATH_TARGET_WINDOWS "Windows" platform.
		 */
		template<class U, std::enable_if_t<!std::is_same<::IUnknown*, U*>::value && !std::is_base_of<T, U>::value, int> = 0>
		explicit ComPtr(U* ptr) noexcept : _pointer(nullptr) {
			HRESULT result = ptr->QueryInterface(DEATH_IID_PPV_ARGS(&_pointer));
			DEATH_ASSERT(result >= 0, ("ComPtr::QueryInterface() failed with error 0x{:.8x}", result), );
		}
#endif

		/** @brief Copy constructor */
		ComPtr(const ComPtr& other) noexcept : ComPtr(other.get()) {}

		/** @overload */
		template<typename U, std::enable_if_t<std::is_convertible<U*, T*>::value, int> = 0>
		ComPtr(const ComPtr<U>& other) noexcept : ComPtr(static_cast<T*>(other.get())) {}

		/** @brief Move constructor */
		ComPtr(ComPtr&& other) noexcept : _pointer(other.detach()) {}

		/** @overload */
		template<typename U, std::enable_if_t<std::is_convertible<U*, T*>::value, int> = 0>
		ComPtr(ComPtr<U>&& other) noexcept : _pointer(other.detach()) {}

		/** @brief Destructor */
		~ComPtr() noexcept {
			if (_pointer != nullptr) {
				_pointer->Release();
			}
		}

		/** @brief Relinquishes ownership and returns the internal interface pointer */
		[[nodiscard]] T* detach() noexcept {
			auto prev = _pointer;
			_pointer = nullptr;
			return prev;
		}

		/** @brief Returns the pointer */
		DEATH_CONSTEXPR14 T* get() const noexcept {
			return _pointer;
		}

		/** @brief Releases the pointer and sets it to the specified value (or `nullptr` by default) */
		void reset(T* ptr = nullptr) noexcept {
			if (_pointer == ptr) {
				return;
			}
			auto prev = _pointer;
			_pointer = ptr;
			if (_pointer != nullptr) {
				_pointer->AddRef();
			}
			if (prev != nullptr) {
				prev->Release();
			}
		}

		/** @brief `nullptr` assignment */
		ComPtr& operator=(std::nullptr_t) noexcept {
			reset();
			return *this;
		}

		/** @brief Pointer assignment */
		ComPtr& operator=(T* ptr) noexcept {
			reset(ptr);
			return *this;
		}

		/** @brief Copy assignment */
		ComPtr& operator=(const ComPtr& other) noexcept {
			reset(other.get());
			return *this;
		}

		/** @overload */
		template<typename U, std::enable_if_t<std::is_convertible<U*, T*>::value, int> = 0>
		ComPtr& operator=(const ComPtr<U>& other) noexcept {
			reset(other.get());
			return *this;
		}

		/** @brief Move assignment */
		ComPtr& operator=(ComPtr&& other) noexcept {
			if (_pointer != other._pointer) {
				if (_pointer != nullptr) {
					_pointer->Release();
				}
				_pointer = other.detach();
			}

			return *this;
		}

		/** @overload */
		template<typename U, std::enable_if_t<std::is_convertible<U*, T*>::value, int> = 0>
		ComPtr& operator=(ComPtr<U>&& other) noexcept {
			if (_pointer != other._pointer) {
				if (_pointer != nullptr) {
					_pointer->Release();
				}
				_pointer = other.detach();
			}

			return *this;
		}

		/** @brief Returns the pointer */
		DEATH_CONSTEXPR14 operator T*() const noexcept {
			return _pointer;
		}

		/** @brief Dereferences the pointer */
		DEATH_CONSTEXPR14 T& operator*() const noexcept {
			return *_pointer;
		}

		/** @brief Allows direct calls against the pointer */
		DEATH_CONSTEXPR14 T* operator->() const noexcept {
			return _pointer;
		}

		/** @brief Returns the address of the internal pointer if the pointer is not initialized yet to be populated by external call */
		DEATH_CONSTEXPR14 T** operator&() {
			DEATH_DEBUG_ASSERT(_pointer == nullptr, "Cannot get direct access to initialized pointer", nullptr);
			return &_pointer;
		}

		/** @brief Whether the pointer is assigned */
		explicit operator bool() const noexcept {
			return (_pointer != nullptr);
		}

#if defined(DEATH_TARGET_MSVC) || defined(DOXYGEN_GENERATING_OUTPUT)
#	ifdef DOXYGEN_GENERATING_OUTPUT
		/**
		 * @brief Tries to cast the instance to another type
		 *
		 * @partialsupport Available only on @ref DEATH_TARGET_WINDOWS "Windows" platform.
		 */
		template<class U>
		HRESULT as(U** result) const noexcept;
#	else
		/**
		 * @brief Tries to cast the instance to another type
		 *
		 * @partialsupport Available only on @ref DEATH_TARGET_WINDOWS "Windows" platform.
		 */
		template<class U, std::enable_if_t<std::is_convertible<T, U>::value, int> = 0>
		HRESULT as(U** result) const noexcept {
			*result = _pointer;
			(*result)->AddRef();
			return S_OK;
		}

		/** @overload */
		template<class U, std::enable_if_t<!std::is_convertible<T, U>::value, int> = 0>
		HRESULT as(U** result) const noexcept {
			return _pointer->QueryInterface(DEATH_IID_PPV_ARGS(result));
		}
#	endif

		/** @overload */
		HRESULT as(REFIID riid, void** result) const noexcept {
			return _pointer->QueryInterface(riid, result);
		}
#endif

	private:
		T* _pointer;
	};

#ifndef DOXYGEN_GENERATING_OUTPUT
	// Comparison with another ComPtr<T> instances
	template<typename TLeft, typename TRight>
	bool operator==(const ComPtr<TLeft>& left, const ComPtr<TRight>& right) noexcept
	{
		static_assert(std::is_convertible<TLeft*, TRight*>::value || std::is_convertible<TRight*, TLeft*>::value,
			"Comparison operator requires left and right pointers to be compatible");
		return (left.get() == right.get());
	}

	template<typename TLeft>
	bool operator==(const ComPtr<TLeft>& left, std::nullptr_t) noexcept
	{
		return (left.get() == nullptr);
	}

	template<typename TLeft, typename TRight>
	bool operator!=(const ComPtr<TLeft>& left, const ComPtr<TRight>& right) noexcept
	{
		return (!(left == right));
	}

	template<typename TLeft, typename TRight>
	bool operator<(const ComPtr<TLeft>& left, const ComPtr<TRight>& right) noexcept
	{
		static_assert(std::is_convertible<TLeft*, TRight*>::value || std::is_convertible<TRight*, TLeft*>::value,
			"Comparison operator requires left and right pointers to be compatible");
		return (left.get() < right.get());
	}

	template<typename TLeft, typename TRight>
	bool operator>=(const ComPtr<TLeft>& left, const ComPtr<TRight>& right) noexcept
	{
		return (!(left < right));
	}

	template<typename TLeft, typename TRight>
	bool operator>(const ComPtr<TLeft>& left, const ComPtr<TRight>& right) noexcept
	{
		return (right < left);
	}

	template<typename TLeft, typename TRight>
	bool operator<=(const ComPtr<TLeft>& left, const ComPtr<TRight>& right) noexcept
	{
		return (!(right < left));
	}

	template<typename TRight>
	bool operator==(std::nullptr_t, const ComPtr<TRight>& right) noexcept
	{
		return (right.get() == nullptr);
	}

	template<typename TLeft>
	bool operator!=(const ComPtr<TLeft>& left, std::nullptr_t) noexcept
	{
		return (left.get() != nullptr);
	}

	template<typename TRight>
	bool operator!=(std::nullptr_t, const ComPtr<TRight>& right) noexcept
	{
		return (right.get() != nullptr);
	}

	// Comparison with raw pointers
	template<typename TLeft, typename TRight>
	bool operator==(const ComPtr<TLeft>& left, TRight* right) noexcept
	{
		static_assert(std::is_convertible<TLeft*, TRight*>::value || std::is_convertible<TRight*, TLeft*>::value,
			"Comparison operator requires left and right pointers to be compatible");
		return (left.get() == right);
	}

	template <typename TLeft, typename TRight>
	bool operator<(const ComPtr<TLeft>& left, TRight* right) noexcept
	{
		static_assert(std::is_convertible<TLeft*, TRight*>::value || std::is_convertible<TRight*, TLeft*>::value,
			"Comparison operator requires left and right pointers to be compatible");
		return (left.get() < right);
	}

	template <typename TLeft, typename ErrLeft, typename TRight>
	bool operator!=(const ComPtr<TLeft>& left, TRight* right) noexcept
	{
		return (!(left == right));
	}

	template <typename TLeft, typename ErrLeft, typename TRight>
	bool operator>=(const ComPtr<TLeft>& left, TRight* right) noexcept
	{
		return (!(left < right));
	}

	template <typename TLeft, typename ErrLeft, typename TRight>
	bool operator>(const ComPtr<TLeft>& left, TRight* right) noexcept
	{
		return (right < left);
	}

	template <typename TLeft, typename ErrLeft, typename TRight>
	bool operator<=(const ComPtr<TLeft>& left, TRight* right) noexcept
	{
		return (!(right < left));
	}

	template <typename TLeft, typename TRight>
	bool operator==(TLeft* left, const ComPtr<TRight>& right) noexcept
	{
		static_assert(std::is_convertible<TLeft*, TRight*>::value || std::is_convertible<TRight*, TLeft*>::value,
			"Comparison operator requires left and right pointers to be compatible");
		return (left == right.get());
	}

	template <typename TLeft, typename TRight>
	bool operator<(TLeft* left, const ComPtr<TRight>& right) noexcept
	{
		static_assert(std::is_convertible<TLeft*, TRight*>::value || std::is_convertible<TRight*, TLeft*>::value,
			"Comparison operator requires left and right pointers to be compatible");
		return (left < right.get());
	}

	template <typename TLeft, typename TRight>
	bool operator!=(TLeft* left, const ComPtr<TRight>& right) noexcept
	{
		return (!(left == right));
	}

	template <typename TLeft, typename TRight>
	bool operator>=(TLeft* left, const ComPtr<TRight>& right) noexcept
	{
		return (!(left < right));
	}

	template <typename TLeft, typename TRight>
	bool operator>(TLeft* left, const ComPtr<TRight>& right) noexcept
	{
		return (right < left);
	}

	template <typename TLeft, typename TRight>
	bool operator<=(TLeft* left, const ComPtr<TRight>& right) noexcept
	{
		return (!(right < left));
	}
#endif

}}