VirtualBox

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

Last change on this file since 30254 was 29931, checked in by vboxsync, 15 years ago

com/ptr.h, com/array.h: Added isNotNull() methods. It is easier to read if (ptrSomething.isNotNull()) than if (!ptrSomething->isNull()) because of the double negation in the latter case. (Easier for people not used to double negation from their native tongue, anyway.)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.2 KB
Line 
1/** @file
2 * MS COM / XPCOM Abstraction Layer:
3 * Smart COM pointer classes declaration
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
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/* Make sure all the stdint.h macros are included - must come first! */
31#ifndef __STDC_LIMIT_MACROS
32# define __STDC_LIMIT_MACROS
33#endif
34#ifndef __STDC_CONSTANT_MACROS
35# define __STDC_CONSTANT_MACROS
36#endif
37
38#if !defined (VBOX_WITH_XPCOM)
39
40#include <atlbase.h>
41
42#ifndef _ATL_IIDOF
43# define _ATL_IIDOF(c) __uuidof(c)
44#endif
45
46#else /* !defined (VBOX_WITH_XPCOM) */
47
48#include <nsXPCOM.h>
49#include <nsIComponentManager.h>
50#include <nsCOMPtr.h>
51#include <ipcIService.h>
52#include <nsIServiceManagerUtils.h>
53#include <ipcCID.h>
54#include <ipcIDConnectService.h>
55
56// official XPCOM headers don't define it yet
57#define IPC_DCONNECTSERVICE_CONTRACTID \
58 "@mozilla.org/ipc/dconnect-service;1"
59
60#endif /* !defined (VBOX_WITH_XPCOM) */
61
62#include <VBox/com/defs.h>
63#include <VBox/com/assert.h>
64
65#define LOGREF(prefix, pObj, cRefs) com::LogRef("%s {%p} cRefs=%d\n", (prefix), (pObj), (cRefs))
66
67namespace com
68{
69 void LogRef(const char *pcszFormat, ...);
70}
71
72/**
73 * Returns @c true if two interface pointers are equal.
74 *
75 * According to the COM Identity Rule, interface pointers are considered to be
76 * equal if and only if IUnknown pointers queried on these interfaces pointers
77 * are equal (e.g. have the same binary value). Equal interface pointers
78 * represent the same object even if they are pointers to different interfaces.
79 *
80 * @param I1 Class of the first interface pointer (must be derived from
81 * IUnknown).
82 * @param I2 Class of the second interface pointer (must be derived from
83 * IUnknown).
84 */
85template <class I1, class I2>
86inline bool ComPtrEquals(I1 *aThis, I2 *aThat)
87{
88 IUnknown *thatUnk = NULL, *thisUnk = NULL;
89 if (aThat)
90 aThat->QueryInterface(COM_IIDOF(IUnknown), (void**)&thatUnk);
91 if (aThis)
92 aThis->QueryInterface(COM_IIDOF(IUnknown), (void**)&thisUnk);
93 bool equal = (thisUnk == thatUnk);
94 if (thisUnk)
95 thisUnk->Release();
96 if (thatUnk)
97 thatUnk->Release();
98 return equal;
99}
100
101/* specialization for <Any, IUnknown> */
102template <class I1>
103inline bool ComPtrEquals(I1 *aThis, IUnknown *aThat)
104{
105 IUnknown *thisUnk = NULL;
106 if (aThis)
107 aThis->QueryInterface(COM_IIDOF(IUnknown), (void**)&thisUnk);
108 bool equal = (thisUnk == aThat);
109 if (thisUnk)
110 thisUnk->Release();
111 return equal;
112}
113
114/** Specialization for <IUnknown, Any> */
115template <class I2>
116inline bool ComPtrEquals(IUnknown *aThis, I2 *aThat)
117{
118 IUnknown *thatUnk = NULL;
119 if (aThat)
120 aThat->QueryInterface(COM_IIDOF(IUnknown), (void**)&thatUnk);
121 bool equal = (aThis == thatUnk);
122 if (thatUnk)
123 thatUnk->Release();
124 return equal;
125}
126
127/* specialization for IUnknown */
128template<>
129inline bool ComPtrEquals<IUnknown, IUnknown>(IUnknown *aThis, IUnknown *aThat)
130{
131 return aThis == aThat;
132}
133
134/**
135 * Base template for smart COM pointers. Not intended to be used directly.
136 */
137template <class C>
138class ComPtrBase
139{
140public:
141
142 /* special template to disable AddRef()/Release() */
143 template <class I>
144 class NoAddRefRelease : public I
145 {
146 private:
147#if !defined (VBOX_WITH_XPCOM)
148 STDMETHOD_(ULONG, AddRef)() = 0;
149 STDMETHOD_(ULONG, Release)() = 0;
150#else /* !defined (VBOX_WITH_XPCOM) */
151 NS_IMETHOD_(nsrefcnt) AddRef(void) = 0;
152 NS_IMETHOD_(nsrefcnt) Release(void) = 0;
153#endif /* !defined (VBOX_WITH_XPCOM) */
154 };
155
156protected:
157
158 ComPtrBase()
159 : p(NULL)
160 {}
161
162 ComPtrBase(const ComPtrBase &that)
163 : p(that.p)
164 {
165 addref();
166 }
167
168 ComPtrBase(C *that_p)
169 : p(that_p)
170 {
171 addref();
172 }
173
174 ~ComPtrBase()
175 {
176 release();
177 }
178
179 ComPtrBase &operator=(const ComPtrBase &that)
180 {
181 safe_assign(that.p);
182 return *this;
183 }
184
185 ComPtrBase &operator=(C *that_p)
186 {
187 safe_assign(that_p);
188 return *this;
189 }
190
191public:
192
193 void setNull()
194 {
195 release();
196 p = NULL;
197 }
198
199 bool isNull() const
200 {
201 return (p == NULL);
202 }
203
204 bool isNotNull() const
205 {
206 return (p != NULL);
207 }
208
209 bool operator!() const { return isNull(); }
210
211 bool operator<(C* that_p) const { return p < that_p; }
212 bool operator==(C* that_p) const { return p == that_p; }
213
214 template <class I>
215 bool equalsTo(I *aThat) const
216 {
217 return ComPtrEquals(p, aThat);
218 }
219
220 template <class OC>
221 bool equalsTo(const ComPtrBase <OC> &oc) const
222 {
223 return equalsTo((OC*)oc);
224 }
225
226 /** Intended to pass instances as in parameters to interface methods */
227 operator C*() const { return p; }
228
229 /**
230 * Dereferences the instance (redirects the -> operator to the managed
231 * pointer).
232 */
233#ifndef IN_SLICKEDIT
234 NoAddRefRelease<C>* operator->() const
235 {
236 AssertMsg(p, ("Managed pointer must not be null\n"));
237 return (NoAddRefRelease<C>*)p;
238 }
239#else /* IN_SLICKEDIT - The editor doesn't quite grok the above magic, sorry about the mess. */
240 C *operator->() const { return this->p; }
241#endif
242
243 template <class I>
244 HRESULT queryInterfaceTo(I **pp) const
245 {
246 if (pp)
247 {
248 if (p)
249 {
250 return p->QueryInterface(COM_IIDOF(I), (void**)pp);
251 }
252 else
253 {
254 *pp = NULL;
255 return S_OK;
256 }
257 }
258
259 return E_INVALIDARG;
260 }
261
262 /** Intended to pass instances as out parameters to interface methods */
263 C **asOutParam()
264 {
265 setNull();
266 return &p;
267 }
268
269private:
270
271 void addref()
272 {
273 if (p)
274 p->AddRef();
275 }
276
277 void release()
278 {
279 if (p)
280 p->Release();
281 }
282
283 void safe_assign (C *that_p)
284 {
285 /* be aware of self-assignment */
286 if (that_p)
287 that_p->AddRef();
288 release();
289 p = that_p;
290 }
291
292 C *p;
293};
294
295/**
296 * Smart COM pointer wrapper that automatically manages refcounting of
297 * interface pointers.
298 *
299 * @param I COM interface class
300 */
301template <class I>
302class ComPtr : public ComPtrBase<I>
303{
304 typedef ComPtrBase<I> Base;
305
306public:
307
308 ComPtr() : Base() {}
309 ComPtr(const ComPtr &that) : Base(that) {}
310 ComPtr& operator=(const ComPtr &that)
311 {
312 Base::operator= (that);
313 return *this;
314 }
315
316 template <class OI>
317 ComPtr(OI *that_p) : Base() { operator=(that_p); }
318
319 /* specialization for I */
320 ComPtr(I *that_p) : Base(that_p) {}
321
322 template <class OC>
323 ComPtr(const ComPtr<OC> &oc) : Base() { operator=((OC*)oc); }
324
325 template <class OI>
326 ComPtr &operator=(OI *that_p)
327 {
328 if (that_p)
329 that_p->QueryInterface(COM_IIDOF(I), (void**)Base::asOutParam());
330 else
331 Base::setNull();
332 return *this;
333 }
334
335 /* specialization for I */
336 ComPtr &operator=(I *that_p)
337 {
338 Base::operator=(that_p);
339 return *this;
340 }
341
342 template <class OC>
343 ComPtr &operator=(const ComPtr<OC> &oc)
344 {
345 return operator=((OC*)oc);
346 }
347
348 /**
349 * Creates an in-process object of the given class ID and starts to
350 * manage a reference to the created object in case of success.
351 */
352 HRESULT createInprocObject (const CLSID &clsid)
353 {
354 HRESULT rc;
355 I *obj = NULL;
356#if !defined (VBOX_WITH_XPCOM)
357 rc = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, _ATL_IIDOF(I),
358 (void**)&obj);
359#else /* !defined (VBOX_WITH_XPCOM) */
360 nsCOMPtr<nsIComponentManager> manager;
361 rc = NS_GetComponentManager(getter_AddRefs(manager));
362 if (SUCCEEDED(rc))
363 rc = manager->CreateInstance(clsid, nsnull, NS_GET_IID(I),
364 (void **) &obj);
365#endif /* !defined (VBOX_WITH_XPCOM) */
366 *this = obj;
367 if (SUCCEEDED(rc))
368 obj->Release();
369 return rc;
370 }
371
372 /**
373 * Creates a local (out-of-process) object of the given class ID and starts
374 * to manage a reference to the created object in case of success.
375 *
376 * Note: In XPCOM, the out-of-process functionality is currently emulated
377 * through in-process wrapper objects (that start a dedicated process and
378 * redirect all object requests to that process). For this reason, this
379 * method is fully equivalent to #createInprocObject() for now.
380 */
381 HRESULT createLocalObject(const CLSID &clsid)
382 {
383#if !defined (VBOX_WITH_XPCOM)
384 HRESULT rc;
385 I *obj = NULL;
386 rc = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, _ATL_IIDOF(I),
387 (void**)&obj);
388 *this = obj;
389 if (SUCCEEDED(rc))
390 obj->Release();
391 return rc;
392#else /* !defined (VBOX_WITH_XPCOM) */
393 return createInprocObject(clsid);
394#endif /* !defined (VBOX_WITH_XPCOM) */
395 }
396
397#ifdef VBOX_WITH_XPCOM
398 /**
399 * Creates an object of the given class ID on the specified server and
400 * starts to manage a reference to the created object in case of success.
401 *
402 * @param serverName Name of the server to create an object within.
403 */
404 HRESULT createObjectOnServer(const CLSID &clsid, const char *serverName)
405 {
406 HRESULT rc;
407 I *obj = NULL;
408 nsCOMPtr<ipcIService> ipcServ = do_GetService(IPC_SERVICE_CONTRACTID, &rc);
409 if (SUCCEEDED(rc))
410 {
411 PRUint32 serverID = 0;
412 rc = ipcServ->ResolveClientName(serverName, &serverID);
413 if (SUCCEEDED (rc))
414 {
415 nsCOMPtr<ipcIDConnectService> dconServ = do_GetService(IPC_DCONNECTSERVICE_CONTRACTID, &rc);
416 if (SUCCEEDED(rc))
417 rc = dconServ->CreateInstance(serverID, clsid, NS_GET_IID(I),
418 (void**)&obj);
419 }
420 }
421 *this = obj;
422 if (SUCCEEDED(rc))
423 obj->Release();
424 return rc;
425 }
426#endif
427};
428
429/**
430 * Specialization of ComPtr<> for IUnknown to guarantee identity
431 * by always doing QueryInterface() when constructing or assigning from
432 * another interface pointer disregarding its type.
433 */
434template<>
435class ComPtr<IUnknown> : public ComPtrBase<IUnknown>
436{
437 typedef ComPtrBase<IUnknown> Base;
438
439public:
440
441 ComPtr() : Base() {}
442 ComPtr(const ComPtr &that) : Base (that) {}
443 ComPtr& operator=(const ComPtr &that)
444 {
445 Base::operator=(that);
446 return *this;
447 }
448
449 template <class OI>
450 ComPtr(OI *that_p) : Base() { operator=(that_p); }
451
452 template <class OC>
453 ComPtr(const ComPtr<OC> &oc) : Base() { operator=((OC*)oc); }
454
455 template <class OI>
456 ComPtr &operator=(OI *that_p)
457 {
458 if (that_p)
459 that_p->QueryInterface(COM_IIDOF(IUnknown), (void**)Base::asOutParam());
460 else
461 Base::setNull();
462 return *this;
463 }
464
465 template <class OC>
466 ComPtr &operator=(const ComPtr<OC> &oc)
467 {
468 return operator=((OC*)oc);
469 }
470};
471
472/**
473 * Smart COM pointer wrapper that automatically manages refcounting of
474 * pointers to interface implementation classes created on the component's
475 * (i.e. the server's) side. Differs from ComPtr by providing additional
476 * platform independent operations for creating new class instances.
477 *
478 * @param C class that implements some COM interface
479 */
480template <class C>
481class ComObjPtr : public ComPtrBase<C>
482{
483 typedef ComPtrBase<C> Base;
484
485public:
486
487 ComObjPtr() : Base() {}
488 ComObjPtr(const ComObjPtr &that) : Base(that) {}
489 ComObjPtr(C *that_p) : Base(that_p) {}
490
491 ComObjPtr& operator=(const ComObjPtr &that)
492 {
493 Base::operator=(that);
494 return *this;
495 }
496
497 ComObjPtr& operator=(C *that_p)
498 {
499 Base::operator=(that_p);
500 return *this;
501 }
502
503#ifdef IN_SLICKEDIT /* Doesn't fully grok the stuff otherwise, sorry for the bloat. */
504 C *operator->() const { return this->p; }
505#endif
506
507 /**
508 * Creates a new server-side object of the given component class and
509 * immediately starts to manage a pointer to the created object (the
510 * previous pointer, if any, is of course released when appropriate).
511 *
512 * @note This method should be used with care on weakly referenced
513 * smart pointers because it leaves the newly created object completely
514 * unreferenced (i.e., with reference count equal to zero),
515 *
516 * @note Win32: when VBOX_COM_OUTOFPROC_MODULE is defined, the created
517 * object doesn't increase the lock count of the server module, as it
518 * does otherwise.
519 */
520 HRESULT createObject()
521 {
522 HRESULT rc;
523#if !defined (VBOX_WITH_XPCOM)
524# ifdef VBOX_COM_OUTOFPROC_MODULE
525 CComObjectNoLock<C> *obj = new CComObjectNoLock<C>();
526 if (obj)
527 {
528 obj->InternalFinalConstructAddRef();
529 rc = obj->FinalConstruct();
530 obj->InternalFinalConstructRelease();
531 }
532 else
533 rc = E_OUTOFMEMORY;
534# else
535 CComObject<C> *obj = NULL;
536 rc = CComObject<C>::CreateInstance(&obj);
537# endif
538#else /* !defined (VBOX_WITH_XPCOM) */
539 CComObject<C> *obj = new CComObject<C>();
540 if (obj)
541 rc = obj->FinalConstruct();
542 else
543 rc = E_OUTOFMEMORY;
544#endif /* !defined (VBOX_WITH_XPCOM) */
545 *this = obj;
546 return rc;
547 }
548};
549#endif
Note: See TracBrowser for help on using the repository browser.

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