Chromium Code Reviews| OLD | NEW | 
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef MIDIPortMap_h | |
| 6 #define MIDIPortMap_h | |
| 7 | |
| 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" | |
| 12 #include "core/dom/Iterator.h" | |
| 13 #include "platform/heap/Handle.h" | |
| 14 #include "wtf/HashMap.h" | |
| 15 #include "wtf/text/StringHash.h" | |
| 16 #include "wtf/text/WTFString.h" | |
| 17 | |
| 18 namespace blink { | |
| 19 | |
| 20 template <typename T> | |
| 21 class MIDIPortMap : public GarbageCollected<MIDIPortMap<T> > { | |
| 22 public: | |
| 23 MIDIPortMap(const HeapHashMap<String, Member<T> >& entries) : m_entries(entr ies) { } | |
| 24 | |
| 25 // IDL attributes / methods | |
| 26 size_t size() const { return m_entries.size(); } | |
| 27 Iterator* keys(); | |
| 28 Iterator* entries(); | |
| 29 Iterator* values(); | |
| 30 T* get(const String& key) const; | |
| 31 bool has(const String& key) const { return m_entries.contains(key); } | |
| 32 Iterator* iterator(ScriptState*, ExceptionState&) { return values(); } | |
| 33 | |
| 34 void trace(Visitor* visitor) | |
| 35 { | |
| 36 visitor->trace(m_entries); | |
| 37 } | |
| 38 | |
| 39 private: | |
| 40 typedef HeapHashMap<String, Member<T> > MapType; | |
| 41 typedef typename HeapHashMap<String, Member<T> >::const_iterator IteratorTyp e; | |
| 42 struct KeySelector { | |
| 43 static const String& select(ScriptState*, IteratorType i) { return i->ke y; } | |
| 44 }; | |
| 45 struct ValueSelector { | |
| 46 static T* select(ScriptState*, IteratorType i) { return i->value; } | |
| 47 }; | |
| 48 struct EntrySelector { | |
| 49 static Vector<ScriptValue> select(ScriptState* scriptState, IteratorType i) | |
| 50 { | |
| 51 Vector<ScriptValue> entry; | |
| 52 entry.append(ScriptValue(scriptState, v8String(scriptState->isolate( ), i->key))); | |
| 53 entry.append(ScriptValue(scriptState, V8ValueTraits<T*>::toV8Value(i ->value, scriptState->context()->Global(), scriptState->isolate()))); | |
| 54 return entry; | |
| 55 } | |
| 56 }; | |
| 57 | |
| 58 // Note: This template class relies on the fact that m_map.m_entries will | |
| 59 // never be modified once it is created. | |
| 60 template <typename Selector> | |
| 61 class MapIterator : public Iterator { | |
| 62 public: | |
| 63 MapIterator(MIDIPortMap<T>* map, IteratorType iterator, IteratorType end ) | |
| 64 : m_map(map) | |
| 65 , m_iterator(iterator) | |
| 66 , m_end(end) { } | |
| 67 | |
| 68 virtual ScriptValue next(ScriptState* scriptState, ExceptionState&) OVER RIDE | |
| 69 { | |
| 70 if (m_iterator == m_end) | |
| 71 return ScriptValue(scriptState, v8DoneIteratorResult(scriptState ->isolate())); | |
| 72 ScriptValue result(scriptState, v8IteratorResult(scriptState, Select or::select(scriptState, m_iterator))); | |
| 73 ++m_iterator; | |
| 74 return result; | |
| 75 } | |
| 76 | |
| 77 virtual ScriptValue next(ScriptState*, ScriptValue, ExceptionState& exce ptionState) OVERRIDE | |
| 78 { | |
| 79 exceptionState.throwDOMException(InvalidModificationError, "Not impl emented"); | |
| 
 
haraken
2014/08/31 07:18:57
Just help me understand: Is there any way for user
 
yhirano
2014/09/01 02:22:11
Yes, it can be called from a script. See [1]. It i
 
haraken
2014/09/01 07:17:48
I just thought it would be problematic to expose "
 
yhirano
2014/09/01 11:32:52
I reread the spec and found ignoring the parameter
 
 | |
| 80 return ScriptValue(); | |
| 81 } | |
| 82 | |
| 83 virtual void trace(Visitor* visitor) OVERRIDE | |
| 84 { | |
| 85 visitor->trace(m_map); | |
| 86 Iterator::trace(visitor); | |
| 87 } | |
| 88 | |
| 89 private: | |
| 90 // m_map is stored just for keeping it alive. | |
| 91 const Member<const MIDIPortMap<T> > m_map; | |
| 
 
haraken
2014/08/31 07:18:57
Why do we need this? trace(m_entries) in MIDIPortM
 
yhirano
2014/09/01 02:22:11
Scripts can hold iterators independently. i.e. you
 
haraken
2014/09/01 07:17:48
Makes sense. A comment about it will be helpful.
 
yhirano
2014/09/01 11:32:52
Done.
 
 | |
| 92 IteratorType m_iterator; | |
| 93 const IteratorType m_end; | |
| 94 }; | |
| 95 | |
| 96 const MapType m_entries; | |
| 97 }; | |
| 98 | |
| 99 template <typename T> | |
| 100 Iterator* MIDIPortMap<T>::keys() | |
| 101 { | |
| 102 return new MapIterator<KeySelector>(this, m_entries.begin(), m_entries.end() ); | |
| 103 } | |
| 104 | |
| 105 template <typename T> | |
| 106 Iterator* MIDIPortMap<T>::entries() | |
| 107 { | |
| 108 return new MapIterator<EntrySelector>(this, m_entries.begin(), m_entries.end ()); | |
| 109 } | |
| 110 | |
| 111 template <typename T> | |
| 112 Iterator* MIDIPortMap<T>::values() | |
| 113 { | |
| 114 return new MapIterator<ValueSelector>(this, m_entries.begin(), m_entries.end ()); | |
| 115 } | |
| 116 | |
| 117 template <typename T> | |
| 118 T* MIDIPortMap<T>::get(const String& key) const | |
| 119 { | |
| 120 return has(key) ? m_entries.get(key) : 0; | |
| 121 } | |
| 122 | |
| 123 } // namespace blink | |
| 124 | |
| 125 #endif | |
| OLD | NEW |