Line data Source code
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/Unused.h"
13 :
14 : /**
15 : * Hashtable key class to use with nsTHashtable/nsBaseHashtable
16 : */
17 : class nsURIHashKey : public PLDHashEntryHdr
18 : {
19 : public:
20 : typedef nsIURI* KeyType;
21 : typedef const nsIURI* KeyTypePointer;
22 :
23 359 : explicit nsURIHashKey(const nsIURI* aKey) :
24 359 : mKey(const_cast<nsIURI*>(aKey)) { MOZ_COUNT_CTOR(nsURIHashKey); }
25 42 : nsURIHashKey(const nsURIHashKey& toCopy) :
26 42 : mKey(toCopy.mKey) { MOZ_COUNT_CTOR(nsURIHashKey); }
27 187 : ~nsURIHashKey() { MOZ_COUNT_DTOR(nsURIHashKey); }
28 :
29 40 : nsIURI* GetKey() const { return mKey; }
30 :
31 1294 : bool KeyEquals(const nsIURI* aKey) const {
32 : bool eq;
33 1294 : if (!mKey) {
34 0 : return !aKey;
35 : }
36 1294 : if (NS_SUCCEEDED(mKey->Equals(const_cast<nsIURI*>(aKey), &eq))) {
37 1294 : return eq;
38 : }
39 0 : return false;
40 : }
41 :
42 1936 : static const nsIURI* KeyToPointer(nsIURI* aKey) { return aKey; }
43 1978 : static PLDHashNumber HashKey(const nsIURI* aKey) {
44 1978 : if (!aKey) {
45 : // If the key is null, return hash for empty string.
46 0 : return mozilla::HashString(EmptyCString());
47 : }
48 3956 : nsAutoCString spec;
49 : // If GetSpec() fails, ignoring the failure and proceeding with an
50 : // empty |spec| seems like the best thing to do.
51 1978 : mozilla::Unused << const_cast<nsIURI*>(aKey)->GetSpec(spec);
52 1978 : return mozilla::HashString(spec);
53 : }
54 :
55 : enum { ALLOW_MEMMOVE = true };
56 :
57 : protected:
58 : nsCOMPtr<nsIURI> mKey;
59 : };
60 :
61 : #endif // nsURIHashKey_h__
|