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 : #ifndef TRANSFRMX_EXPANDEDNAMEMAP_H
7 : #define TRANSFRMX_EXPANDEDNAMEMAP_H
8 :
9 : #include "nsAutoPtr.h"
10 : #include "nsError.h"
11 : #include "txExpandedName.h"
12 : #include "nsTArray.h"
13 :
14 48 : class txExpandedNameMap_base {
15 : protected:
16 : /**
17 : * Adds an item, if an item with this key already exists an error is
18 : * returned
19 : * @param aKey key for item to add
20 : * @param aValue value of item to add
21 : * @return errorcode
22 : */
23 : nsresult addItem(const txExpandedName& aKey, void* aValue);
24 :
25 : /**
26 : * Sets an item, if an item with this key already exists it is overwritten
27 : * with the new value
28 : * @param aKey key for item to set
29 : * @param aValue value of item to set
30 : * @return errorcode
31 : */
32 : nsresult setItem(const txExpandedName& aKey, void* aValue,
33 : void** aOldValue);
34 :
35 : /**
36 : * Gets an item
37 : * @param aKey key for item to get
38 : * @return item with specified key, or null if no such item exists
39 : */
40 : void* getItem(const txExpandedName& aKey) const;
41 :
42 : /**
43 : * Removes an item, deleting it if the map owns the values
44 : * @param aKey key for item to remove
45 : * @return item with specified key, or null if it has been deleted
46 : * or no such item exists
47 : */
48 : void* removeItem(const txExpandedName& aKey);
49 :
50 : /**
51 : * Clears the items
52 : */
53 0 : void clearItems()
54 : {
55 0 : mItems.Clear();
56 0 : }
57 :
58 : class iterator_base {
59 : public:
60 0 : explicit iterator_base(txExpandedNameMap_base& aMap)
61 0 : : mMap(aMap),
62 0 : mCurrentPos(uint32_t(-1))
63 : {
64 0 : }
65 :
66 0 : bool next()
67 : {
68 0 : return ++mCurrentPos < mMap.mItems.Length();
69 : }
70 :
71 : const txExpandedName key()
72 : {
73 : NS_ASSERTION(mCurrentPos < mMap.mItems.Length(),
74 : "invalid position in txExpandedNameMap::iterator");
75 : return txExpandedName(mMap.mItems[mCurrentPos].mNamespaceID,
76 : mMap.mItems[mCurrentPos].mLocalName);
77 : }
78 :
79 : protected:
80 0 : void* itemValue()
81 : {
82 0 : NS_ASSERTION(mCurrentPos < mMap.mItems.Length(),
83 : "invalid position in txExpandedNameMap::iterator");
84 0 : return mMap.mItems[mCurrentPos].mValue;
85 : }
86 :
87 : private:
88 : txExpandedNameMap_base& mMap;
89 : uint32_t mCurrentPos;
90 : };
91 :
92 : friend class iterator_base;
93 :
94 : friend class txMapItemComparator;
95 123 : struct MapItem {
96 : int32_t mNamespaceID;
97 : nsCOMPtr<nsIAtom> mLocalName;
98 : void* mValue;
99 : };
100 :
101 : nsTArray<MapItem> mItems;
102 : };
103 :
104 : template<class E>
105 48 : class txExpandedNameMap : public txExpandedNameMap_base
106 : {
107 : public:
108 123 : nsresult add(const txExpandedName& aKey, E* aValue)
109 : {
110 123 : return addItem(aKey, (void*)aValue);
111 : }
112 :
113 0 : nsresult set(const txExpandedName& aKey, E* aValue)
114 : {
115 : void* oldValue;
116 0 : return setItem(aKey, (void*)aValue, &oldValue);
117 : }
118 :
119 0 : E* get(const txExpandedName& aKey) const
120 : {
121 0 : return (E*)getItem(aKey);
122 : }
123 :
124 0 : E* remove(const txExpandedName& aKey)
125 : {
126 0 : return (E*)removeItem(aKey);
127 : }
128 :
129 : void clear()
130 : {
131 : clearItems();
132 : }
133 :
134 : class iterator : public iterator_base
135 : {
136 : public:
137 0 : explicit iterator(txExpandedNameMap& aMap)
138 0 : : iterator_base(aMap)
139 : {
140 0 : }
141 :
142 0 : E* value()
143 : {
144 0 : return (E*)itemValue();
145 : }
146 : };
147 : };
148 :
149 : template<class E>
150 0 : class txOwningExpandedNameMap : public txExpandedNameMap_base
151 : {
152 : public:
153 0 : ~txOwningExpandedNameMap()
154 : {
155 0 : clear();
156 0 : }
157 :
158 0 : nsresult add(const txExpandedName& aKey, E* aValue)
159 : {
160 0 : return addItem(aKey, (void*)aValue);
161 : }
162 :
163 0 : nsresult set(const txExpandedName& aKey, E* aValue)
164 : {
165 0 : nsAutoPtr<E> oldValue;
166 0 : return setItem(aKey, (void*)aValue, getter_Transfers(oldValue));
167 : }
168 :
169 0 : E* get(const txExpandedName& aKey) const
170 : {
171 0 : return (E*)getItem(aKey);
172 : }
173 :
174 0 : void remove(const txExpandedName& aKey)
175 : {
176 0 : delete (E*)removeItem(aKey);
177 0 : }
178 :
179 0 : void clear()
180 : {
181 0 : uint32_t i, len = mItems.Length();
182 0 : for (i = 0; i < len; ++i) {
183 0 : delete (E*)mItems[i].mValue;
184 : }
185 0 : clearItems();
186 0 : }
187 :
188 : class iterator : public iterator_base
189 : {
190 : public:
191 0 : explicit iterator(txOwningExpandedNameMap& aMap)
192 0 : : iterator_base(aMap)
193 : {
194 0 : }
195 :
196 0 : E* value()
197 : {
198 0 : return (E*)itemValue();
199 : }
200 : };
201 : };
202 :
203 : #endif //TRANSFRMX_EXPANDEDNAMEMAP_H
|