summaryrefslogtreecommitdiffstats
path: root/xpcom/ds/nsRefCountedHashtable.h
blob: e3a0456a09c15f46dc202705431574b30f801e05 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef XPCOM_DS_NSREFCOUNTEDHASHTABLE_H_
#define XPCOM_DS_NSREFCOUNTEDHASHTABLE_H_

#include "nsBaseHashtable.h"
#include "nsHashKeys.h"

/**
 * templated hashtable class maps keys to reference pointers.
 * See nsBaseHashtable for complete declaration.
 * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
 *   for a complete specification.
 * @param PtrType the reference-type being wrapped
 * @see nsClassHashtable, nsTHashMap
 */
template <class KeyClass, class PtrType>
class nsRefCountedHashtable
    : public nsBaseHashtable<
          KeyClass, PtrType,
          typename mozilla::detail::SmartPtrTraits<PtrType>::RawPointerType> {
 public:
  using KeyType = typename KeyClass::KeyType;
  using SmartPtrTraits = mozilla::detail::SmartPtrTraits<PtrType>;
  using PointeeType = typename SmartPtrTraits::PointeeType;
  using RawPointerType = typename SmartPtrTraits::RawPointerType;
  using base_type = nsBaseHashtable<KeyClass, PtrType, RawPointerType>;

  using base_type::base_type;

  static_assert(SmartPtrTraits::IsRefCounted);

  /**
   * @copydoc nsBaseHashtable::Get
   * @param aData This is an XPCOM getter, so aData is already_addrefed.
   *   If the key doesn't exist, *aData will be set to nullptr.
   */
  bool Get(KeyType aKey, RawPointerType* aData) const;

  /**
   * @copydoc nsBaseHashtable::Get
   */
  [[nodiscard]] already_AddRefed<PointeeType> Get(KeyType aKey) const;

  /**
   * Gets a weak reference to the hashtable entry.
   * @param aFound If not nullptr, will be set to true if the entry is found,
   *               to false otherwise.
   * @return The entry, or nullptr if not found. Do not release this pointer!
   */
  [[nodiscard]] RawPointerType GetWeak(KeyType aKey,
                                       bool* aFound = nullptr) const;

  using base_type::InsertOrUpdate;

  template <typename U,
            typename = std::enable_if_t<std::is_base_of_v<PointeeType, U>>>
  void InsertOrUpdate(
      KeyType aKey,
      typename SmartPtrTraits::template OtherSmartPtrType<U>&& aData);

  template <typename U,
            typename = std::enable_if_t<std::is_base_of_v<PointeeType, U>>>
  [[nodiscard]] bool InsertOrUpdate(
      KeyType aKey,
      typename SmartPtrTraits::template OtherSmartPtrType<U>&& aData,
      const mozilla::fallible_t&);

  template <typename U,
            typename = std::enable_if_t<std::is_base_of_v<PointeeType, U>>>
  void InsertOrUpdate(KeyType aKey, already_AddRefed<U>&& aData);

  template <typename U,
            typename = std::enable_if_t<std::is_base_of_v<PointeeType, U>>>
  [[nodiscard]] bool InsertOrUpdate(KeyType aKey, already_AddRefed<U>&& aData,
                                    const mozilla::fallible_t&);

  /**
   * Remove the entry associated with aKey (if any), optionally _moving_ its
   * current value into *aData, thereby avoiding calls to AddRef and Release.
   * Return true if found.
   * @param aKey the key to remove from the hashtable
   * @param aData where to move the value (if non-null).  If an entry is not
   *              found it will be set to nullptr.
   * @return true if an entry for aKey was found (and removed)
   */
  inline bool Remove(KeyType aKey, RawPointerType* aData = nullptr);

  nsRefCountedHashtable Clone() const {
    return this->template CloneAs<nsRefCountedHashtable>();
  }
};

template <typename K, typename T>
inline void ImplCycleCollectionUnlink(nsRefCountedHashtable<K, T>& aField) {
  aField.Clear();
}

template <typename K, typename T>
inline void ImplCycleCollectionTraverse(
    nsCycleCollectionTraversalCallback& aCallback,
    nsRefCountedHashtable<K, T>& aField, const char* aName,
    uint32_t aFlags = 0) {
  for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
    CycleCollectionNoteChild(aCallback, iter.UserData(), aName, aFlags);
  }
}

//
// nsRefCountedHashtable definitions
//

template <class KeyClass, class PtrType>
bool nsRefCountedHashtable<KeyClass, PtrType>::Get(
    KeyType aKey, RawPointerType* aRefPtr) const {
  typename base_type::EntryType* ent = this->GetEntry(aKey);

  if (ent) {
    if (aRefPtr) {
      *aRefPtr = ent->GetData();

      NS_IF_ADDREF(*aRefPtr);
    }

    return true;
  }

  // if the key doesn't exist, set *aRefPtr to null
  // so that it is a valid XPCOM getter
  if (aRefPtr) {
    *aRefPtr = nullptr;
  }

  return false;
}

template <class KeyClass, class PtrType>
already_AddRefed<typename nsRefCountedHashtable<KeyClass, PtrType>::PointeeType>
nsRefCountedHashtable<KeyClass, PtrType>::Get(KeyType aKey) const {
  typename base_type::EntryType* ent = this->GetEntry(aKey);
  if (!ent) {
    return nullptr;
  }

  PtrType copy = ent->GetData();
  return copy.forget();
}

template <class KeyClass, class PtrType>
typename nsRefCountedHashtable<KeyClass, PtrType>::RawPointerType
nsRefCountedHashtable<KeyClass, PtrType>::GetWeak(KeyType aKey,
                                                  bool* aFound) const {
  typename base_type::EntryType* ent = this->GetEntry(aKey);

  if (ent) {
    if (aFound) {
      *aFound = true;
    }

    return ent->GetData();
  }

  // Key does not exist, return nullptr and set aFound to false
  if (aFound) {
    *aFound = false;
  }

  return nullptr;
}

template <class KeyClass, class PtrType>
template <typename U, typename>
void nsRefCountedHashtable<KeyClass, PtrType>::InsertOrUpdate(
    KeyType aKey,
    typename SmartPtrTraits::template OtherSmartPtrType<U>&& aData) {
  if (!InsertOrUpdate(aKey, std::move(aData), mozilla::fallible)) {
    NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount());
  }
}

template <class KeyClass, class PtrType>
template <typename U, typename>
bool nsRefCountedHashtable<KeyClass, PtrType>::InsertOrUpdate(
    KeyType aKey,
    typename SmartPtrTraits::template OtherSmartPtrType<U>&& aData,
    const mozilla::fallible_t&) {
  typename base_type::EntryType* ent = this->PutEntry(aKey, mozilla::fallible);

  if (!ent) {
    return false;
  }

  ent->SetData(std::move(aData));

  return true;
}

template <class KeyClass, class PtrType>
template <typename U, typename>
void nsRefCountedHashtable<KeyClass, PtrType>::InsertOrUpdate(
    KeyType aKey, already_AddRefed<U>&& aData) {
  if (!InsertOrUpdate(aKey, std::move(aData), mozilla::fallible)) {
    NS_ABORT_OOM(this->mTable.EntrySize() * this->mTable.EntryCount());
  }
}

template <class KeyClass, class PtrType>
template <typename U, typename>
bool nsRefCountedHashtable<KeyClass, PtrType>::InsertOrUpdate(
    KeyType aKey, already_AddRefed<U>&& aData, const mozilla::fallible_t&) {
  typename base_type::EntryType* ent = this->PutEntry(aKey, mozilla::fallible);

  if (!ent) {
    return false;
  }

  ent->SetData(std::move(aData));

  return true;
}

template <class KeyClass, class PtrType>
bool nsRefCountedHashtable<KeyClass, PtrType>::Remove(KeyType aKey,
                                                      RawPointerType* aRefPtr) {
  typename base_type::EntryType* ent = this->GetEntry(aKey);

  if (ent) {
    if (aRefPtr) {
      ent->GetModifiableData()->forget(aRefPtr);
    }
    this->RemoveEntry(ent);
    return true;
  }

  if (aRefPtr) {
    *aRefPtr = nullptr;
  }
  return false;
}

#endif  // XPCOM_DS_NSREFCOUNTEDHASHTABLE_H_