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 
30   nsInterfaceHashtable() = default;
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    * Allows inserting a value into the hashtable, moving its owning reference
57    * count into the hashtable, avoiding an AddRef.
58    */
Put(KeyType aKey,already_AddRefed<Interface> && aData)59   void Put(KeyType aKey, already_AddRefed<Interface>&& aData) {
60     if (!Put(aKey, std::move(aData), mozilla::fallible)) {
61       NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount());
62     }
63   }
64 
65   [[nodiscard]] bool Put(KeyType aKey, already_AddRefed<Interface>&& aData,
66                          const mozilla::fallible_t&);
67   using base_type::Put;
68 
69   /**
70    * Remove the entry associated with aKey (if any), optionally _moving_ its
71    * current value into *aData, thereby avoiding calls to AddRef and Release.
72    * Return true if found.
73    * @param aKey the key to remove from the hashtable
74    * @param aData where to move the value (if non-null).  If an entry is not
75    *              found it will be set to nullptr.
76    * @return true if an entry for aKey was found (and removed)
77    */
78   inline bool Remove(KeyType aKey, Interface** aData = nullptr);
79 };
80 
81 template <typename K, typename T>
ImplCycleCollectionUnlink(nsInterfaceHashtable<K,T> & aField)82 inline void ImplCycleCollectionUnlink(nsInterfaceHashtable<K, T>& aField) {
83   aField.Clear();
84 }
85 
86 template <typename K, typename T>
87 inline void ImplCycleCollectionTraverse(
88     nsCycleCollectionTraversalCallback& aCallback,
89     const nsInterfaceHashtable<K, T>& aField, const char* aName,
90     uint32_t aFlags = 0) {
91   for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
92     CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags);
93   }
94 }
95 
96 //
97 // nsInterfaceHashtable definitions
98 //
99 
100 template <class KeyClass, class Interface>
Get(KeyType aKey,UserDataType * aInterface)101 bool nsInterfaceHashtable<KeyClass, Interface>::Get(
102     KeyType aKey, UserDataType* aInterface) const {
103   typename base_type::EntryType* ent = this->GetEntry(aKey);
104 
105   if (ent) {
106     if (aInterface) {
107       *aInterface = ent->GetData();
108 
109       NS_IF_ADDREF(*aInterface);
110     }
111 
112     return true;
113   }
114 
115   // if the key doesn't exist, set *aInterface to null
116   // so that it is a valid XPCOM getter
117   if (aInterface) {
118     *aInterface = nullptr;
119   }
120 
121   return false;
122 }
123 
124 template <class KeyClass, class Interface>
Get(KeyType aKey)125 already_AddRefed<Interface> nsInterfaceHashtable<KeyClass, Interface>::Get(
126     KeyType aKey) const {
127   typename base_type::EntryType* ent = this->GetEntry(aKey);
128   if (!ent) {
129     return nullptr;
130   }
131 
132   nsCOMPtr<Interface> copy = ent->GetData();
133   return copy.forget();
134 }
135 
136 template <class KeyClass, class Interface>
GetWeak(KeyType aKey,bool * aFound)137 Interface* nsInterfaceHashtable<KeyClass, Interface>::GetWeak(
138     KeyType aKey, bool* aFound) const {
139   typename base_type::EntryType* ent = this->GetEntry(aKey);
140 
141   if (ent) {
142     if (aFound) {
143       *aFound = true;
144     }
145 
146     return ent->GetData();
147   }
148 
149   // Key does not exist, return nullptr and set aFound to false
150   if (aFound) {
151     *aFound = false;
152   }
153   return nullptr;
154 }
155 
156 template <class KeyClass, class Interface>
Put(KeyType aKey,already_AddRefed<Interface> && aValue,const mozilla::fallible_t &)157 bool nsInterfaceHashtable<KeyClass, Interface>::Put(
158     KeyType aKey, already_AddRefed<Interface>&& aValue,
159     const mozilla::fallible_t&) {
160   typename base_type::EntryType* ent = this->PutEntry(aKey);
161   if (!ent) {
162     return false;
163   }
164 
165   ent->SetData(std::move(aValue));
166   return true;
167 }
168 
169 template <class KeyClass, class Interface>
Remove(KeyType aKey,Interface ** aData)170 bool nsInterfaceHashtable<KeyClass, Interface>::Remove(KeyType aKey,
171                                                        Interface** aData) {
172   typename base_type::EntryType* ent = this->GetEntry(aKey);
173 
174   if (ent) {
175     if (aData) {
176       ent->GetModifiableData()->forget(aData);
177     }
178     this->RemoveEntry(ent);
179     return true;
180   }
181 
182   if (aData) {
183     *aData = nullptr;
184   }
185   return false;
186 }
187 
188 #endif  // nsInterfaceHashtable_h__
189