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

--HG--
extra : rebase_source : d87f6cbdd2bab54f5923821409c9197f98b372f4
This commit is contained in:
Joshua Cranmer 2014-01-21 18:01:36 -06:00
parent 614d100a46
commit 9e4e7d3356
2 changed files with 66 additions and 0 deletions

View File

@ -6,6 +6,7 @@
#include "nsCOMArray.h"
#include "mozilla/MemoryReporting.h"
#include "mozilla/mozalloc.h"
#include "nsCOMPtr.h"
@ -278,3 +279,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.
moz_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**>(moz_xmalloc(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;
}

View File

@ -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!