Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/ds/nsRefPtrHashtable.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 nsRefPtrHashtable_h__
8
#define nsRefPtrHashtable_h__
9
10
#include "nsBaseHashtable.h"
11
#include "nsHashKeys.h"
12
#include "nsAutoPtr.h"
13
14
/**
15
 * templated hashtable class maps keys to reference 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 PtrType the reference-type being wrapped
20
 * @see nsDataHashtable, nsClassHashtable
21
 */
22
template<class KeyClass, class PtrType>
23
class nsRefPtrHashtable
24
  : public nsBaseHashtable<KeyClass, RefPtr<PtrType>, PtrType*>
25
{
26
public:
27
  typedef typename KeyClass::KeyType KeyType;
28
  typedef PtrType* UserDataType;
29
  typedef nsBaseHashtable<KeyClass, RefPtr<PtrType>, PtrType*> base_type;
30
31
0
  nsRefPtrHashtable() {}
32
  explicit nsRefPtrHashtable(uint32_t aInitLength)
33
    : nsBaseHashtable<KeyClass, RefPtr<PtrType>, PtrType*>(aInitLength)
34
  {
35
  }
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<PtrType> 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
  PtrType* GetWeak(KeyType aKey, bool* aFound = nullptr) const;
56
57
  // Overload Put, rather than overriding it.
58
  using base_type::Put;
59
60
  void Put(KeyType aKey, already_AddRefed<PtrType> aData);
61
62
  MOZ_MUST_USE bool Put(KeyType aKey, already_AddRefed<PtrType> aData,
63
                        const mozilla::fallible_t&);
64
65
  /**
66
   * Remove the entry associated with aKey (if any), optionally _moving_ its
67
   * current value into *aData, thereby avoiding calls to AddRef and Release.
68
   * Return true if found.
69
   * @param aKey the key to remove from the hashtable
70
   * @param aData where to move the value (if non-null).  If an entry is not
71
   *              found it will be set to nullptr.
72
   * @return true if an entry for aKey was found (and removed)
73
   */
74
  inline bool Remove(KeyType aKey, UserDataType* aData = nullptr);
75
};
76
77
template<typename K, typename T>
78
inline void
79
ImplCycleCollectionUnlink(nsRefPtrHashtable<K, T>& aField)
80
{
81
  aField.Clear();
82
}
83
84
template<typename K, typename T>
85
inline void
86
ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback& aCallback,
87
                            nsRefPtrHashtable<K, T>& aField,
88
                            const char* aName,
89
                            uint32_t aFlags = 0)
90
{
91
  for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
92
    CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags);
93
  }
94
}
95
96
//
97
// nsRefPtrHashtable definitions
98
//
99
100
template<class KeyClass, class PtrType>
101
bool
102
nsRefPtrHashtable<KeyClass, PtrType>::Get(KeyType aKey,
103
                                          UserDataType* aRefPtr) const
104
{
105
  typename base_type::EntryType* ent = this->GetEntry(aKey);
106
107
  if (ent) {
108
    if (aRefPtr) {
109
      *aRefPtr = ent->mData;
110
111
      NS_IF_ADDREF(*aRefPtr);
112
    }
113
114
    return true;
115
  }
116
117
  // if the key doesn't exist, set *aRefPtr to null
118
  // so that it is a valid XPCOM getter
119
  if (aRefPtr) {
120
    *aRefPtr = nullptr;
121
  }
122
123
  return false;
124
}
125
126
template<class KeyClass, class PtrType>
127
already_AddRefed<PtrType>
128
nsRefPtrHashtable<KeyClass, PtrType>::Get(KeyType aKey) const
129
{
130
  typename base_type::EntryType* ent = this->GetEntry(aKey);
131
  if (!ent) {
132
    return nullptr;
133
  }
134
135
  RefPtr<PtrType> copy = ent->mData;
136
  return copy.forget();
137
}
138
139
template<class KeyClass, class PtrType>
140
PtrType*
141
nsRefPtrHashtable<KeyClass, PtrType>::GetWeak(KeyType aKey, bool* aFound) const
142
{
143
  typename base_type::EntryType* ent = this->GetEntry(aKey);
144
145
  if (ent) {
146
    if (aFound) {
147
      *aFound = true;
148
    }
149
150
    return ent->mData;
151
  }
152
153
  // Key does not exist, return nullptr and set aFound to false
154
  if (aFound) {
155
    *aFound = false;
156
  }
157
158
  return nullptr;
159
}
160
161
template<class KeyClass, class PtrType>
162
void
163
nsRefPtrHashtable<KeyClass, PtrType>::Put(KeyType aKey,
164
                                          already_AddRefed<PtrType> aData)
165
{
166
  if (!Put(aKey, std::move(aData), mozilla::fallible)) {
167
    NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount());
168
  }
169
}
170
171
template<class KeyClass, class PtrType>
172
bool
173
nsRefPtrHashtable<KeyClass, PtrType>::Put(KeyType aKey,
174
                                          already_AddRefed<PtrType> aData,
175
                                          const mozilla::fallible_t&)
176
{
177
  typename base_type::EntryType* ent = this->PutEntry(aKey, mozilla::fallible);
178
179
  if (!ent) {
180
    return false;
181
  }
182
183
  ent->mData = aData;
184
185
  return true;
186
}
187
188
template<class KeyClass, class PtrType>
189
bool
190
nsRefPtrHashtable<KeyClass, PtrType>::Remove(KeyType aKey,
191
                                             UserDataType* aRefPtr)
192
{
193
  typename base_type::EntryType* ent = this->GetEntry(aKey);
194
195
  if (ent) {
196
    if (aRefPtr) {
197
      ent->mData.forget(aRefPtr);
198
    }
199
    this->RemoveEntry(ent);
200
    return true;
201
  }
202
203
  if (aRefPtr) {
204
    *aRefPtr = nullptr;
205
  }
206
  return false;
207
}
208
209
#endif // nsRefPtrHashtable_h__