1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/xslt/base/txExpandedNameMap.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,202 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef TRANSFRMX_EXPANDEDNAMEMAP_H 1.10 +#define TRANSFRMX_EXPANDEDNAMEMAP_H 1.11 + 1.12 +#include "nsError.h" 1.13 +#include "txXMLUtils.h" 1.14 +#include "nsTArray.h" 1.15 + 1.16 +class txExpandedNameMap_base { 1.17 +protected: 1.18 + /** 1.19 + * Adds an item, if an item with this key already exists an error is 1.20 + * returned 1.21 + * @param aKey key for item to add 1.22 + * @param aValue value of item to add 1.23 + * @return errorcode 1.24 + */ 1.25 + nsresult addItem(const txExpandedName& aKey, void* aValue); 1.26 + 1.27 + /** 1.28 + * Sets an item, if an item with this key already exists it is overwritten 1.29 + * with the new value 1.30 + * @param aKey key for item to set 1.31 + * @param aValue value of item to set 1.32 + * @return errorcode 1.33 + */ 1.34 + nsresult setItem(const txExpandedName& aKey, void* aValue, 1.35 + void** aOldValue); 1.36 + 1.37 + /** 1.38 + * Gets an item 1.39 + * @param aKey key for item to get 1.40 + * @return item with specified key, or null if no such item exists 1.41 + */ 1.42 + void* getItem(const txExpandedName& aKey) const; 1.43 + 1.44 + /** 1.45 + * Removes an item, deleting it if the map owns the values 1.46 + * @param aKey key for item to remove 1.47 + * @return item with specified key, or null if it has been deleted 1.48 + * or no such item exists 1.49 + */ 1.50 + void* removeItem(const txExpandedName& aKey); 1.51 + 1.52 + /** 1.53 + * Clears the items 1.54 + */ 1.55 + void clearItems() 1.56 + { 1.57 + mItems.Clear(); 1.58 + } 1.59 + 1.60 + class iterator_base { 1.61 + public: 1.62 + iterator_base(txExpandedNameMap_base& aMap) 1.63 + : mMap(aMap), 1.64 + mCurrentPos(uint32_t(-1)) 1.65 + { 1.66 + } 1.67 + 1.68 + bool next() 1.69 + { 1.70 + return ++mCurrentPos < mMap.mItems.Length(); 1.71 + } 1.72 + 1.73 + const txExpandedName key() 1.74 + { 1.75 + NS_ASSERTION(mCurrentPos < mMap.mItems.Length(), 1.76 + "invalid position in txExpandedNameMap::iterator"); 1.77 + return txExpandedName(mMap.mItems[mCurrentPos].mNamespaceID, 1.78 + mMap.mItems[mCurrentPos].mLocalName); 1.79 + } 1.80 + 1.81 + protected: 1.82 + void* itemValue() 1.83 + { 1.84 + NS_ASSERTION(mCurrentPos < mMap.mItems.Length(), 1.85 + "invalid position in txExpandedNameMap::iterator"); 1.86 + return mMap.mItems[mCurrentPos].mValue; 1.87 + } 1.88 + 1.89 + private: 1.90 + txExpandedNameMap_base& mMap; 1.91 + uint32_t mCurrentPos; 1.92 + }; 1.93 + 1.94 + friend class iterator_base; 1.95 + 1.96 + friend class txMapItemComparator; 1.97 + struct MapItem { 1.98 + int32_t mNamespaceID; 1.99 + nsCOMPtr<nsIAtom> mLocalName; 1.100 + void* mValue; 1.101 + }; 1.102 + 1.103 + nsTArray<MapItem> mItems; 1.104 +}; 1.105 + 1.106 +template<class E> 1.107 +class txExpandedNameMap : public txExpandedNameMap_base 1.108 +{ 1.109 +public: 1.110 + nsresult add(const txExpandedName& aKey, E* aValue) 1.111 + { 1.112 + return addItem(aKey, (void*)aValue); 1.113 + } 1.114 + 1.115 + nsresult set(const txExpandedName& aKey, E* aValue) 1.116 + { 1.117 + void* oldValue; 1.118 + return setItem(aKey, (void*)aValue, &oldValue); 1.119 + } 1.120 + 1.121 + E* get(const txExpandedName& aKey) const 1.122 + { 1.123 + return (E*)getItem(aKey); 1.124 + } 1.125 + 1.126 + E* remove(const txExpandedName& aKey) 1.127 + { 1.128 + return (E*)removeItem(aKey); 1.129 + } 1.130 + 1.131 + void clear() 1.132 + { 1.133 + clearItems(); 1.134 + } 1.135 + 1.136 + class iterator : public iterator_base 1.137 + { 1.138 + public: 1.139 + iterator(txExpandedNameMap& aMap) 1.140 + : iterator_base(aMap) 1.141 + { 1.142 + } 1.143 + 1.144 + E* value() 1.145 + { 1.146 + return (E*)itemValue(); 1.147 + } 1.148 + }; 1.149 +}; 1.150 + 1.151 +template<class E> 1.152 +class txOwningExpandedNameMap : public txExpandedNameMap_base 1.153 +{ 1.154 +public: 1.155 + ~txOwningExpandedNameMap() 1.156 + { 1.157 + clear(); 1.158 + } 1.159 + 1.160 + nsresult add(const txExpandedName& aKey, E* aValue) 1.161 + { 1.162 + return addItem(aKey, (void*)aValue); 1.163 + } 1.164 + 1.165 + nsresult set(const txExpandedName& aKey, E* aValue) 1.166 + { 1.167 + nsAutoPtr<E> oldValue; 1.168 + return setItem(aKey, (void*)aValue, getter_Transfers(oldValue)); 1.169 + } 1.170 + 1.171 + E* get(const txExpandedName& aKey) const 1.172 + { 1.173 + return (E*)getItem(aKey); 1.174 + } 1.175 + 1.176 + void remove(const txExpandedName& aKey) 1.177 + { 1.178 + delete (E*)removeItem(aKey); 1.179 + } 1.180 + 1.181 + void clear() 1.182 + { 1.183 + uint32_t i, len = mItems.Length(); 1.184 + for (i = 0; i < len; ++i) { 1.185 + delete (E*)mItems[i].mValue; 1.186 + } 1.187 + clearItems(); 1.188 + } 1.189 + 1.190 + class iterator : public iterator_base 1.191 + { 1.192 + public: 1.193 + iterator(txOwningExpandedNameMap& aMap) 1.194 + : iterator_base(aMap) 1.195 + { 1.196 + } 1.197 + 1.198 + E* value() 1.199 + { 1.200 + return (E*)itemValue(); 1.201 + } 1.202 + }; 1.203 +}; 1.204 + 1.205 +#endif //TRANSFRMX_EXPANDEDNAMEMAP_H