From cba113ca2826bc4814be2f69a7704c865a37d4ea Mon Sep 17 00:00:00 2001 From: Felix Geyer Date: Fri, 29 Jul 2011 17:55:18 +0200 Subject: Imported Upstream version 4.1.0-dfsg --- include/VBox/com/array.h | 750 ++++++++++++++++++++++++----------------------- 1 file changed, 383 insertions(+), 367 deletions(-) (limited to 'include/VBox/com/array.h') diff --git a/include/VBox/com/array.h b/include/VBox/com/array.h index 5e0e427d3..3e82e13db 100644 --- a/include/VBox/com/array.h +++ b/include/VBox/com/array.h @@ -4,7 +4,7 @@ */ /* - * Copyright (C) 2006-2007 Oracle Corporation + * Copyright (C) 2006-2011 Oracle Corporation * * This file is part of VirtualBox Open Source Edition (OSE), as * available from http://www.virtualbox.org. This file is free software; @@ -55,44 +55,44 @@ * component classes using the following declarations: * @code - STDMETHOD(TestArrays) (ComSafeArrayIn (LONG, aIn), - ComSafeArrayOut (LONG, aOut), - ComSafeArrayOut (LONG, aRet)); + STDMETHOD(TestArrays)(ComSafeArrayIn(LONG, aIn), + ComSafeArrayOut(LONG, aOut), + ComSafeArrayOut(LONG, aRet)); * @endcode * * And the following function bodies: * @code - STDMETHODIMP Component::TestArrays (ComSafeArrayIn (LONG, aIn), - ComSafeArrayOut (LONG, aOut), - ComSafeArrayOut (LONG, aRet)) + STDMETHODIMP Component::TestArrays(ComSafeArrayIn(LONG, aIn), + ComSafeArrayOut(LONG, aOut), + ComSafeArrayOut(LONG, aRet)) { - if (ComSafeArrayInIsNull (aIn)) + if (ComSafeArrayInIsNull(aIn)) return E_INVALIDARG; - if (ComSafeArrayOutIsNull (aOut)) + if (ComSafeArrayOutIsNull(aOut)) return E_POINTER; - if (ComSafeArrayOutIsNull (aRet)) + if (ComSafeArrayOutIsNull(aRet)) return E_POINTER; // Use SafeArray to access the input array parameter - com::SafeArray in (ComSafeArrayInArg (aIn)); + com::SafeArray in(ComSafeArrayInArg(aIn)); for (size_t i = 0; i < in.size(); ++ i) - LogFlow (("*** in[%u]=%d\n", i, in [i])); + LogFlow(("*** in[%u]=%d\n", i, in[i])); // Use SafeArray to create the return array (the same technique is used // for output array parameters) - SafeArray ret (in.size() * 2); + SafeArray ret(in.size() * 2); for (size_t i = 0; i < in.size(); ++ i) { - ret [i] = in [i]; - ret [i + in.size()] = in [i] * 10; + ret[i] = in[i]; + ret[i + in.size()] = in[i] * 10; } - ret.detachTo (ComSafeArrayOutArg (aRet)); + ret.detachTo(ComSafeArrayOutArg(aRet)); return S_OK; } @@ -102,25 +102,25 @@ * Such methods can be called from the client code using the following pattern: * @code - ComPtr component; + ComPtr component; // ... - com::SafeArray in (3); - in [0] = -1; - in [1] = -2; - in [2] = -3; + com::SafeArray in(3); + in[0] = -1; + in[1] = -2; + in[2] = -3; - com::SafeArray out; - com::SafeArray ret; + com::SafeArray out; + com::SafeArray ret; - HRESULT rc = component->TestArrays (ComSafeArrayAsInParam (in), - ComSafeArrayAsOutParam (out), - ComSafeArrayAsOutParam (ret)); + HRESULT rc = component->TestArrays(ComSafeArrayAsInParam(in), + ComSafeArrayAsOutParam(out), + ComSafeArrayAsOutParam(ret)); - if (SUCCEEDED (rc)) + if (SUCCEEDED(rc)) for (size_t i = 0; i < ret.size(); ++ i) - printf ("*** ret[%u]=%d\n", i, ret [i]); + printf("*** ret[%u]=%d\n", i, ret[i]); * @endcode * @@ -129,12 +129,12 @@ * of its contents. This can be used in method implementations like this: * @code - STDMETHODIMP Component::COMGETTER(Values) (ComSafeArrayOut (int, aValues)) + STDMETHODIMP Component::COMGETTER(Values)(ComSafeArrayOut(int, aValues)) { - // ... assume there is a |std::list mValues| data member + // ... assume there is a |std::list mValues| data member - com::SafeArray values (mValues); - values.detachTo (ComSafeArrayOutArg (aValues)); + com::SafeArray values(mValues); + values.detachTo(ComSafeArrayOutArg(aValues)); return S_OK; } @@ -150,7 +150,7 @@ * SafeConstGUIDArray, customized SafeArray<> specializations. * * Also note that in order to pass input BSTR array parameters declared - * using the ComSafeArrayIn (IN_BSTR, aParam) macro to the SafeArray<> + * using the ComSafeArrayIn(IN_BSTR, aParam) macro to the SafeArray<> * constructor using the ComSafeArrayInArg() macro, you should use IN_BSTR * as the SafeArray<> template argument, not just BSTR. * @@ -160,15 +160,16 @@ * ). This implementation functions identically to com::SafeArray. */ -#if defined (VBOX_WITH_XPCOM) +#ifdef VBOX_WITH_XPCOM # include #endif #include "VBox/com/defs.h" #include "VBox/com/ptr.h" #include "VBox/com/assert.h" +#include "iprt/cpp/list.h" -#if defined (VBOX_WITH_XPCOM) +#ifdef VBOX_WITH_XPCOM /** * Wraps the given com::SafeArray instance to generate an expression that is @@ -178,7 +179,7 @@ * @param aArray com::SafeArray instance to pass as an input parameter. */ #define ComSafeArrayAsInParam(aArray) \ - (aArray).size(), (aArray).__asInParam_Arr ((aArray).raw()) + (aArray).size(), (aArray).__asInParam_Arr((aArray).raw()) /** * Wraps the given com::SafeArray instance to generate an expression that is @@ -190,13 +191,13 @@ #define ComSafeArrayAsOutParam(aArray) \ (aArray).__asOutParam_Size(), (aArray).__asOutParam_Arr() -#else /* defined (VBOX_WITH_XPCOM) */ +#else /* !VBOX_WITH_XPCOM */ #define ComSafeArrayAsInParam(aArray) (aArray).__asInParam() #define ComSafeArrayAsOutParam(aArray) (aArray).__asOutParam() -#endif /* defined (VBOX_WITH_XPCOM) */ +#endif /* !VBOX_WITH_XPCOM */ /** * @@ -204,7 +205,7 @@ namespace com { -#if defined (VBOX_WITH_XPCOM) +#ifdef VBOX_WITH_XPCOM //////////////////////////////////////////////////////////////////////////////// @@ -213,19 +214,19 @@ namespace com * * @param T Type of array elements. */ -template +template struct SafeArrayTraits { protected: /** Initializes memory for aElem. */ - static void Init (T &aElem) { aElem = 0; } + static void Init(T &aElem) { aElem = 0; } /** Initializes memory occupied by aElem. */ - static void Uninit (T &aElem) { aElem = 0; } + static void Uninit(T &aElem) { aElem = 0; } /** Creates a deep copy of aFrom and stores it in aTo. */ - static void Copy (const T &aFrom, T &aTo) { aTo = aFrom; } + static void Copy(const T &aFrom, T &aTo) { aTo = aFrom; } public: @@ -234,96 +235,96 @@ public: * reason for this magic is that XPIDL declares input strings * (char/PRUnichar pointers) as const but doesn't do so for pointers to * arrays. */ - static T *__asInParam_Arr (T *aArr) { return aArr; } - static T *__asInParam_Arr (const T *aArr) { return const_cast (aArr); } + static T *__asInParam_Arr(T *aArr) { return aArr; } + static T *__asInParam_Arr(const T *aArr) { return const_cast(aArr); } }; -template -struct SafeArrayTraits +template +struct SafeArrayTraits { // Arbitrary pointers are not supported }; template<> -struct SafeArrayTraits +struct SafeArrayTraits { protected: - static void Init (PRUnichar * &aElem) { aElem = NULL; } + static void Init(PRUnichar * &aElem) { aElem = NULL; } - static void Uninit (PRUnichar * &aElem) + static void Uninit(PRUnichar * &aElem) { if (aElem) { - ::SysFreeString (aElem); + ::SysFreeString(aElem); aElem = NULL; } } - static void Copy (const PRUnichar * aFrom, PRUnichar * &aTo) + static void Copy(const PRUnichar * aFrom, PRUnichar * &aTo) { - AssertCompile (sizeof (PRUnichar) == sizeof (OLECHAR)); - aTo = aFrom ? ::SysAllocString ((const OLECHAR *) aFrom) : NULL; + AssertCompile(sizeof(PRUnichar) == sizeof(OLECHAR)); + aTo = aFrom ? ::SysAllocString((const OLECHAR *)aFrom) : NULL; } public: /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard */ - static const PRUnichar **__asInParam_Arr (PRUnichar **aArr) + static const PRUnichar **__asInParam_Arr(PRUnichar **aArr) { - return const_cast (aArr); + return const_cast(aArr); } - static const PRUnichar **__asInParam_Arr (const PRUnichar **aArr) { return aArr; } + static const PRUnichar **__asInParam_Arr(const PRUnichar **aArr) { return aArr; } }; template<> -struct SafeArrayTraits +struct SafeArrayTraits { protected: - static void Init (const PRUnichar * &aElem) { aElem = NULL; } - static void Uninit (const PRUnichar * &aElem) + static void Init(const PRUnichar * &aElem) { aElem = NULL; } + static void Uninit(const PRUnichar * &aElem) { if (aElem) { - ::SysFreeString (const_cast (aElem)); + ::SysFreeString(const_cast(aElem)); aElem = NULL; } } - static void Copy (const PRUnichar * aFrom, const PRUnichar * &aTo) + static void Copy(const PRUnichar * aFrom, const PRUnichar * &aTo) { - AssertCompile (sizeof (PRUnichar) == sizeof (OLECHAR)); - aTo = aFrom ? ::SysAllocString ((const OLECHAR *) aFrom) : NULL; + AssertCompile(sizeof(PRUnichar) == sizeof(OLECHAR)); + aTo = aFrom ? ::SysAllocString((const OLECHAR *)aFrom) : NULL; } public: /* Magic to workaround strict rules of par. 4.4.4 of the C++ standard */ - static const PRUnichar **__asInParam_Arr (const PRUnichar **aArr) { return aArr; } + static const PRUnichar **__asInParam_Arr(const PRUnichar **aArr) { return aArr; } }; template<> -struct SafeArrayTraits +struct SafeArrayTraits { protected: - static void Init (nsID * &aElem) { aElem = NULL; } + static void Init(nsID * &aElem) { aElem = NULL; } - static void Uninit (nsID * &aElem) + static void Uninit(nsID * &aElem) { if (aElem) { - ::nsMemory::Free (aElem); + ::nsMemory::Free(aElem); aElem = NULL; } } - static void Copy (const nsID * aFrom, nsID * &aTo) + static void Copy(const nsID * aFrom, nsID * &aTo) { if (aFrom) { - aTo = (nsID *) ::nsMemory::Alloc (sizeof (nsID)); + aTo = (nsID *) ::nsMemory::Alloc(sizeof(nsID)); if (aTo) *aTo = *aFrom; } @@ -335,20 +336,20 @@ protected: * no-op Init() and Uninit() which are necessary for SafeArray<> but should * be never called in context of SafeConstGUIDArray. */ - static void Init (const nsID * &aElem) { NOREF (aElem); AssertFailed(); } - static void Uninit (const nsID * &aElem) { NOREF (aElem); AssertFailed(); } + static void Init(const nsID * &aElem) { NOREF(aElem); AssertFailed(); } + static void Uninit(const nsID * &aElem) { NOREF(aElem); AssertFailed(); } public: /** Magic to workaround strict rules of par. 4.4.4 of the C++ standard. */ - static const nsID **__asInParam_Arr (nsID **aArr) + static const nsID **__asInParam_Arr(nsID **aArr) { - return const_cast (aArr); + return const_cast(aArr); } - static const nsID **__asInParam_Arr (const nsID **aArr) { return aArr; } + static const nsID **__asInParam_Arr(const nsID **aArr) { return aArr; } }; -#else /* defined (VBOX_WITH_XPCOM) */ +#else /* !VBOX_WITH_XPCOM */ //////////////////////////////////////////////////////////////////////////////// @@ -356,8 +357,8 @@ struct SafeArrayTraitsBase { protected: - static SAFEARRAY *CreateSafeArray (VARTYPE aVarType, SAFEARRAYBOUND *aBound) - { return SafeArrayCreate (aVarType, 1, aBound); } + static SAFEARRAY *CreateSafeArray(VARTYPE aVarType, SAFEARRAYBOUND *aBound) + { return SafeArrayCreate(aVarType, 1, aBound); } }; /** @@ -372,16 +373,16 @@ protected: // Returns the number of VarType() elements necessary for aSize // elements of T - static ULONG VarCount (size_t aSize); + static ULONG VarCount(size_t aSize); // Returns the number of elements of T that fit into the given number of - // VarType() elements (opposite to VarCount (size_t aSize)). - static size_t Size (ULONG aVarCount); + // VarType() elements (opposite to VarCount(size_t aSize)). + static size_t Size(ULONG aVarCount); // Creates a deep copy of aFrom and stores it in aTo - static void Copy (ULONG aFrom, ULONG &aTo); + static void Copy(ULONG aFrom, ULONG &aTo); */ -template +template struct SafeArrayTraits : public SafeArrayTraitsBase { protected: @@ -393,33 +394,33 @@ protected: static VARTYPE VarType() { - if (sizeof (T) % 8 == 0) return VT_I8; - if (sizeof (T) % 4 == 0) return VT_I4; - if (sizeof (T) % 2 == 0) return VT_I2; + if (sizeof(T) % 8 == 0) return VT_I8; + if (sizeof(T) % 4 == 0) return VT_I4; + if (sizeof(T) % 2 == 0) return VT_I2; return VT_I1; } - static ULONG VarCount (size_t aSize) + static ULONG VarCount(size_t aSize) { - if (sizeof (T) % 8 == 0) return (ULONG) ((sizeof (T) / 8) * aSize); - if (sizeof (T) % 4 == 0) return (ULONG) ((sizeof (T) / 4) * aSize); - if (sizeof (T) % 2 == 0) return (ULONG) ((sizeof (T) / 2) * aSize); - return (ULONG) (sizeof (T) * aSize); + if (sizeof(T) % 8 == 0) return (ULONG)((sizeof(T) / 8) * aSize); + if (sizeof(T) % 4 == 0) return (ULONG)((sizeof(T) / 4) * aSize); + if (sizeof(T) % 2 == 0) return (ULONG)((sizeof(T) / 2) * aSize); + return (ULONG)(sizeof(T) * aSize); } - static size_t Size (ULONG aVarCount) + static size_t Size(ULONG aVarCount) { - if (sizeof (T) % 8 == 0) return (size_t) (aVarCount * 8) / sizeof (T); - if (sizeof (T) % 4 == 0) return (size_t) (aVarCount * 4) / sizeof (T); - if (sizeof (T) % 2 == 0) return (size_t) (aVarCount * 2) / sizeof (T); - return (size_t) aVarCount / sizeof (T); + if (sizeof(T) % 8 == 0) return (size_t)(aVarCount * 8) / sizeof(T); + if (sizeof(T) % 4 == 0) return (size_t)(aVarCount * 4) / sizeof(T); + if (sizeof(T) % 2 == 0) return (size_t)(aVarCount * 2) / sizeof(T); + return (size_t) aVarCount / sizeof(T); } - static void Copy (T aFrom, T &aTo) { aTo = aFrom; } + static void Copy(T aFrom, T &aTo) { aTo = aFrom; } }; -template -struct SafeArrayTraits +template +struct SafeArrayTraits { // Arbitrary pointer types are not supported }; @@ -428,70 +429,70 @@ struct SafeArrayTraits * we specialize it for some of them in order to use the correct VT_ type */ template<> -struct SafeArrayTraits : public SafeArrayTraitsBase +struct SafeArrayTraits : public SafeArrayTraitsBase { protected: static VARTYPE VarType() { return VT_I4; } - static ULONG VarCount (size_t aSize) { return (ULONG) aSize; } - static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; } + static ULONG VarCount(size_t aSize) { return (ULONG)aSize; } + static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; } - static void Copy (LONG aFrom, LONG &aTo) { aTo = aFrom; } + static void Copy(LONG aFrom, LONG &aTo) { aTo = aFrom; } }; template<> -struct SafeArrayTraits : public SafeArrayTraitsBase +struct SafeArrayTraits : public SafeArrayTraitsBase { protected: static VARTYPE VarType() { return VT_UI4; } - static ULONG VarCount (size_t aSize) { return (ULONG) aSize; } - static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; } + static ULONG VarCount(size_t aSize) { return (ULONG)aSize; } + static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; } - static void Copy (ULONG aFrom, ULONG &aTo) { aTo = aFrom; } + static void Copy(ULONG aFrom, ULONG &aTo) { aTo = aFrom; } }; template<> -struct SafeArrayTraits : public SafeArrayTraitsBase +struct SafeArrayTraits : public SafeArrayTraitsBase { protected: static VARTYPE VarType() { return VT_I8; } - static ULONG VarCount (size_t aSize) { return (ULONG) aSize; } - static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; } + static ULONG VarCount(size_t aSize) { return (ULONG)aSize; } + static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; } - static void Copy (LONG64 aFrom, LONG64 &aTo) { aTo = aFrom; } + static void Copy(LONG64 aFrom, LONG64 &aTo) { aTo = aFrom; } }; template<> -struct SafeArrayTraits : public SafeArrayTraitsBase +struct SafeArrayTraits : public SafeArrayTraitsBase { protected: static VARTYPE VarType() { return VT_UI8; } - static ULONG VarCount (size_t aSize) { return (ULONG) aSize; } - static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; } + static ULONG VarCount(size_t aSize) { return (ULONG)aSize; } + static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; } - static void Copy (ULONG64 aFrom, ULONG64 &aTo) { aTo = aFrom; } + static void Copy(ULONG64 aFrom, ULONG64 &aTo) { aTo = aFrom; } }; template<> -struct SafeArrayTraits : public SafeArrayTraitsBase +struct SafeArrayTraits : public SafeArrayTraitsBase { protected: static VARTYPE VarType() { return VT_BSTR; } - static ULONG VarCount (size_t aSize) { return (ULONG) aSize; } - static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; } + static ULONG VarCount(size_t aSize) { return (ULONG)aSize; } + static size_t Size(ULONG aVarCount) { return (size_t)aVarCount; } - static void Copy (BSTR aFrom, BSTR &aTo) + static void Copy(BSTR aFrom, BSTR &aTo) { - aTo = aFrom ? ::SysAllocString ((const OLECHAR *) aFrom) : NULL; + aTo = aFrom ? ::SysAllocString((const OLECHAR *)aFrom) : NULL; } }; template<> -struct SafeArrayTraits : public SafeArrayTraitsBase +struct SafeArrayTraits : public SafeArrayTraitsBase { protected: @@ -499,15 +500,15 @@ protected: static VARTYPE VarType() { return VT_UI8; } /* GUID is 128 bit, so we need two VT_UI8 */ - static ULONG VarCount (size_t aSize) + static ULONG VarCount(size_t aSize) { - AssertCompileSize (GUID, 16); - return (ULONG) (aSize * 2); + AssertCompileSize(GUID, 16); + return (ULONG)(aSize * 2); } - static size_t Size (ULONG aVarCount) { return (size_t) aVarCount / 2; } + static size_t Size(ULONG aVarCount) { return (size_t)aVarCount / 2; } - static void Copy (GUID aFrom, GUID &aTo) { aTo = aFrom; } + static void Copy(GUID aFrom, GUID &aTo) { aTo = aFrom; } }; /** @@ -516,13 +517,13 @@ protected: */ class OutSafeArrayDipper { - OutSafeArrayDipper (SAFEARRAY **aArr, void **aRaw) - : arr (aArr), raw (aRaw) { Assert (*aArr == NULL && *aRaw == NULL); } + OutSafeArrayDipper(SAFEARRAY **aArr, void **aRaw) + : arr(aArr), raw(aRaw) { Assert(*aArr == NULL && *aRaw == NULL); } SAFEARRAY **arr; void **raw; - template friend class SafeArray; + template friend class SafeArray; public: @@ -530,15 +531,15 @@ public: { if (*arr != NULL) { - HRESULT rc = SafeArrayAccessData (*arr, raw); - AssertComRC (rc); + HRESULT rc = SafeArrayAccessData(*arr, raw); + AssertComRC(rc); } } operator SAFEARRAY **() { return arr; } }; -#endif /* defined (VBOX_WITH_XPCOM) */ +#endif /* !VBOX_WITH_XPCOM */ //////////////////////////////////////////////////////////////////////////////// @@ -568,7 +569,7 @@ public: * * @note This class is not thread-safe. */ -template > +template > class SafeArray : public Traits { public: @@ -588,7 +589,7 @@ public: * was not enough memory for creating an array of the requested size. * The constructor will also assert in this case. */ - SafeArray (size_t aSize) { resize (aSize); } + SafeArray(size_t aSize) { resize(aSize); } /** * Weakly attaches this instance to the existing array passed in a method @@ -596,7 +597,7 @@ public: * always wrap the parameter name in the ComSafeArrayInArg macro call like * this: *
-     *  SafeArray safeArray (ComSafeArrayInArg (aArg));
+     *  SafeArray safeArray(ComSafeArrayInArg(aArg));
      * 
* * Note that this constructor doesn't take the ownership of the array. In @@ -605,40 +606,40 @@ public: * * @param aArg Input method parameter to attach to. */ - SafeArray (ComSafeArrayIn (T, aArg)) + SafeArray(ComSafeArrayIn(T, aArg)) { -#if defined (VBOX_WITH_XPCOM) +#ifdef VBOX_WITH_XPCOM - AssertReturnVoid (aArg != NULL); + AssertReturnVoid(aArg != NULL); m.size = aArgSize; m.arr = aArg; m.isWeak = true; -#else /* defined (VBOX_WITH_XPCOM) */ +#else /* !VBOX_WITH_XPCOM */ - AssertReturnVoid (aArg != NULL); - SAFEARRAY *arg = *aArg; + AssertReturnVoid(aArg != NULL); + SAFEARRAY *arg = aArg; if (arg) { - AssertReturnVoid (arg->cDims == 1); + AssertReturnVoid(arg->cDims == 1); VARTYPE vt; - HRESULT rc = SafeArrayGetVartype (arg, &vt); - AssertComRCReturnVoid (rc); - AssertMsgReturnVoid (vt == VarType(), - ("Expected vartype %d, got %d.\n", - VarType(), vt)); - - rc = SafeArrayAccessData (arg, (void HUGEP **) &m.raw); - AssertComRCReturnVoid (rc); + HRESULT rc = SafeArrayGetVartype(arg, &vt); + AssertComRCReturnVoid(rc); + AssertMsgReturnVoid(vt == VarType(), + ("Expected vartype %d, got %d.\n", + VarType(), vt)); + + rc = SafeArrayAccessData(arg, (void HUGEP **)&m.raw); + AssertComRCReturnVoid(rc); } m.arr = arg; m.isWeak = true; -#endif /* defined (VBOX_WITH_XPCOM) */ +#endif /* !VBOX_WITH_XPCOM */ } /** @@ -650,19 +651,19 @@ public: * @param C Standard C++ container template class (normally deduced from * @c aCntr). */ - template