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 332557 77950beb90dd5636e0d3929638bdd11c899c9bd8
parent 332556 1b848daf2102cb0636c9475ae424836abef5ca7f
child 332558 0e1f1a9c666d2ba8ddba5246fb4e705244edbeb4
push id9858
push userjlund@mozilla.com
push dateMon, 01 Aug 2016 14:37:10 +0000
treeherdermozilla-aurora@203106ef6cb6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1290524
milestone50.0a1
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
 {