michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsSupportsArray_h__ michael@0: #define nsSupportsArray_h__ michael@0: michael@0: //#define DEBUG_SUPPORTSARRAY 1 michael@0: michael@0: #include "nsISupportsArray.h" michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: static const uint32_t kAutoArraySize = 8; michael@0: michael@0: class nsSupportsArray MOZ_FINAL : public nsISupportsArray { michael@0: public: michael@0: nsSupportsArray(void); michael@0: ~nsSupportsArray(void); // nonvirtual since we're not subclassed michael@0: michael@0: static nsresult michael@0: Create(nsISupports *aOuter, REFNSIID aIID, void **aResult); michael@0: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: michael@0: NS_DECL_NSISERIALIZABLE michael@0: michael@0: // nsICollection methods: michael@0: NS_IMETHOD Count(uint32_t *result) { *result = mCount; return NS_OK; } michael@0: NS_IMETHOD GetElementAt(uint32_t aIndex, nsISupports* *result); michael@0: NS_IMETHOD QueryElementAt(uint32_t aIndex, const nsIID & aIID, void * *aResult) { michael@0: if (aIndex < mCount) { michael@0: nsISupports* element = mArray[aIndex]; michael@0: if (nullptr != element) michael@0: return element->QueryInterface(aIID, aResult); michael@0: } michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: NS_IMETHOD SetElementAt(uint32_t aIndex, nsISupports* value) { michael@0: return ReplaceElementAt(value, aIndex) ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: NS_IMETHOD AppendElement(nsISupports *aElement) { michael@0: // XXX Invalid cast of bool to nsresult (bug 778110) michael@0: return (nsresult)InsertElementAt(aElement, mCount)/* ? NS_OK : NS_ERROR_FAILURE*/; michael@0: } michael@0: // XXX this is badly named - should be RemoveFirstElement michael@0: NS_IMETHOD RemoveElement(nsISupports *aElement) { michael@0: // XXX Invalid cast of bool to nsresult (bug 778110) michael@0: return (nsresult)RemoveElement(aElement, 0)/* ? NS_OK : NS_ERROR_FAILURE*/; michael@0: } michael@0: NS_IMETHOD_(bool) MoveElement(int32_t aFrom, int32_t aTo); michael@0: NS_IMETHOD Enumerate(nsIEnumerator* *result); michael@0: NS_IMETHOD Clear(void); michael@0: michael@0: // nsISupportsArray methods: michael@0: NS_IMETHOD_(bool) Equals(const nsISupportsArray* aOther); michael@0: michael@0: NS_IMETHOD_(int32_t) IndexOf(const nsISupports* aPossibleElement); michael@0: NS_IMETHOD_(int32_t) IndexOfStartingAt(const nsISupports* aPossibleElement, michael@0: uint32_t aStartIndex = 0); michael@0: NS_IMETHOD_(int32_t) LastIndexOf(const nsISupports* aPossibleElement); michael@0: michael@0: NS_IMETHOD GetIndexOf(nsISupports *aPossibleElement, int32_t *_retval) { michael@0: *_retval = IndexOf(aPossibleElement); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHOD GetIndexOfStartingAt(nsISupports *aPossibleElement, michael@0: uint32_t aStartIndex, int32_t *_retval) { michael@0: *_retval = IndexOfStartingAt(aPossibleElement, aStartIndex); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHOD GetLastIndexOf(nsISupports *aPossibleElement, int32_t *_retval) { michael@0: *_retval = LastIndexOf(aPossibleElement); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHOD_(bool) InsertElementAt(nsISupports* aElement, uint32_t aIndex); michael@0: michael@0: NS_IMETHOD_(bool) ReplaceElementAt(nsISupports* aElement, uint32_t aIndex); michael@0: michael@0: NS_IMETHOD_(bool) RemoveElementAt(uint32_t aIndex) { michael@0: return RemoveElementsAt(aIndex,1); michael@0: } michael@0: NS_IMETHOD_(bool) RemoveElement(const nsISupports* aElement, uint32_t aStartIndex = 0); michael@0: NS_IMETHOD_(bool) RemoveLastElement(const nsISupports* aElement); michael@0: michael@0: NS_IMETHOD DeleteLastElement(nsISupports *aElement) { michael@0: return (RemoveLastElement(aElement) ? NS_OK : NS_ERROR_FAILURE); michael@0: } michael@0: michael@0: NS_IMETHOD DeleteElementAt(uint32_t aIndex) { michael@0: return (RemoveElementAt(aIndex) ? NS_OK : NS_ERROR_FAILURE); michael@0: } michael@0: michael@0: NS_IMETHOD_(bool) AppendElements(nsISupportsArray* aElements) { michael@0: return InsertElementsAt(aElements,mCount); michael@0: } michael@0: michael@0: NS_IMETHOD Compact(void); michael@0: michael@0: NS_IMETHOD Clone(nsISupportsArray **_retval); michael@0: michael@0: NS_IMETHOD_(bool) InsertElementsAt(nsISupportsArray *aOther, uint32_t aIndex); michael@0: michael@0: NS_IMETHOD_(bool) RemoveElementsAt(uint32_t aIndex, uint32_t aCount); michael@0: michael@0: NS_IMETHOD_(bool) SizeTo(int32_t aSize); michael@0: protected: michael@0: void DeleteArray(void); michael@0: michael@0: NS_IMETHOD_(void) GrowArrayBy(int32_t aGrowBy); michael@0: michael@0: nsISupports** mArray; michael@0: uint32_t mArraySize; michael@0: uint32_t mCount; michael@0: nsISupports* mAutoArray[kAutoArraySize]; michael@0: #if DEBUG_SUPPORTSARRAY michael@0: uint32_t mMaxCount; michael@0: uint32_t mMaxSize; michael@0: #endif michael@0: michael@0: private: michael@0: // Copy constructors are not allowed michael@0: nsSupportsArray(const nsISupportsArray& other); michael@0: }; michael@0: michael@0: #endif // nsSupportsArray_h__