Chromium Code Reviews| Index: Source/core/dom/Iterable.h |
| diff --git a/Source/core/dom/Iterable.h b/Source/core/dom/Iterable.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4913ab80a230e46d3958dc02eb15e2a6be6179a1 |
| --- /dev/null |
| +++ b/Source/core/dom/Iterable.h |
| @@ -0,0 +1,170 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
|
jsbell
2015/01/12 17:14:07
New file needs Source/core/core.gypi entry?
Jens Widell
2015/01/12 17:24:14
Compiles fine without it (apparently) but most hea
|
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef Iterable_h |
| +#define Iterable_h |
| + |
| +#include "bindings/core/v8/V8IteratorResultValue.h" |
| +#include "core/dom/Iterator.h" |
| + |
| +namespace blink { |
| + |
| +// Typically, use one of ValueIterable<> and PairIterable<> (below) instead! |
| +template <typename KeyType, typename ValueType> |
| +class Iterable { |
| +public: |
| + virtual ~Iterable() { } |
| + |
| + Iterator* keys(ScriptState* scriptState, ExceptionState& exceptionState) |
| + { |
| + IterationSource* source = this->startIteration(scriptState, exceptionState); |
| + if (!source) |
| + return nullptr; |
| + return new IterableIterator<KeySelector>(source); |
| + } |
| + |
| + Iterator* values(ScriptState* scriptState, ExceptionState& exceptionState) |
| + { |
| + IterationSource* source = this->startIteration(scriptState, exceptionState); |
| + if (!source) |
| + return nullptr; |
| + return new IterableIterator<ValueSelector>(source); |
| + } |
| + |
| + Iterator* entries(ScriptState* scriptState, ExceptionState& exceptionState) |
| + { |
| + IterationSource* source = this->startIteration(scriptState, exceptionState); |
| + if (!source) |
| + return nullptr; |
| + return new IterableIterator<EntrySelector>(source); |
| + } |
| + |
| + class IterationSource : public GarbageCollectedFinalized<IterationSource> { |
| + public: |
| + virtual ~IterationSource() { } |
| + |
| + // If end of iteration has been reached: return false. |
| + // Otherwise: set |key| and |value| and return true. |
| + virtual bool next(ScriptState*, KeyType&, ValueType&, ExceptionState&) = 0; |
| + |
| + virtual void trace(Visitor*) { } |
| + }; |
| + |
| +protected: |
| + virtual IterationSource* startIteration(ScriptState*, ExceptionState&) = 0; |
| + |
| +private: |
| + struct KeySelector { |
| + static KeyType select(ScriptState*, KeyType key, ValueType value) |
| + { |
| + return key; |
| + } |
| + }; |
| + struct ValueSelector { |
| + static ValueType select(ScriptState*, KeyType key, ValueType value) |
| + { |
| + return value; |
| + } |
| + }; |
| + struct EntrySelector { |
| + static Vector<ScriptValue> select(ScriptState* scriptState, KeyType key, ValueType value) |
| + { |
| + v8::Local<v8::Object> creationContext = scriptState->context()->Global(); |
| + v8::Isolate* isolate = scriptState->isolate(); |
| + |
| + Vector<ScriptValue> entry; |
| + entry.append(ScriptValue(scriptState, toV8(key, creationContext, isolate))); |
| + entry.append(ScriptValue(scriptState, toV8(value, creationContext, isolate))); |
| + return entry; |
| + } |
| + }; |
| + |
| + template <typename Selector> |
| + class IterableIterator final : public Iterator { |
| + public: |
| + IterableIterator(IterationSource* source) |
| + : m_source(source) |
| + { |
| + } |
| + |
| + ScriptValue next(ScriptState* scriptState, ExceptionState& exceptionState) override |
| + { |
| + KeyType key; |
| + ValueType value; |
| + |
| + if (!m_source->next(scriptState, key, value, exceptionState)) |
| + return v8IteratorResultDone(scriptState); |
| + |
| + return v8IteratorResult(scriptState, Selector::select(scriptState, key, value)); |
| + } |
| + |
| + ScriptValue next(ScriptState* scriptState, ScriptValue, ExceptionState& exceptionState) override |
| + { |
| + return next(scriptState, exceptionState); |
| + } |
| + |
| + void trace(Visitor* visitor) override |
| + { |
| + visitor->trace(m_source); |
| + Iterator::trace(visitor); |
| + } |
| + |
| + private: |
| + Member<IterationSource> m_source; |
| + }; |
| +}; |
| + |
| +// Utiltity mixin base-class for classes implementing IDL interfaces with "iterable<T>". |
| +template <typename ValueType> |
| +class ValueIterable : public Iterable<unsigned, ValueType> { |
| +public: |
| + Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) |
| + { |
| + return this->values(scriptState, exceptionState); |
| + } |
| + |
| + class IterationSource : public Iterable<unsigned, ValueType>::IterationSource { |
| + public: |
| + virtual ~IterationSource() { } |
| + |
| + // If end of iteration has been reached: return false. |
| + // Otherwise: set |value| and return true. |
| + // Note: |this->m_index| is the index being accessed. |
| + virtual bool next(ScriptState*, ValueType&, ExceptionState&) = 0; |
| + |
| + virtual void trace(Visitor*) { } |
| + |
| + protected: |
| + IterationSource() |
| + : m_index(0) |
| + { |
| + } |
| + |
| + unsigned m_index; |
|
jsbell
2015/01/12 17:14:08
Are there cases where this would be updated by a d
|
| + |
| + private: |
| + bool next(ScriptState* scriptState, unsigned& key, ValueType& value, ExceptionState& exceptionState) override |
| + { |
| + if (!next(scriptState, value, exceptionState)) |
| + return false; |
| + key = m_index; |
| + ++m_index; |
| + return true; |
| + } |
| + }; |
| +}; |
| + |
| +// Utiltity mixin base-class for classes implementing IDL interfaces with "iterable<T1, T2>". |
| +template <typename KeyType, typename ValueType> |
| +class PairIterable : public Iterable<KeyType, ValueType> { |
| +public: |
| + Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) |
| + { |
| + return this->entries(scriptState, exceptionState); |
| + } |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // Iterable_h |