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
11 #include "txExpandedName.h"
14 class txExpandedNameMap_base
{
17 * Adds an item, if an item with this key already exists an error is
19 * @param aKey key for item to add
20 * @param aValue value of item to add
23 nsresult
addItem(const txExpandedName
& aKey
, void* aValue
);
26 * Sets an item, if an item with this key already exists it is overwritten
28 * @param aKey key for item to set
29 * @param aValue value of item to set
32 nsresult
setItem(const txExpandedName
& aKey
, void* aValue
,
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;
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
);
60 explicit iterator_base(txExpandedNameMap_base
& aMap
)
62 mCurrentPos(uint32_t(-1))
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
);
82 NS_ASSERTION(mCurrentPos
< mMap
.mItems
.Length(),
83 "invalid position in txExpandedNameMap::iterator");
84 return mMap
.mItems
[mCurrentPos
].mValue
;
88 txExpandedNameMap_base
& mMap
;
92 friend class iterator_base
;
94 friend class txMapItemComparator
;
97 nsCOMPtr
<nsIAtom
> mLocalName
;
101 nsTArray
<MapItem
> mItems
;
105 class txExpandedNameMap
: public txExpandedNameMap_base
108 nsresult
add(const txExpandedName
& aKey
, E
* aValue
)
110 return addItem(aKey
, (void*)aValue
);
113 nsresult
set(const txExpandedName
& aKey
, E
* aValue
)
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
);
134 class iterator
: public iterator_base
137 explicit iterator(txExpandedNameMap
& aMap
)
138 : iterator_base(aMap
)
144 return (E
*)itemValue();
150 class txOwningExpandedNameMap
: public txExpandedNameMap_base
153 ~txOwningExpandedNameMap()
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
);
181 uint32_t i
, len
= mItems
.Length();
182 for (i
= 0; i
< len
; ++i
) {
183 delete (E
*)mItems
[i
].mValue
;
188 class iterator
: public iterator_base
191 explicit iterator(txOwningExpandedNameMap
& aMap
)
192 : iterator_base(aMap
)
198 return (E
*)itemValue();
203 #endif //TRANSFRMX_EXPANDEDNAMEMAP_H