1 | /** @file
|
---|
2 | *
|
---|
3 | * MS COM / XPCOM Abstraction Layer:
|
---|
4 | * Smart COM pointer classes declaration
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006 InnoTek Systemberatung GmbH
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License as published by the Free Software Foundation,
|
---|
14 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
15 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
16 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * If you received this file as part of a commercial VirtualBox
|
---|
19 | * distribution, then only the terms of your commercial VirtualBox
|
---|
20 | * license agreement apply instead of the previous paragraph.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #ifndef __VBox_com_ptr_h__
|
---|
24 | #define __VBox_com_ptr_h__
|
---|
25 |
|
---|
26 | #if defined (__WIN__)
|
---|
27 |
|
---|
28 | #include <atlbase.h>
|
---|
29 |
|
---|
30 | #ifndef _ATL_IIDOF
|
---|
31 | # define _ATL_IIDOF(c) __uuidof(c)
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #else // !defined (__WIN__)
|
---|
35 |
|
---|
36 | #include <nsXPCOM.h>
|
---|
37 | #include <nsIComponentManager.h>
|
---|
38 | #include <nsCOMPtr.h>
|
---|
39 | #include <ipcIService.h>
|
---|
40 | #include <nsIServiceManagerUtils.h>
|
---|
41 | #include <ipcCID.h>
|
---|
42 | #include <ipcIDConnectService.h>
|
---|
43 |
|
---|
44 | // official XPCOM headers don't define it yet
|
---|
45 | #define IPC_DCONNECTSERVICE_CONTRACTID \
|
---|
46 | "@mozilla.org/ipc/dconnect-service;1"
|
---|
47 |
|
---|
48 | #endif // !defined (__WIN__)
|
---|
49 |
|
---|
50 | #include <VBox/com/defs.h>
|
---|
51 | #include <VBox/com/assert.h>
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Strong referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.
|
---|
55 | */
|
---|
56 | template <class C>
|
---|
57 | class ComStrongRef
|
---|
58 | {
|
---|
59 | protected:
|
---|
60 | static void addref (C *p) { p->AddRef(); }
|
---|
61 | static void release (C *p) { p->Release(); }
|
---|
62 | };
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Weak referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.
|
---|
66 | */
|
---|
67 | template <class C>
|
---|
68 | class ComWeakRef
|
---|
69 | {
|
---|
70 | protected:
|
---|
71 | static void addref (C *p) {}
|
---|
72 | static void release (C *p) {}
|
---|
73 | };
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * Base template for smart COM pointers. Not intended to be used directly.
|
---|
77 | */
|
---|
78 | template <class C, template <class> class RefOps = ComStrongRef>
|
---|
79 | class ComPtrBase : protected RefOps <C>
|
---|
80 | {
|
---|
81 | public:
|
---|
82 |
|
---|
83 | // a special template to disable AddRef()/Release()
|
---|
84 | template <class I>
|
---|
85 | class NoAddRefRelease : public I {
|
---|
86 | private:
|
---|
87 | #ifdef __WIN__
|
---|
88 | STDMETHOD_(ULONG, AddRef)() = 0;
|
---|
89 | STDMETHOD_(ULONG, Release)() = 0;
|
---|
90 | #else
|
---|
91 | NS_IMETHOD_(nsrefcnt) AddRef(void) = 0;
|
---|
92 | NS_IMETHOD_(nsrefcnt) Release(void) = 0;
|
---|
93 | #endif
|
---|
94 | };
|
---|
95 |
|
---|
96 | protected:
|
---|
97 |
|
---|
98 | ComPtrBase () : p (NULL) {}
|
---|
99 | ComPtrBase (const ComPtrBase &that) : p (that.p) { addref(); }
|
---|
100 | ComPtrBase (C *that_p) : p (that_p) { addref(); }
|
---|
101 |
|
---|
102 | ~ComPtrBase() { release(); }
|
---|
103 |
|
---|
104 | ComPtrBase &operator= (const ComPtrBase &that) {
|
---|
105 | safe_assign (that.p);
|
---|
106 | return *this;
|
---|
107 | }
|
---|
108 | ComPtrBase &operator= (C *that_p) {
|
---|
109 | safe_assign (that_p);
|
---|
110 | return *this;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public:
|
---|
114 |
|
---|
115 | void setNull() {
|
---|
116 | release();
|
---|
117 | p = NULL;
|
---|
118 | }
|
---|
119 |
|
---|
120 | bool isNull() const {
|
---|
121 | return (p == NULL);
|
---|
122 | }
|
---|
123 | bool operator! () const { return isNull(); }
|
---|
124 |
|
---|
125 | bool operator< (C* that_p) const { return p < that_p; }
|
---|
126 | bool operator== (C* that_p) const { return p == that_p; }
|
---|
127 |
|
---|
128 | template <class I>
|
---|
129 | bool equalsTo (I *i) const {
|
---|
130 | IUnknown *this_unk = NULL, *that_unk = NULL;
|
---|
131 | if (i)
|
---|
132 | i->QueryInterface (COM_IIDOF (IUnknown), (void**) &that_unk);
|
---|
133 | if (p)
|
---|
134 | p->QueryInterface (COM_IIDOF (IUnknown), (void**) &this_unk);
|
---|
135 | bool equal = this_unk == that_unk;
|
---|
136 | if (that_unk)
|
---|
137 | that_unk->Release();
|
---|
138 | if (this_unk)
|
---|
139 | this_unk->Release();
|
---|
140 | return equal;
|
---|
141 | }
|
---|
142 |
|
---|
143 | template <class OC>
|
---|
144 | bool equalsTo (const ComPtrBase <OC> &oc) const {
|
---|
145 | return equalsTo ((OC *) oc);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /** Intended to pass instances as in parameters to interface methods */
|
---|
149 | operator C* () const { return p; }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Derefereces the instance (redirects the -> operator to the managed
|
---|
153 | * pointer).
|
---|
154 | */
|
---|
155 | NoAddRefRelease <C> *operator-> () const {
|
---|
156 | AssertMsg (p, ("Managed pointer must not be null\n"));
|
---|
157 | return (NoAddRefRelease <C> *) p;
|
---|
158 | }
|
---|
159 |
|
---|
160 | template <class I>
|
---|
161 | HRESULT queryInterfaceTo (I **pp) const {
|
---|
162 | if (pp) {
|
---|
163 | if (p) {
|
---|
164 | return p->QueryInterface (COM_IIDOF (I), (void**) pp);
|
---|
165 | } else {
|
---|
166 | *pp = NULL;
|
---|
167 | return S_OK;
|
---|
168 | }
|
---|
169 | } else {
|
---|
170 | return E_INVALIDARG;
|
---|
171 | }
|
---|
172 | }
|
---|
173 |
|
---|
174 | /** Intended to pass instances as out parameters to interface methods */
|
---|
175 | C **asOutParam() {
|
---|
176 | setNull();
|
---|
177 | return &p;
|
---|
178 | }
|
---|
179 |
|
---|
180 | private:
|
---|
181 |
|
---|
182 | void addref() {
|
---|
183 | if (p)
|
---|
184 | RefOps <C>::addref (p);
|
---|
185 | }
|
---|
186 | void release() {
|
---|
187 | if (p)
|
---|
188 | RefOps <C>::release (p);
|
---|
189 | }
|
---|
190 |
|
---|
191 | void safe_assign (C *that_p) {
|
---|
192 | // be aware of self-assignment
|
---|
193 | if (that_p)
|
---|
194 | RefOps <C>::addref (that_p);
|
---|
195 | release();
|
---|
196 | p = that_p;
|
---|
197 | }
|
---|
198 |
|
---|
199 | C *p;
|
---|
200 | };
|
---|
201 |
|
---|
202 | /**
|
---|
203 | * Smart COM pointer wrapper that automatically manages refcounting of
|
---|
204 | * interface pointers.
|
---|
205 | *
|
---|
206 | * @param I COM interface class
|
---|
207 | */
|
---|
208 | template <class I, template <class> class RefOps = ComStrongRef>
|
---|
209 | class ComPtr : public ComPtrBase <I, RefOps>
|
---|
210 | {
|
---|
211 | typedef ComPtrBase <I, RefOps> Base;
|
---|
212 |
|
---|
213 | public:
|
---|
214 |
|
---|
215 | ComPtr () : Base() {}
|
---|
216 | ComPtr (const ComPtr &that) : Base (that) {}
|
---|
217 | ComPtr &operator= (const ComPtr &that) {
|
---|
218 | Base::operator= (that);
|
---|
219 | return *this;
|
---|
220 | }
|
---|
221 |
|
---|
222 | template <class OI>
|
---|
223 | ComPtr (OI *that_p) : Base () { operator= (that_p); }
|
---|
224 | // specialization for I
|
---|
225 | ComPtr (I *that_p) : Base (that_p) {}
|
---|
226 |
|
---|
227 | template <class OC>
|
---|
228 | ComPtr (const ComPtr <OC, RefOps> &oc) : Base () { operator= ((OC *) oc); }
|
---|
229 |
|
---|
230 | template <class OI>
|
---|
231 | ComPtr &operator= (OI *that_p) {
|
---|
232 | if (that_p)
|
---|
233 | that_p->QueryInterface (COM_IIDOF (I), (void **) Base::asOutParam());
|
---|
234 | else
|
---|
235 | Base::setNull();
|
---|
236 | return *this;
|
---|
237 | }
|
---|
238 | // specialization for I
|
---|
239 | ComPtr &operator= (I *that_p) {
|
---|
240 | Base::operator= (that_p);
|
---|
241 | return *this;
|
---|
242 | }
|
---|
243 |
|
---|
244 | template <class OC>
|
---|
245 | ComPtr &operator= (const ComPtr <OC, RefOps> &oc) {
|
---|
246 | return operator= ((OC *) oc);
|
---|
247 | }
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * Createas an in-process object of the given class ID and starts to
|
---|
251 | * manage a reference to the created object in case of success.
|
---|
252 | */
|
---|
253 | HRESULT createInprocObject (const CLSID &clsid) {
|
---|
254 | HRESULT rc;
|
---|
255 | I *obj = NULL;
|
---|
256 | #if defined (__WIN__)
|
---|
257 | rc = CoCreateInstance (clsid, NULL, CLSCTX_INPROC_SERVER, _ATL_IIDOF (I),
|
---|
258 | (void **) &obj);
|
---|
259 | #else
|
---|
260 | nsCOMPtr <nsIComponentManager> manager;
|
---|
261 | rc = NS_GetComponentManager (getter_AddRefs (manager));
|
---|
262 | if (SUCCEEDED (rc))
|
---|
263 | rc = manager->CreateInstance (clsid, nsnull, NS_GET_IID (I),
|
---|
264 | (void **) &obj);
|
---|
265 | #endif
|
---|
266 | *this = obj;
|
---|
267 | if (SUCCEEDED (rc))
|
---|
268 | obj->Release();
|
---|
269 | return rc;
|
---|
270 | }
|
---|
271 |
|
---|
272 | /**
|
---|
273 | * Createas a local (out-of-process) object of the given class ID and starts
|
---|
274 | * to manage a reference to the created object in case of success.
|
---|
275 | *
|
---|
276 | * @param serverName
|
---|
277 | * name of the server to create the object within (Linux only)
|
---|
278 | */
|
---|
279 | HRESULT createLocalObject (const CLSID &clsid, const char *serverName) {
|
---|
280 | HRESULT rc;
|
---|
281 | I *obj = NULL;
|
---|
282 | #if defined (__WIN__)
|
---|
283 | rc = CoCreateInstance (clsid, NULL, CLSCTX_LOCAL_SERVER, _ATL_IIDOF (I),
|
---|
284 | (void **) &obj);
|
---|
285 | #else
|
---|
286 | nsCOMPtr <ipcIService> ipcServ = do_GetService (IPC_SERVICE_CONTRACTID, &rc);
|
---|
287 | if (SUCCEEDED (rc)) {
|
---|
288 | PRUint32 serverID = 0;
|
---|
289 | rc = ipcServ->ResolveClientName (serverName, &serverID);
|
---|
290 | if (SUCCEEDED (rc)) {
|
---|
291 | nsCOMPtr <ipcIDConnectService> dconServ =
|
---|
292 | do_GetService (IPC_DCONNECTSERVICE_CONTRACTID, &rc);
|
---|
293 | if (SUCCEEDED (rc))
|
---|
294 | rc = dconServ->CreateInstance (serverID, clsid, NS_GET_IID (I),
|
---|
295 | (void **) &obj);
|
---|
296 | }
|
---|
297 | }
|
---|
298 | #endif
|
---|
299 | *this = obj;
|
---|
300 | if (SUCCEEDED (rc))
|
---|
301 | obj->Release();
|
---|
302 | return rc;
|
---|
303 | }
|
---|
304 | };
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Specialization of ComPtr<> for IUnknown to guarantee identity
|
---|
308 | * by always doing QueryInterface() when constructing or assigning from
|
---|
309 | * another interface pointer disregarding its type.
|
---|
310 | */
|
---|
311 | template <template <class> class RefOps>
|
---|
312 | class ComPtr <IUnknown, RefOps> : public ComPtrBase <IUnknown, RefOps>
|
---|
313 | {
|
---|
314 | typedef ComPtrBase <IUnknown, RefOps> Base;
|
---|
315 |
|
---|
316 | public:
|
---|
317 |
|
---|
318 | ComPtr () : Base() {}
|
---|
319 | ComPtr (const ComPtr &that) : Base (that) {}
|
---|
320 | ComPtr &operator= (const ComPtr &that) {
|
---|
321 | Base::operator= (that);
|
---|
322 | return *this;
|
---|
323 | }
|
---|
324 |
|
---|
325 | template <class OI>
|
---|
326 | ComPtr (OI *that_p) : Base () { operator= (that_p); }
|
---|
327 |
|
---|
328 | template <class OC>
|
---|
329 | ComPtr (const ComPtr <OC, RefOps> &oc) : Base () { operator= ((OC *) oc); }
|
---|
330 |
|
---|
331 | template <class OI>
|
---|
332 | ComPtr &operator= (OI *that_p) {
|
---|
333 | if (that_p)
|
---|
334 | that_p->QueryInterface (COM_IIDOF (IUnknown), (void **) Base::asOutParam());
|
---|
335 | else
|
---|
336 | Base::setNull();
|
---|
337 | return *this;
|
---|
338 | }
|
---|
339 |
|
---|
340 | template <class OC>
|
---|
341 | ComPtr &operator= (const ComPtr <OC, RefOps> &oc) {
|
---|
342 | return operator= ((OC *) oc);
|
---|
343 | }
|
---|
344 | };
|
---|
345 |
|
---|
346 | /**
|
---|
347 | * Smart COM pointer wrapper that automatically manages refcounting of
|
---|
348 | * pointers to interface implementation classes created on the component's
|
---|
349 | * (i.e. the server's) side. Differs from ComPtr by providing additional
|
---|
350 | * platform independent operations for creating new class instances.
|
---|
351 | *
|
---|
352 | * @param C class that implements some COM interface
|
---|
353 | */
|
---|
354 | template <class C, template <class> class RefOps = ComStrongRef>
|
---|
355 | class ComObjPtr : public ComPtrBase <C, RefOps>
|
---|
356 | {
|
---|
357 | typedef ComPtrBase <C, RefOps> Base;
|
---|
358 |
|
---|
359 | public:
|
---|
360 |
|
---|
361 | ComObjPtr () : Base() {}
|
---|
362 | ComObjPtr (const ComObjPtr &that) : Base (that) {}
|
---|
363 | ComObjPtr (C *that_p) : Base (that_p) {}
|
---|
364 | ComObjPtr &operator= (const ComObjPtr &that) {
|
---|
365 | Base::operator= (that);
|
---|
366 | return *this;
|
---|
367 | }
|
---|
368 | ComObjPtr &operator= (C *that_p) {
|
---|
369 | Base::operator= (that_p);
|
---|
370 | return *this;
|
---|
371 | }
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Creates a new server-side object of the given component class and
|
---|
375 | * immediately starts to manage a pointer to the created object (the
|
---|
376 | * previous pointer, if any, is of course released when appropriate).
|
---|
377 | *
|
---|
378 | * @note This method should be used with care on weakly referenced
|
---|
379 | * smart pointers because it leaves the newly created object completely
|
---|
380 | * unreferenced (i.e., with reference count equal to zero),
|
---|
381 | *
|
---|
382 | * @note Win32: when VBOX_COM_OUTOFPROC_MODULE is defined, the created
|
---|
383 | * object doesn't increase the lock count of the server module, as it
|
---|
384 | * does otherwise.
|
---|
385 | */
|
---|
386 | HRESULT createObject() {
|
---|
387 | HRESULT rc;
|
---|
388 | #if defined (__WIN__)
|
---|
389 | # ifdef VBOX_COM_OUTOFPROC_MODULE
|
---|
390 | CComObjectNoLock <C> *obj = new CComObjectNoLock <C>();
|
---|
391 | if (obj) {
|
---|
392 | obj->InternalFinalConstructAddRef();
|
---|
393 | rc = obj->FinalConstruct();
|
---|
394 | obj->InternalFinalConstructRelease();
|
---|
395 | } else {
|
---|
396 | rc = E_OUTOFMEMORY;
|
---|
397 | }
|
---|
398 | # else
|
---|
399 | CComObject <C> *obj = NULL;
|
---|
400 | rc = CComObject <C>::CreateInstance (&obj);
|
---|
401 | # endif
|
---|
402 | #else
|
---|
403 | CComObject <C> *obj = new CComObject <C>();
|
---|
404 | if (obj) {
|
---|
405 | rc = obj->FinalConstruct();
|
---|
406 | } else {
|
---|
407 | rc = E_OUTOFMEMORY;
|
---|
408 | }
|
---|
409 | #endif
|
---|
410 | *this = obj;
|
---|
411 | return rc;
|
---|
412 | }
|
---|
413 | };
|
---|
414 |
|
---|
415 | #endif // __VBox_com_ptr_h__
|
---|