1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Jonas Sicking.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jonas Sicking <sicking@bigfoot.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef TRANSFRMX_EXPANDEDNAMEMAP_H
40 : #define TRANSFRMX_EXPANDEDNAMEMAP_H
41 :
42 : #include "txError.h"
43 : #include "txXMLUtils.h"
44 : #include "nsTArray.h"
45 :
46 44912 : class txExpandedNameMap_base {
47 : protected:
48 : /**
49 : * Adds an item, if an item with this key already exists an error is
50 : * returned
51 : * @param aKey key for item to add
52 : * @param aValue value of item to add
53 : * @return errorcode
54 : */
55 : nsresult addItem(const txExpandedName& aKey, void* aValue);
56 :
57 : /**
58 : * Sets an item, if an item with this key already exists it is overwritten
59 : * with the new value
60 : * @param aKey key for item to set
61 : * @param aValue value of item to set
62 : * @return errorcode
63 : */
64 : nsresult setItem(const txExpandedName& aKey, void* aValue,
65 : void** aOldValue);
66 :
67 : /**
68 : * Gets an item
69 : * @param aKey key for item to get
70 : * @return item with specified key, or null if no such item exists
71 : */
72 : void* getItem(const txExpandedName& aKey) const;
73 :
74 : /**
75 : * Removes an item, deleting it if the map owns the values
76 : * @param aKey key for item to remove
77 : * @return item with specified key, or null if it has been deleted
78 : * or no such item exists
79 : */
80 : void* removeItem(const txExpandedName& aKey);
81 :
82 : /**
83 : * Clears the items
84 : */
85 0 : void clearItems()
86 : {
87 0 : mItems.Clear();
88 0 : }
89 :
90 : class iterator_base {
91 : public:
92 0 : iterator_base(txExpandedNameMap_base& aMap)
93 : : mMap(aMap),
94 0 : mCurrentPos(PRUint32(-1))
95 : {
96 0 : }
97 :
98 0 : bool next()
99 : {
100 0 : return ++mCurrentPos < mMap.mItems.Length();
101 : }
102 :
103 : const txExpandedName key()
104 : {
105 : NS_ASSERTION(mCurrentPos >= 0 &&
106 : mCurrentPos < mMap.mItems.Length(),
107 : "invalid position in txExpandedNameMap::iterator");
108 : return txExpandedName(mMap.mItems[mCurrentPos].mNamespaceID,
109 : mMap.mItems[mCurrentPos].mLocalName);
110 : }
111 :
112 : protected:
113 0 : void* itemValue()
114 : {
115 0 : NS_ASSERTION(mCurrentPos >= 0 &&
116 : mCurrentPos < mMap.mItems.Length(),
117 : "invalid position in txExpandedNameMap::iterator");
118 0 : return mMap.mItems[mCurrentPos].mValue;
119 : }
120 :
121 : private:
122 : txExpandedNameMap_base& mMap;
123 : PRUint32 mCurrentPos;
124 : };
125 :
126 : friend class iterator_base;
127 :
128 : friend class txMapItemComparator;
129 115087 : struct MapItem {
130 : PRInt32 mNamespaceID;
131 : nsCOMPtr<nsIAtom> mLocalName;
132 : void* mValue;
133 : };
134 :
135 : nsTArray<MapItem> mItems;
136 : };
137 :
138 : template<class E>
139 : class txExpandedNameMap : public txExpandedNameMap_base
140 44912 : {
141 : public:
142 57564 : nsresult add(const txExpandedName& aKey, E* aValue)
143 : {
144 57564 : return addItem(aKey, (void*)aValue);
145 : }
146 :
147 0 : nsresult set(const txExpandedName& aKey, E* aValue)
148 : {
149 : void* oldValue;
150 0 : return setItem(aKey, (void*)aValue, &oldValue);
151 : }
152 :
153 0 : E* get(const txExpandedName& aKey) const
154 : {
155 0 : return (E*)getItem(aKey);
156 : }
157 :
158 0 : E* remove(const txExpandedName& aKey)
159 : {
160 0 : return (E*)removeItem(aKey);
161 : }
162 :
163 : void clear()
164 : {
165 : clearItems();
166 : }
167 :
168 : class iterator : public iterator_base
169 : {
170 : public:
171 0 : iterator(txExpandedNameMap& aMap)
172 0 : : iterator_base(aMap)
173 : {
174 0 : }
175 :
176 0 : E* value()
177 : {
178 0 : return (E*)itemValue();
179 : }
180 : };
181 : };
182 :
183 : template<class E>
184 : class txOwningExpandedNameMap : public txExpandedNameMap_base
185 0 : {
186 : public:
187 0 : ~txOwningExpandedNameMap()
188 : {
189 0 : clear();
190 0 : }
191 :
192 0 : nsresult add(const txExpandedName& aKey, E* aValue)
193 : {
194 0 : return addItem(aKey, (void*)aValue);
195 : }
196 :
197 0 : nsresult set(const txExpandedName& aKey, E* aValue)
198 : {
199 0 : nsAutoPtr<E> oldValue;
200 0 : return setItem(aKey, (void*)aValue, getter_Transfers(oldValue));
201 : }
202 :
203 0 : E* get(const txExpandedName& aKey) const
204 : {
205 0 : return (E*)getItem(aKey);
206 : }
207 :
208 0 : void remove(const txExpandedName& aKey)
209 : {
210 0 : delete (E*)removeItem(aKey);
211 0 : }
212 :
213 0 : void clear()
214 : {
215 0 : PRUint32 i, len = mItems.Length();
216 0 : for (i = 0; i < len; ++i) {
217 0 : delete (E*)mItems[i].mValue;
218 : }
219 0 : clearItems();
220 0 : }
221 :
222 : class iterator : public iterator_base
223 : {
224 : public:
225 0 : iterator(txOwningExpandedNameMap& aMap)
226 0 : : iterator_base(aMap)
227 : {
228 0 : }
229 :
230 0 : E* value()
231 : {
232 0 : return (E*)itemValue();
233 : }
234 : };
235 : };
236 :
237 : #endif //TRANSFRMX_EXPANDEDNAMEMAP_H
|