txExpandedNameMap.h 4.7 KB

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