michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsRefPtrHashtable_h__ michael@0: #define nsRefPtrHashtable_h__ michael@0: michael@0: #include "nsBaseHashtable.h" michael@0: #include "nsHashKeys.h" michael@0: #include "nsAutoPtr.h" michael@0: michael@0: /** michael@0: * templated hashtable class maps keys to reference pointers. michael@0: * See nsBaseHashtable for complete declaration. michael@0: * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h michael@0: * for a complete specification. michael@0: * @param RefPtr the reference-type being wrapped michael@0: * @see nsDataHashtable, nsClassHashtable michael@0: */ michael@0: template michael@0: class nsRefPtrHashtable : michael@0: public nsBaseHashtable< KeyClass, nsRefPtr , RefPtr* > michael@0: { michael@0: public: michael@0: typedef typename KeyClass::KeyType KeyType; michael@0: typedef RefPtr* UserDataType; michael@0: typedef nsBaseHashtable< KeyClass, nsRefPtr , RefPtr* > base_type; michael@0: michael@0: nsRefPtrHashtable() michael@0: { michael@0: } michael@0: explicit nsRefPtrHashtable(uint32_t aInitSize) michael@0: : nsBaseHashtable,RefPtr*>(aInitSize) michael@0: { michael@0: } michael@0: michael@0: /** michael@0: * @copydoc nsBaseHashtable::Get michael@0: * @param pData This is an XPCOM getter, so pData is already_addrefed. michael@0: * If the key doesn't exist, pData will be set to nullptr. michael@0: */ michael@0: bool Get(KeyType aKey, UserDataType* pData) const; michael@0: michael@0: /** michael@0: * Gets a weak reference to the hashtable entry. michael@0: * @param aFound If not nullptr, will be set to true if the entry is found, michael@0: * to false otherwise. michael@0: * @return The entry, or nullptr if not found. Do not release this pointer! michael@0: */ michael@0: RefPtr* GetWeak(KeyType aKey, bool* aFound = nullptr) const; michael@0: michael@0: // Overload Remove, rather than overriding it. michael@0: using base_type::Remove; michael@0: michael@0: /** michael@0: * Remove the data for the associated key, swapping the current value into michael@0: * pData, thereby avoiding calls to AddRef and Release. michael@0: * @param aKey the key to remove from the hashtable michael@0: * @param pData This is an XPCOM getter, so pData is already_addrefed. michael@0: * If the key doesn't exist, pData will be set to nullptr. Must be non-null. michael@0: */ michael@0: bool Remove(KeyType aKey, UserDataType* pData); michael@0: }; michael@0: michael@0: template michael@0: inline void michael@0: ImplCycleCollectionUnlink(nsRefPtrHashtable& aField) michael@0: { michael@0: aField.Clear(); michael@0: } michael@0: michael@0: template michael@0: inline void michael@0: ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback, michael@0: nsRefPtrHashtable& aField, michael@0: const char* aName, michael@0: uint32_t aFlags = 0) michael@0: { michael@0: nsBaseHashtableCCTraversalData userData(aCallback, aName, aFlags); michael@0: michael@0: aField.EnumerateRead(ImplCycleCollectionTraverse_EnumFunc, michael@0: &userData); michael@0: } michael@0: michael@0: // michael@0: // nsRefPtrHashtable definitions michael@0: // michael@0: michael@0: template michael@0: bool michael@0: nsRefPtrHashtable::Get michael@0: (KeyType aKey, UserDataType* pRefPtr) const michael@0: { michael@0: typename base_type::EntryType* ent = this->GetEntry(aKey); michael@0: michael@0: if (ent) { michael@0: if (pRefPtr) { michael@0: *pRefPtr = ent->mData; michael@0: michael@0: NS_IF_ADDREF(*pRefPtr); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // if the key doesn't exist, set *pRefPtr to null michael@0: // so that it is a valid XPCOM getter michael@0: if (pRefPtr) { michael@0: *pRefPtr = nullptr; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: template michael@0: RefPtr* michael@0: nsRefPtrHashtable::GetWeak michael@0: (KeyType aKey, bool* aFound) const michael@0: { michael@0: typename base_type::EntryType* ent = this->GetEntry(aKey); michael@0: michael@0: if (ent) { michael@0: if (aFound) { michael@0: *aFound = true; michael@0: } michael@0: michael@0: return ent->mData; michael@0: } michael@0: michael@0: // Key does not exist, return nullptr and set aFound to false michael@0: if (aFound) { michael@0: *aFound = false; michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: template michael@0: bool michael@0: nsRefPtrHashtable::Remove(KeyType aKey, michael@0: UserDataType* pRefPtr) michael@0: { michael@0: MOZ_ASSERT(pRefPtr); michael@0: typename base_type::EntryType* ent = this->GetEntry(aKey); michael@0: michael@0: if (ent) { michael@0: ent->mData.forget(pRefPtr); michael@0: this->Remove(aKey); michael@0: return true; michael@0: } michael@0: michael@0: // If the key doesn't exist, set *pRefPtr to null michael@0: // so that it is a valid XPCOM getter. michael@0: *pRefPtr = nullptr; michael@0: return false; michael@0: } michael@0: michael@0: #endif // nsRefPtrHashtable_h__