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"
9 class txMapItemComparator
{
11 bool Equals(const txExpandedNameMap_base::MapItem
& aItem
,
12 const txExpandedName
& aKey
) const {
13 return aItem
.mNamespaceID
== aKey
.mNamespaceID
&&
14 aItem
.mLocalName
== aKey
.mLocalName
;
19 * Adds an item, if an item with this key already exists an error is
21 * @param aKey key for item to add
22 * @param aValue value of item to add
25 nsresult
txExpandedNameMap_base::addItem(const txExpandedName
& aKey
,
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
;
41 * Sets an item, if an item with this key already exists it is overwritten
43 * @param aKey key for item to set
44 * @param aValue value of item to set
47 nsresult
txExpandedNameMap_base::setItem(const txExpandedName
& aKey
,
48 void* aValue
, void** aOldValue
) {
50 size_t pos
= mItems
.IndexOf(aKey
, 0, txMapItemComparator());
51 if (pos
!= mItems
.NoIndex
) {
52 *aOldValue
= mItems
[pos
].mValue
;
53 mItems
[pos
].mValue
= aValue
;
57 MapItem
* item
= mItems
.AppendElement();
58 item
->mNamespaceID
= aKey
.mNamespaceID
;
59 item
->mLocalName
= aKey
.mLocalName
;
60 item
->mValue
= aValue
;
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
;
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
);