Bumping manifests a=b2g-bump
[gecko.git] / dom / xslt / base / txExpandedNameMap.h
blobde0861427fc6ea0a1d53ac305901dda0fffb262a
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/. */
6 #ifndef TRANSFRMX_EXPANDEDNAMEMAP_H
7 #define TRANSFRMX_EXPANDEDNAMEMAP_H
9 #include "nsAutoPtr.h"
10 #include "nsError.h"
11 #include "txExpandedName.h"
12 #include "nsTArray.h"
14 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
23 nsresult addItem(const txExpandedName& aKey, void* aValue);
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
32 nsresult setItem(const txExpandedName& aKey, void* aValue,
33 void** aOldValue);
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
40 void* getItem(const txExpandedName& aKey) const;
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
48 void* removeItem(const txExpandedName& aKey);
50 /**
51 * Clears the items
53 void clearItems()
55 mItems.Clear();
58 class iterator_base {
59 public:
60 explicit iterator_base(txExpandedNameMap_base& aMap)
61 : mMap(aMap),
62 mCurrentPos(uint32_t(-1))
66 bool next()
68 return ++mCurrentPos < mMap.mItems.Length();
71 const txExpandedName key()
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);
79 protected:
80 void* itemValue()
82 NS_ASSERTION(mCurrentPos < mMap.mItems.Length(),
83 "invalid position in txExpandedNameMap::iterator");
84 return mMap.mItems[mCurrentPos].mValue;
87 private:
88 txExpandedNameMap_base& mMap;
89 uint32_t mCurrentPos;
92 friend class iterator_base;
94 friend class txMapItemComparator;
95 struct MapItem {
96 int32_t mNamespaceID;
97 nsCOMPtr<nsIAtom> mLocalName;
98 void* mValue;
101 nsTArray<MapItem> mItems;
104 template<class E>
105 class txExpandedNameMap : public txExpandedNameMap_base
107 public:
108 nsresult add(const txExpandedName& aKey, E* aValue)
110 return addItem(aKey, (void*)aValue);
113 nsresult set(const txExpandedName& aKey, E* aValue)
115 void* oldValue;
116 return setItem(aKey, (void*)aValue, &oldValue);
119 E* get(const txExpandedName& aKey) const
121 return (E*)getItem(aKey);
124 E* remove(const txExpandedName& aKey)
126 return (E*)removeItem(aKey);
129 void clear()
131 clearItems();
134 class iterator : public iterator_base
136 public:
137 explicit iterator(txExpandedNameMap& aMap)
138 : iterator_base(aMap)
142 E* value()
144 return (E*)itemValue();
149 template<class E>
150 class txOwningExpandedNameMap : public txExpandedNameMap_base
152 public:
153 ~txOwningExpandedNameMap()
155 clear();
158 nsresult add(const txExpandedName& aKey, E* aValue)
160 return addItem(aKey, (void*)aValue);
163 nsresult set(const txExpandedName& aKey, E* aValue)
165 nsAutoPtr<E> oldValue;
166 return setItem(aKey, (void*)aValue, getter_Transfers(oldValue));
169 E* get(const txExpandedName& aKey) const
171 return (E*)getItem(aKey);
174 void remove(const txExpandedName& aKey)
176 delete (E*)removeItem(aKey);
179 void clear()
181 uint32_t i, len = mItems.Length();
182 for (i = 0; i < len; ++i) {
183 delete (E*)mItems[i].mValue;
185 clearItems();
188 class iterator : public iterator_base
190 public:
191 explicit iterator(txOwningExpandedNameMap& aMap)
192 : iterator_base(aMap)
196 E* value()
198 return (E*)itemValue();
203 #endif //TRANSFRMX_EXPANDEDNAMEMAP_H