Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/xslt/base/txExpandedNameMap.cpp
Line
Count
Source (jump to first uncovered line)
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
633
                  const txExpandedName& aKey) const {
14
633
      return aItem.mNamespaceID == aKey.mNamespaceID &&
15
633
             aItem.mLocalName == aKey.mLocalName;
16
633
    }
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
123
{
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
0
    }
33
123
34
123
    MapItem* item = mItems.AppendElement();
35
123
    NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
36
123
37
123
    item->mNamespaceID = aKey.mNamespaceID;
38
123
    item->mLocalName = aKey.mLocalName;
39
123
    item->mValue = aValue;
40
123
41
123
    return NS_OK;
42
123
}
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
0
{
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
0
    }
62
0
63
0
    MapItem* item = mItems.AppendElement();
64
0
    NS_ENSURE_TRUE(item, NS_ERROR_OUT_OF_MEMORY);
65
0
66
0
    item->mNamespaceID = aKey.mNamespaceID;
67
0
    item->mLocalName = aKey.mLocalName;
68
0
    item->mValue = aValue;
69
0
70
0
    return NS_OK;
71
0
}
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
void* txExpandedNameMap_base::getItem(const txExpandedName& aKey) const
79
0
{
80
0
    size_t pos = mItems.IndexOf(aKey, 0, txMapItemComparator());
81
0
    if (pos != mItems.NoIndex) {
82
0
        return mItems[pos].mValue;
83
0
    }
84
0
85
0
    return nullptr;
86
0
}
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
void* txExpandedNameMap_base::removeItem(const txExpandedName& aKey)
95
0
{
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
0
    }
102
0
103
0
    return value;
104
0
}