bug 1316119 - add a GetOrInsert to nsBaseHashtable that returns a reference to the value r=froydnj a=lizzard
authorTrevor Saunders <tbsaunde@tbsaunde.org>
Tue, 08 Nov 2016 15:54:45 -0500
changeset 352927 8fe631df8c66929543a95624674338fbcb061ed2
parent 352926 a9fdf64f8f34654aa7d2685c96d53f680ac6aa1d
child 352928 b1dbd3dcc086e90bf9c94ecb9984301a6178340c
push id6795
push userjlund@mozilla.com
push dateMon, 23 Jan 2017 14:19:46 +0000
treeherdermozilla-esr52@76101b503191 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj, lizzard
bugs1316119
milestone52.0a2
bug 1316119 - add a GetOrInsert to nsBaseHashtable that returns a reference to the value r=froydnj a=lizzard This intends to expose basically the same functionality as operator[] on unordered_map.
xpcom/glue/nsBaseHashtable.h
--- a/xpcom/glue/nsBaseHashtable.h
+++ b/xpcom/glue/nsBaseHashtable.h
@@ -111,16 +111,32 @@ public:
     if (!ent) {
       return UserDataType{};
     }
 
     return ent->mData;
   }
 
   /**
+   * Add key to the table if not already present, and return a reference to its
+   * value.  If key is not already in the table then the value is default
+   * constructed.
+   */
+  DataType& GetOrInsert(const KeyType& aKey)
+  {
+    EntryType* ent = this->GetEntry(aKey);
+    if (ent) {
+      return ent->mData;
+    }
+
+    ent = this->PutEntry(aKey);
+    return ent->mData;
+  }
+
+  /**
    * put a new value for the associated key
    * @param aKey the key to put
    * @param aData the new data
    * @return always true, unless memory allocation failed
    */
   void Put(KeyType aKey, const UserDataType& aData)
   {
     if (!Put(aKey, aData, mozilla::fallible)) {