mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-03-03 23:30:46 +00:00
Bug 1310040 - Make nsSupportsArray implement nsIArray. r=froydnj
This makes nsSupportsArray implement the |nsIArray| interface. This will allow us to replace references to nsISupportsArray as a param in our gecko interfaces with nsIArray instead. The goal is to remove this adapter (and nsISupportsArray) after a full release cycle. MozReview-Commit-ID: If7RiO5muIk
This commit is contained in:
parent
ac6647aa97
commit
3ee733ccd7
@ -19,6 +19,12 @@ interface nsICollection : nsISerializable
|
||||
void AppendElement(in nsISupports item);
|
||||
void RemoveElement(in nsISupports item);
|
||||
|
||||
/**
|
||||
* This clashes with |nsISimpleEnumerator nsIArray.enumerate()| (only on the
|
||||
* binary side), so it is renamed with a 'Deprecated' prefix in favor of the
|
||||
* non-deprecated |nsIArray.enumerate|.
|
||||
*/
|
||||
[binaryname(DeprecatedEnumerate)]
|
||||
nsIEnumerator Enumerate();
|
||||
|
||||
void Clear();
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "nsArrayEnumerator.h"
|
||||
#include "nsIObjectInputStream.h"
|
||||
#include "nsIObjectOutputStream.h"
|
||||
#include "nsSupportsArray.h"
|
||||
@ -47,7 +48,7 @@ nsSupportsArray::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
||||
return it->QueryInterface(aIID, aResult);
|
||||
}
|
||||
|
||||
NS_IMPL_ISUPPORTS(nsSupportsArray, nsISupportsArray, nsICollection,
|
||||
NS_IMPL_ISUPPORTS(nsSupportsArray, nsIArray, nsISupportsArray, nsICollection,
|
||||
nsISerializable)
|
||||
|
||||
NS_IMETHODIMP
|
||||
@ -172,7 +173,7 @@ nsSupportsArray::Clear(void)
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSupportsArray::Enumerate(nsIEnumerator** aResult)
|
||||
nsSupportsArray::DeprecatedEnumerate(nsIEnumerator** aResult)
|
||||
{
|
||||
RefPtr<nsSupportsArrayEnumerator> e = new nsSupportsArrayEnumerator(this);
|
||||
e.forget(aResult);
|
||||
@ -208,3 +209,39 @@ NS_NewISupportsArray(nsISupportsArray** aInstancePtrResult)
|
||||
(void**)aInstancePtrResult);
|
||||
return rv;
|
||||
}
|
||||
|
||||
/**
|
||||
* nsIArray adapters.
|
||||
*/
|
||||
NS_IMETHODIMP
|
||||
nsSupportsArray::GetLength(uint32_t* aLength) {
|
||||
return Count(aLength);
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSupportsArray::QueryElementAt(uint32_t aIndex, const nsIID& aIID, void** aResult)
|
||||
{
|
||||
nsISupports* element = mArray.SafeElementAt(aIndex);
|
||||
if (element) {
|
||||
return element->QueryInterface(aIID, aResult);
|
||||
}
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSupportsArray::IndexOf(uint32_t aStartIndex, nsISupports* aElement, uint32_t* aResult)
|
||||
{
|
||||
int32_t idx = mArray.IndexOf(aElement, aStartIndex);
|
||||
if (idx < 0) {
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
|
||||
*aResult = static_cast<uint32_t>(idx);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
NS_IMETHODIMP
|
||||
nsSupportsArray::Enumerate(nsISimpleEnumerator** aResult)
|
||||
{
|
||||
return NS_NewArrayEnumerator(aResult, this);
|
||||
}
|
||||
|
@ -11,7 +11,8 @@
|
||||
#include "nsCOMArray.h"
|
||||
#include "mozilla/Attributes.h"
|
||||
|
||||
class nsSupportsArray final : public nsISupportsArray
|
||||
class nsSupportsArray final : public nsISupportsArray,
|
||||
public nsIArray
|
||||
{
|
||||
~nsSupportsArray(void); // nonvirtual since we're not subclassed
|
||||
|
||||
@ -33,15 +34,6 @@ public:
|
||||
}
|
||||
NS_IMETHOD GetElementAt(uint32_t aIndex, nsISupports** aResult) override;
|
||||
MOZ_MUST_USE NS_IMETHOD
|
||||
QueryElementAt(uint32_t aIndex, const nsIID& aIID, void** aResult) override
|
||||
{
|
||||
nsISupports* element = mArray.SafeElementAt(aIndex);
|
||||
if (element) {
|
||||
return element->QueryInterface(aIID, aResult);
|
||||
}
|
||||
return NS_ERROR_FAILURE;
|
||||
}
|
||||
MOZ_MUST_USE NS_IMETHOD
|
||||
SetElementAt(uint32_t aIndex, nsISupports* aValue) override
|
||||
{
|
||||
return ReplaceElementAt(aValue, aIndex) ? NS_OK : NS_ERROR_FAILURE;
|
||||
@ -53,7 +45,7 @@ public:
|
||||
}
|
||||
// XXX this is badly named - should be RemoveFirstElement
|
||||
MOZ_MUST_USE NS_IMETHOD RemoveElement(nsISupports* aElement) override;
|
||||
NS_IMETHOD Enumerate(nsIEnumerator** aResult) override;
|
||||
NS_IMETHOD DeprecatedEnumerate(nsIEnumerator** aResult) override;
|
||||
NS_IMETHOD Clear(void) override;
|
||||
|
||||
// nsISupportsArray methods:
|
||||
@ -81,6 +73,11 @@ public:
|
||||
|
||||
MOZ_MUST_USE NS_IMETHOD Clone(nsISupportsArray** aResult) override;
|
||||
|
||||
/**
|
||||
* nsIArray adapters.
|
||||
*/
|
||||
NS_DECL_NSIARRAY
|
||||
|
||||
private:
|
||||
// Copy constructors are not allowed
|
||||
explicit nsSupportsArray(const nsISupportsArray& aOther);
|
||||
|
@ -107,6 +107,46 @@ function test_enumerate()
|
||||
do_check_eq(arr.length, i);
|
||||
}
|
||||
|
||||
function test_nssupportsarray_interop() {
|
||||
// Tests to check that an nsSupportsArray instance can behave like an
|
||||
// nsIArray.
|
||||
let test = Components.classes["@mozilla.org/supports-array;1"]
|
||||
.createInstance(Ci.nsISupportsArray);
|
||||
|
||||
let str = new SupportsString();
|
||||
str.data = "element";
|
||||
test.AppendElement(str);
|
||||
|
||||
// Now query to an nsIArray.
|
||||
let iarray = test.QueryInterface(Ci.nsIArray);
|
||||
do_check_neq(iarray, null);
|
||||
|
||||
// Make sure |nsIArray.length| works.
|
||||
do_check_eq(iarray.length, 1);
|
||||
|
||||
// Make sure |nsIArray.queryElementAt| works.
|
||||
let elm = iarray.queryElementAt(0, Ci.nsISupportsString);
|
||||
do_check_eq(elm.data, "element");
|
||||
|
||||
// Make sure |nsIArray.indexOf| works.
|
||||
let idx = iarray.indexOf(0, str);
|
||||
do_check_eq(idx, 0);
|
||||
|
||||
// Make sure |nsIArray.enumerate| works.
|
||||
let en = iarray.enumerate();
|
||||
do_check_neq(en, null);
|
||||
let i = 0;
|
||||
while (en.hasMoreElements()) {
|
||||
let str = en.getNext();
|
||||
do_check_true(str instanceof Ci.nsISupportsString);
|
||||
do_check_eq(str.data, "element");
|
||||
i++;
|
||||
}
|
||||
|
||||
do_check_eq(iarray.length, i);
|
||||
}
|
||||
|
||||
|
||||
var tests = [
|
||||
test_appending_null_actually_inserts,
|
||||
test_object_gets_appended,
|
||||
@ -114,6 +154,7 @@ var tests = [
|
||||
test_replace_element,
|
||||
test_clear,
|
||||
test_enumerate,
|
||||
test_nssupportsarray_interop,
|
||||
];
|
||||
|
||||
function run_test() {
|
||||
|
Loading…
x
Reference in New Issue
Block a user