| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef Iterable_h | 5 #ifndef Iterable_h |
| 6 #define Iterable_h | 6 #define Iterable_h |
| 7 | 7 |
| 8 #include "bindings/core/v8/V8IteratorResultValue.h" | 8 #include "bindings/core/v8/V8IteratorResultValue.h" |
| 9 #include "bindings/core/v8/V8ScriptRunner.h" | 9 #include "bindings/core/v8/V8ScriptRunner.h" |
| 10 #include "core/dom/Iterator.h" | 10 #include "core/dom/Iterator.h" |
| 11 | 11 |
| 12 namespace blink { | 12 namespace blink { |
| 13 | 13 |
| 14 // Typically, use one of ValueIterable<> and PairIterable<> (below) instead! | 14 // Typically, you should use PairIterable<> (below) instead. |
| 15 // Also, note that value iterators are set up automatically by the bindings |
| 16 // code and the operations below come directly from V8. |
| 15 template <typename KeyType, typename ValueType> | 17 template <typename KeyType, typename ValueType> |
| 16 class Iterable { | 18 class Iterable { |
| 17 public: | 19 public: |
| 18 Iterator* keysForBinding(ScriptState* scriptState, | 20 Iterator* keysForBinding(ScriptState* scriptState, |
| 19 ExceptionState& exceptionState) { | 21 ExceptionState& exceptionState) { |
| 20 IterationSource* source = this->startIteration(scriptState, exceptionState); | 22 IterationSource* source = this->startIteration(scriptState, exceptionState); |
| 21 if (!source) | 23 if (!source) |
| 22 return nullptr; | 24 return nullptr; |
| 23 return new IterableIterator<KeySelector>(source); | 25 return new IterableIterator<KeySelector>(source); |
| 24 } | 26 } |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 visitor->trace(m_source); | 160 visitor->trace(m_source); |
| 159 Iterator::trace(visitor); | 161 Iterator::trace(visitor); |
| 160 } | 162 } |
| 161 | 163 |
| 162 private: | 164 private: |
| 163 Member<IterationSource> m_source; | 165 Member<IterationSource> m_source; |
| 164 }; | 166 }; |
| 165 }; | 167 }; |
| 166 | 168 |
| 167 // Utiltity mixin base-class for classes implementing IDL interfaces with | 169 // Utiltity mixin base-class for classes implementing IDL interfaces with |
| 168 // "iterable<T>". | |
| 169 template <typename ValueType> | |
| 170 class ValueIterable : public Iterable<unsigned, ValueType> { | |
| 171 public: | |
| 172 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) { | |
| 173 return this->valuesForBinding(scriptState, exceptionState); | |
| 174 } | |
| 175 | |
| 176 class IterationSource | |
| 177 : public Iterable<unsigned, ValueType>::IterationSource { | |
| 178 public: | |
| 179 IterationSource() : m_index(0) {} | |
| 180 | |
| 181 ~IterationSource() override {} | |
| 182 | |
| 183 // If end of iteration has been reached or an exception thrown: return | |
| 184 // false. | |
| 185 // Otherwise: set |value| and return true. | |
| 186 // Note: |this->m_index| is the index being accessed. | |
| 187 virtual bool next(ScriptState*, ValueType&, ExceptionState&) = 0; | |
| 188 | |
| 189 protected: | |
| 190 unsigned m_index; | |
| 191 | |
| 192 private: | |
| 193 bool next(ScriptState* scriptState, | |
| 194 unsigned& key, | |
| 195 ValueType& value, | |
| 196 ExceptionState& exceptionState) final { | |
| 197 if (!next(scriptState, value, exceptionState)) | |
| 198 return false; | |
| 199 key = m_index; | |
| 200 ++m_index; | |
| 201 return true; | |
| 202 } | |
| 203 }; | |
| 204 }; | |
| 205 | |
| 206 // Utiltity mixin base-class for classes implementing IDL interfaces with | |
| 207 // "iterable<T1, T2>". | 170 // "iterable<T1, T2>". |
| 208 template <typename KeyType, typename ValueType> | 171 template <typename KeyType, typename ValueType> |
| 209 class PairIterable : public Iterable<KeyType, ValueType> { | 172 class PairIterable : public Iterable<KeyType, ValueType> { |
| 210 public: | 173 public: |
| 211 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) { | 174 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState) { |
| 212 return this->entriesForBinding(scriptState, exceptionState); | 175 return this->entriesForBinding(scriptState, exceptionState); |
| 213 } | 176 } |
| 214 }; | 177 }; |
| 215 | 178 |
| 216 } // namespace blink | 179 } // namespace blink |
| 217 | 180 |
| 218 #endif // Iterable_h | 181 #endif // Iterable_h |
| OLD | NEW |