|
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 #ifndef nsSupportsArray_h__ |
|
7 #define nsSupportsArray_h__ |
|
8 |
|
9 //#define DEBUG_SUPPORTSARRAY 1 |
|
10 |
|
11 #include "nsISupportsArray.h" |
|
12 #include "mozilla/Attributes.h" |
|
13 |
|
14 static const uint32_t kAutoArraySize = 8; |
|
15 |
|
16 class nsSupportsArray MOZ_FINAL : public nsISupportsArray { |
|
17 public: |
|
18 nsSupportsArray(void); |
|
19 ~nsSupportsArray(void); // nonvirtual since we're not subclassed |
|
20 |
|
21 static nsresult |
|
22 Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); |
|
23 |
|
24 NS_DECL_THREADSAFE_ISUPPORTS |
|
25 |
|
26 NS_DECL_NSISERIALIZABLE |
|
27 |
|
28 // nsICollection methods: |
|
29 NS_IMETHOD Count(uint32_t *result) { *result = mCount; return NS_OK; } |
|
30 NS_IMETHOD GetElementAt(uint32_t aIndex, nsISupports* *result); |
|
31 NS_IMETHOD QueryElementAt(uint32_t aIndex, const nsIID & aIID, void * *aResult) { |
|
32 if (aIndex < mCount) { |
|
33 nsISupports* element = mArray[aIndex]; |
|
34 if (nullptr != element) |
|
35 return element->QueryInterface(aIID, aResult); |
|
36 } |
|
37 return NS_ERROR_FAILURE; |
|
38 } |
|
39 NS_IMETHOD SetElementAt(uint32_t aIndex, nsISupports* value) { |
|
40 return ReplaceElementAt(value, aIndex) ? NS_OK : NS_ERROR_FAILURE; |
|
41 } |
|
42 NS_IMETHOD AppendElement(nsISupports *aElement) { |
|
43 // XXX Invalid cast of bool to nsresult (bug 778110) |
|
44 return (nsresult)InsertElementAt(aElement, mCount)/* ? NS_OK : NS_ERROR_FAILURE*/; |
|
45 } |
|
46 // XXX this is badly named - should be RemoveFirstElement |
|
47 NS_IMETHOD RemoveElement(nsISupports *aElement) { |
|
48 // XXX Invalid cast of bool to nsresult (bug 778110) |
|
49 return (nsresult)RemoveElement(aElement, 0)/* ? NS_OK : NS_ERROR_FAILURE*/; |
|
50 } |
|
51 NS_IMETHOD_(bool) MoveElement(int32_t aFrom, int32_t aTo); |
|
52 NS_IMETHOD Enumerate(nsIEnumerator* *result); |
|
53 NS_IMETHOD Clear(void); |
|
54 |
|
55 // nsISupportsArray methods: |
|
56 NS_IMETHOD_(bool) Equals(const nsISupportsArray* aOther); |
|
57 |
|
58 NS_IMETHOD_(int32_t) IndexOf(const nsISupports* aPossibleElement); |
|
59 NS_IMETHOD_(int32_t) IndexOfStartingAt(const nsISupports* aPossibleElement, |
|
60 uint32_t aStartIndex = 0); |
|
61 NS_IMETHOD_(int32_t) LastIndexOf(const nsISupports* aPossibleElement); |
|
62 |
|
63 NS_IMETHOD GetIndexOf(nsISupports *aPossibleElement, int32_t *_retval) { |
|
64 *_retval = IndexOf(aPossibleElement); |
|
65 return NS_OK; |
|
66 } |
|
67 |
|
68 NS_IMETHOD GetIndexOfStartingAt(nsISupports *aPossibleElement, |
|
69 uint32_t aStartIndex, int32_t *_retval) { |
|
70 *_retval = IndexOfStartingAt(aPossibleElement, aStartIndex); |
|
71 return NS_OK; |
|
72 } |
|
73 |
|
74 NS_IMETHOD GetLastIndexOf(nsISupports *aPossibleElement, int32_t *_retval) { |
|
75 *_retval = LastIndexOf(aPossibleElement); |
|
76 return NS_OK; |
|
77 } |
|
78 |
|
79 NS_IMETHOD_(bool) InsertElementAt(nsISupports* aElement, uint32_t aIndex); |
|
80 |
|
81 NS_IMETHOD_(bool) ReplaceElementAt(nsISupports* aElement, uint32_t aIndex); |
|
82 |
|
83 NS_IMETHOD_(bool) RemoveElementAt(uint32_t aIndex) { |
|
84 return RemoveElementsAt(aIndex,1); |
|
85 } |
|
86 NS_IMETHOD_(bool) RemoveElement(const nsISupports* aElement, uint32_t aStartIndex = 0); |
|
87 NS_IMETHOD_(bool) RemoveLastElement(const nsISupports* aElement); |
|
88 |
|
89 NS_IMETHOD DeleteLastElement(nsISupports *aElement) { |
|
90 return (RemoveLastElement(aElement) ? NS_OK : NS_ERROR_FAILURE); |
|
91 } |
|
92 |
|
93 NS_IMETHOD DeleteElementAt(uint32_t aIndex) { |
|
94 return (RemoveElementAt(aIndex) ? NS_OK : NS_ERROR_FAILURE); |
|
95 } |
|
96 |
|
97 NS_IMETHOD_(bool) AppendElements(nsISupportsArray* aElements) { |
|
98 return InsertElementsAt(aElements,mCount); |
|
99 } |
|
100 |
|
101 NS_IMETHOD Compact(void); |
|
102 |
|
103 NS_IMETHOD Clone(nsISupportsArray **_retval); |
|
104 |
|
105 NS_IMETHOD_(bool) InsertElementsAt(nsISupportsArray *aOther, uint32_t aIndex); |
|
106 |
|
107 NS_IMETHOD_(bool) RemoveElementsAt(uint32_t aIndex, uint32_t aCount); |
|
108 |
|
109 NS_IMETHOD_(bool) SizeTo(int32_t aSize); |
|
110 protected: |
|
111 void DeleteArray(void); |
|
112 |
|
113 NS_IMETHOD_(void) GrowArrayBy(int32_t aGrowBy); |
|
114 |
|
115 nsISupports** mArray; |
|
116 uint32_t mArraySize; |
|
117 uint32_t mCount; |
|
118 nsISupports* mAutoArray[kAutoArraySize]; |
|
119 #if DEBUG_SUPPORTSARRAY |
|
120 uint32_t mMaxCount; |
|
121 uint32_t mMaxSize; |
|
122 #endif |
|
123 |
|
124 private: |
|
125 // Copy constructors are not allowed |
|
126 nsSupportsArray(const nsISupportsArray& other); |
|
127 }; |
|
128 |
|
129 #endif // nsSupportsArray_h__ |