VirtualBox

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

Last change on this file since 3269 was 2981, checked in by vboxsync, 17 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 KB
Line 
1/** @file
2 *
3 * MS COM / XPCOM Abstraction Layer:
4 * Smart COM pointer classes declaration
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek 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 (VBOX_WITH_XPCOM)
27
28#include <atlbase.h>
29
30#ifndef _ATL_IIDOF
31# define _ATL_IIDOF(c) __uuidof(c)
32#endif
33
34#else /* !defined (VBOX_WITH_XPCOM) */
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 (VBOX_WITH_XPCOM) */
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 */
56template <class C>
57class ComStrongRef
58{
59protected:
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 */
67template <class C>
68class ComWeakRef
69{
70protected:
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 */
78template <class C, template <class> class RefOps = ComStrongRef>
79class ComPtrBase : protected RefOps <C>
80{
81public:
82
83 // a special template to disable AddRef()/Release()
84 template <class I>
85 class NoAddRefRelease : public I {
86 private:
87#if !defined (VBOX_WITH_XPCOM)
88 STDMETHOD_(ULONG, AddRef)() = 0;
89 STDMETHOD_(ULONG, Release)() = 0;
90#else /* !defined (VBOX_WITH_XPCOM) */
91 NS_IMETHOD_(nsrefcnt) AddRef(void) = 0;
92 NS_IMETHOD_(nsrefcnt) Release(void) = 0;
93#endif /* !defined (VBOX_WITH_XPCOM) */
94 };
95
96protected:
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
113public:
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
180private:
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 */
208template <class I, template <class> class RefOps = ComStrongRef>
209class ComPtr : public ComPtrBase <I, RefOps>
210{
211 typedef ComPtrBase <I, RefOps> Base;
212
213public:
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 {
255 HRESULT rc;
256 I *obj = NULL;
257#if !defined (VBOX_WITH_XPCOM)
258 rc = CoCreateInstance (clsid, NULL, CLSCTX_INPROC_SERVER, _ATL_IIDOF (I),
259 (void **) &obj);
260#else /* !defined (VBOX_WITH_XPCOM) */
261 nsCOMPtr <nsIComponentManager> manager;
262 rc = NS_GetComponentManager (getter_AddRefs (manager));
263 if (SUCCEEDED (rc))
264 rc = manager->CreateInstance (clsid, nsnull, NS_GET_IID (I),
265 (void **) &obj);
266#endif /* !defined (VBOX_WITH_XPCOM) */
267 *this = obj;
268 if (SUCCEEDED (rc))
269 obj->Release();
270 return rc;
271 }
272
273 /**
274 * Createas a local (out-of-process) object of the given class ID and starts
275 * to manage a reference to the created object in case of success.
276 *
277 * Note: In XPCOM, the out-of-process functionality is currently emulated
278 * through in-process wrapper objects (that start a dedicated process and
279 * redirect all object requests to that process). For this reason, this
280 * method is fully equivalent to #createInprocObject() for now.
281 */
282 HRESULT createLocalObject (const CLSID &clsid)
283 {
284#if !defined (VBOX_WITH_XPCOM)
285 HRESULT rc;
286 I *obj = NULL;
287 rc = CoCreateInstance (clsid, NULL, CLSCTX_LOCAL_SERVER, _ATL_IIDOF (I),
288 (void **) &obj);
289 *this = obj;
290 if (SUCCEEDED (rc))
291 obj->Release();
292 return rc;
293#else /* !defined (VBOX_WITH_XPCOM) */
294 return createInprocObject (clsid);
295#endif /* !defined (VBOX_WITH_XPCOM) */
296 }
297
298#ifdef VBOX_WITH_XPCOM
299 /**
300 * Createas an object of the given class ID on the specified server and
301 * starts to manage a reference to the created object in case of success.
302 *
303 * @param serverName Name of the server to create an object within.
304 */
305 HRESULT createObjectOnServer (const CLSID &clsid, const char *serverName)
306 {
307 HRESULT rc;
308 I *obj = NULL;
309 nsCOMPtr <ipcIService> ipcServ = do_GetService (IPC_SERVICE_CONTRACTID, &rc);
310 if (SUCCEEDED (rc))
311 {
312 PRUint32 serverID = 0;
313 rc = ipcServ->ResolveClientName (serverName, &serverID);
314 if (SUCCEEDED (rc)) {
315 nsCOMPtr <ipcIDConnectService> dconServ =
316 do_GetService (IPC_DCONNECTSERVICE_CONTRACTID, &rc);
317 if (SUCCEEDED (rc))
318 rc = dconServ->CreateInstance (serverID, clsid, NS_GET_IID (I),
319 (void **) &obj);
320 }
321 }
322 *this = obj;
323 if (SUCCEEDED (rc))
324 obj->Release();
325 return rc;
326 }
327#endif
328};
329
330/**
331 * Specialization of ComPtr<> for IUnknown to guarantee identity
332 * by always doing QueryInterface() when constructing or assigning from
333 * another interface pointer disregarding its type.
334 */
335template <template <class> class RefOps>
336class ComPtr <IUnknown, RefOps> : public ComPtrBase <IUnknown, RefOps>
337{
338 typedef ComPtrBase <IUnknown, RefOps> Base;
339
340public:
341
342 ComPtr () : Base() {}
343 ComPtr (const ComPtr &that) : Base (that) {}
344 ComPtr &operator= (const ComPtr &that) {
345 Base::operator= (that);
346 return *this;
347 }
348
349 template <class OI>
350 ComPtr (OI *that_p) : Base () { operator= (that_p); }
351
352 template <class OC>
353 ComPtr (const ComPtr <OC, RefOps> &oc) : Base () { operator= ((OC *) oc); }
354
355 template <class OI>
356 ComPtr &operator= (OI *that_p) {
357 if (that_p)
358 that_p->QueryInterface (COM_IIDOF (IUnknown), (void **) Base::asOutParam());
359 else
360 Base::setNull();
361 return *this;
362 }
363
364 template <class OC>
365 ComPtr &operator= (const ComPtr <OC, RefOps> &oc) {
366 return operator= ((OC *) oc);
367 }
368};
369
370/**
371 * Smart COM pointer wrapper that automatically manages refcounting of
372 * pointers to interface implementation classes created on the component's
373 * (i.e. the server's) side. Differs from ComPtr by providing additional
374 * platform independent operations for creating new class instances.
375 *
376 * @param C class that implements some COM interface
377 */
378template <class C, template <class> class RefOps = ComStrongRef>
379class ComObjPtr : public ComPtrBase <C, RefOps>
380{
381 typedef ComPtrBase <C, RefOps> Base;
382
383public:
384
385 ComObjPtr () : Base() {}
386 ComObjPtr (const ComObjPtr &that) : Base (that) {}
387 ComObjPtr (C *that_p) : Base (that_p) {}
388 ComObjPtr &operator= (const ComObjPtr &that) {
389 Base::operator= (that);
390 return *this;
391 }
392 ComObjPtr &operator= (C *that_p) {
393 Base::operator= (that_p);
394 return *this;
395 }
396
397 /**
398 * Creates a new server-side object of the given component class and
399 * immediately starts to manage a pointer to the created object (the
400 * previous pointer, if any, is of course released when appropriate).
401 *
402 * @note This method should be used with care on weakly referenced
403 * smart pointers because it leaves the newly created object completely
404 * unreferenced (i.e., with reference count equal to zero),
405 *
406 * @note Win32: when VBOX_COM_OUTOFPROC_MODULE is defined, the created
407 * object doesn't increase the lock count of the server module, as it
408 * does otherwise.
409 */
410 HRESULT createObject() {
411 HRESULT rc;
412#if !defined (VBOX_WITH_XPCOM)
413# ifdef VBOX_COM_OUTOFPROC_MODULE
414 CComObjectNoLock <C> *obj = new CComObjectNoLock <C>();
415 if (obj) {
416 obj->InternalFinalConstructAddRef();
417 rc = obj->FinalConstruct();
418 obj->InternalFinalConstructRelease();
419 } else {
420 rc = E_OUTOFMEMORY;
421 }
422# else
423 CComObject <C> *obj = NULL;
424 rc = CComObject <C>::CreateInstance (&obj);
425# endif
426#else /* !defined (VBOX_WITH_XPCOM) */
427 CComObject <C> *obj = new CComObject <C>();
428 if (obj) {
429 rc = obj->FinalConstruct();
430 } else {
431 rc = E_OUTOFMEMORY;
432 }
433#endif /* !defined (VBOX_WITH_XPCOM) */
434 *this = obj;
435 return rc;
436 }
437};
438
439#endif // __VBox_com_ptr_h__
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