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 nsClassHashtable_h__ michael@0: #define nsClassHashtable_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 C++ object 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 Class the class-type being wrapped michael@0: * @see nsInterfaceHashtable, nsClassHashtable michael@0: */ michael@0: template michael@0: class nsClassHashtable : michael@0: public nsBaseHashtable< KeyClass, nsAutoPtr, T* > michael@0: { michael@0: public: michael@0: typedef typename KeyClass::KeyType KeyType; michael@0: typedef T* UserDataType; michael@0: typedef nsBaseHashtable< KeyClass, nsAutoPtr, T* > base_type; michael@0: michael@0: nsClassHashtable() michael@0: { michael@0: } michael@0: explicit nsClassHashtable(uint32_t aInitSize) michael@0: : nsBaseHashtable,T*>(aInitSize) michael@0: { michael@0: } michael@0: michael@0: /** michael@0: * @copydoc nsBaseHashtable::Get michael@0: * @param pData 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: * @returns nullptr if the key is not present. michael@0: */ michael@0: UserDataType Get(KeyType aKey) const; michael@0: michael@0: /** michael@0: * Remove the entry for the given key from the hashtable and return it in michael@0: * aOut. If the key is not in the hashtable, aOut's pointer is set to michael@0: * nullptr. michael@0: * michael@0: * Normally, an entry is deleted when it's removed from an nsClassHashtable, michael@0: * but this function transfers ownership of the entry back to the caller michael@0: * through aOut -- the entry will be deleted when aOut goes out of scope. michael@0: * michael@0: * @param aKey the key to get and remove from the hashtable michael@0: */ michael@0: void RemoveAndForget(KeyType aKey, nsAutoPtr &aOut); michael@0: }; michael@0: michael@0: // michael@0: // nsClassHashtable definitions michael@0: // michael@0: michael@0: template michael@0: bool michael@0: nsClassHashtable::Get(KeyType aKey, T** retVal) const michael@0: { michael@0: typename base_type::EntryType* ent = this->GetEntry(aKey); michael@0: michael@0: if (ent) michael@0: { michael@0: if (retVal) michael@0: *retVal = ent->mData; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: if (retVal) michael@0: *retVal = nullptr; michael@0: michael@0: return false; michael@0: } michael@0: michael@0: template michael@0: T* michael@0: nsClassHashtable::Get(KeyType aKey) const michael@0: { michael@0: typename base_type::EntryType* ent = this->GetEntry(aKey); michael@0: michael@0: if (!ent) michael@0: return nullptr; michael@0: michael@0: return ent->mData; michael@0: } michael@0: michael@0: template michael@0: void michael@0: nsClassHashtable::RemoveAndForget(KeyType aKey, nsAutoPtr &aOut) michael@0: { michael@0: aOut = nullptr; michael@0: nsAutoPtr ptr; michael@0: michael@0: typename base_type::EntryType *ent = this->GetEntry(aKey); michael@0: if (!ent) michael@0: return; michael@0: michael@0: // Transfer ownership from ent->mData into aOut. michael@0: aOut = ent->mData; michael@0: michael@0: this->Remove(aKey); michael@0: } michael@0: michael@0: #endif // nsClassHashtable_h__