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