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 nsInterfaceHashtable_h__ michael@0: #define nsInterfaceHashtable_h__ michael@0: michael@0: #include "nsBaseHashtable.h" michael@0: #include "nsHashKeys.h" michael@0: #include "nsCOMPtr.h" michael@0: michael@0: /** michael@0: * templated hashtable class maps keys to interface 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 Interface the interface-type being wrapped michael@0: * @see nsDataHashtable, nsClassHashtable michael@0: */ michael@0: template michael@0: class nsInterfaceHashtable : michael@0: public nsBaseHashtable< KeyClass, nsCOMPtr , Interface* > michael@0: { michael@0: public: michael@0: typedef typename KeyClass::KeyType KeyType; michael@0: typedef Interface* UserDataType; michael@0: typedef nsBaseHashtable< KeyClass, nsCOMPtr , Interface* > michael@0: base_type; michael@0: michael@0: nsInterfaceHashtable() michael@0: { michael@0: } michael@0: explicit nsInterfaceHashtable(uint32_t aInitSize) michael@0: : nsBaseHashtable,Interface*>(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: * @copydoc nsBaseHashtable::Get michael@0: */ michael@0: already_AddRefed Get(KeyType aKey) 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: Interface* GetWeak(KeyType aKey, bool* aFound = nullptr) const; michael@0: }; michael@0: michael@0: template michael@0: inline void michael@0: ImplCycleCollectionUnlink(nsInterfaceHashtable& 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: const nsInterfaceHashtable& 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: // nsInterfaceHashtable definitions michael@0: // michael@0: michael@0: template michael@0: bool michael@0: nsInterfaceHashtable::Get michael@0: (KeyType aKey, UserDataType* pInterface) const michael@0: { michael@0: typename base_type::EntryType* ent = this->GetEntry(aKey); michael@0: michael@0: if (ent) michael@0: { michael@0: if (pInterface) michael@0: { michael@0: *pInterface = ent->mData; michael@0: michael@0: NS_IF_ADDREF(*pInterface); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: // if the key doesn't exist, set *pInterface to null michael@0: // so that it is a valid XPCOM getter michael@0: if (pInterface) michael@0: *pInterface = nullptr; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: template michael@0: already_AddRefed michael@0: nsInterfaceHashtable::Get(KeyType aKey) const michael@0: { michael@0: typename base_type::EntryType* ent = this->GetEntry(aKey); michael@0: if (!ent) michael@0: return nullptr; michael@0: michael@0: nsCOMPtr copy = ent->mData; michael@0: return copy.forget(); michael@0: } michael@0: michael@0: template michael@0: Interface* michael@0: nsInterfaceHashtable::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: { michael@0: if (aFound) michael@0: *aFound = true; 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: return nullptr; michael@0: } michael@0: michael@0: #endif // nsInterfaceHashtable_h__