Backed out 2 changesets (bug 1881078, bug 1879806) for causing dt failures @ devtools...
[gecko.git] / netwerk / base / nsURIHashKey.h
blobfb98ae0d21f5bd4e4a653174723fd389931e6157
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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__
8 #include <utility>
10 #include "PLDHashTable.h"
11 #include "mozilla/Unused.h"
12 #include "nsCOMPtr.h"
13 #include "nsHashKeys.h"
14 #include "nsIURI.h"
16 /**
17 * Hashtable key class to use with nsTHashtable/nsBaseHashtable
19 class nsURIHashKey : public PLDHashEntryHdr {
20 public:
21 typedef nsIURI* KeyType;
22 typedef const nsIURI* KeyTypePointer;
24 nsURIHashKey() { MOZ_COUNT_CTOR(nsURIHashKey); }
25 explicit nsURIHashKey(const nsIURI* aKey) : mKey(const_cast<nsIURI*>(aKey)) {
26 MOZ_COUNT_CTOR(nsURIHashKey);
28 nsURIHashKey(nsURIHashKey&& toMove)
29 : PLDHashEntryHdr(std::move(toMove)), mKey(std::move(toMove.mKey)) {
30 MOZ_COUNT_CTOR(nsURIHashKey);
32 MOZ_COUNTED_DTOR(nsURIHashKey)
34 nsURIHashKey& operator=(const nsURIHashKey& aOther) {
35 mKey = aOther.mKey;
36 return *this;
39 nsIURI* GetKey() const { return mKey; }
41 bool KeyEquals(const nsIURI* aKey) const {
42 bool eq;
43 if (!mKey) {
44 return !aKey;
46 if (NS_SUCCEEDED(mKey->Equals(const_cast<nsIURI*>(aKey), &eq))) {
47 return eq;
49 return false;
52 static const nsIURI* KeyToPointer(nsIURI* aKey) { return aKey; }
53 static PLDHashNumber HashKey(const nsIURI* aKey) {
54 if (!aKey) {
55 // If the key is null, return hash for empty string.
56 return mozilla::HashString(""_ns);
58 nsAutoCString spec;
59 // If GetSpec() fails, ignoring the failure and proceeding with an
60 // empty |spec| seems like the best thing to do.
61 mozilla::Unused << const_cast<nsIURI*>(aKey)->GetSpec(spec);
62 return mozilla::HashString(spec);
65 enum { ALLOW_MEMMOVE = true };
67 protected:
68 nsCOMPtr<nsIURI> mKey;
71 #endif // nsURIHashKey_h__