1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsRefPtrHashtable.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,159 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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 nsRefPtrHashtable_h__ 1.10 +#define nsRefPtrHashtable_h__ 1.11 + 1.12 +#include "nsBaseHashtable.h" 1.13 +#include "nsHashKeys.h" 1.14 +#include "nsAutoPtr.h" 1.15 + 1.16 +/** 1.17 + * templated hashtable class maps keys to reference pointers. 1.18 + * See nsBaseHashtable for complete declaration. 1.19 + * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h 1.20 + * for a complete specification. 1.21 + * @param RefPtr the reference-type being wrapped 1.22 + * @see nsDataHashtable, nsClassHashtable 1.23 + */ 1.24 +template<class KeyClass, class RefPtr> 1.25 +class nsRefPtrHashtable : 1.26 + public nsBaseHashtable< KeyClass, nsRefPtr<RefPtr> , RefPtr* > 1.27 +{ 1.28 +public: 1.29 + typedef typename KeyClass::KeyType KeyType; 1.30 + typedef RefPtr* UserDataType; 1.31 + typedef nsBaseHashtable< KeyClass, nsRefPtr<RefPtr> , RefPtr* > base_type; 1.32 + 1.33 + nsRefPtrHashtable() 1.34 + { 1.35 + } 1.36 + explicit nsRefPtrHashtable(uint32_t aInitSize) 1.37 + : nsBaseHashtable<KeyClass,nsRefPtr<RefPtr>,RefPtr*>(aInitSize) 1.38 + { 1.39 + } 1.40 + 1.41 + /** 1.42 + * @copydoc nsBaseHashtable::Get 1.43 + * @param pData This is an XPCOM getter, so pData is already_addrefed. 1.44 + * If the key doesn't exist, pData will be set to nullptr. 1.45 + */ 1.46 + bool Get(KeyType aKey, UserDataType* pData) const; 1.47 + 1.48 + /** 1.49 + * Gets a weak reference to the hashtable entry. 1.50 + * @param aFound If not nullptr, will be set to true if the entry is found, 1.51 + * to false otherwise. 1.52 + * @return The entry, or nullptr if not found. Do not release this pointer! 1.53 + */ 1.54 + RefPtr* GetWeak(KeyType aKey, bool* aFound = nullptr) const; 1.55 + 1.56 + // Overload Remove, rather than overriding it. 1.57 + using base_type::Remove; 1.58 + 1.59 + /** 1.60 + * Remove the data for the associated key, swapping the current value into 1.61 + * pData, thereby avoiding calls to AddRef and Release. 1.62 + * @param aKey the key to remove from the hashtable 1.63 + * @param pData This is an XPCOM getter, so pData is already_addrefed. 1.64 + * If the key doesn't exist, pData will be set to nullptr. Must be non-null. 1.65 + */ 1.66 + bool Remove(KeyType aKey, UserDataType* pData); 1.67 +}; 1.68 + 1.69 +template <typename K, typename T> 1.70 +inline void 1.71 +ImplCycleCollectionUnlink(nsRefPtrHashtable<K, T>& aField) 1.72 +{ 1.73 + aField.Clear(); 1.74 +} 1.75 + 1.76 +template <typename K, typename T> 1.77 +inline void 1.78 +ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback, 1.79 + nsRefPtrHashtable<K, T>& aField, 1.80 + const char* aName, 1.81 + uint32_t aFlags = 0) 1.82 +{ 1.83 + nsBaseHashtableCCTraversalData userData(aCallback, aName, aFlags); 1.84 + 1.85 + aField.EnumerateRead(ImplCycleCollectionTraverse_EnumFunc<typename K::KeyType,T*>, 1.86 + &userData); 1.87 +} 1.88 + 1.89 +// 1.90 +// nsRefPtrHashtable definitions 1.91 +// 1.92 + 1.93 +template<class KeyClass, class RefPtr> 1.94 +bool 1.95 +nsRefPtrHashtable<KeyClass,RefPtr>::Get 1.96 + (KeyType aKey, UserDataType* pRefPtr) const 1.97 +{ 1.98 + typename base_type::EntryType* ent = this->GetEntry(aKey); 1.99 + 1.100 + if (ent) { 1.101 + if (pRefPtr) { 1.102 + *pRefPtr = ent->mData; 1.103 + 1.104 + NS_IF_ADDREF(*pRefPtr); 1.105 + } 1.106 + 1.107 + return true; 1.108 + } 1.109 + 1.110 + // if the key doesn't exist, set *pRefPtr to null 1.111 + // so that it is a valid XPCOM getter 1.112 + if (pRefPtr) { 1.113 + *pRefPtr = nullptr; 1.114 + } 1.115 + 1.116 + return false; 1.117 +} 1.118 + 1.119 +template<class KeyClass, class RefPtr> 1.120 +RefPtr* 1.121 +nsRefPtrHashtable<KeyClass,RefPtr>::GetWeak 1.122 + (KeyType aKey, bool* aFound) const 1.123 +{ 1.124 + typename base_type::EntryType* ent = this->GetEntry(aKey); 1.125 + 1.126 + if (ent) { 1.127 + if (aFound) { 1.128 + *aFound = true; 1.129 + } 1.130 + 1.131 + return ent->mData; 1.132 + } 1.133 + 1.134 + // Key does not exist, return nullptr and set aFound to false 1.135 + if (aFound) { 1.136 + *aFound = false; 1.137 + } 1.138 + 1.139 + return nullptr; 1.140 +} 1.141 + 1.142 +template<class KeyClass, class RefPtr> 1.143 +bool 1.144 +nsRefPtrHashtable<KeyClass,RefPtr>::Remove(KeyType aKey, 1.145 + UserDataType* pRefPtr) 1.146 +{ 1.147 + MOZ_ASSERT(pRefPtr); 1.148 + typename base_type::EntryType* ent = this->GetEntry(aKey); 1.149 + 1.150 + if (ent) { 1.151 + ent->mData.forget(pRefPtr); 1.152 + this->Remove(aKey); 1.153 + return true; 1.154 + } 1.155 + 1.156 + // If the key doesn't exist, set *pRefPtr to null 1.157 + // so that it is a valid XPCOM getter. 1.158 + *pRefPtr = nullptr; 1.159 + return false; 1.160 +} 1.161 + 1.162 +#endif // nsRefPtrHashtable_h__