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
Родитель 614d100a46
Коммит 9e4e7d3356
2 изменённых файлов: 66 добавлений и 0 удалений

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

@ -6,6 +6,7 @@
#include "nsCOMArray.h" #include "nsCOMArray.h"
#include "mozilla/MemoryReporting.h" #include "mozilla/MemoryReporting.h"
#include "mozilla/mozalloc.h"
#include "nsCOMPtr.h" #include "nsCOMPtr.h"
@ -278,3 +279,29 @@ nsCOMArray_base::SizeOfExcludingThis(
return n; 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;
}

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

@ -89,6 +89,8 @@ protected:
mArray.SwapElements(aOther.mArray); mArray.SwapElements(aOther.mArray);
} }
void Adopt(nsISupports** aElements, uint32_t aCount);
uint32_t Forget(nsISupports*** aElements);
public: public:
// elements in the array (including null elements!) // elements in the array (including null elements!)
int32_t Count() const { int32_t Count() const {
@ -230,8 +232,16 @@ class nsCOMArray : public nsCOMArray_base
explicit explicit
nsCOMArray(const nsCOMArray<T>& aOther) : nsCOMArray_base(aOther) { } nsCOMArray(const nsCOMArray<T>& aOther) : nsCOMArray_base(aOther) { }
nsCOMArray(nsCOMArray<T>&& aOther) { SwapElements(aOther); }
~nsCOMArray() {} ~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 // these do NOT refcount on the way out, for speed
T* ObjectAt(int32_t aIndex) const { T* ObjectAt(int32_t aIndex) const {
return static_cast<T*>(nsCOMArray_base::ObjectAt(aIndex)); return static_cast<T*>(nsCOMArray_base::ObjectAt(aIndex));
@ -387,6 +397,35 @@ class nsCOMArray : public nsCOMArray_base
aMallocSizeOf, aData); 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: private:
// don't implement these! // don't implement these!