forked from EasyIME/libIME
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComPtr.h
158 lines (125 loc) · 2.92 KB
/
ComPtr.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// Copyright (C) 2013 Hong Jen Yee (PCMan) <[email protected]>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the
// Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
// Boston, MA 02110-1301, USA.
//
#ifndef IME_COM_PTR_H
#define IME_COM_PTR_H
#include <utility>
// ATL-indepdent smart pointers for COM objects
// very similar to the ones provided by ATL (CComPtr & CComQIPtr).
namespace Ime {
// a smart pointer for COM interface/object
// automatic AddRef() on copy and Release() on destruction.
template <class T>
class ComPtr {
public:
ComPtr(void): p_(nullptr) {
}
ComPtr(T* p, bool ref = true): p_(p) {
if (p_ && ref) {
p_->AddRef();
}
}
ComPtr(ComPtr&& other) noexcept : p_(other.p_) {
other.p_ = nullptr;
}
ComPtr(const ComPtr& other): p_(other.p_) {
if (p_) {
p_->AddRef();
}
}
~ComPtr(void) {
if(p_) {
p_->Release();
}
}
T& operator * () const {
return *p_;
}
T** operator & () {
return &p_;
}
T* operator-> () const {
return p_;
}
operator T* () const {
return p_;
}
bool operator !() const {
return !p_;
}
bool operator == (T* p) const {
return p == p_;
}
bool operator != (T* p) const {
return p != p_;
}
bool operator < (T* p) const {
return p_ < p;
}
ComPtr& operator = (ComPtr&& other) noexcept {
p_ = other.p_;
other.p_ = nullptr;
return *this;
}
ComPtr& operator = (const ComPtr& other) {
return operator = (other.p_);
}
ComPtr& operator = (T* p) {
T* old = p_;
p_ = p;
if (p_) {
p_->AddRef();
}
if (old) {
old->Release();
}
return *this;
}
protected:
T* p_;
};
// a smart pointer for COM interface/object with automatic QueryInterface
// QueryInterface() for interface T was done automatically on
// assignment or construction.
// automatic AddRef() on copy and Release() on destruction.
template <class T>
class ComQIPtr: public ComPtr<T> {
public:
ComQIPtr(void): ComPtr<T>() {
}
ComQIPtr(T* p): ComPtr<T>(p) {
}
ComQIPtr(const ComQIPtr& other): ComPtr<T>(other) {
}
ComQIPtr(ComQIPtr&& other) noexcept : ComPtr<T>(std::move(other)) {
}
ComQIPtr(IUnknown* p) : ComPtr<T>() {
if(p) {
p->QueryInterface(__uuidof(T), (void**)&p_);
}
}
ComQIPtr& operator = (IUnknown* p) {
ComPtr<T>::operator = (nullptr);
if(p) {
p->QueryInterface(__uuidof(T), (void**)&p_);
}
return *this;
}
};
}
#endif