VirtualBox

source: vbox/trunk/include/VBox/com/ptr.h@ 6065

Last change on this file since 6065 was 5999, checked in by vboxsync, 17 years ago

The Giant CDDL Dual-License Header Change.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette