ptr.h revision ca847843b2752c41c31589bfc878629cc7db7040
/** @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;
* 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
/* Make sure all the stdint.h macros are included - must come first! */
#ifndef __STDC_LIMIT_MACROS
# define __STDC_LIMIT_MACROS
#endif
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif
#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) */
{
void LogRef(const char *pcszFormat, ...);
}
/**
* Strong referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.
*/
{
static void addref(C *p)
{
}
static void release(C *p)
{
p->Release();
}
};
/**
* Weak referencing operators. Used as a second argument to ComPtr<>/ComObjPtr<>.
*/
{
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).
*/
{
if (aThat)
if (aThis)
if (thisUnk)
if (thatUnk)
return equal;
}
/* specialization for <Any, IUnknown> */
{
if (aThis)
if (thisUnk)
return equal;
}
/** Specialization for <IUnknown, Any> */
{
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);
}
{
return ComPtrEquals (p, aThat);
}
{
}
/** Intended to pass instances as in parameters to interface methods */
operator C* () const { return p; }
/**
* Dereferences 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;
}
{
}
/**
* Creates 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;
}
/**
* Creates 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
/**
* Creates 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