ptr.h revision c98fb3e16fcd571a790eab772c0c66173d225205
/** @file
* MS COM / XPCOM Abstraction Layer:
* Smart COM pointer classes declaration
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License 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.
*/
#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 \
#endif /* !defined (VBOX_WITH_XPCOM) */
/**
* Strong referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.
*/
{
};
/**
* Weak referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.
*/
{
static void addref (C *p) {}
static void release (C *p) {}
};
/**
* Base template for smart COM pointers. Not intended to be used directly.
*/
{
// a special template to disable AddRef()/Release()
#if !defined (VBOX_WITH_XPCOM)
#else /* !defined (VBOX_WITH_XPCOM) */
#endif /* !defined (VBOX_WITH_XPCOM) */
};
ComPtrBase () : p (NULL) {}
~ComPtrBase() { release(); }
safe_assign (that.p);
return *this;
}
return *this;
}
void setNull() {
release();
p = NULL;
}
bool isNull() const {
return (p == NULL);
}
bool equalsTo (I *i) const {
if (i)
if (p)
if (that_unk)
if (this_unk)
return equal;
}
}
/** 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;
}
if (pp) {
if (p) {
} else {
return S_OK;
}
} else {
return E_INVALIDARG;
}
}
/** Intended to pass instances as out parameters to interface methods */
C **asOutParam() {
setNull();
return &p;
}
void addref() {
if (p)
}
void release() {
if (p)
}
void safe_assign (C *that_p) {
// be aware of self-assignment
if (that_p)
release();
p = that_p;
}
C *p;
};
/**
* Smart COM pointer wrapper that automatically manages refcounting of
* interface pointers.
*
* @param I COM interface class
*/
{
return *this;
}
// specialization for I
if (that_p)
else
return *this;
}
// specialization for I
return *this;
}
}
/**
* Createas an in-process object of the given class ID and starts to
* manage a reference to the created object in case of success.
*/
{
#if !defined (VBOX_WITH_XPCOM)
(void **) &obj);
#else /* !defined (VBOX_WITH_XPCOM) */
(void **) &obj);
#endif /* !defined (VBOX_WITH_XPCOM) */
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.
*/
{
#if !defined (VBOX_WITH_XPCOM)
(void **) &obj);
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.
*/
{
{
(void **) &obj);
}
}
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.
*/
{
return *this;
}
if (that_p)
else
return *this;
}
}
};
/**
* 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
*/
{
return *this;
}
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() {
#if !defined (VBOX_WITH_XPCOM)
# ifdef VBOX_COM_OUTOFPROC_MODULE
if (obj) {
} else {
rc = E_OUTOFMEMORY;
}
# else
# endif
#else /* !defined (VBOX_WITH_XPCOM) */
if (obj) {
} else {
rc = E_OUTOFMEMORY;
}
#endif /* !defined (VBOX_WITH_XPCOM) */
return rc;
}
};
#endif