Bug 450881: Add helpers for XPIDL arrays to nsCOMArray, r=froydnj

This commit is contained in:
Joshua Cranmer 2014-01-22 16:19:00 -06:00
Родитель f6e2c0cc5d
Коммит 33e4c4f0ca
2 изменённых файлов: 65 добавлений и 0 удалений

Просмотреть файл

@ -278,3 +278,29 @@ nsCOMArray_base::SizeOfExcludingThis(
return n;
}
void
nsCOMArray_base::Adopt(nsISupports** aElements, uint32_t aSize)
{
Clear();
mArray.AppendElements(aElements, aSize);
// Free the allocated array as well.
NS_Free(aElements);
}
uint32_t
nsCOMArray_base::Forget(nsISupports*** elements)
{
uint32_t length = Length();
size_t array_size = sizeof(nsISupports*) * length;
nsISupports** array = static_cast<nsISupports**>(NS_Alloc(array_size));
memmove(array, Elements(), array_size);
*elements = array;
// Don't Release the contained pointers; the caller of the method will
// do this eventually.
mArray.Clear();
return length;
}

Просмотреть файл

@ -89,6 +89,8 @@ protected:
mArray.SwapElements(aOther.mArray);
}
void Adopt(nsISupports** aElements, uint32_t aCount);
uint32_t Forget(nsISupports*** aElements);
public:
// elements in the array (including null elements!)
int32_t Count() const {
@ -230,8 +232,16 @@ class nsCOMArray : public nsCOMArray_base
explicit
nsCOMArray(const nsCOMArray<T>& aOther) : nsCOMArray_base(aOther) { }
nsCOMArray(nsCOMArray<T>&& aOther) { SwapElements(aOther); }
~nsCOMArray() {}
// We have a move assignment operator, but no copy assignment operator.
nsCOMArray<T>& operator=(nsCOMArray<T>&& aOther) {
SwapElements(aOther);
return *this;
}
// these do NOT refcount on the way out, for speed
T* ObjectAt(int32_t aIndex) const {
return static_cast<T*>(nsCOMArray_base::ObjectAt(aIndex));
@ -387,6 +397,35 @@ class nsCOMArray : public nsCOMArray_base
aMallocSizeOf, aData);
}
/**
* Adopt parameters that resulted from an XPIDL outparam. The aElements
* parameter will be freed as a result of the call.
*
* Example usage:
* nsCOMArray<nsISomeInterface> array;
* nsISomeInterface** elements;
* uint32_t length;
* ptr->GetSomeArray(&elements, &length);
* array.Adopt(elements, length);
*/
void Adopt(T** aElements, uint32_t aSize) {
nsCOMArray_base::Adopt(reinterpret_cast<nsISupports**>(aElements),
aSize);
}
/**
* Export the contents of this array to an XPIDL outparam. The array will be
* Clear()'d after this operation.
*
* Example usage:
* nsCOMArray<nsISomeInterface> array;
* *length = array.Forget(retval);
*/
uint32_t Forget(T*** elements) {
return nsCOMArray_base::Forget(
reinterpret_cast<nsISupports***>(elements));
}
private:
// don't implement these!