bug 706554 - Add nsRefPtrHashKey. r=bsmedberg
authorTed Mielczarek <ted.mielczarek@gmail.com>
Wed, 30 Nov 2011 16:30:55 -0500
changeset 86142 043724be277131896b90db0c4c16c489e4436029
parent 86141 4c09a651bcc1040f73f61b088f62db34777db15c
child 86143 de34e4fefcade8bfbd7f2590fc426ad1cbeb2a8c
push id805
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 18:17:35 +0000
treeherdermozilla-aurora@6fb3bf232436 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbsmedberg
bugs706554
milestone12.0a1
bug 706554 - Add nsRefPtrHashKey. r=bsmedberg
xpcom/glue/nsHashKeys.h
--- a/xpcom/glue/nsHashKeys.h
+++ b/xpcom/glue/nsHashKeys.h
@@ -219,16 +219,49 @@ public:
   }
   enum { ALLOW_MEMMOVE = true };
 
 private:
   nsCOMPtr<nsISupports> mSupports;
 };
 
 /**
+ * hashkey wrapper using refcounted * KeyType
+ *
+ * @see nsTHashtable::EntryType for specification
+ */
+template<class T>
+class nsRefPtrHashKey : public PLDHashEntryHdr
+{
+public:
+  typedef T* KeyType;
+  typedef const T* KeyTypePointer;
+
+  nsRefPtrHashKey(const T* key) :
+    mKey(const_cast<T*>(key)) { }
+  nsRefPtrHashKey(const nsRefPtrHashKey& toCopy) :
+    mKey(toCopy.mKey) { }
+  ~nsRefPtrHashKey() { }
+
+  KeyType GetKey() const { return mKey; }
+  
+  bool KeyEquals(KeyTypePointer aKey) const { return aKey == mKey; }
+
+  static KeyTypePointer KeyToPointer(KeyType aKey) { return aKey; }
+  static PLDHashNumber HashKey(KeyTypePointer aKey)
+  {
+    return NS_PTR_TO_INT32(aKey) >>2;
+  }
+  enum { ALLOW_MEMMOVE = true };
+
+private:
+  nsRefPtr<T> mKey;
+};
+
+/**
  * hashkey wrapper using T* KeyType
  *
  * @see nsTHashtable::EntryType for specification
  */
 template<class T>
 class nsPtrHashKey : public PLDHashEntryHdr
 {
  public: