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

Unified Diff: Source/modules/webmidi/MIDIPortMap.h

Issue 513203002: Introduce MIDIInputMap and MIDIOutputMap. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@iterator-adhoc
Patch Set: Created 6 years, 4 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
Index: Source/modules/webmidi/MIDIPortMap.h
diff --git a/Source/modules/webmidi/MIDIPortMap.h b/Source/modules/webmidi/MIDIPortMap.h
new file mode 100644
index 0000000000000000000000000000000000000000..17111ca5b484a3b5fac8b9cc9b2592532f080b18
--- /dev/null
+++ b/Source/modules/webmidi/MIDIPortMap.h
@@ -0,0 +1,125 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MIDIPortMap_h
+#define MIDIPortMap_h
+
+#include "bindings/core/v8/ExceptionState.h"
+#include "bindings/core/v8/ScriptState.h"
+#include "bindings/core/v8/ScriptValue.h"
+#include "bindings/core/v8/V8Binding.h"
+#include "core/dom/Iterator.h"
+#include "platform/heap/Handle.h"
+#include "wtf/HashMap.h"
+#include "wtf/text/StringHash.h"
+#include "wtf/text/WTFString.h"
+
+namespace blink {
+
+template <typename T>
+class MIDIPortMap : public GarbageCollected<MIDIPortMap<T> > {
+public:
+ MIDIPortMap(const HeapHashMap<String, Member<T> >& entries) : m_entries(entries) { }
+
+ // IDL attributes / methods
+ size_t size() const { return m_entries.size(); }
+ Iterator* keys();
+ Iterator* entries();
+ Iterator* values();
+ T* get(const String& key) const;
+ bool has(const String& key) const { return m_entries.contains(key); }
+ Iterator* iterator(ScriptState*, ExceptionState&) { return values(); }
+
+ void trace(Visitor* visitor)
+ {
+ visitor->trace(m_entries);
+ }
+
+private:
+ typedef HeapHashMap<String, Member<T> > MapType;
+ typedef typename HeapHashMap<String, Member<T> >::const_iterator IteratorType;
+ struct KeySelector {
+ static const String& select(ScriptState*, IteratorType i) { return i->key; }
+ };
+ struct ValueSelector {
+ static T* select(ScriptState*, IteratorType i) { return i->value; }
+ };
+ struct EntrySelector {
+ static Vector<ScriptValue> select(ScriptState* scriptState, IteratorType i)
+ {
+ Vector<ScriptValue> entry;
+ entry.append(ScriptValue(scriptState, v8String(scriptState->isolate(), i->key)));
+ entry.append(ScriptValue(scriptState, V8ValueTraits<T*>::toV8Value(i->value, scriptState->context()->Global(), scriptState->isolate())));
+ return entry;
+ }
+ };
+
+ // Note: This template class relies on the fact that m_map.m_entries will
+ // never be modified once it is created.
+ template <typename Selector>
+ class MapIterator : public Iterator {
+ public:
+ MapIterator(MIDIPortMap<T>* map, IteratorType iterator, IteratorType end)
+ : m_map(map)
+ , m_iterator(iterator)
+ , m_end(end) { }
+
+ virtual ScriptValue next(ScriptState* scriptState, ExceptionState&) OVERRIDE
+ {
+ if (m_iterator == m_end)
+ return ScriptValue(scriptState, v8DoneIteratorResult(scriptState->isolate()));
+ ScriptValue result(scriptState, v8IteratorResult(scriptState, Selector::select(scriptState, m_iterator)));
+ ++m_iterator;
+ return result;
+ }
+
+ virtual ScriptValue next(ScriptState*, ScriptValue, ExceptionState& exceptionState) OVERRIDE
+ {
+ exceptionState.throwDOMException(InvalidModificationError, "Not implemented");
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
+ return ScriptValue();
+ }
+
+ virtual void trace(Visitor* visitor) OVERRIDE
+ {
+ visitor->trace(m_map);
+ Iterator::trace(visitor);
+ }
+
+ private:
+ // m_map is stored just for keeping it alive.
+ 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.
+ IteratorType m_iterator;
+ const IteratorType m_end;
+ };
+
+ const MapType m_entries;
+};
+
+template <typename T>
+Iterator* MIDIPortMap<T>::keys()
+{
+ return new MapIterator<KeySelector>(this, m_entries.begin(), m_entries.end());
+}
+
+template <typename T>
+Iterator* MIDIPortMap<T>::entries()
+{
+ return new MapIterator<EntrySelector>(this, m_entries.begin(), m_entries.end());
+}
+
+template <typename T>
+Iterator* MIDIPortMap<T>::values()
+{
+ return new MapIterator<ValueSelector>(this, m_entries.begin(), m_entries.end());
+}
+
+template <typename T>
+T* MIDIPortMap<T>::get(const String& key) const
+{
+ return has(key) ? m_entries.get(key) : 0;
+}
+
+} // namespace blink
+
+#endif
« Source/modules/webmidi/MIDIOutputMap.idl ('K') | « Source/modules/webmidi/MIDIOutputMap.idl ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698