OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 Iterable_h |
| 6 #define Iterable_h |
| 7 |
| 8 #include "bindings/core/v8/V8IteratorResultValue.h" |
| 9 #include "core/dom/Iterator.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 // Typically, use one of ValueIterable<> and PairIterable<> (below) instead! |
| 14 template <typename KeyType, typename ValueType> |
| 15 class Iterable { |
| 16 public: |
| 17 virtual ~Iterable() { } |
| 18 |
| 19 Iterator* keys(ScriptState* scriptState, ExceptionState& exceptionState) |
| 20 { |
| 21 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); |
| 22 if (!source) |
| 23 return nullptr; |
| 24 return new IterableIterator<KeySelector>(source); |
| 25 } |
| 26 |
| 27 Iterator* values(ScriptState* scriptState, ExceptionState& exceptionState) |
| 28 { |
| 29 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); |
| 30 if (!source) |
| 31 return nullptr; |
| 32 return new IterableIterator<ValueSelector>(source); |
| 33 } |
| 34 |
| 35 Iterator* entries(ScriptState* scriptState, ExceptionState& exceptionState) |
| 36 { |
| 37 IterationSource* source = this->startIteration(scriptState, exceptionSta
te); |
| 38 if (!source) |
| 39 return nullptr; |
| 40 return new IterableIterator<EntrySelector>(source); |
| 41 } |
| 42 |
| 43 class IterationSource : public GarbageCollectedFinalized<IterationSource> { |
| 44 public: |
| 45 virtual ~IterationSource() { } |
| 46 |
| 47 // If end of iteration has been reached or an exception thrown: return f
alse. |
| 48 // Otherwise: set |key| and |value| and return true. |
| 49 virtual bool next(ScriptState*, KeyType&, ValueType&, ExceptionState&) =
0; |
| 50 |
| 51 virtual void trace(Visitor*) { } |
| 52 }; |
| 53 |
| 54 private: |
| 55 virtual IterationSource* startIteration(ScriptState*, ExceptionState&) = 0; |
| 56 |
| 57 struct KeySelector { |
| 58 static const KeyType& select(ScriptState*, const KeyType& key, const Val
ueType& value) |
| 59 { |
| 60 return key; |
| 61 } |
| 62 }; |
| 63 struct ValueSelector { |
| 64 static const ValueType& select(ScriptState*, const KeyType& key, const V
alueType& value) |
| 65 { |
| 66 return value; |
| 67 } |
| 68 }; |
| 69 struct EntrySelector { |
| 70 static Vector<ScriptValue, 2> select(ScriptState* scriptState, const Key
Type& key, const ValueType& value) |
| 71 { |
| 72 v8::Local<v8::Object> creationContext = scriptState->context()->Glob
al(); |
| 73 v8::Isolate* isolate = scriptState->isolate(); |
| 74 |
| 75 Vector<ScriptValue, 2> entry; |
| 76 entry.append(ScriptValue(scriptState, toV8(key, creationContext, iso
late))); |
| 77 entry.append(ScriptValue(scriptState, toV8(value, creationContext, i
solate))); |
| 78 return entry; |
| 79 } |
| 80 }; |
| 81 |
| 82 template <typename Selector> |
| 83 class IterableIterator final : public Iterator { |
| 84 public: |
| 85 explicit IterableIterator(IterationSource* source) |
| 86 : m_source(source) |
| 87 { |
| 88 } |
| 89 |
| 90 ScriptValue next(ScriptState* scriptState, ExceptionState& exceptionStat
e) override |
| 91 { |
| 92 KeyType key; |
| 93 ValueType value; |
| 94 |
| 95 if (!m_source->next(scriptState, key, value, exceptionState)) |
| 96 return v8IteratorResultDone(scriptState); |
| 97 |
| 98 return v8IteratorResult(scriptState, Selector::select(scriptState, k
ey, value)); |
| 99 } |
| 100 |
| 101 ScriptValue next(ScriptState* scriptState, ScriptValue, ExceptionState&
exceptionState) override |
| 102 { |
| 103 return next(scriptState, exceptionState); |
| 104 } |
| 105 |
| 106 void trace(Visitor* visitor) override |
| 107 { |
| 108 visitor->trace(m_source); |
| 109 Iterator::trace(visitor); |
| 110 } |
| 111 |
| 112 private: |
| 113 Member<IterationSource> m_source; |
| 114 }; |
| 115 }; |
| 116 |
| 117 // Utiltity mixin base-class for classes implementing IDL interfaces with "itera
ble<T>". |
| 118 template <typename ValueType> |
| 119 class ValueIterable : public Iterable<unsigned, ValueType> { |
| 120 public: |
| 121 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) |
| 122 { |
| 123 return this->values(scriptState, exceptionState); |
| 124 } |
| 125 |
| 126 class IterationSource : public Iterable<unsigned, ValueType>::IterationSourc
e { |
| 127 public: |
| 128 IterationSource() |
| 129 : m_index(0) |
| 130 { |
| 131 } |
| 132 |
| 133 ~IterationSource() override { } |
| 134 |
| 135 // If end of iteration has been reached or an exception thrown: return f
alse. |
| 136 // Otherwise: set |value| and return true. |
| 137 // Note: |this->m_index| is the index being accessed. |
| 138 virtual bool next(ScriptState*, ValueType&, ExceptionState&) = 0; |
| 139 |
| 140 protected: |
| 141 unsigned m_index; |
| 142 |
| 143 private: |
| 144 bool next(ScriptState* scriptState, unsigned& key, ValueType& value, Exc
eptionState& exceptionState) final |
| 145 { |
| 146 if (!next(scriptState, value, exceptionState)) |
| 147 return false; |
| 148 key = m_index; |
| 149 ++m_index; |
| 150 return true; |
| 151 } |
| 152 }; |
| 153 }; |
| 154 |
| 155 // Utiltity mixin base-class for classes implementing IDL interfaces with "itera
ble<T1, T2>". |
| 156 template <typename KeyType, typename ValueType> |
| 157 class PairIterable : public Iterable<KeyType, ValueType> { |
| 158 public: |
| 159 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) |
| 160 { |
| 161 return this->entries(scriptState, exceptionState); |
| 162 } |
| 163 }; |
| 164 |
| 165 } // namespace blink |
| 166 |
| 167 #endif // Iterable_h |
OLD | NEW |