::const_iterator it = aCntr.begin();
it != aCntr.end(); ++ it, ++ i)
#if defined (VBOX_WITH_XPCOM)
Copy (*it, m.arr [i]);
#else
Copy (*it, m.raw [i]);
#endif
}
/**
* Destroys this instance after calling #setNull() to release allocated
* resources. See #setNull() for more details.
*/
virtual ~SafeArray() { setNull(); }
/**
* Returns @c true if this instance represents a null array.
*/
bool isNull() const { return m.arr == NULL; }
/**
* Resets this instance to null and, if this instance is not a weak one,
* releases any resources occupied by the array data.
*
* @note This method destroys (cleans up) all elements of the array using
* the corresponding cleanup routine for the element type before the
* array itself is destroyed.
*/
virtual void setNull() { m.uninit(); }
/**
* Returns @c true if this instance is weak. A weak instance doesn't own the
* array data and therefore operations manipulating the ownership (e.g.
* #detachTo()) are forbidden and will assert.
*/
bool isWeak() const { return m.isWeak; }
/** Number of elements in the array. */
size_t size() const
{
#if defined (VBOX_WITH_XPCOM)
if (m.arr)
return m.size;
return 0;
#else
if (m.arr)
return Size (m.arr->rgsabound [0].cElements);
return 0;
#endif
}
/**
* Resizes the array preserving its contents when possible. If the new size
* is bigger than the old size, new elements are initialized with null
* values. If the new size is smaller than the old size, the contents of the
* array above the new size is lost.
*
* @param aNewSize New number of elements in the array.
* @return @c true on success and false if there is not enough
* memory for resizing.
*/
virtual bool resize (size_t aNewSize)
{
/// @todo Implement me!
NOREF (aNewSize);
AssertFailedReturn (false);
}
/**
* Reinitializes this instance by preallocating space for the given number
* of elements. The previous array contents is lost.
*
* @param aNewSize New number of elements in the array.
* @return @c true on success and false if there is not enough
* memory for resizing.
*/
virtual bool reset (size_t aNewSize)
{
m.uninit();
#if defined (VBOX_WITH_XPCOM)
/* Note: for zero-sized arrays, we use the size of 1 because whether
* malloc(0) returns a null pointer or not (which is used in isNull())
* is implementation-dependent according to the C standard. */
m.arr = (T *) nsMemory::Alloc (RT_MAX (aNewSize, 1) * sizeof (T));
AssertReturn (m.arr != NULL, false);
m.size = aNewSize;
for (size_t i = 0; i < m.size; ++ i)
Init (m.arr [i]);
#else
SAFEARRAYBOUND bound = { VarCount (aNewSize), 0 };
m.arr = SafeArrayCreate (VarType(), 1, &bound);
AssertReturn (m.arr != NULL, false);
AssertReturn (accessRaw() != NULL, false);
#endif
return true;
}
/**
* Returns a pointer to the raw array data. Use this raw pointer with care
* as no type or bound checking is done for you in this case.
*
* @note This method returns @c NULL when this instance is null.
* @see #operator[]
*/
T *raw()
{
#if defined (VBOX_WITH_XPCOM)
return m.arr;
#else
return accessRaw();
#endif
}
/**
* Const version of #raw().
*/
const T *raw() const
{
#if defined (VBOX_WITH_XPCOM)
return m.arr;
#else
return accessRaw();
#endif
}
/**
* Array access operator that returns an array element by reference. A bit
* safer than #raw(): asserts and returns an invalid reference if this
* instance is null or if the index is out of bounds.
*
* @note For weak instances, this call will succeed but the behavior of
* changing the contents of an element of the weak array instance is
* undefined and may lead to a program crash on some platforms.
*/
T &operator[] (size_t aIdx)
{
AssertReturn (m.arr != NULL, *((T *) NULL));
AssertReturn (aIdx < size(), *((T *) NULL));
#if defined (VBOX_WITH_XPCOM)
return m.arr [aIdx];
#else
AssertReturn (accessRaw() != NULL, *((T *) NULL));
return m.raw [aIdx];
#endif
}
/**
* Const version of #operator[] that returns an array element by value.
*/
const T operator[] (size_t aIdx) const
{
AssertReturn (m.arr != NULL, *((T *) NULL));
AssertReturn (aIdx < size(), *((T *) NULL));
#if defined (VBOX_WITH_XPCOM)
return m.arr [aIdx];
#else
AssertReturn (unconst (this)->accessRaw() != NULL, *((T *) NULL));
return m.raw [aIdx];
#endif
}
/**
* Creates a copy of this array and stores it in a method parameter declared
* using the ComSafeArrayOut macro. When using this call, always wrap the
* parameter name in the ComSafeArrayOutArg macro call like this:
*
* safeArray.cloneTo (ComSafeArrayOutArg (aArg));
*
*
* @note It is assumed that the ownership of the returned copy is
* transferred to the caller of the method and he is responsible to free the
* array data when it is no more necessary.
*
* @param aArg Output method parameter to clone to.
*/
virtual const SafeArray &cloneTo (ComSafeArrayOut (T, aArg)) const
{
/// @todo Implement me!
#if defined (VBOX_WITH_XPCOM)
NOREF (aArgSize);
NOREF (aArg);
#else
NOREF (aArg);
#endif
AssertFailedReturn (*this);
}
/**
* Transfers the ownership of this array's data to the specified location
* declared using the ComSafeArrayOut macro and makes this array a null
* array. When using this call, always wrap the parameter name in the
* ComSafeArrayOutArg macro call like this:
*
* safeArray.detachTo (ComSafeArrayOutArg (aArg));
*
*
* Detaching the null array is also possible in which case the location will
* receive NULL.
*
* @note Since the ownership of the array data is transferred to the
* caller of the method, he is responsible to free the array data when it is
* no more necessary.
*
* @param aArg Location to detach to.
*/
virtual SafeArray &detachTo (ComSafeArrayOut (T, aArg))
{
AssertReturn (m.isWeak == false, *this);
#if defined (VBOX_WITH_XPCOM)
AssertReturn (aArgSize != NULL, *this);
AssertReturn (aArg != NULL, *this);
*aArgSize = m.size;
*aArg = m.arr;
m.isWeak = false;
m.size = 0;
m.arr = NULL;
#else /* defined (VBOX_WITH_XPCOM) */
AssertReturn (aArg != NULL, *this);
*aArg = m.arr;
if (m.raw)
{
HRESULT rc = SafeArrayUnaccessData (m.arr);
AssertComRCReturn (rc, *this);
m.raw = NULL;
}
m.isWeak = false;
m.arr = NULL;
#endif /* defined (VBOX_WITH_XPCOM) */
return *this;
}
// public methods for internal purposes only
#if defined (VBOX_WITH_XPCOM)
/** Internal function. Never call it directly. */
PRUint32 *__asOutParam_Size() { setNull(); return &m.size; }
/** Internal function Never call it directly. */
T **__asOutParam_Arr() { Assert (isNull()); return &m.arr; }
#else /* defined (VBOX_WITH_XPCOM) */
/** Internal function Never call it directly. */
SAFEARRAY ** __asInParam() { return &m.arr; }
/** Internal function Never call it directly. */
SAFEARRAY ** __asOutParam() { setNull(); return &m.arr; }
#endif /* defined (VBOX_WITH_XPCOM) */
static const SafeArray Null;
protected:
DECLARE_CLS_COPY_CTOR_ASSIGN_NOOP(SafeArray)
#if defined (VBOX_WITH_XPCOM)
#else /* defined (VBOX_WITH_XPCOM) */
/** Requests access to the raw data pointer. */
T *accessRaw()
{
if (m.arr && m.raw == NULL)
{
HRESULT rc = SafeArrayAccessData (m.arr, (void HUGEP **) &m.raw);
AssertComRCReturn (rc, NULL);
}
return m.raw;
}
#endif /* defined (VBOX_WITH_XPCOM) */
struct Data
{
Data()
: isWeak (false)
#if defined (VBOX_WITH_XPCOM)
, size (0), arr (NULL)
#else
, arr (NULL), raw (NULL)
#endif
{}
~Data() { uninit(); }
void uninit()
{
#if defined (VBOX_WITH_XPCOM)
if (arr)
{
if (!isWeak)
{
for (size_t i = 0; i < size; ++ i)
Uninit (arr [i]);
nsMemory::Free ((void *) arr);
isWeak = false;
}
arr = NULL;
}
#else /* defined (VBOX_WITH_XPCOM) */
if (arr)
{
if (raw)
{
SafeArrayUnaccessData (arr);
raw = NULL;
}
if (!isWeak)
{
HRESULT rc = SafeArrayDestroy (arr);
AssertComRCReturnVoid (rc);
isWeak = false;
}
arr = NULL;
}
#endif /* defined (VBOX_WITH_XPCOM) */
}
bool isWeak : 1;
#if defined (VBOX_WITH_XPCOM)
PRUint32 size;
T *arr;
#else
SAFEARRAY *arr;
T *raw;
#endif
};
Data m;
};
////////////////////////////////////////////////////////////////////////////////
#if defined (VBOX_WITH_XPCOM)
/**
* Version of com::SafeArray for arrays of GUID.
*
* In MS COM, GUID arrays store GUIDs by value and therefore input arrays are
* represented using |GUID *| and out arrays -- using |GUID **|. In XPCOM,
* GUID arrays store pointers to nsID so that input arrays are |const nsID **|
* and out arrays are |nsID ***|. Due to this difference, it is impossible to
* work with arrays of GUID on both platforms by simply using com::SafeArray
* . This class is intended to provide some level of cross-platform
* behavior.
*
* The basic usage pattern is basically similar to com::SafeArray<> except that
* you use ComSafeGUIDArrayIn* and ComSafeGUIDArrayOut* macros instead of
* ComSafeArrayIn* and ComSafeArrayOut*. Another important nuance is that the
* raw() array type is different (nsID **, or GUID ** on XPCOM and GUID * on MS
* COM) so it is recommended to use operator[] instead that always returns a
* GUID by value.
*
* Note that due to const modifiers, you cannot use SafeGUIDArray for input GUID
* arrays. Please use SafeConstGUIDArray for this instead.
*
* Other than mentioned above, the functionality of this class is equivalent to
* com::SafeArray<>. See the description of that template and its methods for
* more information.
*
* Output GUID arrays are handled by a separate class, SafeGUIDArrayOut, since
* this class cannot handle them because of const modifiers.
*/
class SafeGUIDArray : public SafeArray
{
public:
typedef SafeArray Base;
class nsIDRef
{
public:
nsIDRef (nsID * &aVal) : mVal (aVal) {}
operator const nsID &() const { return mVal ? *mVal : *Empty; }
operator nsID() const { return mVal ? *mVal : *Empty; }
const nsID *operator&() const { return mVal ? mVal : Empty; }
nsIDRef &operator= (const nsID &aThat)
{
if (mVal == NULL)
Copy (&aThat, mVal);
else
*mVal = aThat;
return *this;
}
private:
nsID * &mVal;
static const nsID *Empty;
friend class SafeGUIDArray;
};
/** See SafeArray<>::SafeArray(). */
SafeGUIDArray() {}
/** See SafeArray<>::SafeArray (size_t). */
SafeGUIDArray (size_t aSize) : Base (aSize) {}
/**
* Array access operator that returns an array element by reference. As a
* special case, the return value of this operator on XPCOM is a nsID (GUID)
* reference, instead of a nsID pointer (the actual SafeArray template
* argument), for compatibility with the MS COM version.
*
* The rest is equivalent to SafeArray<>::operator[].
*/
nsIDRef operator[] (size_t aIdx)
{
Assert (m.arr != NULL);
Assert (aIdx < size());
return nsIDRef (m.arr [aIdx]);
}
/**
* Const version of #operator[] that returns an array element by value.
*/
const nsID &operator[] (size_t aIdx) const
{
Assert (m.arr != NULL);
Assert (aIdx < size());
return m.arr [aIdx] ? *m.arr [aIdx] : *nsIDRef::Empty;
}
};
/**
* Version of com::SafeArray for const arrays of GUID.
*
* This class is used to work with input GUID array parameters in method
* implementations. See SafeGUIDArray for more details.
*/
class SafeConstGUIDArray : public SafeArray >
{
public:
typedef SafeArray > Base;
/** See SafeArray<>::SafeArray(). */
SafeConstGUIDArray() {}
/* See SafeArray<>::SafeArray (ComSafeArrayIn (T, aArg)). */
SafeConstGUIDArray (ComSafeGUIDArrayIn (aArg))
: Base (ComSafeGUIDArrayInArg (aArg)) {}
/**
* Array access operator that returns an array element by reference. As a
* special case, the return value of this operator on XPCOM is nsID (GUID)
* instead of nsID *, for compatibility with the MS COM version.
*
* The rest is equivalent to SafeArray<>::operator[].
*/
const nsID &operator[] (size_t aIdx) const
{
AssertReturn (m.arr != NULL, **((const nsID * *) NULL));
AssertReturn (aIdx < size(), **((const nsID * *) NULL));
return *m.arr [aIdx];
}
private:
/* These are disabled because of const */
bool reset (size_t aNewSize) { NOREF (aNewSize); return false; }
};
#else /* defined (VBOX_WITH_XPCOM) */
typedef SafeArray SafeGUIDArray;
typedef SafeArray > SafeConstGUIDArray;
#endif /* defined (VBOX_WITH_XPCOM) */
////////////////////////////////////////////////////////////////////////////////
#if defined (VBOX_WITH_XPCOM)
template
struct SafeIfaceArrayTraits
{
protected:
static void Init (I * &aElem) { aElem = NULL; }
static void Uninit (I * &aElem)
{
if (aElem)
{
aElem->Release();
aElem = NULL;
}
}
static void Copy (I * aFrom, I * &aTo)
{
if (aFrom != NULL)
{
aTo = aFrom;
aTo->AddRef();
}
else
aTo = NULL;
}
public:
/* Magic to workaround strict rules of par. 4.4.4 of the C++ standard. */
static I **__asInParam_Arr (I **aArr) { return aArr; }
static I **__asInParam_Arr (const I **aArr) { return const_cast (aArr); }
};
#else /* defined (VBOX_WITH_XPCOM) */
template
struct SafeIfaceArrayTraits
{
protected:
static VARTYPE VarType() { return VT_UNKNOWN; }
static ULONG VarCount (size_t aSize) { return (ULONG) aSize; }
static size_t Size (ULONG aVarCount) { return (size_t) aVarCount; }
static void Copy (I * aFrom, I * &aTo)
{
if (aFrom != NULL)
{
aTo = aFrom;
aTo->AddRef();
}
else
aTo = NULL;
}
};
#endif /* defined (VBOX_WITH_XPCOM) */
////////////////////////////////////////////////////////////////////////////////
/**
* Version of com::SafeArray for arrays of interface pointers.
*
* Except that it manages arrays of interface pointers, the usage of this class
* is identical to com::SafeArray.
*
* @param I Interface class (no asterisk).
*/
template
class SafeIfaceArray : public SafeArray >
{
public:
typedef SafeArray > Base;
/**
* Creates a null array.
*/
SafeIfaceArray() {}
/**
* Creates a new array of the given size. All elements of the newly created
* array initialized with null values.
*
* @param aSize Initial number of elements in the array. Must be greater
* than 0.
*
* @note If this object remains null after construction it means that there
* was not enough memory for creating an array of the requested size.
* The constructor will also assert in this case.
*/
SafeIfaceArray (size_t aSize) { reset (aSize); }
/**
* Weakly attaches this instance to the existing array passed in a method
* parameter declared using the ComSafeArrayIn macro. When using this call,
* always wrap the parameter name in the ComSafeArrayOutArg macro call like
* this:
*
* SafeArray safeArray (ComSafeArrayInArg (aArg));
*
*
* Note that this constructor doesn't take the ownership of the array. In
* particular, it means that operations that operate on the ownership (e.g.
* #detachTo()) are forbidden and will assert.
*
* @param aArg Input method parameter to attach to.
*/
SafeIfaceArray (ComSafeArrayIn (I *, aArg))
{
#if defined (VBOX_WITH_XPCOM)
AssertReturnVoid (aArg != NULL);
Base::m.size = aArgSize;
Base::m.arr = aArg;
Base::m.isWeak = true;
#else /* defined (VBOX_WITH_XPCOM) */
AssertReturnVoid (aArg != NULL);
SAFEARRAY *arg = *aArg;
if (arg)
{
AssertReturnVoid (arg->cDims == 1);
VARTYPE vt;
HRESULT rc = SafeArrayGetVartype (arg, &vt);
AssertComRCReturnVoid (rc);
AssertMsgReturnVoid (vt == VT_UNKNOWN,
("Expected vartype VT_UNKNOWN, got %d.\n",
VarType(), vt));
GUID guid;
rc = SafeArrayGetIID (arg, &guid);
AssertComRCReturnVoid (rc);
AssertMsgReturnVoid (InlineIsEqualGUID (_ATL_IIDOF (I), guid),
("Expected IID {%Vuuid}, got {%Vuuid}.\n",
&_ATL_IIDOF (I), &guid));
}
m.arr = arg;
m.isWeak = true;
AssertReturnVoid (accessRaw() != NULL);
#endif /* defined (VBOX_WITH_XPCOM) */
}
/**
* Creates a deep copy of the given standard C++ container that stores
* interface pointers as objects of the ComPtr class.
*
* @param aCntr Container object to copy.
*
* @param C Standard C++ container template class (normally deduced from
* @c aCntr).
* @param A Standard C++ allocator class (deduced from @c aCntr).
* @param OI Argument to the ComPtr template (deduced from @c aCntr).
*/
template class C, class A, class OI>
SafeIfaceArray (const C , A> & aCntr)
{
typedef C , A> List;
reset (aCntr.size());
AssertReturnVoid (!Base::isNull());
int i = 0;
for (typename List::const_iterator it = aCntr.begin();
it != aCntr.end(); ++ it, ++ i)
#if defined (VBOX_WITH_XPCOM)
Copy (*it, Base::m.arr [i]);
#else
Copy (*it, Base::m.raw [i]);
#endif
}
/**
* Creates a deep copy of the given standard C++ container that stores
* interface pointers as objects of the ComObjPtr class.
*
* @param aCntr Container object to copy.
*
* @param C Standard C++ container template class (normally deduced from
* @c aCntr).
* @param A Standard C++ allocator class (deduced from @c aCntr).
* @param OI Argument to the ComObjPtr template (deduced from @c aCntr).
*/
template class C, class A, class OI>
SafeIfaceArray (const C , A> & aCntr)
{
typedef C , A> List;
reset (aCntr.size());
AssertReturnVoid (!Base::isNull());
int i = 0;
for (typename List::const_iterator it = aCntr.begin();
it != aCntr.end(); ++ it, ++ i)
#if defined (VBOX_WITH_XPCOM)
Copy (*it, Base::m.arr [i]);
#else
Copy (*it, Base::m.raw [i]);
#endif
}
/**
* Reinitializes this instance by preallocating space for the given number
* of elements. The previous array contents is lost.
*
* @param aNewSize New number of elements in the array.
* @return @c true on success and false if there is not enough
* memory for resizing.
*/
virtual bool reset (size_t aNewSize)
{
Base::m.uninit();
#if defined (VBOX_WITH_XPCOM)
/* Note: for zero-sized arrays, we use the size of 1 because whether
* malloc(0) returns a null pointer or not (which is used in isNull())
* is implementation-dependent according to the C standard. */
Base::m.arr = (I **) nsMemory::Alloc (RT_MAX (aNewSize, 1) * sizeof (I *));
AssertReturn (Base::m.arr != NULL, false);
Base::m.size = aNewSize;
for (size_t i = 0; i < Base::m.size; ++ i)
Init (Base::m.arr [i]);
#else
SAFEARRAYBOUND bound = { (ULONG)aNewSize, 0 };
m.arr = SafeArrayCreateEx (VT_UNKNOWN, 1, &bound,
(PVOID) &_ATL_IIDOF (I));
AssertReturn (m.arr != NULL, false);
AssertReturn (accessRaw() != NULL, false);
#endif
return true;
}
};
} /* namespace com */
/** @} */
#endif /* ___VBox_com_array_h */