From 92effd6bae394c7e2b58c5dc15d29c580738e1d9 Mon Sep 17 00:00:00 2001 From: "cbiesinger%web.de" Date: Thu, 11 Aug 2005 19:43:18 +0000 Subject: [PATCH] bug 228794, r=bsmedberg sr=alecf add nsInterfaceHashtable::GetWeak --- xpcom/glue/nsInterfaceHashtable.h | 34 +++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/xpcom/glue/nsInterfaceHashtable.h b/xpcom/glue/nsInterfaceHashtable.h index a526febbddde..592705a8737a 100644 --- a/xpcom/glue/nsInterfaceHashtable.h +++ b/xpcom/glue/nsInterfaceHashtable.h @@ -64,6 +64,15 @@ public: * If the key doesn't exist, pData will be set to nsnull. */ PRBool Get(KeyType aKey, UserDataType* pData) const; + + /** + * Gets a weak reference to the hashtable entry. + * If no entry is found, nsnull will be returned, and *aFound will be set + * to PR_FALSE (if aFound is not null). + * Otherwise, the pointer to the component will be returned, and *aFound will + * be set to PR_TRUE (if aFound is not null). Do not release this pointer! + */ + Interface* GetWeak(KeyType aKey, PRBool* aFound = nsnull) const; }; /** @@ -86,6 +95,10 @@ public: * If the key doesn't exist, pData will be set to nsnull. */ PRBool Get(KeyType aKey, UserDataType* pData) const; + + // GetWeak does not make sense on a multi-threaded hashtable, where another + // thread may remove the entry (and hence release it) as soon as GetWeak + // returns }; @@ -121,6 +134,27 @@ nsInterfaceHashtable::Get return PR_FALSE; } +template +Interface* +nsInterfaceHashtable::GetWeak + (KeyType aKey, PRBool* aFound) const +{ + typename nsBaseHashtable, Interface*>::EntryType* ent = + GetEntry(aKey); + + if (ent) + { + if (aFound) + *aFound = PR_TRUE; + + return ent->mData; + } + + // Key does not exist, return nsnull and set aFound to PR_FALSE + if (aFound) + *aFound = PR_FALSE; + return nsnull; +} // // nsInterfaceHashtableMT definitions