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