nsPointerHashKeys.h 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /* -*- Mode: C++; tab-width: 8; 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. /* Definitions for nsPtrHashKey<T> and nsVoidPtrHashKey. */
  6. #ifndef nsPointerHashKeys_h
  7. #define nsPointerHashKeys_h
  8. #include "nscore.h"
  9. #include "mozilla/Attributes.h"
  10. /**
  11. * hashkey wrapper using T* KeyType
  12. *
  13. * @see nsTHashtable::EntryType for specification
  14. */
  15. template<class T>
  16. class nsPtrHashKey : public PLDHashEntryHdr
  17. {
  18. public:
  19. typedef T* KeyType;
  20. typedef const T* KeyTypePointer;
  21. explicit nsPtrHashKey(const T* aKey) : mKey(const_cast<T*>(aKey)) {}
  22. nsPtrHashKey(const nsPtrHashKey<T>& aToCopy) : mKey(aToCopy.mKey) {}
  23. ~nsPtrHashKey() {}
  24. KeyType GetKey() const { return mKey; }
  25. bool KeyEquals(KeyTypePointer aKey) const { return aKey == mKey; }
  26. static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
  27. static PLDHashNumber HashKey(KeyTypePointer aKey)
  28. {
  29. return NS_PTR_TO_UINT32(aKey) >> 2;
  30. }
  31. enum { ALLOW_MEMMOVE = true };
  32. protected:
  33. T* MOZ_NON_OWNING_REF mKey;
  34. };
  35. typedef nsPtrHashKey<const void> nsVoidPtrHashKey;
  36. #endif // nsPointerHashKeys_h