1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsClassHashtable.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,117 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nsClassHashtable_h__ 1.10 +#define nsClassHashtable_h__ 1.11 + 1.12 +#include "nsBaseHashtable.h" 1.13 +#include "nsHashKeys.h" 1.14 +#include "nsAutoPtr.h" 1.15 + 1.16 +/** 1.17 + * templated hashtable class maps keys to C++ object pointers. 1.18 + * See nsBaseHashtable for complete declaration. 1.19 + * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h 1.20 + * for a complete specification. 1.21 + * @param Class the class-type being wrapped 1.22 + * @see nsInterfaceHashtable, nsClassHashtable 1.23 + */ 1.24 +template<class KeyClass,class T> 1.25 +class nsClassHashtable : 1.26 + public nsBaseHashtable< KeyClass, nsAutoPtr<T>, T* > 1.27 +{ 1.28 +public: 1.29 + typedef typename KeyClass::KeyType KeyType; 1.30 + typedef T* UserDataType; 1.31 + typedef nsBaseHashtable< KeyClass, nsAutoPtr<T>, T* > base_type; 1.32 + 1.33 + nsClassHashtable() 1.34 + { 1.35 + } 1.36 + explicit nsClassHashtable(uint32_t aInitSize) 1.37 + : nsBaseHashtable<KeyClass,nsAutoPtr<T>,T*>(aInitSize) 1.38 + { 1.39 + } 1.40 + 1.41 + /** 1.42 + * @copydoc nsBaseHashtable::Get 1.43 + * @param pData if the key doesn't exist, pData will be set to nullptr. 1.44 + */ 1.45 + bool Get(KeyType aKey, UserDataType* pData) const; 1.46 + 1.47 + /** 1.48 + * @copydoc nsBaseHashtable::Get 1.49 + * @returns nullptr if the key is not present. 1.50 + */ 1.51 + UserDataType Get(KeyType aKey) const; 1.52 + 1.53 + /** 1.54 + * Remove the entry for the given key from the hashtable and return it in 1.55 + * aOut. If the key is not in the hashtable, aOut's pointer is set to 1.56 + * nullptr. 1.57 + * 1.58 + * Normally, an entry is deleted when it's removed from an nsClassHashtable, 1.59 + * but this function transfers ownership of the entry back to the caller 1.60 + * through aOut -- the entry will be deleted when aOut goes out of scope. 1.61 + * 1.62 + * @param aKey the key to get and remove from the hashtable 1.63 + */ 1.64 + void RemoveAndForget(KeyType aKey, nsAutoPtr<T> &aOut); 1.65 +}; 1.66 + 1.67 +// 1.68 +// nsClassHashtable definitions 1.69 +// 1.70 + 1.71 +template<class KeyClass,class T> 1.72 +bool 1.73 +nsClassHashtable<KeyClass,T>::Get(KeyType aKey, T** retVal) const 1.74 +{ 1.75 + typename base_type::EntryType* ent = this->GetEntry(aKey); 1.76 + 1.77 + if (ent) 1.78 + { 1.79 + if (retVal) 1.80 + *retVal = ent->mData; 1.81 + 1.82 + return true; 1.83 + } 1.84 + 1.85 + if (retVal) 1.86 + *retVal = nullptr; 1.87 + 1.88 + return false; 1.89 +} 1.90 + 1.91 +template<class KeyClass,class T> 1.92 +T* 1.93 +nsClassHashtable<KeyClass,T>::Get(KeyType aKey) const 1.94 +{ 1.95 + typename base_type::EntryType* ent = this->GetEntry(aKey); 1.96 + 1.97 + if (!ent) 1.98 + return nullptr; 1.99 + 1.100 + return ent->mData; 1.101 +} 1.102 + 1.103 +template<class KeyClass,class T> 1.104 +void 1.105 +nsClassHashtable<KeyClass,T>::RemoveAndForget(KeyType aKey, nsAutoPtr<T> &aOut) 1.106 +{ 1.107 + aOut = nullptr; 1.108 + nsAutoPtr<T> ptr; 1.109 + 1.110 + typename base_type::EntryType *ent = this->GetEntry(aKey); 1.111 + if (!ent) 1.112 + return; 1.113 + 1.114 + // Transfer ownership from ent->mData into aOut. 1.115 + aOut = ent->mData; 1.116 + 1.117 + this->Remove(aKey); 1.118 +} 1.119 + 1.120 +#endif // nsClassHashtable_h__