Backed out changeset 9d8b4c0b99ed (bug 1945683) for causing btime failures. CLOSED...
[gecko.git] / dom / xslt / base / txExpandedNameMap.cpp
blob996799abf0134076532094c64b1c12c1eb932590
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/. */
6 #include "txExpandedNameMap.h"
7 #include "txCore.h"
9 class txMapItemComparator {
10 public:
11 bool Equals(const txExpandedNameMap_base::MapItem& aItem,
12 const txExpandedName& aKey) const {
13 return aItem.mNamespaceID == aKey.mNamespaceID &&
14 aItem.mLocalName == aKey.mLocalName;
18 /**
19 * Adds an item, if an item with this key already exists an error is
20 * returned
21 * @param aKey key for item to add
22 * @param aValue value of item to add
23 * @return errorcode
25 nsresult txExpandedNameMap_base::addItem(const txExpandedName& aKey,
26 void* aValue) {
27 size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
28 if (pos != mItems.NoIndex) {
29 return NS_ERROR_XSLT_ALREADY_SET;
32 MapItem* item = mItems.AppendElement();
33 item->mNamespaceID = aKey.mNamespaceID;
34 item->mLocalName = aKey.mLocalName;
35 item->mValue = aValue;
37 return NS_OK;
40 /**
41 * Sets an item, if an item with this key already exists it is overwritten
42 * with the new value
43 * @param aKey key for item to set
44 * @param aValue value of item to set
45 * @return errorcode
47 nsresult txExpandedNameMap_base::setItem(const txExpandedName& aKey,
48 void* aValue, void** aOldValue) {
49 *aOldValue = nullptr;
50 size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
51 if (pos != mItems.NoIndex) {
52 *aOldValue = mItems[pos].mValue;
53 mItems[pos].mValue = aValue;
54 return NS_OK;
57 MapItem* item = mItems.AppendElement();
58 item->mNamespaceID = aKey.mNamespaceID;
59 item->mLocalName = aKey.mLocalName;
60 item->mValue = aValue;
62 return NS_OK;
65 /**
66 * Gets an item
67 * @param aKey key for item to get
68 * @return item with specified key, or null if no such item exists
70 void* txExpandedNameMap_base::getItem(const txExpandedName& aKey) const {
71 size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
72 if (pos != mItems.NoIndex) {
73 return mItems[pos].mValue;
76 return nullptr;
79 /**
80 * Removes an item, deleting it if the map owns the values
81 * @param aKey key for item to remove
82 * @return item with specified key, or null if it has been deleted
83 * or no such item exists
85 void* txExpandedNameMap_base::removeItem(const txExpandedName& aKey) {
86 void* value = nullptr;
87 size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
88 if (pos != mItems.NoIndex) {
89 value = mItems[pos].mValue;
90 mItems.RemoveElementAt(pos);
93 return value;