summaryrefslogtreecommitdiff
path: root/src/win32/ComPtr.hxx
blob: 01a8ef525c0a84e509c88f26fedb9c402a338f40 (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
111
112
113
114
115
116
117
118
/*
 * Copyright 2020-2021 The Music Player Daemon Project
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef MPD_WIN32_COMPTR_HXX
#define MPD_WIN32_COMPTR_HXX

#include "win32/HResult.hxx"
#include <cstddef>
#include <objbase.h>
#include <utility>
#include <windows.h>

// RAII for Object in Microsoft Component Object Model(COM)
// https://docs.microsoft.com/zh-tw/windows/win32/api/_com/
template <typename T>
class ComPtr {
public:
	using pointer = T *;
	using reference = T &;
	using element_type = T;

	constexpr ComPtr() noexcept : ptr(nullptr) {}
	constexpr ComPtr(std::nullptr_t) noexcept : ptr(nullptr) {}
	explicit constexpr ComPtr(pointer p) noexcept : ptr(p) {}

	ComPtr(const ComPtr &u) noexcept : ptr(u.ptr) {
		if (ptr) {
			ptr->AddRef();
		}
	}
	constexpr ComPtr(ComPtr &&u) noexcept : ptr(std::exchange(u.ptr, nullptr)) {}

	ComPtr &operator=(const ComPtr &r) noexcept {
		reset();
		ptr = r.ptr;
		if (ptr) {
			ptr->AddRef();
		}
		return *this;
	}
	constexpr ComPtr &operator=(ComPtr &&r) noexcept {
		std::swap(ptr, r.ptr);
		return *this;
	}
	ComPtr &operator=(std::nullptr_t) noexcept {
		reset();
		return *this;
	}

	~ComPtr() noexcept { reset(); }

	pointer release() noexcept { return std::exchange(ptr, nullptr); }
	void reset() noexcept {
		if (ptr) {
			release()->Release();
		}
	}
	void swap(ComPtr &other) noexcept { std::swap(ptr, other.ptr); }

	pointer get() const noexcept { return ptr; }
	explicit operator bool() const noexcept { return ptr; }

	reference operator*() const noexcept { return *ptr; }
	pointer operator->() const noexcept { return ptr; }

	void CoCreateInstance(REFCLSID class_id, LPUNKNOWN unknown_outer = nullptr,
			      DWORD class_context = CLSCTX_ALL) {
		HRESULT result =
			::CoCreateInstance(class_id, unknown_outer, class_context,
					   __uuidof(T), reinterpret_cast<void **>(&ptr));
		if (FAILED(result)) {
			throw FormatHResultError(result, "Unable to create instance");
		}
	}

	T **Address() noexcept {
		reset();
		return &ptr;
	}
	template <typename U = void>
	U **AddressCast() noexcept {
		reset();
		return reinterpret_cast<U **>(&ptr);
	}

	template <typename U = void>
	U *Cast() noexcept {
		return reinterpret_cast<U *>(ptr);
	}

private:
	pointer ptr;
};

namespace std {
template <typename T>
void swap(ComPtr<T> &lhs, ComPtr<T> &rhs) noexcept {
	lhs.swap(rhs);
}
} // namespace std

#endif