Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(193)

Unified Diff: third_party/WebKit/Source/core/dom/WeakIdentifierMap.h

Issue 2086643002: Clean up WeakIdentifierMap<> implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/core/dom/WeakIdentifierMap.h
diff --git a/third_party/WebKit/Source/core/dom/WeakIdentifierMap.h b/third_party/WebKit/Source/core/dom/WeakIdentifierMap.h
index aa0791a7da332549d67e25fc24a8576c7fce50a0..b430f3375b52a2d7527c8546b54b8c28488de2d8 100644
--- a/third_party/WebKit/Source/core/dom/WeakIdentifierMap.h
+++ b/third_party/WebKit/Source/core/dom/WeakIdentifierMap.h
@@ -8,97 +8,48 @@
#include "platform/heap/Handle.h"
#include "wtf/Allocator.h"
#include "wtf/HashMap.h"
-#include "wtf/Vector.h"
namespace blink {
-template<typename T> struct IdentifierGenerator;
+// TODO(sof): WeakIdentifierMap<> belongs (out) in wtf/, but
+// cannot until GarbageCollected<> can be used from WTF.
-template<> struct IdentifierGenerator<int> {
- STATIC_ONLY(IdentifierGenerator);
- using IdentifierType = int;
- static IdentifierType next()
- {
- static int s_lastId = 0;
- return ++s_lastId;
- }
-};
+template<typename T, typename IdentifierType, bool isGarbageCollected>
+class WeakIdentifierMapBase {
+ USING_FAST_MALLOC(WeakIdentifierMapBase);
+protected:
+ using ObjectToIdentifier = HashMap<T*, IdentifierType>;
+ using IdentifierToObject = HashMap<IdentifierType, T*>;
-template<typename T> struct WeakIdentifierMapTraits {
- STATIC_ONLY(WeakIdentifierMapTraits);
- static void removedFromIdentifierMap(T*) { }
- static void addedToIdentifierMap(T*) { }
+ ObjectToIdentifier m_objectToIdentifier;
+ IdentifierToObject m_identifierToObject;
};
-template<typename T,
- typename Generator = IdentifierGenerator<int>,
- typename Traits = WeakIdentifierMapTraits<T>,
- bool isGarbageCollected = IsGarbageCollectedType<T>::value> class WeakIdentifierMap;
-
-template<typename T, typename Generator, typename Traits> class WeakIdentifierMap<T, Generator, Traits, false> {
- USING_FAST_MALLOC(WeakIdentifierMap);
+template<typename T, typename IdentifierType>
+class WeakIdentifierMapBase<T, IdentifierType, true> : public GarbageCollected<WeakIdentifierMapBase<T, IdentifierType, true>> {
public:
- using IdentifierType = typename Generator::IdentifierType;
-
- static IdentifierType identifier(T* object)
- {
- IdentifierType result = instance().m_objectToIdentifier.get(object);
-
- if (WTF::isHashTraitsEmptyValue<HashTraits<IdentifierType>>(result)) {
- result = Generator::next();
- instance().put(object, result);
- }
- return result;
- }
-
- static T* lookup(IdentifierType identifier)
- {
- return instance().m_identifierToObject.get(identifier);
- }
-
- static void notifyObjectDestroyed(T* object)
- {
- instance().objectDestroyed(object);
- }
-
-private:
- static WeakIdentifierMap<T, Generator, Traits>& instance();
- WeakIdentifierMap() { }
- ~WeakIdentifierMap();
-
- void put(T* object, IdentifierType identifier)
- {
- DCHECK(object && !m_objectToIdentifier.contains(object));
- m_objectToIdentifier.set(object, identifier);
- m_identifierToObject.set(identifier, object);
- Traits::addedToIdentifierMap(object);
- }
-
- void objectDestroyed(T* object)
+ DEFINE_INLINE_TRACE()
{
- IdentifierType identifier = m_objectToIdentifier.take(object);
- if (!WTF::isHashTraitsEmptyValue<HashTraits<IdentifierType>>(identifier))
- m_identifierToObject.remove(identifier);
+ visitor->trace(m_objectToIdentifier);
+ visitor->trace(m_identifierToObject);
}
-
- using ObjectToIdentifier = HashMap<T*, IdentifierType>;
- using IdentifierToObject = HashMap<IdentifierType, T*>;
+protected:
+ using ObjectToIdentifier = HeapHashMap<WeakMember<T>, IdentifierType>;
+ using IdentifierToObject = HeapHashMap<IdentifierType, WeakMember<T>>;
ObjectToIdentifier m_objectToIdentifier;
IdentifierToObject m_identifierToObject;
};
-template<typename T, typename Generator, typename Traits> class WeakIdentifierMap<T, Generator, Traits, true>
- : public GarbageCollected<WeakIdentifierMap<T, Generator, Traits, true>> {
+template<typename T, typename IdentifierType = int>
+class WeakIdentifierMap final : public WeakIdentifierMapBase<T, IdentifierType, IsGarbageCollectedType<T>::value> {
public:
- using IdentifierType = typename Generator::IdentifierType;
-
static IdentifierType identifier(T* object)
{
- IdentifierType result = instance().m_objectToIdentifier->get(object);
+ IdentifierType result = instance().m_objectToIdentifier.get(object);
if (WTF::isHashTraitsEmptyValue<HashTraits<IdentifierType>>(result)) {
- result = Generator::next();
+ result = next();
instance().put(object, result);
}
return result;
@@ -106,38 +57,38 @@ public:
static T* lookup(IdentifierType identifier)
{
- return instance().m_identifierToObject->get(identifier);
+ return instance().m_identifierToObject.get(identifier);
}
- static void notifyObjectDestroyed(T* object) { }
-
- DEFINE_INLINE_TRACE()
+ static void notifyObjectDestroyed(T* object)
{
- visitor->trace(m_objectToIdentifier);
- visitor->trace(m_identifierToObject);
+ instance().objectDestroyed(object);
}
private:
- static WeakIdentifierMap<T, Generator, Traits>& instance();
+ static WeakIdentifierMap<T, IdentifierType>& instance();
+
+ WeakIdentifierMap() { }
- WeakIdentifierMap()
- : m_objectToIdentifier(new ObjectToIdentifier())
- , m_identifierToObject(new IdentifierToObject())
+ static IdentifierType next()
{
+ static IdentifierType s_lastId = 0;
+ return ++s_lastId;
}
void put(T* object, IdentifierType identifier)
{
- DCHECK(object && !m_objectToIdentifier->contains(object));
- m_objectToIdentifier->set(object, identifier);
- m_identifierToObject->set(identifier, object);
+ DCHECK(object && !this->m_objectToIdentifier.contains(object));
+ this->m_objectToIdentifier.set(object, identifier);
+ this->m_identifierToObject.set(identifier, object);
}
- using ObjectToIdentifier = HeapHashMap<WeakMember<T>, IdentifierType>;
- using IdentifierToObject = HeapHashMap<IdentifierType, WeakMember<T>>;
-
- Member<ObjectToIdentifier> m_objectToIdentifier;
- Member<IdentifierToObject> m_identifierToObject;
+ void objectDestroyed(T* object)
+ {
+ IdentifierType identifier = this->m_objectToIdentifier.take(object);
+ if (!WTF::isHashTraitsEmptyValue<HashTraits<IdentifierType>>(identifier))
+ this->m_identifierToObject.remove(identifier);
+ }
};
#define DECLARE_WEAK_IDENTIFIER_MAP(T, ...) \
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698