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

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: rebase Created 6 years, 3 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
« no previous file with comments | « Source/modules/webmidi/MIDIOutputMap.idl ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..c70b50b71c9bc07b0058aaa91a6451938e236ab0
--- /dev/null
+++ b/Source/modules/webmidi/MIDIPortMap.h
@@ -0,0 +1,127 @@
+// 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:
+ explicit 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* scriptState, ScriptValue, ExceptionState& exceptionState) OVERRIDE
+ {
+ return next(scriptState, exceptionState);
+ }
+
+ virtual void trace(Visitor* visitor) OVERRIDE
+ {
+ visitor->trace(m_map);
+ Iterator::trace(visitor);
+ }
+
+ private:
+ // m_map is stored just for keeping it alive. It needs to be kept
+ // alive while JavaScript holds the iterator to it.
+ const Member<const MIDIPortMap<T> > m_map;
+ 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
« 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