Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/ds/nsInterfaceHashtable.h
Line
Count
Source (jump to first uncovered line)
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
{
26
public:
27
  typedef typename KeyClass::KeyType KeyType;
28
  typedef Interface* UserDataType;
29
  typedef nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*> base_type;
30
31
6
  nsInterfaceHashtable() {}
nsInterfaceHashtable<nsISupportsHashKey, nsIConsoleListener>::nsInterfaceHashtable()
Line
Count
Source
31
1
  nsInterfaceHashtable() {}
nsInterfaceHashtable<nsStringHashKey, nsIVariant>::nsInterfaceHashtable()
Line
Count
Source
31
5
  nsInterfaceHashtable() {}
32
  explicit nsInterfaceHashtable(uint32_t aInitLength)
33
    : nsBaseHashtable<KeyClass, nsCOMPtr<Interface>, Interface*>(aInitLength)
34
0
  {
35
0
  }
36
37
  /**
38
   * @copydoc nsBaseHashtable::Get
39
   * @param aData This is an XPCOM getter, so aData is already_addrefed.
40
   *   If the key doesn't exist, aData will be set to nullptr.
41
   */
42
  bool Get(KeyType aKey, UserDataType* aData) const;
43
44
  /**
45
   * @copydoc nsBaseHashtable::Get
46
   */
47
  already_AddRefed<Interface> Get(KeyType aKey) const;
48
49
  /**
50
   * Gets a weak reference to the hashtable entry.
51
   * @param aFound If not nullptr, will be set to true if the entry is found,
52
   *               to false otherwise.
53
   * @return The entry, or nullptr if not found. Do not release this pointer!
54
   */
55
  Interface* GetWeak(KeyType aKey, bool* aFound = nullptr) const;
56
57
  /**
58
   * Allows inserting a value into the hashtable, moving its owning reference
59
   * count into the hashtable, avoiding an AddRef.
60
   */
61
  void Put(KeyType aKey, already_AddRefed<Interface>&& aData)
62
0
  {
63
0
    if (!Put(aKey, std::move(aData), mozilla::fallible)) {
64
0
      NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount());
65
0
    }
66
0
  }
67
68
  MOZ_MUST_USE bool Put(KeyType aKey, already_AddRefed<Interface>&& aData,
69
                        const mozilla::fallible_t&);
70
  using base_type::Put;
71
72
  /**
73
   * Remove the entry associated with aKey (if any), optionally _moving_ its
74
   * current value into *aData, thereby avoiding calls to AddRef and Release.
75
   * Return true if found.
76
   * @param aKey the key to remove from the hashtable
77
   * @param aData where to move the value (if non-null).  If an entry is not
78
   *              found it will be set to nullptr.
79
   * @return true if an entry for aKey was found (and removed)
80
   */
81
  inline bool Remove(KeyType aKey, Interface** aData = nullptr);
82
};
83
84
template<typename K, typename T>
85
inline void
86
ImplCycleCollectionUnlink(nsInterfaceHashtable<K, T>& aField)
87
0
{
88
0
  aField.Clear();
89
0
}
90
91
template<typename K, typename T>
92
inline void
93
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
94
                            const nsInterfaceHashtable<K, T>& aField,
95
                            const char* aName,
96
                            uint32_t aFlags = 0)
97
0
{
98
0
  for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
99
0
    CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags);
100
0
  }
101
0
}
102
103
//
104
// nsInterfaceHashtable definitions
105
//
106
107
template<class KeyClass, class Interface>
108
bool
109
nsInterfaceHashtable<KeyClass, Interface>::Get(KeyType aKey,
110
                                               UserDataType* aInterface) const
111
0
{
112
0
  typename base_type::EntryType* ent = this->GetEntry(aKey);
113
0
114
0
  if (ent) {
115
0
    if (aInterface) {
116
0
      *aInterface = ent->mData;
117
0
118
0
      NS_IF_ADDREF(*aInterface);
119
0
    }
120
0
121
0
    return true;
122
0
  }
123
0
124
0
  // if the key doesn't exist, set *aInterface to null
125
0
  // so that it is a valid XPCOM getter
126
0
  if (aInterface) {
127
0
    *aInterface = nullptr;
128
0
  }
129
0
130
0
  return false;
131
0
}
Unexecuted instantiation: nsInterfaceHashtable<nsStringHashKey, nsIVariant>::Get(nsTSubstring<char16_t> const&, nsIVariant**) const
Unexecuted instantiation: nsInterfaceHashtable<nsCharPtrHashKey, nsISupports>::Get(char const*, nsISupports**) const
132
133
template<class KeyClass, class Interface>
134
already_AddRefed<Interface>
135
nsInterfaceHashtable<KeyClass, Interface>::Get(KeyType aKey) const
136
{
137
  typename base_type::EntryType* ent = this->GetEntry(aKey);
138
  if (!ent) {
139
    return nullptr;
140
  }
141
142
  nsCOMPtr<Interface> copy = ent->mData;
143
  return copy.forget();
144
}
145
146
template<class KeyClass, class Interface>
147
Interface*
148
nsInterfaceHashtable<KeyClass, Interface>::GetWeak(KeyType aKey,
149
                                                   bool* aFound) const
150
0
{
151
0
  typename base_type::EntryType* ent = this->GetEntry(aKey);
152
0
153
0
  if (ent) {
154
0
    if (aFound) {
155
0
      *aFound = true;
156
0
    }
157
0
158
0
    return ent->mData;
159
0
  }
160
0
161
0
  // Key does not exist, return nullptr and set aFound to false
162
0
  if (aFound) {
163
0
    *aFound = false;
164
0
  }
165
0
  return nullptr;
166
0
}
Unexecuted instantiation: nsInterfaceHashtable<nsISupportsHashKey, nsIConsoleListener>::GetWeak(nsISupports*, bool*) const
Unexecuted instantiation: nsInterfaceHashtable<nsStringHashKey, nsIVariant>::GetWeak(nsTSubstring<char16_t> const&, bool*) const
167
168
template<class KeyClass, class Interface>
169
bool
170
nsInterfaceHashtable<KeyClass, Interface>::Put(KeyType aKey,
171
                                               already_AddRefed<Interface>&& aValue,
172
                                               const mozilla::fallible_t&)
173
0
{
174
0
  typename base_type::EntryType* ent = this->PutEntry(aKey);
175
0
  if (!ent) {
176
0
    return false;
177
0
  }
178
0
179
0
  ent->mData = aValue;
180
0
  return true;
181
0
}
182
183
template<class KeyClass, class Interface>
184
bool
185
nsInterfaceHashtable<KeyClass, Interface>::Remove(KeyType aKey,
186
                                                  Interface** aData)
187
0
{
188
0
  typename base_type::EntryType* ent = this->GetEntry(aKey);
189
0
190
0
  if (ent) {
191
0
    if (aData) {
192
0
      ent->mData.forget(aData);
193
0
    }
194
0
    this->RemoveEntry(ent);
195
0
    return true;
196
0
  }
197
0
198
0
  if (aData) {
199
0
    *aData = nullptr;
200
0
  }
201
0
  return false;
202
0
}
Unexecuted instantiation: nsInterfaceHashtable<nsISupportsHashKey, nsIConsoleListener>::Remove(nsISupports*, nsIConsoleListener**)
Unexecuted instantiation: nsInterfaceHashtable<nsStringHashKey, nsIVariant>::Remove(nsTSubstring<char16_t> const&, nsIVariant**)
Unexecuted instantiation: nsInterfaceHashtable<nsCharPtrHashKey, nsISupports>::Remove(char const*, nsISupports**)
Unexecuted instantiation: nsInterfaceHashtable<nsPtrHashKey<mozilla::dom::Element>, mozilla::dom::Element>::Remove(mozilla::dom::Element*, mozilla::dom::Element**)
203
204
#endif // nsInterfaceHashtable_h__