Back to index

lightning-sunbird  0.9+nobinonly
nsRefPtrHashtable.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is C++ hashtable templates.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Benjamin Smedberg.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Neil Rashbrook <neil@parkwaycc.co.uk>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #ifndef nsRefPtrHashtable_h__
00040 #define nsRefPtrHashtable_h__
00041 
00042 #include "nsBaseHashtable.h"
00043 #include "nsHashKeys.h"
00044 #include "nsAutoPtr.h"
00045 
00054 template<class KeyClass, class RefPtr>
00055 class nsRefPtrHashtable :
00056   public nsBaseHashtable< KeyClass, nsRefPtr<RefPtr> , RefPtr* >
00057 {
00058 public:
00059   typedef typename KeyClass::KeyType KeyType;
00060   typedef RefPtr* UserDataType;
00061 
00067   PRBool Get(KeyType aKey, UserDataType* pData) const;
00068 
00075   RefPtr* GetWeak(KeyType aKey, PRBool* aFound = nsnull) const;
00076 };
00077 
00084 template<class KeyClass, class RefPtr>
00085 class nsRefPtrHashtableMT :
00086   public nsBaseHashtableMT< KeyClass, nsRefPtr<RefPtr> , RefPtr* >
00087 {
00088 public:
00089   typedef typename KeyClass::KeyType KeyType;
00090   typedef RefPtr* UserDataType;
00091 
00097   PRBool Get(KeyType aKey, UserDataType* pData) const;
00098 
00099   // GetWeak does not make sense on a multi-threaded hashtable, where another
00100   // thread may remove the entry (and hence release it) as soon as GetWeak
00101   // returns
00102 };
00103 
00104 
00105 //
00106 // nsRefPtrHashtable definitions
00107 //
00108 
00109 template<class KeyClass, class RefPtr>
00110 PRBool
00111 nsRefPtrHashtable<KeyClass,RefPtr>::Get
00112   (KeyType aKey, UserDataType* pRefPtr) const
00113 {
00114   typename nsBaseHashtable<KeyClass, nsRefPtr<RefPtr>, RefPtr*>::EntryType* ent =
00115     GetEntry(aKey);
00116 
00117   if (ent)
00118   {
00119     if (pRefPtr)
00120     {
00121       *pRefPtr = ent->mData;
00122 
00123       NS_IF_ADDREF(*pRefPtr);
00124     }
00125 
00126     return PR_TRUE;
00127   }
00128 
00129   // if the key doesn't exist, set *pRefPtr to null
00130   // so that it is a valid XPCOM getter
00131   if (pRefPtr)
00132     *pRefPtr = nsnull;
00133 
00134   return PR_FALSE;
00135 }
00136 
00137 template<class KeyClass, class RefPtr>
00138 RefPtr*
00139 nsRefPtrHashtable<KeyClass,RefPtr>::GetWeak
00140   (KeyType aKey, PRBool* aFound) const
00141 {
00142   typename nsBaseHashtable<KeyClass, nsRefPtr<RefPtr>, RefPtr*>::EntryType* ent =
00143     GetEntry(aKey);
00144 
00145   if (ent)
00146   {
00147     if (aFound)
00148       *aFound = PR_TRUE;
00149 
00150     return ent->mData;
00151   }
00152 
00153   // Key does not exist, return nsnull and set aFound to PR_FALSE
00154   if (aFound)
00155     *aFound = PR_FALSE;
00156   return nsnull;
00157 }
00158 
00159 //
00160 // nsRefPtrHashtableMT definitions
00161 //
00162 
00163 template<class KeyClass, class RefPtr>
00164 PRBool
00165 nsRefPtrHashtableMT<KeyClass,RefPtr>::Get
00166   (KeyType aKey, UserDataType* pRefPtr) const
00167 {
00168   PR_Lock(this->mLock);
00169 
00170   typename nsBaseHashtableMT<KeyClass, nsRefPtr<RefPtr>, RefPtr*>::EntryType* ent =
00171     GetEntry(aKey);
00172 
00173   if (ent)
00174   {
00175     if (pRefPtr)
00176     {
00177       *pRefPtr = ent->mData;
00178 
00179       NS_IF_ADDREF(*pRefPtr);
00180     }
00181 
00182     PR_Unlock(this->mLock);
00183 
00184     return PR_TRUE;
00185   }
00186 
00187   // if the key doesn't exist, set *pRefPtr to null
00188   // so that it is a valid XPCOM getter
00189   if (pRefPtr)
00190     *pRefPtr = nsnull;
00191 
00192   PR_Unlock(this->mLock);
00193 
00194   return PR_FALSE;
00195 }
00196 
00197 #endif // nsRefPtrHashtable_h__