Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 : #include "txCore.h"
8 :
9 : class txMapItemComparator
10 : {
11 : public:
12 633 : bool Equals(const txExpandedNameMap_base::MapItem& aItem,
13 : const txExpandedName& aKey) const {
14 1266 : return aItem.mNamespaceID == aKey.mNamespaceID &&
15 1266 : 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 123 : nsresult txExpandedNameMap_base::addItem(const txExpandedName& aKey,
27 : void* aValue)
28 : {
29 123 : size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
30 123 : if (pos != mItems.NoIndex) {
31 0 : return NS_ERROR_XSLT_ALREADY_SET;
32 : }
33 :
34 123 : MapItem* item = mItems.AppendElement();
35 123 : NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
36 :
37 123 : item->mNamespaceID = aKey.mNamespaceID;
38 123 : item->mLocalName = aKey.mLocalName;
39 123 : item->mValue = aValue;
40 :
41 123 : return NS_OK;
42 : }
43 :
44 : /**
45 : * Sets an item, if an item with this key already exists it is overwritten
46 : * with the new value
47 : * @param aKey key for item to set
48 : * @param aValue value of item to set
49 : * @return errorcode
50 : */
51 0 : nsresult txExpandedNameMap_base::setItem(const txExpandedName& aKey,
52 : void* aValue,
53 : void** aOldValue)
54 : {
55 0 : *aOldValue = nullptr;
56 0 : size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
57 0 : if (pos != mItems.NoIndex) {
58 0 : *aOldValue = mItems[pos].mValue;
59 0 : mItems[pos].mValue = aValue;
60 0 : return NS_OK;
61 : }
62 :
63 0 : MapItem* item = mItems.AppendElement();
64 0 : NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
65 :
66 0 : item->mNamespaceID = aKey.mNamespaceID;
67 0 : item->mLocalName = aKey.mLocalName;
68 0 : item->mValue = aValue;
69 :
70 0 : return NS_OK;
71 : }
72 :
73 : /**
74 : * Gets an item
75 : * @param aKey key for item to get
76 : * @return item with specified key, or null if no such item exists
77 : */
78 0 : void* txExpandedNameMap_base::getItem(const txExpandedName& aKey) const
79 : {
80 0 : size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
81 0 : if (pos != mItems.NoIndex) {
82 0 : return mItems[pos].mValue;
83 : }
84 :
85 0 : return nullptr;
86 : }
87 :
88 : /**
89 : * Removes an item, deleting it if the map owns the values
90 : * @param aKey key for item to remove
91 : * @return item with specified key, or null if it has been deleted
92 : * or no such item exists
93 : */
94 0 : void* txExpandedNameMap_base::removeItem(const txExpandedName& aKey)
95 : {
96 0 : void* value = nullptr;
97 0 : size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
98 0 : if (pos != mItems.NoIndex) {
99 0 : value = mItems[pos].mValue;
100 0 : mItems.RemoveElementAt(pos);
101 : }
102 :
103 0 : return value;
104 : }
|