dom/xslt/base/txExpandedNameMap.cpp

branch
TOR_BUG_9701
changeset 8
97036ab72558
equal deleted inserted replaced
-1:000000000000 0:4180b89de0cf
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 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 {
29 uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
30 if (pos != mItems.NoIndex) {
31 return NS_ERROR_XSLT_ALREADY_SET;
32 }
33
34 MapItem* item = mItems.AppendElement();
35 NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
36
37 item->mNamespaceID = aKey.mNamespaceID;
38 item->mLocalName = aKey.mLocalName;
39 item->mValue = aValue;
40
41 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 nsresult txExpandedNameMap_base::setItem(const txExpandedName& aKey,
52 void* aValue,
53 void** aOldValue)
54 {
55 *aOldValue = nullptr;
56 uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
57 if (pos != mItems.NoIndex) {
58 *aOldValue = mItems[pos].mValue;
59 mItems[pos].mValue = aValue;
60
61 return NS_OK;
62 }
63
64 MapItem* item = mItems.AppendElement();
65 NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
66
67 item->mNamespaceID = aKey.mNamespaceID;
68 item->mLocalName = aKey.mLocalName;
69 item->mValue = aValue;
70
71 return NS_OK;
72 }
73
74 /**
75 * Gets an item
76 * @param aKey key for item to get
77 * @return item with specified key, or null if no such item exists
78 */
79 void* txExpandedNameMap_base::getItem(const txExpandedName& aKey) const
80 {
81 uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
82 if (pos != mItems.NoIndex) {
83 return mItems[pos].mValue;
84 }
85
86 return nullptr;
87 }
88
89 /**
90 * Removes an item, deleting it if the map owns the values
91 * @param aKey key for item to remove
92 * @return item with specified key, or null if it has been deleted
93 * or no such item exists
94 */
95 void* txExpandedNameMap_base::removeItem(const txExpandedName& aKey)
96 {
97 void* value = nullptr;
98 uint32_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
99 if (pos != mItems.NoIndex) {
100 value = mItems[pos].mValue;
101 mItems.RemoveElementAt(pos);
102 }
103
104 return value;
105 }

mercurial