VirtualBox

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

Last change on this file since 5605 was 4071, checked in by vboxsync, 17 years ago

Biggest check-in ever. New source code headers for all (C) innotek files.

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