Bug 1860492 - Change file name in test @ toolkit/components/antitracking/test/browser...
[gecko.git] / xpcom / ds / nsClassHashtable.h
bloba1a2384f7e9564cec25fdc5345ffbe71bb0965eb
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/. */
7 #ifndef nsClassHashtable_h__
8 #define nsClassHashtable_h__
10 #include <utility>
12 #include "mozilla/UniquePtr.h"
13 #include "nsBaseHashtable.h"
14 #include "nsHashKeys.h"
16 /**
17 * Helper class that provides methods to wrap and unwrap the UserDataType.
19 template <class T>
20 class nsUniquePtrConverter {
21 public:
22 using UserDataType = T*;
23 using DataType = mozilla::UniquePtr<T>;
25 static UserDataType Unwrap(DataType& src) { return src.get(); }
26 static DataType Wrap(UserDataType&& src) { return DataType(std::move(src)); }
27 static DataType Wrap(const UserDataType& src) { return DataType(src); }
30 /**
31 * templated hashtable class maps keys to C++ object pointers.
32 * See nsBaseHashtable for complete declaration.
33 * @param KeyClass a wrapper-class for the hashtable key, see nsHashKeys.h
34 * for a complete specification.
35 * @param Class the class-type being wrapped
36 * @see nsInterfaceHashtable, nsClassHashtable
38 template <class KeyClass, class T>
39 class nsClassHashtable : public nsBaseHashtable<KeyClass, mozilla::UniquePtr<T>,
40 T*, nsUniquePtrConverter<T>> {
41 public:
42 typedef typename KeyClass::KeyType KeyType;
43 typedef T* UserDataType;
44 typedef nsBaseHashtable<KeyClass, mozilla::UniquePtr<T>, T*,
45 nsUniquePtrConverter<T>>
46 base_type;
48 using base_type::IsEmpty;
49 using base_type::Remove;
51 nsClassHashtable() = default;
52 explicit nsClassHashtable(uint32_t aInitLength) : base_type(aInitLength) {}
54 /**
55 * @copydoc nsBaseHashtable::Get
56 * @param aData if the key doesn't exist, pData will be set to nullptr.
58 bool Get(KeyType aKey, UserDataType* aData) const;
60 /**
61 * @copydoc nsBaseHashtable::Get
62 * @returns nullptr if the key is not present.
64 [[nodiscard]] UserDataType Get(KeyType aKey) const;
67 template <typename K, typename T>
68 inline void ImplCycleCollectionUnlink(nsClassHashtable<K, T>& aField) {
69 aField.Clear();
72 template <typename K, typename T>
73 inline void ImplCycleCollectionTraverse(
74 nsCycleCollectionTraversalCallback& aCallback,
75 const nsClassHashtable<K, T>& aField, const char* aName,
76 uint32_t aFlags = 0) {
77 for (auto iter = aField.ConstIter(); !iter.Done(); iter.Next()) {
78 ImplCycleCollectionTraverse(aCallback, *iter.UserData(), aName, aFlags);
83 // nsClassHashtable definitions
86 template <class KeyClass, class T>
87 bool nsClassHashtable<KeyClass, T>::Get(KeyType aKey, T** aRetVal) const {
88 typename base_type::EntryType* ent = this->GetEntry(aKey);
90 if (ent) {
91 if (aRetVal) {
92 *aRetVal = ent->GetData().get();
95 return true;
98 if (aRetVal) {
99 *aRetVal = nullptr;
102 return false;
105 template <class KeyClass, class T>
106 T* nsClassHashtable<KeyClass, T>::Get(KeyType aKey) const {
107 typename base_type::EntryType* ent = this->GetEntry(aKey);
108 if (!ent) {
109 return nullptr;
112 return ent->GetData().get();
115 #endif // nsClassHashtable_h__