/work/obj-fuzz/dist/include/nsURIHashKey.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
3 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
4 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
5 | | #ifndef nsURIHashKey_h__ |
6 | | #define nsURIHashKey_h__ |
7 | | |
8 | | #include "PLDHashTable.h" |
9 | | #include "nsCOMPtr.h" |
10 | | #include "nsIURI.h" |
11 | | #include "nsHashKeys.h" |
12 | | #include "mozilla/Move.h" |
13 | | #include "mozilla/Unused.h" |
14 | | |
15 | | /** |
16 | | * Hashtable key class to use with nsTHashtable/nsBaseHashtable |
17 | | */ |
18 | | class nsURIHashKey : public PLDHashEntryHdr |
19 | | { |
20 | | public: |
21 | | typedef nsIURI* KeyType; |
22 | | typedef const nsIURI* KeyTypePointer; |
23 | | |
24 | | explicit nsURIHashKey(const nsIURI* aKey) : |
25 | | mKey(const_cast<nsIURI*>(aKey)) { MOZ_COUNT_CTOR(nsURIHashKey); } |
26 | | nsURIHashKey(nsURIHashKey&& toMove) |
27 | | : PLDHashEntryHdr(std::move(toMove)) |
28 | | , mKey(std::move(toMove.mKey)) |
29 | 0 | { |
30 | 0 | MOZ_COUNT_CTOR(nsURIHashKey); |
31 | 0 | } |
32 | | ~nsURIHashKey() { MOZ_COUNT_DTOR(nsURIHashKey); } |
33 | | |
34 | | nsIURI* GetKey() const { return mKey; } |
35 | | |
36 | | bool KeyEquals(const nsIURI* aKey) const { |
37 | | bool eq; |
38 | | if (!mKey) { |
39 | | return !aKey; |
40 | | } |
41 | | if (NS_SUCCEEDED(mKey->Equals(const_cast<nsIURI*>(aKey), &eq))) { |
42 | | return eq; |
43 | | } |
44 | | return false; |
45 | | } |
46 | | |
47 | | static const nsIURI* KeyToPointer(nsIURI* aKey) { return aKey; } |
48 | | static PLDHashNumber HashKey(const nsIURI* aKey) { |
49 | | if (!aKey) { |
50 | | // If the key is null, return hash for empty string. |
51 | | return mozilla::HashString(EmptyCString()); |
52 | | } |
53 | | nsAutoCString spec; |
54 | | // If GetSpec() fails, ignoring the failure and proceeding with an |
55 | | // empty |spec| seems like the best thing to do. |
56 | | mozilla::Unused << const_cast<nsIURI*>(aKey)->GetSpec(spec); |
57 | | return mozilla::HashString(spec); |
58 | | } |
59 | | |
60 | | enum { ALLOW_MEMMOVE = true }; |
61 | | |
62 | | protected: |
63 | | nsCOMPtr<nsIURI> mKey; |
64 | | }; |
65 | | |
66 | | #endif // nsURIHashKey_h__ |