From 2aa4a82499d4becd2284cdb482213d541b8804dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 16:29:10 +0200 Subject: Adding upstream version 86.0.1. Signed-off-by: Daniel Baumann --- xpcom/ds/nsInterfaceHashtable.h | 188 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 xpcom/ds/nsInterfaceHashtable.h (limited to 'xpcom/ds/nsInterfaceHashtable.h') diff --git a/xpcom/ds/nsInterfaceHashtable.h b/xpcom/ds/nsInterfaceHashtable.h new file mode 100644 index 0000000000..9c9378e8a0 --- /dev/null +++ b/xpcom/ds/nsInterfaceHashtable.h @@ -0,0 +1,188 @@ +/* -*- 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 nsInterfaceHashtable_h__ +#define nsInterfaceHashtable_h__ + +#include "nsBaseHashtable.h" +#include "nsHashKeys.h" +#include "nsCOMPtr.h" + +/** + * templated hashtable class maps keys to interface pointers. + * See nsBaseHashtable for complete declaration. + * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h + * for a complete specification. + * @param Interface the interface-type being wrapped + * @see nsDataHashtable, nsClassHashtable + */ +template +class nsInterfaceHashtable + : public nsBaseHashtable, Interface*> { + public: + typedef typename KeyClass::KeyType KeyType; + typedef Interface* UserDataType; + typedef nsBaseHashtable, Interface*> base_type; + + nsInterfaceHashtable() = default; + explicit nsInterfaceHashtable(uint32_t aInitLength) + : nsBaseHashtable, Interface*>( + aInitLength) {} + + /** + * @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, UserDataType* aData) const; + + /** + * @copydoc nsBaseHashtable::Get + */ + 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! + */ + Interface* GetWeak(KeyType aKey, bool* aFound = nullptr) const; + + /** + * Allows inserting a value into the hashtable, moving its owning reference + * count into the hashtable, avoiding an AddRef. + */ + void Put(KeyType aKey, already_AddRefed&& aData) { + if (!Put(aKey, std::move(aData), mozilla::fallible)) { + NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount()); + } + } + + [[nodiscard]] bool Put(KeyType aKey, already_AddRefed&& aData, + const mozilla::fallible_t&); + using base_type::Put; + + /** + * 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, Interface** aData = nullptr); +}; + +template +inline void ImplCycleCollectionUnlink(nsInterfaceHashtable& aField) { + aField.Clear(); +} + +template +inline void ImplCycleCollectionTraverse( + nsCycleCollectionTraversalCallback& aCallback, + const nsInterfaceHashtable& aField, const char* aName, + uint32_t aFlags = 0) { + for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) { + CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags); + } +} + +// +// nsInterfaceHashtable definitions +// + +template +bool nsInterfaceHashtable::Get( + KeyType aKey, UserDataType* aInterface) const { + typename base_type::EntryType* ent = this->GetEntry(aKey); + + if (ent) { + if (aInterface) { + *aInterface = ent->GetData(); + + NS_IF_ADDREF(*aInterface); + } + + return true; + } + + // if the key doesn't exist, set *aInterface to null + // so that it is a valid XPCOM getter + if (aInterface) { + *aInterface = nullptr; + } + + return false; +} + +template +already_AddRefed nsInterfaceHashtable::Get( + KeyType aKey) const { + typename base_type::EntryType* ent = this->GetEntry(aKey); + if (!ent) { + return nullptr; + } + + nsCOMPtr copy = ent->GetData(); + return copy.forget(); +} + +template +Interface* nsInterfaceHashtable::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 +bool nsInterfaceHashtable::Put( + KeyType aKey, already_AddRefed&& aValue, + const mozilla::fallible_t&) { + typename base_type::EntryType* ent = this->PutEntry(aKey); + if (!ent) { + return false; + } + + ent->SetData(std::move(aValue)); + return true; +} + +template +bool nsInterfaceHashtable::Remove(KeyType aKey, + Interface** aData) { + typename base_type::EntryType* ent = this->GetEntry(aKey); + + if (ent) { + if (aData) { + ent->GetModifiableData()->forget(aData); + } + this->RemoveEntry(ent); + return true; + } + + if (aData) { + *aData = nullptr; + } + return false; +} + +#endif // nsInterfaceHashtable_h__ -- cgit v1.2.3