ptr.h revision 4308046ac332e114e99a20a2f1255f5cf0ef315d
/** @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 (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.
*/
#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) {}
};
/**
* Equality operations for the ComPtrBase template.
*/
{
{
if (aThat)
if (aThis)
if (thisUnk)
if (thatUnk)
return equal;
}
/* specialization for IUnknown */
template<>
{
if (aThis)
if (thisUnk)
return equal;
}
};
/** Specialization for IUnknown */
template<>
{
{
if (aThat)
if (thatUnk)
return equal;
}
/* specialization for IUnknown */
template<>
{
}
};
/**
* Base template for smart COM pointers. Not intended to be used directly.
*/
{
/* 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);
}
{
}
{
}
/** 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;
}
}
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.
*/
{
#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