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

Side by Side Diff: Source/core/dom/Iterable.h

Issue 848673002: Add keys(), values() and entries() methods on iterable<> interfaces (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 11 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 unified diff | Download patch
OLDNEW
(Empty)
1 // 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
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: return false.
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 protected:
55 virtual IterationSource* startIteration(ScriptState*, ExceptionState&) = 0;
56
57 private:
58 struct KeySelector {
59 static KeyType select(ScriptState*, KeyType key, ValueType value)
60 {
61 return key;
62 }
63 };
64 struct ValueSelector {
65 static ValueType select(ScriptState*, KeyType key, ValueType value)
66 {
67 return value;
68 }
69 };
70 struct EntrySelector {
71 static Vector<ScriptValue> select(ScriptState* scriptState, KeyType key, ValueType value)
72 {
73 v8::Local<v8::Object> creationContext = scriptState->context()->Glob al();
74 v8::Isolate* isolate = scriptState->isolate();
75
76 Vector<ScriptValue> entry;
77 entry.append(ScriptValue(scriptState, toV8(key, creationContext, iso late)));
78 entry.append(ScriptValue(scriptState, toV8(value, creationContext, i solate)));
79 return entry;
80 }
81 };
82
83 template <typename Selector>
84 class IterableIterator final : public Iterator {
85 public:
86 IterableIterator(IterationSource* source)
87 : m_source(source)
88 {
89 }
90
91 ScriptValue next(ScriptState* scriptState, ExceptionState& exceptionStat e) override
92 {
93 KeyType key;
94 ValueType value;
95
96 if (!m_source->next(scriptState, key, value, exceptionState))
97 return v8IteratorResultDone(scriptState);
98
99 return v8IteratorResult(scriptState, Selector::select(scriptState, k ey, value));
100 }
101
102 ScriptValue next(ScriptState* scriptState, ScriptValue, ExceptionState& exceptionState) override
103 {
104 return next(scriptState, exceptionState);
105 }
106
107 void trace(Visitor* visitor) override
108 {
109 visitor->trace(m_source);
110 Iterator::trace(visitor);
111 }
112
113 private:
114 Member<IterationSource> m_source;
115 };
116 };
117
118 // Utiltity mixin base-class for classes implementing IDL interfaces with "itera ble<T>".
119 template <typename ValueType>
120 class ValueIterable : public Iterable<unsigned, ValueType> {
121 public:
122 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState)
123 {
124 return this->values(scriptState, exceptionState);
125 }
126
127 class IterationSource : public Iterable<unsigned, ValueType>::IterationSourc e {
128 public:
129 virtual ~IterationSource() { }
130
131 // If end of iteration has been reached: return false.
132 // Otherwise: set |value| and return true.
133 // Note: |this->m_index| is the index being accessed.
134 virtual bool next(ScriptState*, ValueType&, ExceptionState&) = 0;
135
136 virtual void trace(Visitor*) { }
137
138 protected:
139 IterationSource()
140 : m_index(0)
141 {
142 }
143
144 unsigned m_index;
jsbell 2015/01/12 17:14:08 Are there cases where this would be updated by a d
145
146 private:
147 bool next(ScriptState* scriptState, unsigned& key, ValueType& value, Exc eptionState& exceptionState) override
148 {
149 if (!next(scriptState, value, exceptionState))
150 return false;
151 key = m_index;
152 ++m_index;
153 return true;
154 }
155 };
156 };
157
158 // Utiltity mixin base-class for classes implementing IDL interfaces with "itera ble<T1, T2>".
159 template <typename KeyType, typename ValueType>
160 class PairIterable : public Iterable<KeyType, ValueType> {
161 public:
162 Iterator* iterator(ScriptState* scriptState, ExceptionState& exceptionState)
163 {
164 return this->entries(scriptState, exceptionState);
165 }
166 };
167
168 } // namespace blink
169
170 #endif // Iterable_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698