Bug 1041822 - Add LookupOrAdd to nsClassHashtable. r=bsmedberg
authorBlake Kaplan <mrbkap@gmail.com>
Mon, 11 Aug 2014 11:13:36 -0700
changeset 198876 d20cfaa4220b62da01767efde0e6df99e1145cdb
parent 198875 84d14acbb09bbf86903d8a1fe81c2b00c4197975
child 198877 7f5a7dc734207085c2e8a7b8c4103285307b8f8f
push id47521
push usermrbkap@mozilla.com
push dateMon, 11 Aug 2014 18:13:51 +0000
treeherdermozilla-inbound@7f5a7dc73420 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs1041822
milestone34.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 1041822 - Add LookupOrAdd to nsClassHashtable. r=bsmedberg
xpcom/glue/nsClassHashtable.h
--- a/xpcom/glue/nsClassHashtable.h
+++ b/xpcom/glue/nsClassHashtable.h
@@ -31,16 +31,23 @@ 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.
+   */
+  UserDataType LookupOrAdd(KeyType aKey);
+
+  /**
    * @copydoc nsBaseHashtable::Get
    * @param aData if the key doesn't exist, pData will be set to nullptr.
    */
   bool Get(KeyType aKey, UserDataType* aData) const;
 
   /**
    * @copydoc nsBaseHashtable::Get
    * @returns nullptr if the key is not present.
@@ -61,16 +68,27 @@ public:
   void RemoveAndForget(KeyType aKey, nsAutoPtr<T>& aOut);
 };
 
 //
 // nsClassHashtable definitions
 //
 
 template<class KeyClass, class T>
+T*
+nsClassHashtable<KeyClass, T>::LookupOrAdd(KeyType aKey)
+{
+  typename base_type::EntryType* ent = this->PutEntry(aKey);
+  if (!ent->mData) {
+    ent->mData = new T();
+  }
+  return ent->mData;
+}
+
+template<class KeyClass, class T>
 bool
 nsClassHashtable<KeyClass, T>::Get(KeyType aKey, T** aRetVal) const
 {
   typename base_type::EntryType* ent = this->GetEntry(aKey);
 
   if (ent) {
     if (aRetVal) {
       *aRetVal = ent->mData;