/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #ifndef XPCOM_DS_NSREFCOUNTEDHASHTABLE_H_ #define XPCOM_DS_NSREFCOUNTEDHASHTABLE_H_ #include "nsBaseHashtable.h" #include "nsHashKeys.h" /** * templated hashtable class maps keys to reference pointers. * See nsBaseHashtable for complete declaration. * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h * for a complete specification. * @param PtrType the reference-type being wrapped * @see nsClassHashtable, nsTHashMap */ template class nsRefCountedHashtable : public nsBaseHashtable< KeyClass, PtrType, typename mozilla::detail::SmartPtrTraits::RawPointerType> { public: using KeyType = typename KeyClass::KeyType; using SmartPtrTraits = mozilla::detail::SmartPtrTraits; using PointeeType = typename SmartPtrTraits::PointeeType; using RawPointerType = typename SmartPtrTraits::RawPointerType; using base_type = nsBaseHashtable; using base_type::base_type; static_assert(SmartPtrTraits::IsRefCounted); /** * @copydoc nsBaseHashtable::Get * @param aData This is an XPCOM getter, so aData is already_addrefed. * If the key doesn't exist, *aData will be set to nullptr. */ bool Get(KeyType aKey, RawPointerType* aData) const; /** * @copydoc nsBaseHashtable::Get */ [[nodiscard]] already_AddRefed Get(KeyType aKey) const; /** * Gets a weak reference to the hashtable entry. * @param aFound If not nullptr, will be set to true if the entry is found, * to false otherwise. * @return The entry, or nullptr if not found. Do not release this pointer! */ [[nodiscard]] RawPointerType GetWeak(KeyType aKey, bool* aFound = nullptr) const; using base_type::InsertOrUpdate; template >> void InsertOrUpdate( KeyType aKey, typename SmartPtrTraits::template OtherSmartPtrType&& aData); template >> [[nodiscard]] bool InsertOrUpdate( KeyType aKey, typename SmartPtrTraits::template OtherSmartPtrType&& aData, const mozilla::fallible_t&); template >> void InsertOrUpdate(KeyType aKey, already_AddRefed&& aData); template >> [[nodiscard]] bool InsertOrUpdate(KeyType aKey, already_AddRefed&& aData, const mozilla::fallible_t&); /** * Remove the entry associated with aKey (if any), optionally _moving_ its * current value into *aData, thereby avoiding calls to AddRef and Release. * Return true if found. * @param aKey the key to remove from the hashtable * @param aData where to move the value (if non-null). If an entry is not * found it will be set to nullptr. * @return true if an entry for aKey was found (and removed) */ inline bool Remove(KeyType aKey, RawPointerType* aData = nullptr); nsRefCountedHashtable Clone() const { return this->template CloneAs(); } }; template inline void ImplCycleCollectionUnlink(nsRefCountedHashtable& aField) { aField.Clear(); } template inline void ImplCycleCollectionTraverse( nsCycleCollectionTraversalCallback& aCallback, nsRefCountedHashtable& aField, const char* aName, uint32_t aFlags = 0) { for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) { CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags); } } // // nsRefCountedHashtable definitions // template bool nsRefCountedHashtable::Get( KeyType aKey, RawPointerType* aRefPtr) const { typename base_type::EntryType* ent = this->GetEntry(aKey); if (ent) { if (aRefPtr) { *aRefPtr = ent->GetData(); NS_IF_ADDREF(*aRefPtr); } return true; } // if the key doesn't exist, set *aRefPtr to null // so that it is a valid XPCOM getter if (aRefPtr) { *aRefPtr = nullptr; } return false; } template already_AddRefed::PointeeType> nsRefCountedHashtable::Get(KeyType aKey) const { typename base_type::EntryType* ent = this->GetEntry(aKey); if (!ent) { return nullptr; } PtrType copy = ent->GetData(); return copy.forget(); } template typename nsRefCountedHashtable::RawPointerType nsRefCountedHashtable::GetWeak(KeyType aKey, bool* aFound) const { typename base_type::EntryType* ent = this->GetEntry(aKey); if (ent) { if (aFound) { *aFound = true; } return ent->GetData(); } // Key does not exist, return nullptr and set aFound to false if (aFound) { *aFound = false; } return nullptr; } template template void nsRefCountedHashtable::InsertOrUpdate( KeyType aKey, typename SmartPtrTraits::template OtherSmartPtrType&& aData) { if (!InsertOrUpdate(aKey, std::move(aData), mozilla::fallible)) { NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount()); } } template template bool nsRefCountedHashtable::InsertOrUpdate( KeyType aKey, typename SmartPtrTraits::template OtherSmartPtrType&& aData, const mozilla::fallible_t&) { typename base_type::EntryType* ent = this->PutEntry(aKey, mozilla::fallible); if (!ent) { return false; } ent->SetData(std::move(aData)); return true; } template template void nsRefCountedHashtable::InsertOrUpdate( KeyType aKey, already_AddRefed&& aData) { if (!InsertOrUpdate(aKey, std::move(aData), mozilla::fallible)) { NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount()); } } template template bool nsRefCountedHashtable::InsertOrUpdate( KeyType aKey, already_AddRefed&& aData, const mozilla::fallible_t&) { typename base_type::EntryType* ent = this->PutEntry(aKey, mozilla::fallible); if (!ent) { return false; } ent->SetData(std::move(aData)); return true; } template bool nsRefCountedHashtable::Remove(KeyType aKey, RawPointerType* aRefPtr) { typename base_type::EntryType* ent = this->GetEntry(aKey); if (ent) { if (aRefPtr) { ent->GetModifiableData()->forget(aRefPtr); } this->RemoveEntry(ent); return true; } if (aRefPtr) { *aRefPtr = nullptr; } return false; } #endif // XPCOM_DS_NSREFCOUNTEDHASHTABLE_H_