From 93e1959ffb47668970434456578c9510148ddb2c Mon Sep 17 00:00:00 2001 From: Trevor Saunders Date: Tue, 8 Nov 2016 15:54:45 -0500 Subject: [PATCH] bug 1316119 - add a GetOrInsert to nsBaseHashtable that returns a reference to the value r=froydnj This intends to expose basically the same functionality as operator[] on unordered_map. --- xpcom/glue/nsBaseHashtable.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/xpcom/glue/nsBaseHashtable.h b/xpcom/glue/nsBaseHashtable.h index 985aa3b7e713..f52df3dd186a 100644 --- a/xpcom/glue/nsBaseHashtable.h +++ b/xpcom/glue/nsBaseHashtable.h @@ -115,6 +115,22 @@ public: 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