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

Side by Side Diff: Source/modules/webmidi/MIDIPortMap.h

Issue 862633002: IDL: Add keys()/values()/entries() to maplike<>/setlike<> interfaces (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: rebased Created 5 years, 11 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 unified diff | Download patch
« no previous file with comments | « Source/modules/webmidi/MIDIOutputMap.idl ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" 9 #include "bindings/core/v8/ScriptState.h"
10 #include "bindings/core/v8/ScriptValue.h" 10 #include "bindings/core/v8/ScriptValue.h"
11 #include "bindings/core/v8/V8Binding.h" 11 #include "bindings/core/v8/V8Binding.h"
12 #include "bindings/core/v8/V8IteratorResultValue.h" 12 #include "core/dom/Iterable.h"
13 #include "core/dom/Iterator.h"
14 #include "platform/heap/Handle.h" 13 #include "platform/heap/Handle.h"
15 #include "wtf/HashMap.h" 14 #include "wtf/HashMap.h"
16 #include "wtf/text/StringHash.h" 15 #include "wtf/text/StringHash.h"
17 #include "wtf/text/WTFString.h" 16 #include "wtf/text/WTFString.h"
18 17
19 namespace blink { 18 namespace blink {
20 19
21 template <typename T> 20 template <typename T>
22 class MIDIPortMap : public GarbageCollected<MIDIPortMap<T>> { 21 class MIDIPortMap : public GarbageCollected<MIDIPortMap<T>>, public PairIterable <String, T*> {
23 public: 22 public:
24 explicit MIDIPortMap(const HeapHashMap<String, Member<T>>& entries) : m_entr ies(entries) { } 23 explicit MIDIPortMap(const HeapHashMap<String, Member<T>>& entries) : m_entr ies(entries) { }
25 24
26 // IDL attributes / methods 25 // IDL attributes / methods
27 size_t size() const { return m_entries.size(); } 26 size_t size() const { return m_entries.size(); }
28 Iterator* keys();
29 Iterator* entries();
30 Iterator* values();
31 T* get(const String& key) const; 27 T* get(const String& key) const;
32 bool has(const String& key) const { return m_entries.contains(key); } 28 bool has(const String& key) const { return m_entries.contains(key); }
33 Iterator* iterator(ScriptState*, ExceptionState&) { return entries(); }
34 29
35 virtual void trace(Visitor* visitor) 30 virtual void trace(Visitor* visitor)
36 { 31 {
37 visitor->trace(m_entries); 32 visitor->trace(m_entries);
38 } 33 }
39 34
40 private: 35 private:
41 typedef HeapHashMap<String, Member<T>> MapType; 36 typedef HeapHashMap<String, Member<T>> MapType;
42 typedef typename HeapHashMap<String, Member<T>>::const_iterator IteratorType ; 37 typedef typename HeapHashMap<String, Member<T>>::const_iterator IteratorType ;
43 struct KeySelector { 38
44 static const String& select(ScriptState*, IteratorType i) { return i->ke y; } 39 typename PairIterable<String, T*>::IterationSource* startIteration(ScriptSta te*, ExceptionState&) override
45 }; 40 {
46 struct ValueSelector { 41 return new MapIterationSource(this, m_entries.begin(), m_entries.end());
47 static T* select(ScriptState*, IteratorType i) { return i->value; } 42 }
48 };
49 struct EntrySelector {
50 static Vector<ScriptValue> select(ScriptState* scriptState, IteratorType i)
51 {
52 Vector<ScriptValue> entry;
53 entry.append(ScriptValue(scriptState, v8String(scriptState->isolate( ), i->key)));
54 entry.append(ScriptValue(scriptState, toV8(i->value, scriptState->co ntext()->Global(), scriptState->isolate())));
55 return entry;
56 }
57 };
58 43
59 // Note: This template class relies on the fact that m_map.m_entries will 44 // Note: This template class relies on the fact that m_map.m_entries will
60 // never be modified once it is created. 45 // never be modified once it is created.
61 template <typename Selector> 46 class MapIterationSource final : public PairIterable<String, T*>::IterationS ource {
62 class MapIterator : public Iterator {
63 public: 47 public:
64 MapIterator(MIDIPortMap<T>* map, IteratorType iterator, IteratorType end ) 48 MapIterationSource(MIDIPortMap<T>* map, IteratorType iterator, IteratorT ype end)
65 : m_map(map) 49 : m_map(map)
66 , m_iterator(iterator) 50 , m_iterator(iterator)
67 , m_end(end) 51 , m_end(end)
68 { 52 {
69 } 53 }
70 54
71 virtual ScriptValue next(ScriptState* scriptState, ExceptionState&) over ride 55 bool next(ScriptState* scriptState, String& key, T*& value, ExceptionSta te&) override
72 { 56 {
73 if (m_iterator == m_end) 57 if (m_iterator == m_end)
74 return v8IteratorResultDone(scriptState); 58 return false;
75 ScriptValue result = v8IteratorResult(scriptState, Selector::select( scriptState, m_iterator)); 59 key = m_iterator->key;
60 value = m_iterator->value;
76 ++m_iterator; 61 ++m_iterator;
77 return result; 62 return true;
78 } 63 }
79 64
80 virtual ScriptValue next(ScriptState* scriptState, ScriptValue, Exceptio nState& exceptionState) override 65 void trace(Visitor* visitor) override
81 {
82 return next(scriptState, exceptionState);
83 }
84
85 virtual void trace(Visitor* visitor) override
86 { 66 {
87 visitor->trace(m_map); 67 visitor->trace(m_map);
88 Iterator::trace(visitor); 68 PairIterable<String, T*>::IterationSource::trace(visitor);
89 } 69 }
90 70
91 private: 71 private:
92 // m_map is stored just for keeping it alive. It needs to be kept 72 // m_map is stored just for keeping it alive. It needs to be kept
93 // alive while JavaScript holds the iterator to it. 73 // alive while JavaScript holds the iterator to it.
94 const Member<const MIDIPortMap<T>> m_map; 74 const Member<const MIDIPortMap<T>> m_map;
95 IteratorType m_iterator; 75 IteratorType m_iterator;
96 const IteratorType m_end; 76 const IteratorType m_end;
97 }; 77 };
98 78
99 const MapType m_entries; 79 const MapType m_entries;
100 }; 80 };
101 81
102 template <typename T> 82 template <typename T>
103 Iterator* MIDIPortMap<T>::keys()
104 {
105 return new MapIterator<KeySelector>(this, m_entries.begin(), m_entries.end() );
106 }
107
108 template <typename T>
109 Iterator* MIDIPortMap<T>::entries()
110 {
111 return new MapIterator<EntrySelector>(this, m_entries.begin(), m_entries.end ());
112 }
113
114 template <typename T>
115 Iterator* MIDIPortMap<T>::values()
116 {
117 return new MapIterator<ValueSelector>(this, m_entries.begin(), m_entries.end ());
118 }
119
120 template <typename T>
121 T* MIDIPortMap<T>::get(const String& key) const 83 T* MIDIPortMap<T>::get(const String& key) const
122 { 84 {
123 return has(key) ? m_entries.get(key) : 0; 85 return has(key) ? m_entries.get(key) : 0;
124 } 86 }
125 87
126 } // namespace blink 88 } // namespace blink
127 89
128 #endif 90 #endif
OLDNEW
« no previous file with comments | « Source/modules/webmidi/MIDIOutputMap.idl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698