Bug 1290524: Allow nsClassHashTable::LookupOrAdd to provide arguments to construct the new object.
authorEmilio Cobos Álvarez <ecoal95@gmail.com>
Fri, 29 Jul 2016 10:49:19 -0700
changeset 347523 77950beb90dd5636e0d3929638bdd11c899c9bd8
parent 347522 1b848daf2102cb0636c9475ae424836abef5ca7f
child 347524 0e1f1a9c666d2ba8ddba5246fb4e705244edbeb4
push id6389
push userraliiev@mozilla.com
push dateMon, 19 Sep 2016 13:38:22 +0000
treeherdermozilla-beta@01d67bfe6c81 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1290524
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1290524: Allow nsClassHashTable::LookupOrAdd to provide arguments to construct the new object. MozReview-Commit-ID: 1s1ZaFWLrri
xpcom/glue/nsClassHashtable.h
--- a/xpcom/glue/nsClassHashtable.h
+++ b/xpcom/glue/nsClassHashtable.h
@@ -34,20 +34,20 @@ public:
   nsClassHashtable() {}
   explicit nsClassHashtable(uint32_t aInitLength)
     : nsBaseHashtable<KeyClass, nsAutoPtr<T>, T*>(aInitLength)
   {
   }
 
   /**
    * 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
    * @param aData if the key doesn't exist, pData will be set to nullptr.
    */
   bool Get(KeyType aKey, UserDataType* aData) const;
 
   /**
@@ -70,22 +70,24 @@ public:
   void RemoveAndForget(KeyType aKey, nsAutoPtr<T>& aOut);
 };
 
 //
 // nsClassHashtable definitions
 //
 
 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;
 }
 
 template<class KeyClass, class T>
 bool
 nsClassHashtable<KeyClass, T>::Get(KeyType aKey, T** aRetVal) const
 {