25 #ifndef SBARRAYUTILS_H_
26 #define SBARRAYUTILS_H_
32 #include <nsCOMArray.h>
33 #include <nsComponentManagerUtils.h>
36 #include <nsIMutableArray.h>
37 #include <nsISimpleEnumerator.h>
39 #ifndef SB_THREADSAFE_ARRAY_CONTRACTID
40 #define SB_THREADSAFE_ARRAY_CONTRACTID \
41 "@songbirdnest.com/moz/xpcom/threadsafe-array;1"
48 nsCOMPtr<nsIMutableArray> outArray =
50 NS_ENSURE_SUCCESS(rv, rv);
52 PRInt32
const length = aCOMArray.Count();
53 for (PRInt32 index = 0; index < length; ++index) {
54 rv = outArray->AppendElement(aCOMArray.ObjectAt(index), PR_FALSE);
55 NS_ENSURE_SUCCESS(rv, rv);
58 rv = CallQueryInterface(outArray, aOutArray);
59 NS_ENSURE_SUCCESS(rv, rv);
67 PRUint32 aElementsToCopy = 0)
69 PRUint32 length = aSource.Count();
70 if (aElementsToCopy) {
71 length =
std::min(length, aElementsToCopy);
73 for (PRUint32 index = 0; index < length; ++index) {
74 NS_ENSURE_TRUE(aDest.AppendObject(aSource[index]), NS_ERROR_OUT_OF_MEMORY);
88 nsIMutableArray * aDest,
89 PRBool aWeak = PR_FALSE,
90 PRUint32 aElementsToCopy = 0)
94 PRUint32 elementsToCopy = aElementsToCopy;
95 if (elementsToCopy == 0) {
96 rv = aSrc->GetLength(&elementsToCopy);
97 NS_ENSURE_SUCCESS(rv, rv);
100 nsCOMPtr<nsISimpleEnumerator> it;
101 rv = aSrc->Enumerate(getter_AddRefs(it));
102 NS_ENSURE_SUCCESS(rv, rv);
105 while (NS_SUCCEEDED(it->HasMoreElements(&hasMore)) &&
108 nsCOMPtr<nsISupports> supports;
109 rv = it->GetNext(getter_AddRefs(supports));
110 NS_ENSURE_SUCCESS(rv, rv);
111 rv = aDest->AppendElement(supports, aWeak);
112 NS_ENSURE_SUCCESS(rv, rv);
114 NS_ENSURE_SUCCESS(rv, rv);
127 NS_ENSURE_ARG_POINTER(aSrc);
128 NS_ENSURE_ARG_POINTER(aClonedArray);
132 nsCOMPtr<nsIMutableArray>
array =
134 NS_ENSURE_SUCCESS(rv, rv);
137 NS_ENSURE_SUCCESS(rv, rv);
139 rv = CallQueryInterface(array, aClonedArray);
140 NS_ENSURE_SUCCESS(rv, rv);
157 NS_ENSURE_ARG_POINTER(aSrc);
159 nsCOMPtr<nsISimpleEnumerator> it;
160 rv = aSrc->Enumerate(getter_AddRefs(it));
161 NS_ENSURE_SUCCESS(rv, rv);
163 nsCOMPtr<nsISupports> supports;
165 while (NS_SUCCEEDED(rv = it->HasMoreElements(&hasMore)) && hasMore) {
166 rv = it->GetNext(getter_AddRefs(supports));
167 NS_ENSURE_SUCCESS(rv, rv);
168 nsCOMPtr<T> ptr = do_QueryInterface(supports, &rv);
169 NS_ENSURE_SUCCESS(rv, rv);
170 PRBool success = aDest.AppendObject(ptr);
171 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
173 NS_ENSURE_SUCCESS(rv, rv);
nsresult sbCOMArrayTonsIArray(T &aCOMArray, nsIArray **aOutArray)
nsresult sbAppendnsIArray(nsIArray *aSrc, nsIMutableArray *aDest, PRBool aWeak=PR_FALSE, PRUint32 aElementsToCopy=0)
#define SB_THREADSAFE_ARRAY_CONTRACTID
nsresult sbClonensIArray(nsIArray *aSrc, nsIArray **aClonedArray, PRBool aWeak=PR_FALSE)
StringArrayEnumerator prototype hasMore
nsresult sbAppendnsCOMArray(T const &aSource, T &aDest, PRUint32 aElementsToCopy=0)