Bug 1691894 - Remove nsClassHashtable::LookupForAddFromFactory and use GetOrInsertWith instead. r=xpcom-reviewers,nika

Differential Revision: https://phabricator.services.mozilla.com/D104851
This commit is contained in:
Simon Giesecke 2021-02-15 16:37:51 +00:00
parent 4513a3aee1
commit 27de23cf55
3 changed files with 19 additions and 29 deletions

View File

@ -7626,8 +7626,14 @@ nsresult DatabaseConnection::UpdateRefcountFunction::ProcessValue(
const int64_t id = file.FileInfo().Id();
MOZ_ASSERT(id > 0);
const auto entry = WrapNotNull(mFileInfoEntries.LookupOrAddFromFactory(
id, [&file] { return MakeUnique<FileInfoEntry>(file.FileInfoPtr()); }));
const auto entry =
WrapNotNull(mFileInfoEntries
.GetOrInsertWith(id,
[&file] {
return MakeUnique<FileInfoEntry>(
file.FileInfoPtr());
})
.get());
if (mInSavepoint) {
mSavepointEntriesIndex.Put(id, entry);

View File

@ -58,14 +58,6 @@ class nsClassHashtable : public nsBaseHashtable<KeyClass, mozilla::UniquePtr<T>,
template <typename... Args>
UserDataType LookupOrAdd(KeyType aKey, Args&&... aConstructionArgs);
/**
* Looks up aKey in the hash table. If it doesn't exist a new object of
* KeyClass will be created (using the factory function provided, whose return
* value must be convertible to UniquePtr<T>) and then returned.
*/
template <typename Factory>
UserDataType LookupOrAddFromFactory(KeyType aKey, const Factory& aFactory);
/**
* @copydoc nsBaseHashtable::Get
* @param aData if the key doesn't exist, pData will be set to nullptr.
@ -112,21 +104,13 @@ template <class KeyClass, class T>
template <typename... Args>
T* nsClassHashtable<KeyClass, T>::LookupOrAdd(KeyType aKey,
Args&&... aConstructionArgs) {
return LookupOrAddFromFactory(std::move(aKey), [&] {
return mozilla::MakeUnique<T>(std::forward<Args>(aConstructionArgs)...);
});
}
template <class KeyClass, class T>
template <typename Factory>
T* nsClassHashtable<KeyClass, T>::LookupOrAddFromFactory(
KeyType aKey, const Factory& aFactory) {
auto count = this->Count();
typename base_type::EntryType* ent = this->PutEntry(aKey);
if (count != this->Count()) {
ent->SetData(aFactory());
}
return ent->GetData().get();
return this
->GetOrInsertWith(std::move(aKey),
[&] {
return mozilla::MakeUnique<T>(
std::forward<Args>(aConstructionArgs)...);
})
.get();
}
template <class KeyClass, class T>

View File

@ -1463,7 +1463,7 @@ TEST(Hashtables, ClassHashtable_LookupOrAdd_NotPresent)
EXPECT_EQ(42u, entry->GetChar());
}
TEST(Hashtables, ClassHashtable_LookupOrAddFromFactory_Present)
TEST(Hashtables, ClassHashtable_GetOrInsertWith_Present)
{
nsClassHashtable<nsCStringHashKey, TestUniChar> EntToUniClass(ENTITY_COUNT);
@ -1472,17 +1472,17 @@ TEST(Hashtables, ClassHashtable_LookupOrAddFromFactory_Present)
mozilla::MakeUnique<TestUniCharDerived>(entity.mUnicode));
}
auto* entry = EntToUniClass.LookupOrAddFromFactory(
const auto& entry = EntToUniClass.GetOrInsertWith(
"uml"_ns, [] { return mozilla::MakeUnique<TestUniCharDerived>(42); });
EXPECT_EQ(168u, entry->GetChar());
}
TEST(Hashtables, ClassHashtable_LookupOrAddFromFactory_NotPresent)
TEST(Hashtables, ClassHashtable_GetOrInsertWith_NotPresent)
{
nsClassHashtable<nsCStringHashKey, TestUniChar> EntToUniClass(ENTITY_COUNT);
// This is going to insert a TestUniCharDerived.
auto* entry = EntToUniClass.LookupOrAddFromFactory(
const auto& entry = EntToUniClass.GetOrInsertWith(
"uml"_ns, [] { return mozilla::MakeUnique<TestUniCharDerived>(42); });
EXPECT_EQ(42u, entry->GetChar());
}