Bug 1290524: Allow nsClassHashTable::LookupOrAdd to provide arguments to construct the new object.

MozReview-Commit-ID: 1s1ZaFWLrri
This commit is contained in:
Emilio Cobos Álvarez 2016-07-29 10:49:19 -07:00
parent 220e36cb8d
commit 4d812b8ba1

View File

@ -39,10 +39,10 @@ public:
/**
* Looks up aKey in the hash table. If it doesn't exist a new object of
* KeyClass will be created (using its default constructor) and then
* returned.
* KeyClass will be created (using the arguments provided) and then returned.
*/
UserDataType LookupOrAdd(KeyType aKey);
template<typename... Args>
UserDataType LookupOrAdd(KeyType aKey, Args&&... aConstructionArgs);
/**
* @copydoc nsBaseHashtable::Get
@ -75,12 +75,14 @@ public:
//
template<class KeyClass, class T>
template<typename... Args>
T*
nsClassHashtable<KeyClass, T>::LookupOrAdd(KeyType aKey)
nsClassHashtable<KeyClass, T>::LookupOrAdd(KeyType aKey,
Args&&... aConstructionArgs)
{
typename base_type::EntryType* ent = this->PutEntry(aKey);
if (!ent->mData) {
ent->mData = new T();
ent->mData = new T(mozilla::Forward<Args>(aConstructionArgs)...);
}
return ent->mData;
}