Source code

Revision control

Other Tools

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__
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
public:
20
typedef nsIURI* KeyType;
21
typedef const nsIURI* KeyTypePointer;
22
23
explicit nsURIHashKey(const nsIURI* aKey) : mKey(const_cast<nsIURI*>(aKey)) {
24
MOZ_COUNT_CTOR(nsURIHashKey);
25
}
26
nsURIHashKey(nsURIHashKey&& toMove)
27
: PLDHashEntryHdr(std::move(toMove)), mKey(std::move(toMove.mKey)) {
28
MOZ_COUNT_CTOR(nsURIHashKey);
29
}
30
~nsURIHashKey() { MOZ_COUNT_DTOR(nsURIHashKey); }
31
32
nsIURI* GetKey() const { return mKey; }
33
34
bool KeyEquals(const nsIURI* aKey) const {
35
bool eq;
36
if (!mKey) {
37
return !aKey;
38
}
39
if (NS_SUCCEEDED(mKey->Equals(const_cast<nsIURI*>(aKey), &eq))) {
40
return eq;
41
}
42
return false;
43
}
44
45
static const nsIURI* KeyToPointer(nsIURI* aKey) { return aKey; }
46
static PLDHashNumber HashKey(const nsIURI* aKey) {
47
if (!aKey) {
48
// If the key is null, return hash for empty string.
49
return mozilla::HashString(EmptyCString());
50
}
51
nsAutoCString spec;
52
// If GetSpec() fails, ignoring the failure and proceeding with an
53
// empty |spec| seems like the best thing to do.
54
mozilla::Unused << const_cast<nsIURI*>(aKey)->GetSpec(spec);
55
return mozilla::HashString(spec);
56
}
57
58
enum { ALLOW_MEMMOVE = true };
59
60
protected:
61
nsCOMPtr<nsIURI> mKey;
62
};
63
64
#endif // nsURIHashKey_h__