Commit c9477535 authored by ager@chromium.org's avatar ager@chromium.org

Oilpan: Allowing hashing of RawPtr.

R=erik.corry@gmail.com, eseidel@chromium.org, jochen@chromium.org
BUG=

Review URL: https://codereview.chromium.org/183833009

git-svn-id: svn://svn.chromium.org/blink/trunk@168309 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 5c9139aa
...@@ -727,10 +727,9 @@ template<typename T> struct HashTraits<WebCore::Member<T> > : SimpleClassHashTra ...@@ -727,10 +727,9 @@ template<typename T> struct HashTraits<WebCore::Member<T> > : SimpleClassHashTra
typedef RawPtr<T> PassInType; typedef RawPtr<T> PassInType;
typedef WebCore::Member<T>* IteratorGetType; typedef WebCore::Member<T>* IteratorGetType;
typedef const WebCore::Member<T>* IteratorConstGetType; typedef const WebCore::Member<T>* IteratorConstGetType;
typedef T* IteratorReferenceType; typedef WebCore::Member<T>& IteratorReferenceType;
typedef T* IteratorConstReferenceType; typedef T* const IteratorConstReferenceType;
static IteratorConstGetType getToConstGetConversion(const WebCore::Member<T>* x) { return x->get(); } static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; }
static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return x->get(); }
static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return x->get(); } static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return x->get(); }
// FIXME: Similarly, there is no need for a distinction between PeekOutType // FIXME: Similarly, there is no need for a distinction between PeekOutType
// and PassOutType without reference counting. // and PassOutType without reference counting.
...@@ -755,10 +754,9 @@ template<typename T> struct HashTraits<WebCore::WeakMember<T> > : SimpleClassHas ...@@ -755,10 +754,9 @@ template<typename T> struct HashTraits<WebCore::WeakMember<T> > : SimpleClassHas
typedef RawPtr<T> PassInType; typedef RawPtr<T> PassInType;
typedef WebCore::WeakMember<T>* IteratorGetType; typedef WebCore::WeakMember<T>* IteratorGetType;
typedef const WebCore::WeakMember<T>* IteratorConstGetType; typedef const WebCore::WeakMember<T>* IteratorConstGetType;
typedef T* IteratorReferenceType; typedef WebCore::WeakMember<T>& IteratorReferenceType;
typedef T* IteratorConstReferenceType; typedef T* const IteratorConstReferenceType;
static IteratorConstGetType getToConstGetConversion(const WebCore::WeakMember<T>* x) { return x->get(); } static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; }
static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return x->get(); }
static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return x->get(); } static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return x->get(); }
// FIXME: Similarly, there is no need for a distinction between PeekOutType // FIXME: Similarly, there is no need for a distinction between PeekOutType
// and PassOutType without reference counting. // and PassOutType without reference counting.
......
...@@ -2609,6 +2609,16 @@ TEST(HeapTest, EmbeddedInVector) ...@@ -2609,6 +2609,16 @@ TEST(HeapTest, EmbeddedInVector)
EXPECT_EQ(8, SimpleFinalizedObject::s_destructorCalls); EXPECT_EQ(8, SimpleFinalizedObject::s_destructorCalls);
} }
TEST(HeapTest, RawPtrInHash)
{
HashSet<RawPtr<int> > set;
set.add(new int(42));
set.add(new int(42));
EXPECT_EQ(2u, set.size());
for (HashSet<RawPtr<int> >::iterator it = set.begin(); it != set.end(); ++it)
EXPECT_EQ(42, **it);
}
} // WebCore namespace } // WebCore namespace
namespace WTF { namespace WTF {
......
...@@ -143,6 +143,14 @@ namespace WTF { ...@@ -143,6 +143,14 @@ namespace WTF {
static bool equal(P* a, const RefPtr<P>& b) { return a == b; } static bool equal(P* a, const RefPtr<P>& b) { return a == b; }
static bool equal(const RefPtr<P>& a, P* b) { return a == b; } static bool equal(const RefPtr<P>& a, P* b) { return a == b; }
}; };
template<typename P> struct PtrHash<RawPtr<P> > : PtrHash<P*> {
using PtrHash<P*>::hash;
static unsigned hash(const RawPtr<P>& key) { return hash(key.get()); }
using PtrHash<P*>::equal;
static bool equal(const RawPtr<P>& a, const RawPtr<P>& b) { return a == b; }
static bool equal(P* a, const RawPtr<P>& b) { return a == b; }
static bool equal(const RawPtr<P>& a, P* b) { return a == b; }
};
// default hash function for each type // default hash function for each type
...@@ -189,6 +197,7 @@ namespace WTF { ...@@ -189,6 +197,7 @@ namespace WTF {
template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; }; template<typename P> struct DefaultHash<P*> { typedef PtrHash<P*> Hash; };
template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; }; template<typename P> struct DefaultHash<RefPtr<P> > { typedef PtrHash<RefPtr<P> > Hash; };
template<typename P> struct DefaultHash<RawPtr<P> > { typedef PtrHash<RawPtr<P> > Hash; };
// make IntPairHash the default hash function for pairs of (at most) 32-bit integers. // make IntPairHash the default hash function for pairs of (at most) 32-bit integers.
......
...@@ -86,7 +86,6 @@ namespace WTF { ...@@ -86,7 +86,6 @@ namespace WTF {
typedef const T* IteratorConstGetType; typedef const T* IteratorConstGetType;
typedef T& IteratorReferenceType; typedef T& IteratorReferenceType;
typedef const T& IteratorConstReferenceType; typedef const T& IteratorConstReferenceType;
static IteratorConstGetType getToConstGetConversion(const T* x) { return x; }
static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; } static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; }
static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return *x; } static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return *x; }
// Type for functions that take ownership, such as add. // Type for functions that take ownership, such as add.
...@@ -191,6 +190,26 @@ namespace WTF { ...@@ -191,6 +190,26 @@ namespace WTF {
static PeekOutType peek(std::nullptr_t) { return 0; } static PeekOutType peek(std::nullptr_t) { return 0; }
}; };
template<typename T> struct HashTraits<RawPtr<T> > : SimpleClassHashTraits<RawPtr<T> > {
static const bool needsDestruction = false;
typedef T* PeekInType;
typedef T* PassInType;
typedef RawPtr<T>* IteratorGetType;
typedef const RawPtr<T>* IteratorConstGetType;
typedef RawPtr<T>& IteratorReferenceType;
typedef T* const IteratorConstReferenceType;
static IteratorReferenceType getToReferenceConversion(IteratorGetType x) { return *x; }
static IteratorConstReferenceType getToReferenceConstConversion(IteratorConstGetType x) { return x->get(); }
typedef T* PeekOutType;
typedef T* PassOutType;
template<typename U>
static void store(const U& value, RawPtr<T>& storage) { storage = value; }
static PeekOutType peek(const RawPtr<T>& value) { return value; }
static PassOutType passOut(const RawPtr<T>& value) { return value; }
};
template<> struct HashTraits<String> : SimpleClassHashTraits<String> { template<> struct HashTraits<String> : SimpleClassHashTraits<String> {
static const bool hasIsEmptyValueFunction = true; static const bool hasIsEmptyValueFunction = true;
static bool isEmptyValue(const String&); static bool isEmptyValue(const String&);
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#define WTF_RawPtr_h #define WTF_RawPtr_h
#include <algorithm> #include <algorithm>
#include "wtf/HashTableDeletedValueType.h"
// Ptr is a simple wrapper for a raw pointer that provides the // Ptr is a simple wrapper for a raw pointer that provides the
// interface (get, clear) of other pointer types such as RefPtr, // interface (get, clear) of other pointer types such as RefPtr,
...@@ -61,6 +62,10 @@ public: ...@@ -61,6 +62,10 @@ public:
{ {
} }
// Hash table deleted values, which are only constructed and never copied or destroyed.
RawPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
T* get() const { return m_ptr; } T* get() const { return m_ptr; }
void clear() { m_ptr = 0; } void clear() { m_ptr = 0; }
// FIXME: oilpan: Remove release and leakRef once we remove RefPtrWillBeRawPtr. // FIXME: oilpan: Remove release and leakRef once we remove RefPtrWillBeRawPtr.
...@@ -107,6 +112,7 @@ public: ...@@ -107,6 +112,7 @@ public:
std::swap(m_ptr, o.m_ptr); std::swap(m_ptr, o.m_ptr);
} }
static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
private: private:
T* m_ptr; T* m_ptr;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment