michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 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 TRANSFRMX_EXPANDEDNAMEMAP_H michael@0: #define TRANSFRMX_EXPANDEDNAMEMAP_H michael@0: michael@0: #include "nsError.h" michael@0: #include "txXMLUtils.h" michael@0: #include "nsTArray.h" michael@0: michael@0: class txExpandedNameMap_base { michael@0: protected: michael@0: /** michael@0: * Adds an item, if an item with this key already exists an error is michael@0: * returned michael@0: * @param aKey key for item to add michael@0: * @param aValue value of item to add michael@0: * @return errorcode michael@0: */ michael@0: nsresult addItem(const txExpandedName& aKey, void* aValue); michael@0: michael@0: /** michael@0: * Sets an item, if an item with this key already exists it is overwritten michael@0: * with the new value michael@0: * @param aKey key for item to set michael@0: * @param aValue value of item to set michael@0: * @return errorcode michael@0: */ michael@0: nsresult setItem(const txExpandedName& aKey, void* aValue, michael@0: void** aOldValue); michael@0: michael@0: /** michael@0: * Gets an item michael@0: * @param aKey key for item to get michael@0: * @return item with specified key, or null if no such item exists michael@0: */ michael@0: void* getItem(const txExpandedName& aKey) const; michael@0: michael@0: /** michael@0: * Removes an item, deleting it if the map owns the values michael@0: * @param aKey key for item to remove michael@0: * @return item with specified key, or null if it has been deleted michael@0: * or no such item exists michael@0: */ michael@0: void* removeItem(const txExpandedName& aKey); michael@0: michael@0: /** michael@0: * Clears the items michael@0: */ michael@0: void clearItems() michael@0: { michael@0: mItems.Clear(); michael@0: } michael@0: michael@0: class iterator_base { michael@0: public: michael@0: iterator_base(txExpandedNameMap_base& aMap) michael@0: : mMap(aMap), michael@0: mCurrentPos(uint32_t(-1)) michael@0: { michael@0: } michael@0: michael@0: bool next() michael@0: { michael@0: return ++mCurrentPos < mMap.mItems.Length(); michael@0: } michael@0: michael@0: const txExpandedName key() michael@0: { michael@0: NS_ASSERTION(mCurrentPos < mMap.mItems.Length(), michael@0: "invalid position in txExpandedNameMap::iterator"); michael@0: return txExpandedName(mMap.mItems[mCurrentPos].mNamespaceID, michael@0: mMap.mItems[mCurrentPos].mLocalName); michael@0: } michael@0: michael@0: protected: michael@0: void* itemValue() michael@0: { michael@0: NS_ASSERTION(mCurrentPos < mMap.mItems.Length(), michael@0: "invalid position in txExpandedNameMap::iterator"); michael@0: return mMap.mItems[mCurrentPos].mValue; michael@0: } michael@0: michael@0: private: michael@0: txExpandedNameMap_base& mMap; michael@0: uint32_t mCurrentPos; michael@0: }; michael@0: michael@0: friend class iterator_base; michael@0: michael@0: friend class txMapItemComparator; michael@0: struct MapItem { michael@0: int32_t mNamespaceID; michael@0: nsCOMPtr mLocalName; michael@0: void* mValue; michael@0: }; michael@0: michael@0: nsTArray mItems; michael@0: }; michael@0: michael@0: template michael@0: class txExpandedNameMap : public txExpandedNameMap_base michael@0: { michael@0: public: michael@0: nsresult add(const txExpandedName& aKey, E* aValue) michael@0: { michael@0: return addItem(aKey, (void*)aValue); michael@0: } michael@0: michael@0: nsresult set(const txExpandedName& aKey, E* aValue) michael@0: { michael@0: void* oldValue; michael@0: return setItem(aKey, (void*)aValue, &oldValue); michael@0: } michael@0: michael@0: E* get(const txExpandedName& aKey) const michael@0: { michael@0: return (E*)getItem(aKey); michael@0: } michael@0: michael@0: E* remove(const txExpandedName& aKey) michael@0: { michael@0: return (E*)removeItem(aKey); michael@0: } michael@0: michael@0: void clear() michael@0: { michael@0: clearItems(); michael@0: } michael@0: michael@0: class iterator : public iterator_base michael@0: { michael@0: public: michael@0: iterator(txExpandedNameMap& aMap) michael@0: : iterator_base(aMap) michael@0: { michael@0: } michael@0: michael@0: E* value() michael@0: { michael@0: return (E*)itemValue(); michael@0: } michael@0: }; michael@0: }; michael@0: michael@0: template michael@0: class txOwningExpandedNameMap : public txExpandedNameMap_base michael@0: { michael@0: public: michael@0: ~txOwningExpandedNameMap() michael@0: { michael@0: clear(); michael@0: } michael@0: michael@0: nsresult add(const txExpandedName& aKey, E* aValue) michael@0: { michael@0: return addItem(aKey, (void*)aValue); michael@0: } michael@0: michael@0: nsresult set(const txExpandedName& aKey, E* aValue) michael@0: { michael@0: nsAutoPtr oldValue; michael@0: return setItem(aKey, (void*)aValue, getter_Transfers(oldValue)); michael@0: } michael@0: michael@0: E* get(const txExpandedName& aKey) const michael@0: { michael@0: return (E*)getItem(aKey); michael@0: } michael@0: michael@0: void remove(const txExpandedName& aKey) michael@0: { michael@0: delete (E*)removeItem(aKey); michael@0: } michael@0: michael@0: void clear() michael@0: { michael@0: uint32_t i, len = mItems.Length(); michael@0: for (i = 0; i < len; ++i) { michael@0: delete (E*)mItems[i].mValue; michael@0: } michael@0: clearItems(); michael@0: } michael@0: michael@0: class iterator : public iterator_base michael@0: { michael@0: public: michael@0: iterator(txOwningExpandedNameMap& aMap) michael@0: : iterator_base(aMap) michael@0: { michael@0: } michael@0: michael@0: E* value() michael@0: { michael@0: return (E*)itemValue(); michael@0: } michael@0: }; michael@0: }; michael@0: michael@0: #endif //TRANSFRMX_EXPANDEDNAMEMAP_H