OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef WeakIdentifierMap_h | 5 #ifndef WeakIdentifierMap_h |
6 #define WeakIdentifierMap_h | 6 #define WeakIdentifierMap_h |
7 | 7 |
8 #include "platform/heap/Handle.h" | 8 #include "platform/heap/Handle.h" |
| 9 #include "wtf/Allocator.h" |
9 #include "wtf/HashMap.h" | 10 #include "wtf/HashMap.h" |
10 #include "wtf/Vector.h" | 11 #include "wtf/Vector.h" |
11 | 12 |
12 namespace blink { | 13 namespace blink { |
13 | 14 |
14 template<typename T> struct IdentifierGenerator; | 15 template<typename T> struct IdentifierGenerator; |
15 | 16 |
16 template<> struct IdentifierGenerator<int> { | 17 template<> struct IdentifierGenerator<int> { |
| 18 STATIC_ONLY(IdentifierGenerator); |
17 using IdentifierType = int; | 19 using IdentifierType = int; |
18 static IdentifierType next() | 20 static IdentifierType next() |
19 { | 21 { |
20 static int s_lastId = 0; | 22 static int s_lastId = 0; |
21 return ++s_lastId; | 23 return ++s_lastId; |
22 } | 24 } |
23 }; | 25 }; |
24 | 26 |
25 template<typename T> struct WeakIdentifierMapTraits { | 27 template<typename T> struct WeakIdentifierMapTraits { |
| 28 STATIC_ONLY(WeakIdentifierMapTraits); |
26 static void removedFromIdentifierMap(T*) { } | 29 static void removedFromIdentifierMap(T*) { } |
27 static void addedToIdentifierMap(T*) { } | 30 static void addedToIdentifierMap(T*) { } |
28 }; | 31 }; |
29 | 32 |
30 template<typename T, | 33 template<typename T, |
31 typename Generator = IdentifierGenerator<int>, | 34 typename Generator = IdentifierGenerator<int>, |
32 typename Traits = WeakIdentifierMapTraits<T>, | 35 typename Traits = WeakIdentifierMapTraits<T>, |
33 bool isGarbageCollected = IsGarbageCollectedType<T>::value> class WeakIdenti
fierMap; | 36 bool isGarbageCollected = IsGarbageCollectedType<T>::value> class WeakIdenti
fierMap; |
34 | 37 |
35 template<typename T, typename Generator, typename Traits> class WeakIdentifierMa
p<T, Generator, Traits, false> { | 38 template<typename T, typename Generator, typename Traits> class WeakIdentifierMa
p<T, Generator, Traits, false> { |
| 39 WTF_MAKE_FAST_ALLOCATED(WeakIdentifierMap); |
36 public: | 40 public: |
37 using IdentifierType = typename Generator::IdentifierType; | 41 using IdentifierType = typename Generator::IdentifierType; |
38 using ReferenceType = RawPtr<WeakIdentifierMap<T, Generator, Traits, false>>
; | 42 using ReferenceType = RawPtr<WeakIdentifierMap<T, Generator, Traits, false>>
; |
39 | 43 |
40 static IdentifierType identifier(T* object) | 44 static IdentifierType identifier(T* object) |
41 { | 45 { |
42 IdentifierType result = instance().m_objectToIdentifier.get(object); | 46 IdentifierType result = instance().m_objectToIdentifier.get(object); |
43 | 47 |
44 if (WTF::isHashTraitsEmptyValue<HashTraits<IdentifierType>>(result)) { | 48 if (WTF::isHashTraitsEmptyValue<HashTraits<IdentifierType>>(result)) { |
45 result = Generator::next(); | 49 result = Generator::next(); |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 template class WeakIdentifierMap<T, ##__VA_ARGS__>; \ | 150 template class WeakIdentifierMap<T, ##__VA_ARGS__>; \ |
147 template<> WeakIdentifierMap<T, ##__VA_ARGS__>& WeakIdentifierMap<T, ##__VA_
ARGS__>::instance() \ | 151 template<> WeakIdentifierMap<T, ##__VA_ARGS__>& WeakIdentifierMap<T, ##__VA_
ARGS__>::instance() \ |
148 { \ | 152 { \ |
149 using RefType = WeakIdentifierMap<T, ##__VA_ARGS__>::ReferenceType; \ | 153 using RefType = WeakIdentifierMap<T, ##__VA_ARGS__>::ReferenceType; \ |
150 DEFINE_STATIC_LOCAL(RefType, mapInstance, (new WeakIdentifierMap<T, ##__
VA_ARGS__>())); \ | 154 DEFINE_STATIC_LOCAL(RefType, mapInstance, (new WeakIdentifierMap<T, ##__
VA_ARGS__>())); \ |
151 return *mapInstance; \ | 155 return *mapInstance; \ |
152 } | 156 } |
153 } | 157 } |
154 | 158 |
155 #endif // WeakIdentifierMap_h | 159 #endif // WeakIdentifierMap_h |
OLD | NEW |