txExpandedNameMap.cpp (2777B)
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 #include "txExpandedNameMap.h" 7 8 #include "txCore.h" 9 10 class txMapItemComparator { 11 public: 12 bool Equals(const txExpandedNameMap_base::MapItem& aItem, 13 const txExpandedName& aKey) const { 14 return aItem.mNamespaceID == aKey.mNamespaceID && 15 aItem.mLocalName == aKey.mLocalName; 16 } 17 }; 18 19 /** 20 * Adds an item, if an item with this key already exists an error is 21 * returned 22 * @param aKey key for item to add 23 * @param aValue value of item to add 24 * @return errorcode 25 */ 26 nsresult txExpandedNameMap_base::addItem(const txExpandedName& aKey, 27 void* aValue) { 28 size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator()); 29 if (pos != mItems.NoIndex) { 30 return NS_ERROR_XSLT_ALREADY_SET; 31 } 32 33 MapItem* item = mItems.AppendElement(); 34 item->mNamespaceID = aKey.mNamespaceID; 35 item->mLocalName = aKey.mLocalName; 36 item->mValue = aValue; 37 38 return NS_OK; 39 } 40 41 /** 42 * Sets an item, if an item with this key already exists it is overwritten 43 * with the new value 44 * @param aKey key for item to set 45 * @param aValue value of item to set 46 * @return errorcode 47 */ 48 nsresult txExpandedNameMap_base::setItem(const txExpandedName& aKey, 49 void* aValue, void** aOldValue) { 50 *aOldValue = nullptr; 51 size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator()); 52 if (pos != mItems.NoIndex) { 53 *aOldValue = mItems[pos].mValue; 54 mItems[pos].mValue = aValue; 55 return NS_OK; 56 } 57 58 MapItem* item = mItems.AppendElement(); 59 item->mNamespaceID = aKey.mNamespaceID; 60 item->mLocalName = aKey.mLocalName; 61 item->mValue = aValue; 62 63 return NS_OK; 64 } 65 66 /** 67 * Gets an item 68 * @param aKey key for item to get 69 * @return item with specified key, or null if no such item exists 70 */ 71 void* txExpandedNameMap_base::getItem(const txExpandedName& aKey) const { 72 size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator()); 73 if (pos != mItems.NoIndex) { 74 return mItems[pos].mValue; 75 } 76 77 return nullptr; 78 } 79 80 /** 81 * Removes an item, deleting it if the map owns the values 82 * @param aKey key for item to remove 83 * @return item with specified key, or null if it has been deleted 84 * or no such item exists 85 */ 86 void* txExpandedNameMap_base::removeItem(const txExpandedName& aKey) { 87 void* value = nullptr; 88 size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator()); 89 if (pos != mItems.NoIndex) { 90 value = mItems[pos].mValue; 91 mItems.RemoveElementAt(pos); 92 } 93 94 return value; 95 }