-
Notifications
You must be signed in to change notification settings - Fork 143
/
com_ptr.h
144 lines (113 loc) · 2.89 KB
/
com_ptr.h
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
#ifndef DD_CLR_PROFILER_COM_PTR_H_
#define DD_CLR_PROFILER_COM_PTR_H_
#include <windows.h>
#include <cassert>
// https://msdn.microsoft.com/en-us/magazine/dn904668.aspx
template <typename Interface>
class RemoveAddRefRelease : public Interface {
ULONG __stdcall AddRef() override;
ULONG __stdcall Release() override;
};
template <typename Interface>
class ComPtr {
public:
ComPtr() noexcept = default;
ComPtr(ComPtr const& other) noexcept : m_ptr(other.m_ptr) {
InternalAddRef();
}
template <typename T>
friend class ComPtr;
template <typename T>
ComPtr(ComPtr<T> const& other) noexcept : m_ptr(other.m_ptr) {
InternalAddRef();
}
template <typename T>
ComPtr(ComPtr<T>&& other) noexcept : m_ptr(other.m_ptr) {
other.m_ptr = nullptr;
}
~ComPtr() noexcept { InternalRelease(); }
void Swap(ComPtr& other) noexcept {
Interface* temp = m_ptr;
m_ptr = other.m_ptr;
other.m_ptr = temp;
}
void Reset() noexcept { InternalRelease(); }
Interface* Get() const noexcept { return m_ptr; }
Interface* Detach() noexcept {
Interface* temp = m_ptr;
m_ptr = nullptr;
return temp;
}
void Copy(Interface* other) noexcept { InternalCopy(other); }
void Attach(Interface* other) noexcept {
InternalRelease();
m_ptr = other;
}
Interface** GetAddressOf() noexcept {
assert(m_ptr == nullptr);
return &m_ptr;
}
void CopyTo(Interface** other) const noexcept {
InternalAddRef();
*other = m_ptr;
}
template <typename T>
ComPtr<T> As(IID iid) const noexcept {
ComPtr<T> temp;
m_ptr->QueryInterface(iid, reinterpret_cast<void**>(temp.GetAddressOf()));
return temp;
}
bool IsNull() const noexcept { return nullptr == m_ptr; }
ComPtr& operator=(ComPtr const& other) noexcept {
InternalCopy(other.m_ptr);
return *this;
}
template <typename T>
ComPtr& operator=(ComPtr<T> const& other) noexcept {
InternalCopy(other.m_ptr);
return *this;
}
template <typename T>
ComPtr& operator=(ComPtr<T>&& other) noexcept {
InternalMove(other);
return *this;
}
RemoveAddRefRelease<Interface>* operator->() const noexcept {
return static_cast<RemoveAddRefRelease<Interface>*>(m_ptr);
}
explicit operator bool() const noexcept { return nullptr != m_ptr; }
private:
Interface* m_ptr = nullptr;
void InternalAddRef() const noexcept {
if (m_ptr) {
m_ptr->AddRef();
}
}
void InternalRelease() noexcept {
Interface* temp = m_ptr;
if (temp) {
m_ptr = nullptr;
temp->Release();
}
}
void InternalCopy(Interface* other) noexcept {
if (m_ptr != other) {
InternalRelease();
m_ptr = other;
InternalAddRef();
}
}
template <typename T>
void InternalMove(ComPtr<T>& other) noexcept {
if (m_ptr != other.m_ptr) {
InternalRelease();
m_ptr = other.m_ptr;
other.m_ptr = nullptr;
}
}
};
template <typename Interface>
void swap(ComPtr<Interface>& left, ComPtr<Interface>& right) noexcept {
left.Swap(right);
}
#endif // DD_CLR_PROFILER_COM_PTR_H_