1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef nsInterfaceHashtable_h__
8 #define nsInterfaceHashtable_h__
9 
10 #include "nsBaseHashtable.h"
11 #include "nsHashKeys.h"
12 #include "nsCOMPtr.h"
13 
14 /**
15  * templated hashtable class maps keys to interface pointers.
16  * See nsBaseHashtable for complete declaration.
17  * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
18  *   for a complete specification.
19  * @param Interface the interface-type being wrapped
20  * @see nsDataHashtable, nsClassHashtable
21  */
22 template <class KeyClass, class Interface>
23 class nsInterfaceHashtable
24     : public nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*> {
25  public:
26   typedef typename KeyClass::KeyType KeyType;
27   typedef Interface* UserDataType;
28   typedef nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*> base_type;
29 
nsInterfaceHashtable()30   nsInterfaceHashtable() {}
nsInterfaceHashtable(uint32_t aInitLength)31   explicit nsInterfaceHashtable(uint32_t aInitLength)
32       : nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>(
33             aInitLength) {}
34 
35   /**
36    * @copydoc nsBaseHashtable::Get
37    * @param aData This is an XPCOM getter, so aData is already_addrefed.
38    *   If the key doesn't exist, aData will be set to nullptr.
39    */
40   bool Get(KeyType aKey, UserDataType* aData) const;
41 
42   /**
43    * @copydoc nsBaseHashtable::Get
44    */
45   already_AddRefed<Interface> Get(KeyType aKey) const;
46 
47   /**
48    * Gets a weak reference to the hashtable entry.
49    * @param aFound If not nullptr, will be set to true if the entry is found,
50    *               to false otherwise.
51    * @return The entry, or nullptr if not found. Do not release this pointer!
52    */
53   Interface* GetWeak(KeyType aKey, bool* aFound = nullptr) const;
54 
55   /**
56    * Remove the entry associated with aKey (if any), optionally _moving_ its
57    * current value into *aData, thereby avoiding calls to AddRef and Release.
58    * Return true if found.
59    * @param aKey the key to remove from the hashtable
60    * @param aData where to move the value (if non-null).  If an entry is not
61    *              found it will be set to nullptr.
62    * @return true if an entry for aKey was found (and removed)
63    */
64   inline bool Remove(KeyType aKey, Interface** aData = nullptr);
65 };
66 
67 template <typename K, typename T>
ImplCycleCollectionUnlink(nsInterfaceHashtable<K,T> & aField)68 inline void ImplCycleCollectionUnlink(nsInterfaceHashtable<K, T>& aField) {
69   aField.Clear();
70 }
71 
72 template <typename K, typename T>
73 inline void ImplCycleCollectionTraverse(
74     nsCycleCollectionTraversalCallback& aCallback,
75     const nsInterfaceHashtable<K, T>& aField, const char* aName,
76     uint32_t aFlags = 0) {
77   for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
78     CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags);
79   }
80 }
81 
82 //
83 // nsInterfaceHashtable definitions
84 //
85 
86 template <class KeyClass, class Interface>
Get(KeyType aKey,UserDataType * aInterface)87 bool nsInterfaceHashtable<KeyClass, Interface>::Get(
88     KeyType aKey, UserDataType* aInterface) const {
89   typename base_type::EntryType* ent = this->GetEntry(aKey);
90 
91   if (ent) {
92     if (aInterface) {
93       *aInterface = ent->mData;
94 
95       NS_IF_ADDREF(*aInterface);
96     }
97 
98     return true;
99   }
100 
101   // if the key doesn't exist, set *aInterface to null
102   // so that it is a valid XPCOM getter
103   if (aInterface) {
104     *aInterface = nullptr;
105   }
106 
107   return false;
108 }
109 
110 template <class KeyClass, class Interface>
Get(KeyType aKey)111 already_AddRefed<Interface> nsInterfaceHashtable<KeyClass, Interface>::Get(
112     KeyType aKey) const {
113   typename base_type::EntryType* ent = this->GetEntry(aKey);
114   if (!ent) {
115     return nullptr;
116   }
117 
118   nsCOMPtr<Interface> copy = ent->mData;
119   return copy.forget();
120 }
121 
122 template <class KeyClass, class Interface>
GetWeak(KeyType aKey,bool * aFound)123 Interface* nsInterfaceHashtable<KeyClass, Interface>::GetWeak(
124     KeyType aKey, bool* aFound) const {
125   typename base_type::EntryType* ent = this->GetEntry(aKey);
126 
127   if (ent) {
128     if (aFound) {
129       *aFound = true;
130     }
131 
132     return ent->mData;
133   }
134 
135   // Key does not exist, return nullptr and set aFound to false
136   if (aFound) {
137     *aFound = false;
138   }
139   return nullptr;
140 }
141 
142 template <class KeyClass, class Interface>
Remove(KeyType aKey,Interface ** aData)143 bool nsInterfaceHashtable<KeyClass, Interface>::Remove(KeyType aKey,
144                                                        Interface** aData) {
145   typename base_type::EntryType* ent = this->GetEntry(aKey);
146 
147   if (ent) {
148     if (aData) {
149       ent->mData.forget(aData);
150     }
151     this->RemoveEntry(ent);
152     return true;
153   }
154 
155   if (aData) {
156     *aData = nullptr;
157   }
158   return false;
159 }
160 
161 #endif  // nsInterfaceHashtable_h__
162