From fab96c14107f591a4cbd27af98aa8994dd81563e Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Tue, 23 Aug 2016 10:31:26 +1000 Subject: [PATCH] Bug 1291707 part 2 - Add GetAndRemove to nsDataHashtable. r=froydnj MozReview-Commit-ID: 24zlYwsRGsF --HG-- extra : rebase_source : 904c62cd779133666904d3167cb7e7c31f95a412 extra : source : 6c1df15aad607e70547b51011bd352d8a8bf826d --- xpcom/glue/nsDataHashtable.h | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/xpcom/glue/nsDataHashtable.h b/xpcom/glue/nsDataHashtable.h index 18aa210b7e38..19c0728b4a81 100644 --- a/xpcom/glue/nsDataHashtable.h +++ b/xpcom/glue/nsDataHashtable.h @@ -9,6 +9,7 @@ #include "nsHashKeys.h" #include "nsBaseHashtable.h" +#include "mozilla/Maybe.h" /** * templated hashtable class maps keys to simple datatypes. @@ -22,12 +23,36 @@ template class nsDataHashtable : public nsBaseHashtable { +private: + typedef nsBaseHashtable BaseClass; + public: + using typename BaseClass::KeyType; + using typename BaseClass::EntryType; + nsDataHashtable() {} explicit nsDataHashtable(uint32_t aInitLength) - : nsBaseHashtable(aInitLength) + : BaseClass(aInitLength) { } + + /** + * Retrieve the value for a key and remove the corresponding entry at + * the same time. + * + * @param aKey the key to retrieve and remove + * @return the found value, or Nothing if no entry was found with the + * given key. + */ + mozilla::Maybe GetAndRemove(KeyType aKey) + { + mozilla::Maybe value; + if (EntryType* ent = this->GetEntry(aKey)) { + value.emplace(mozilla::Move(ent->mData)); + this->RemoveEntry(ent); + } + return value; + } }; #endif // nsDataHashtable_h__