Nui
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1
7#ifndef _WRL_CLIENT_H_
8#define _WRL_CLIENT_H_
9
10#pragma once
11
12#if defined(_MSC_VER) && defined(__clang__)
13# pragma clang diagnostic push
14# pragma clang diagnostic ignored "-Wlanguage-extension-token"
15#endif
16
17#include <stddef.h>
18#include <unknwn.h>
19#include <weakreference.h>
20#include <roapi.h>
21
22#include <wrl/def.h>
23#include <wrl/internal.h>
24
25namespace Microsoft
26{
27 namespace WRL
28 {
29 namespace Details
30 {
31 template <typename T>
33 {
34 protected:
35 T* ptr_;
36
37 public:
38 typedef typename T::InterfaceType InterfaceType;
39
40#ifndef __WRL_CLASSIC_COM__
41 operator IInspectable**() const throw()
42 {
43 static_assert(__is_base_of(IInspectable, InterfaceType), "Invalid cast");
44 return reinterpret_cast<IInspectable**>(ptr_->ReleaseAndGetAddressOf());
45 }
46#endif
47
48 operator IUnknown**() const throw()
49 {
50 static_assert(__is_base_of(IUnknown, InterfaceType), "Invalid cast");
51 return reinterpret_cast<IUnknown**>(ptr_->ReleaseAndGetAddressOf());
52 }
53 };
54
55 template <typename T>
57 {
58 public:
59 ComPtrRef(T* ptr) throw()
60 {
62 }
63
64 operator void**() const throw()
65 {
66 return reinterpret_cast<void**>(ComPtrRefBase<T>::ptr_->ReleaseAndGetAddressOf());
67 }
68
69 operator T*() throw()
70 {
71 *ComPtrRefBase<T>::ptr_ = nullptr;
73 }
74
75 operator typename ComPtrRefBase<T>::InterfaceType **() throw()
76 {
77 return ComPtrRefBase<T>::ptr_->ReleaseAndGetAddressOf();
78 }
79
81 {
82 return ComPtrRefBase<T>::ptr_->Get();
83 }
84
85 typename ComPtrRefBase<T>::InterfaceType* const* GetAddressOf() const throw()
86 {
87 return ComPtrRefBase<T>::ptr_->GetAddressOf();
88 }
89
91 {
92 return ComPtrRefBase<T>::ptr_->ReleaseAndGetAddressOf();
93 }
94 };
95
96 }
97
98 template <typename T>
99 class ComPtr
100 {
101 public:
102 typedef T InterfaceType;
103
104 ComPtr() throw()
105 : ptr_(nullptr)
106 {}
107 ComPtr(decltype(nullptr)) throw()
108 : ptr_(nullptr)
109 {}
110
111 template <class U>
112 ComPtr(U* other) throw()
113 : ptr_(other)
114 {
116 }
117
118 ComPtr(const ComPtr& other) throw()
119 : ptr_(other.ptr_)
120 {
122 }
123
124 template <class U>
125 ComPtr(const ComPtr<U>& other) throw()
126 : ptr_(other.Get())
127 {
129 }
130
131 ComPtr(ComPtr&& other) throw()
132 : ptr_(nullptr)
133 {
134 if (this != reinterpret_cast<ComPtr*>(&reinterpret_cast<unsigned char&>(other)))
135 Swap(other);
136 }
137
138 template <class U>
139 ComPtr(ComPtr<U>&& other) throw()
140 : ptr_(other.Detach())
141 {}
142
143 ~ComPtr() throw()
144 {
146 }
147
148 ComPtr& operator=(decltype(nullptr)) throw()
149 {
151 return *this;
152 }
153
155 {
156 if (ptr_ != other)
157 {
159 ptr_ = other;
161 }
162 return *this;
163 }
164
165 template <typename U>
166 ComPtr& operator=(U* other) throw()
167 {
168 if (ptr_ != other)
169 {
171 ptr_ = other;
173 }
174 return *this;
175 }
176
177 ComPtr& operator=(const ComPtr& other) throw()
178 {
179 if (ptr_ != other.ptr_)
180 ComPtr(other).Swap(*this);
181 return *this;
182 }
183
184 template <class U>
185 ComPtr& operator=(const ComPtr<U>& other) throw()
186 {
187 ComPtr(other).Swap(*this);
188 return *this;
189 }
190
191 ComPtr& operator=(ComPtr&& other) throw()
192 {
193 ComPtr(other).Swap(*this);
194 return *this;
195 }
196
197 template <class U>
198 ComPtr& operator=(ComPtr<U>&& other) throw()
199 {
200 ComPtr(other).Swap(*this);
201 return *this;
202 }
203
204 void Swap(ComPtr&& r) throw()
205 {
206 InterfaceType* tmp = ptr_;
207 ptr_ = r.ptr_;
208 r.ptr_ = tmp;
209 }
210
211 void Swap(ComPtr& r) throw()
212 {
213 InterfaceType* tmp = ptr_;
214 ptr_ = r.ptr_;
215 r.ptr_ = tmp;
216 }
217
218 operator Details::BoolType() const throw()
219 {
220 return Get() != nullptr ? &Details::BoolStruct::Member : nullptr;
221 }
222
223 InterfaceType* Get() const throw()
224 {
225 return ptr_;
226 }
227
228 InterfaceType* operator->() const throw()
229 {
230 return ptr_;
231 }
232
234 {
235 return Details::ComPtrRef<ComPtr<T>>(this);
236 }
237
239 {
241 }
242
243 InterfaceType* const* GetAddressOf() const throw()
244 {
245 return &ptr_;
246 }
247
249 {
250 return &ptr_;
251 }
252
254 {
256 return &ptr_;
257 }
258
260 {
261 T* ptr = ptr_;
262 ptr_ = nullptr;
263 return ptr;
264 }
265
266 void Attach(InterfaceType* other) throw()
267 {
268 if (ptr_ != other)
269 {
271 ptr_ = other;
273 }
274 }
275
276 unsigned long Reset()
277 {
278 return InternalRelease();
279 }
280
281 HRESULT CopyTo(InterfaceType** ptr) const throw()
282 {
284 *ptr = ptr_;
285 return S_OK;
286 }
287
288 HRESULT CopyTo(REFIID riid, void** ptr) const throw()
289 {
290 return ptr_->QueryInterface(riid, ptr);
291 }
292
293 template <typename U>
294 HRESULT CopyTo(U** ptr) const throw()
295 {
296 return ptr_->QueryInterface(__uuidof(U), reinterpret_cast<void**>(ptr));
297 }
298
299 template <typename U>
300 HRESULT As(Details::ComPtrRef<ComPtr<U>> p) const throw()
301 {
302 return ptr_->QueryInterface(__uuidof(U), p);
303 }
304
305 template <typename U>
306 HRESULT As(ComPtr<U>* p) const throw()
307 {
308 return ptr_->QueryInterface(__uuidof(U), reinterpret_cast<void**>(p->ReleaseAndGetAddressOf()));
309 }
310
311 HRESULT AsIID(REFIID riid, ComPtr<IUnknown>* p) const throw()
312 {
313 return ptr_->QueryInterface(riid, reinterpret_cast<void**>(p->ReleaseAndGetAddressOf()));
314 }
315
316 /*
317 HRESULT AsWeak(WeakRef *pWeakRef) const throw() {
318 return ::Microsoft::WRL::AsWeak(ptr_, pWeakRef);
319 }
320 */
321
322 protected:
324
325 void InternalAddRef() const throw()
326 {
327 if (ptr_)
328 ptr_->AddRef();
329 }
330
331 unsigned long InternalRelease() throw()
332 {
333 InterfaceType* tmp = ptr_;
334 if (!tmp)
335 return 0;
336 ptr_ = nullptr;
337 return tmp->Release();
338 }
339 };
340 }
341}
342
343template <typename T>
345{
346 static_assert(__is_base_of(IUnknown, typename T::InterfaceType), "Expected COM interface");
347 return pp;
348}
349
350namespace Windows
351{
352 namespace Foundation
353 {
354 template <typename T>
355 inline HRESULT ActivateInstance(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef<T> instance) throw()
356 {
357 return ActivateInstance(classid, instance.ReleaseAndGetAddressOf());
358 }
359
360 template <typename T>
361 inline HRESULT GetActivationFactory(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef<T> factory) throw()
362 {
363 return RoGetActivationFactory(classid, IID_INS_ARGS(factory.ReleaseAndGetAddressOf()));
364 }
365 }
366}
367
368namespace ABI
369{
370 namespace Windows
371 {
372 namespace Foundation
373 {
374 template <typename T>
375 inline HRESULT ActivateInstance(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef<T> instance) throw()
376 {
377 return ActivateInstance(classid, instance.ReleaseAndGetAddressOf());
378 }
379
380 template <typename T>
381 inline HRESULT
383 {
384 return RoGetActivationFactory(classid, IID_INS_ARGS(factory.ReleaseAndGetAddressOf()));
385 }
386 }
387 }
388}
389
390#if defined(_MSC_VER) && defined(__clang__)
391# pragma clang diagnostic pop
392#endif
393
394
395#endif
Definition client.h:100
Details::ComPtrRef< ComPtr< T > > operator&()
Definition client.h:233
ComPtr & operator=(const ComPtr &other)
Definition client.h:177
ComPtr()
Definition client.h:104
ComPtr(ComPtr &&other)
Definition client.h:131
HRESULT As(ComPtr< U > *p) const
Definition client.h:306
ComPtr & operator=(InterfaceType *other)
Definition client.h:154
~ComPtr()
Definition client.h:143
unsigned long InternalRelease()
Definition client.h:331
InterfaceType * Get() const
Definition client.h:223
ComPtr(ComPtr< U > &&other)
Definition client.h:139
ComPtr(decltype(nullptr))
Definition client.h:107
ComPtr(U *other)
Definition client.h:112
HRESULT As(Details::ComPtrRef< ComPtr< U > > p) const
Definition client.h:300
ComPtr & operator=(const ComPtr< U > &other)
Definition client.h:185
ComPtr & operator=(ComPtr &&other)
Definition client.h:191
void Swap(ComPtr &r)
Definition client.h:211
HRESULT CopyTo(REFIID riid, void **ptr) const
Definition client.h:288
const Details::ComPtrRef< const ComPtr< T > > operator&() const
Definition client.h:238
HRESULT CopyTo(InterfaceType **ptr) const
Definition client.h:281
InterfaceType * operator->() const
Definition client.h:228
void Attach(InterfaceType *other)
Definition client.h:266
void InternalAddRef() const
Definition client.h:325
ComPtr(const ComPtr &other)
Definition client.h:118
InterfaceType ** GetAddressOf()
Definition client.h:248
HRESULT AsIID(REFIID riid, ComPtr< IUnknown > *p) const
Definition client.h:311
ComPtr & operator=(U *other)
Definition client.h:166
unsigned long Reset()
Definition client.h:276
ComPtr & operator=(ComPtr< U > &&other)
Definition client.h:198
HRESULT CopyTo(U **ptr) const
Definition client.h:294
ComPtr & operator=(decltype(nullptr))
Definition client.h:148
T InterfaceType
Definition client.h:102
InterfaceType * ptr_
Definition client.h:323
InterfaceType * Detach()
Definition client.h:259
InterfaceType *const * GetAddressOf() const
Definition client.h:243
ComPtr(const ComPtr< U > &other)
Definition client.h:125
void Swap(ComPtr &&r)
Definition client.h:204
InterfaceType ** ReleaseAndGetAddressOf()
Definition client.h:253
T * ptr_
Definition client.h:35
T::InterfaceType InterfaceType
Definition client.h:38
ComPtrRefBase< T >::InterfaceType * operator*()
Definition client.h:80
ComPtrRef(T *ptr)
Definition client.h:59
ComPtrRefBase< T >::InterfaceType ** ReleaseAndGetAddressOf()
Definition client.h:90
ComPtrRefBase< T >::InterfaceType *const * GetAddressOf() const
Definition client.h:85
void ** IID_PPV_ARGS_Helper(::Microsoft::WRL::Details::ComPtrRef< T > pp)
Definition client.h:344
HRESULT GetActivationFactory(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef< T > factory)
Definition client.h:382
HRESULT ActivateInstance(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef< T > instance)
Definition client.h:375
Definition client.h:369
int BoolStruct::* BoolType
Definition internal.h:47
This file has no copyright assigned and is placed in the Public Domain.
Definition client.h:26
HRESULT GetActivationFactory(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef< T > factory)
Definition client.h:361
HRESULT ActivateInstance(HSTRING classid, ::Microsoft::WRL::Details::ComPtrRef< T > instance)
Definition client.h:355
Definition client.h:351
int Member
Definition internal.h:44