1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/xpcom/glue/nsInterfaceHashtable.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 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 nsInterfaceHashtable_h__ 1.10 +#define nsInterfaceHashtable_h__ 1.11 + 1.12 +#include "nsBaseHashtable.h" 1.13 +#include "nsHashKeys.h" 1.14 +#include "nsCOMPtr.h" 1.15 + 1.16 +/** 1.17 + * templated hashtable class maps keys to interface 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 Interface the interface-type being wrapped 1.22 + * @see nsDataHashtable, nsClassHashtable 1.23 + */ 1.24 +template<class KeyClass,class Interface> 1.25 +class nsInterfaceHashtable : 1.26 + public nsBaseHashtable< KeyClass, nsCOMPtr<Interface> , Interface* > 1.27 +{ 1.28 +public: 1.29 + typedef typename KeyClass::KeyType KeyType; 1.30 + typedef Interface* UserDataType; 1.31 + typedef nsBaseHashtable< KeyClass, nsCOMPtr<Interface> , Interface* > 1.32 + base_type; 1.33 + 1.34 + nsInterfaceHashtable() 1.35 + { 1.36 + } 1.37 + explicit nsInterfaceHashtable(uint32_t aInitSize) 1.38 + : nsBaseHashtable<KeyClass,nsCOMPtr<Interface>,Interface*>(aInitSize) 1.39 + { 1.40 + } 1.41 + 1.42 + /** 1.43 + * @copydoc nsBaseHashtable::Get 1.44 + * @param pData This is an XPCOM getter, so pData is already_addrefed. 1.45 + * If the key doesn't exist, pData will be set to nullptr. 1.46 + */ 1.47 + bool Get(KeyType aKey, UserDataType* pData) const; 1.48 + 1.49 + /** 1.50 + * @copydoc nsBaseHashtable::Get 1.51 + */ 1.52 + already_AddRefed<Interface> Get(KeyType aKey) const; 1.53 + 1.54 + /** 1.55 + * Gets a weak reference to the hashtable entry. 1.56 + * @param aFound If not nullptr, will be set to true if the entry is found, 1.57 + * to false otherwise. 1.58 + * @return The entry, or nullptr if not found. Do not release this pointer! 1.59 + */ 1.60 + Interface* GetWeak(KeyType aKey, bool* aFound = nullptr) const; 1.61 +}; 1.62 + 1.63 +template <typename K, typename T> 1.64 +inline void 1.65 +ImplCycleCollectionUnlink(nsInterfaceHashtable<K, T>& aField) 1.66 +{ 1.67 + aField.Clear(); 1.68 +} 1.69 + 1.70 +template <typename K, typename T> 1.71 +inline void 1.72 +ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback, 1.73 + const nsInterfaceHashtable<K, T>& aField, 1.74 + const char* aName, 1.75 + uint32_t aFlags = 0) 1.76 +{ 1.77 + nsBaseHashtableCCTraversalData userData(aCallback, aName, aFlags); 1.78 + 1.79 + aField.EnumerateRead(ImplCycleCollectionTraverse_EnumFunc<typename K::KeyType,T*>, 1.80 + &userData); 1.81 +} 1.82 + 1.83 +// 1.84 +// nsInterfaceHashtable definitions 1.85 +// 1.86 + 1.87 +template<class KeyClass,class Interface> 1.88 +bool 1.89 +nsInterfaceHashtable<KeyClass,Interface>::Get 1.90 + (KeyType aKey, UserDataType* pInterface) const 1.91 +{ 1.92 + typename base_type::EntryType* ent = this->GetEntry(aKey); 1.93 + 1.94 + if (ent) 1.95 + { 1.96 + if (pInterface) 1.97 + { 1.98 + *pInterface = ent->mData; 1.99 + 1.100 + NS_IF_ADDREF(*pInterface); 1.101 + } 1.102 + 1.103 + return true; 1.104 + } 1.105 + 1.106 + // if the key doesn't exist, set *pInterface to null 1.107 + // so that it is a valid XPCOM getter 1.108 + if (pInterface) 1.109 + *pInterface = nullptr; 1.110 + 1.111 + return false; 1.112 +} 1.113 + 1.114 +template<class KeyClass, class Interface> 1.115 +already_AddRefed<Interface> 1.116 +nsInterfaceHashtable<KeyClass,Interface>::Get(KeyType aKey) const 1.117 +{ 1.118 + typename base_type::EntryType* ent = this->GetEntry(aKey); 1.119 + if (!ent) 1.120 + return nullptr; 1.121 + 1.122 + nsCOMPtr<Interface> copy = ent->mData; 1.123 + return copy.forget(); 1.124 +} 1.125 + 1.126 +template<class KeyClass,class Interface> 1.127 +Interface* 1.128 +nsInterfaceHashtable<KeyClass,Interface>::GetWeak 1.129 + (KeyType aKey, bool* aFound) const 1.130 +{ 1.131 + typename base_type::EntryType* ent = this->GetEntry(aKey); 1.132 + 1.133 + if (ent) 1.134 + { 1.135 + if (aFound) 1.136 + *aFound = true; 1.137 + 1.138 + return ent->mData; 1.139 + } 1.140 + 1.141 + // Key does not exist, return nullptr and set aFound to false 1.142 + if (aFound) 1.143 + *aFound = false; 1.144 + return nullptr; 1.145 +} 1.146 + 1.147 +#endif // nsInterfaceHashtable_h__