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: #include "txExpandedNameMap.h" michael@0: #include "txCore.h" michael@0: michael@0: class txMapItemComparator michael@0: { michael@0: public: michael@0: bool Equals(const txExpandedNameMap_base::MapItem& aItem, michael@0: const txExpandedName& aKey) const { michael@0: return aItem.mNamespaceID == aKey.mNamespaceID && michael@0: aItem.mLocalName == aKey.mLocalName; michael@0: } michael@0: }; michael@0: 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 txExpandedNameMap_base::addItem(const txExpandedName& aKey, michael@0: void* aValue) michael@0: { michael@0: uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator()); michael@0: if (pos != mItems.NoIndex) { michael@0: return NS_ERROR_XSLT_ALREADY_SET; michael@0: } michael@0: michael@0: MapItem* item = mItems.AppendElement(); michael@0: NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: item->mNamespaceID = aKey.mNamespaceID; michael@0: item->mLocalName = aKey.mLocalName; michael@0: item->mValue = aValue; michael@0: michael@0: return NS_OK; michael@0: } 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 txExpandedNameMap_base::setItem(const txExpandedName& aKey, michael@0: void* aValue, michael@0: void** aOldValue) michael@0: { michael@0: *aOldValue = nullptr; michael@0: uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator()); michael@0: if (pos != mItems.NoIndex) { michael@0: *aOldValue = mItems[pos].mValue; michael@0: mItems[pos].mValue = aValue; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: MapItem* item = mItems.AppendElement(); michael@0: NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY); michael@0: michael@0: item->mNamespaceID = aKey.mNamespaceID; michael@0: item->mLocalName = aKey.mLocalName; michael@0: item->mValue = aValue; michael@0: michael@0: return NS_OK; michael@0: } 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* txExpandedNameMap_base::getItem(const txExpandedName& aKey) const michael@0: { michael@0: uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator()); michael@0: if (pos != mItems.NoIndex) { michael@0: return mItems[pos].mValue; michael@0: } michael@0: michael@0: return nullptr; michael@0: } 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* txExpandedNameMap_base::removeItem(const txExpandedName& aKey) michael@0: { michael@0: void* value = nullptr; michael@0: uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator()); michael@0: if (pos != mItems.NoIndex) { michael@0: value = mItems[pos].mValue; michael@0: mItems.RemoveElementAt(pos); michael@0: } michael@0: michael@0: return value; michael@0: }