sbArrayUtils.h
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 #ifndef SBARRAYUTILS_H_
26 #define SBARRAYUTILS_H_
27 
28 // Standard includes
29 #include <algorithm>
30 
31 // Mozilla includes
32 #include <nsCOMArray.h>
33 #include <nsComponentManagerUtils.h>
34 
35 // Mozilla interfaces
36 #include <nsIMutableArray.h>
37 #include <nsISimpleEnumerator.h>
38 
39 #ifndef SB_THREADSAFE_ARRAY_CONTRACTID
40 #define SB_THREADSAFE_ARRAY_CONTRACTID \
41  "@songbirdnest.com/moz/xpcom/threadsafe-array;1"
42 #endif /* SB_THREADSAFE_ARRAY_CONTRACTID */
43 
44 template <class T>
45 nsresult sbCOMArrayTonsIArray(T & aCOMArray, nsIArray ** aOutArray)
46 {
47  nsresult rv;
48  nsCOMPtr<nsIMutableArray> outArray =
49  do_CreateInstance(SB_THREADSAFE_ARRAY_CONTRACTID, &rv);
50  NS_ENSURE_SUCCESS(rv, rv);
51 
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);
56  }
57 
58  rv = CallQueryInterface(outArray, aOutArray);
59  NS_ENSURE_SUCCESS(rv, rv);
60 
61  return NS_OK;
62 }
63 
64 template <class T>
65 nsresult sbAppendnsCOMArray(T const & aSource,
66  T & aDest,
67  PRUint32 aElementsToCopy = 0)
68 {
69  PRUint32 length = aSource.Count();
70  if (aElementsToCopy) {
71  length = std::min(length, aElementsToCopy);
72  }
73  for (PRUint32 index = 0; index < length; ++index) {
74  NS_ENSURE_TRUE(aDest.AppendObject(aSource[index]), NS_ERROR_OUT_OF_MEMORY);
75  }
76  return NS_OK;
77 }
86 inline nsresult
87 sbAppendnsIArray(nsIArray * aSrc,
88  nsIMutableArray * aDest,
89  PRBool aWeak = PR_FALSE,
90  PRUint32 aElementsToCopy = 0)
91 {
92  nsresult rv;
93 
94  PRUint32 elementsToCopy = aElementsToCopy;
95  if (elementsToCopy == 0) {
96  rv = aSrc->GetLength(&elementsToCopy);
97  NS_ENSURE_SUCCESS(rv, rv);
98  }
99 
100  nsCOMPtr<nsISimpleEnumerator> it;
101  rv = aSrc->Enumerate(getter_AddRefs(it));
102  NS_ENSURE_SUCCESS(rv, rv);
103 
104  PRBool hasMore;
105  while (NS_SUCCEEDED(it->HasMoreElements(&hasMore)) &&
106  hasMore &&
107  elementsToCopy--) {
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);
113  }
114  NS_ENSURE_SUCCESS(rv, rv);
115 
116  return NS_OK;
117 }
124 inline nsresult
125 sbClonensIArray(nsIArray * aSrc, nsIArray ** aClonedArray, PRBool aWeak = PR_FALSE)
126 {
127  NS_ENSURE_ARG_POINTER(aSrc);
128  NS_ENSURE_ARG_POINTER(aClonedArray);
129 
130  nsresult rv;
131 
132  nsCOMPtr<nsIMutableArray> array =
133  do_CreateInstance(SB_THREADSAFE_ARRAY_CONTRACTID, &rv);
134  NS_ENSURE_SUCCESS(rv, rv);
135 
136  rv = sbAppendnsIArray(aSrc, array, aWeak);
137  NS_ENSURE_SUCCESS(rv, rv);
138 
139  rv = CallQueryInterface(array, aClonedArray);
140  NS_ENSURE_SUCCESS(rv, rv);
141 
142  return NS_OK;
143 }
144 
153 template<class T>
154 nsresult
155 sbClonensIArray(nsIArray * aSrc, nsCOMArray<T> & aDest)
156 {
157  NS_ENSURE_ARG_POINTER(aSrc);
158  nsresult rv;
159  nsCOMPtr<nsISimpleEnumerator> it;
160  rv = aSrc->Enumerate(getter_AddRefs(it));
161  NS_ENSURE_SUCCESS(rv, rv);
162 
163  nsCOMPtr<nsISupports> supports;
164  PRBool hasMore;
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);
172  }
173  NS_ENSURE_SUCCESS(rv, rv);
174 
175  return NS_OK;
176 }
177 
178 #endif /* SBARRAYUTILS_H_ */
nsresult sbCOMArrayTonsIArray(T &aCOMArray, nsIArray **aOutArray)
Definition: sbArrayUtils.h:45
return NS_OK
inArray array
nsresult sbAppendnsIArray(nsIArray *aSrc, nsIMutableArray *aDest, PRBool aWeak=PR_FALSE, PRUint32 aElementsToCopy=0)
Definition: sbArrayUtils.h:87
#define SB_THREADSAFE_ARRAY_CONTRACTID
Definition: sbArrayUtils.h:40
nsresult sbClonensIArray(nsIArray *aSrc, nsIArray **aClonedArray, PRBool aWeak=PR_FALSE)
Definition: sbArrayUtils.h:125
StringArrayEnumerator prototype hasMore
nsresult sbAppendnsCOMArray(T const &aSource, T &aDest, PRUint32 aElementsToCopy=0)
Definition: sbArrayUtils.h:65
#define min(a, b)