OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 MIDIPortMap_h | 5 #ifndef MIDIPortMap_h |
6 #define MIDIPortMap_h | 6 #define MIDIPortMap_h |
7 | 7 |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "bindings/core/v8/ScriptState.h" | |
10 #include "bindings/core/v8/ScriptValue.h" | |
11 #include "bindings/core/v8/V8Binding.h" | 9 #include "bindings/core/v8/V8Binding.h" |
12 #include "core/dom/Iterable.h" | 10 #include "core/dom/Maplike.h" |
13 #include "platform/heap/Handle.h" | 11 #include "platform/heap/Handle.h" |
14 #include "wtf/HashMap.h" | 12 #include "wtf/HashMap.h" |
15 #include "wtf/text/StringHash.h" | 13 #include "wtf/text/StringHash.h" |
16 #include "wtf/text/WTFString.h" | 14 #include "wtf/text/WTFString.h" |
17 | 15 |
18 namespace blink { | 16 namespace blink { |
19 | 17 |
20 template <typename T> | 18 template <typename T> |
21 class MIDIPortMap : public GarbageCollected<MIDIPortMap<T>>, public PairIterable
<String, T*> { | 19 class MIDIPortMap : public GarbageCollected<MIDIPortMap<T>>, public Maplike<Stri
ng, T*> { |
22 public: | 20 public: |
23 explicit MIDIPortMap(const HeapHashMap<String, Member<T>>& entries) : m_entr
ies(entries) { } | 21 explicit MIDIPortMap(const HeapHashMap<String, Member<T>>& entries) : m_entr
ies(entries) { } |
24 | 22 |
25 // IDL attributes / methods | 23 // IDL attributes / methods |
26 size_t size() const { return m_entries.size(); } | 24 size_t size() const { return m_entries.size(); } |
27 T* get(const String& key) const; | |
28 bool has(const String& key) const { return m_entries.contains(key); } | |
29 | 25 |
30 DEFINE_INLINE_VIRTUAL_TRACE() | 26 DEFINE_INLINE_VIRTUAL_TRACE() |
31 { | 27 { |
32 visitor->trace(m_entries); | 28 visitor->trace(m_entries); |
33 } | 29 } |
34 | 30 |
35 private: | 31 private: |
36 typedef HeapHashMap<String, Member<T>> MapType; | 32 typedef HeapHashMap<String, Member<T>> MapType; |
37 typedef typename HeapHashMap<String, Member<T>>::const_iterator IteratorType
; | 33 typedef typename HeapHashMap<String, Member<T>>::const_iterator IteratorType
; |
38 | 34 |
39 typename PairIterable<String, T*>::IterationSource* startIteration(ScriptSta
te*, ExceptionState&) override | 35 typename PairIterable<String, T*>::IterationSource* startIteration(ScriptSta
te*, ExceptionState&) override |
40 { | 36 { |
41 return new MapIterationSource(this, m_entries.begin(), m_entries.end()); | 37 return new MapIterationSource(this, m_entries.begin(), m_entries.end()); |
42 } | 38 } |
43 | 39 |
| 40 bool getMapEntry(ScriptState*, const String& key, T*& value, ExceptionState&
) override |
| 41 { |
| 42 if (!m_entries.contains(key)) |
| 43 return false; |
| 44 value = m_entries.get(key); |
| 45 return true; |
| 46 } |
| 47 |
44 // Note: This template class relies on the fact that m_map.m_entries will | 48 // Note: This template class relies on the fact that m_map.m_entries will |
45 // never be modified once it is created. | 49 // never be modified once it is created. |
46 class MapIterationSource final : public PairIterable<String, T*>::IterationS
ource { | 50 class MapIterationSource final : public PairIterable<String, T*>::IterationS
ource { |
47 public: | 51 public: |
48 MapIterationSource(MIDIPortMap<T>* map, IteratorType iterator, IteratorT
ype end) | 52 MapIterationSource(MIDIPortMap<T>* map, IteratorType iterator, IteratorT
ype end) |
49 : m_map(map) | 53 : m_map(map) |
50 , m_iterator(iterator) | 54 , m_iterator(iterator) |
51 , m_end(end) | 55 , m_end(end) |
52 { | 56 { |
53 } | 57 } |
(...skipping 18 matching lines...) Expand all Loading... |
72 // m_map is stored just for keeping it alive. It needs to be kept | 76 // m_map is stored just for keeping it alive. It needs to be kept |
73 // alive while JavaScript holds the iterator to it. | 77 // alive while JavaScript holds the iterator to it. |
74 const Member<const MIDIPortMap<T>> m_map; | 78 const Member<const MIDIPortMap<T>> m_map; |
75 IteratorType m_iterator; | 79 IteratorType m_iterator; |
76 const IteratorType m_end; | 80 const IteratorType m_end; |
77 }; | 81 }; |
78 | 82 |
79 const MapType m_entries; | 83 const MapType m_entries; |
80 }; | 84 }; |
81 | 85 |
82 template <typename T> | |
83 T* MIDIPortMap<T>::get(const String& key) const | |
84 { | |
85 return has(key) ? m_entries.get(key) : 0; | |
86 } | |
87 | |
88 } // namespace blink | 86 } // namespace blink |
89 | 87 |
90 #endif | 88 #endif |
OLD | NEW |