tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsURIHashKey.h (1950B)


      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 <utility>
      9 
     10 #include "PLDHashTable.h"
     11 #include "nsCOMPtr.h"
     12 #include "nsHashKeys.h"
     13 #include "nsIURI.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  nsURIHashKey() { MOZ_COUNT_CTOR(nsURIHashKey); }
     24  explicit nsURIHashKey(const nsIURI* aKey) : mKey(const_cast<nsIURI*>(aKey)) {
     25    MOZ_COUNT_CTOR(nsURIHashKey);
     26  }
     27  nsURIHashKey(nsURIHashKey&& toMove)
     28      : PLDHashEntryHdr(std::move(toMove)), mKey(std::move(toMove.mKey)) {
     29    MOZ_COUNT_CTOR(nsURIHashKey);
     30  }
     31  MOZ_COUNTED_DTOR(nsURIHashKey)
     32 
     33  nsURIHashKey& operator=(const nsURIHashKey& aOther) {
     34    mKey = aOther.mKey;
     35    return *this;
     36  }
     37 
     38  nsIURI* GetKey() const { return mKey; }
     39 
     40  bool KeyEquals(const nsIURI* aKey) const {
     41    bool eq;
     42    if (!mKey) {
     43      return !aKey;
     44    }
     45    if (NS_SUCCEEDED(mKey->Equals(const_cast<nsIURI*>(aKey), &eq))) {
     46      return eq;
     47    }
     48    return false;
     49  }
     50 
     51  static const nsIURI* KeyToPointer(nsIURI* aKey) { return aKey; }
     52  static PLDHashNumber HashKey(const nsIURI* aKey) {
     53    if (!aKey) {
     54      // If the key is null, return hash for empty string.
     55      return mozilla::HashString(""_ns);
     56    }
     57    nsAutoCString spec;
     58    // If GetSpec() fails, ignoring the failure and proceeding with an
     59    // empty |spec| seems like the best thing to do.
     60    (void)const_cast<nsIURI*>(aKey)->GetSpec(spec);
     61    return mozilla::HashString(spec);
     62  }
     63 
     64  enum { ALLOW_MEMMOVE = true };
     65 
     66 protected:
     67  nsCOMPtr<nsIURI> mKey;
     68 };
     69 
     70 #endif  // nsURIHashKey_h__