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

Side by Side Diff: src/record/SkRecordPattern.h

Issue 273643007: Convert all SkRecordPattern matchers into SkRecord mutators. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: dumper too Created 6 years, 7 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
« no previous file with comments | « src/record/SkRecordOpts.cpp ('k') | tests/RecordOptsTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 #ifndef SkRecordPattern_DEFINED 1 #ifndef SkRecordPattern_DEFINED
2 #define SkRecordPattern_DEFINED 2 #define SkRecordPattern_DEFINED
3 3
4 #include "SkTLogic.h" 4 #include "SkTLogic.h"
5 5
6 namespace SkRecords { 6 namespace SkRecords {
7 7
8 // First, some matchers. These match a single command in the SkRecord, 8 // First, some matchers. These match a single command in the SkRecord,
9 // and may hang onto some data from it. If so, you can get the data by calling .get(). 9 // and may hang onto some data from it. If so, you can get the data by calling .get().
10 10
11 // Matches a command of type T, and stores that command. 11 // Matches a command of type T, and stores that command.
12 template <typename T> 12 template <typename T>
13 class Is { 13 class Is {
14 public: 14 public:
15 Is() : fPtr(NULL) {} 15 Is() : fPtr(NULL) {}
16 16
17 typedef T type; 17 typedef T type;
18 type* get() { return fPtr; } 18 type* get() { return fPtr; }
19 19
20 bool match(T* ptr) { 20 bool operator()(T* ptr) {
21 fPtr = ptr; 21 fPtr = ptr;
22 return true; 22 return true;
23 } 23 }
24 24
25 template <typename U> 25 template <typename U>
26 bool match(U*) { 26 bool operator()(U*) {
27 fPtr = NULL; 27 fPtr = NULL;
28 return false; 28 return false;
29 } 29 }
30 30
31 private: 31 private:
32 type* fPtr; 32 type* fPtr;
33 }; 33 };
34 34
35 // Matches any command that draws, and stores its paint. 35 // Matches any command that draws, and stores its paint.
36 class IsDraw { 36 class IsDraw {
37 SK_CREATE_MEMBER_DETECTOR(paint); 37 SK_CREATE_MEMBER_DETECTOR(paint);
38 public: 38 public:
39 IsDraw() : fPaint(NULL) {} 39 IsDraw() : fPaint(NULL) {}
40 40
41 typedef SkPaint type; 41 typedef SkPaint type;
42 type* get() { return fPaint; } 42 type* get() { return fPaint; }
43 43
44 template <typename T> 44 template <typename T>
45 SK_WHEN(HasMember_paint<T>, bool) match(T* draw) { 45 SK_WHEN(HasMember_paint<T>, bool) operator()(T* draw) {
46 fPaint = AsPtr(draw->paint); 46 fPaint = AsPtr(draw->paint);
47 return true; 47 return true;
48 } 48 }
49 49
50 template <typename T> 50 template <typename T>
51 SK_WHEN(!HasMember_paint<T>, bool) match(T*) { 51 SK_WHEN(!HasMember_paint<T>, bool) operator()(T*) {
52 fPaint = NULL; 52 fPaint = NULL;
53 return false; 53 return false;
54 } 54 }
55 55
56 // SaveLayer has an SkPaint named paint, but it's not a draw. 56 // SaveLayer has an SkPaint named paint, but it's not a draw.
57 bool match(SaveLayer*) { 57 bool operator()(SaveLayer*) {
58 fPaint = NULL; 58 fPaint = NULL;
59 return false; 59 return false;
60 } 60 }
61 61
62 private: 62 private:
63 // Abstracts away whether the paint is always part of the command or optiona l. 63 // Abstracts away whether the paint is always part of the command or optiona l.
64 template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; } 64 template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; }
65 template <typename T> static T* AsPtr(T& x) { return &x; } 65 template <typename T> static T* AsPtr(T& x) { return &x; }
66 66
67 type* fPaint; 67 type* fPaint;
68 }; 68 };
69 69
70 // Matches if Matcher doesn't. Stores nothing. 70 // Matches if Matcher doesn't. Stores nothing.
71 template <typename Matcher> 71 template <typename Matcher>
72 struct Not { 72 struct Not {
73 template <typename T> 73 template <typename T>
74 bool match(T* ptr) { return !Matcher().match(ptr); } 74 bool operator()(T* ptr) { return !Matcher()(ptr); }
75 }; 75 };
76 76
77 // Matches if either of A or B does. Stores nothing. 77 // Matches if either of A or B does. Stores nothing.
78 template <typename A, typename B> 78 template <typename A, typename B>
79 struct Or { 79 struct Or {
80 template <typename T> 80 template <typename T>
81 bool match(T* ptr) { return A().match(ptr) || B().match(ptr); } 81 bool operator()(T* ptr) { return A()(ptr) || B()(ptr); }
82 }; 82 };
83 83
84 // Matches if any of A, B or C does. Stores nothing. 84 // Matches if any of A, B or C does. Stores nothing.
85 template <typename A, typename B, typename C> 85 template <typename A, typename B, typename C>
86 struct Or3 : Or<A, Or<B, C> > {}; 86 struct Or3 : Or<A, Or<B, C> > {};
87 87
88 // We'll use this to choose which implementation of Star suits each Matcher. 88 // We'll use this to choose which implementation of Star suits each Matcher.
89 SK_CREATE_TYPE_DETECTOR(type); 89 SK_CREATE_TYPE_DETECTOR(type);
90 90
91 // Star is a special matcher that matches Matcher 0 or more times _greedily_ in the SkRecord. 91 // Star is a special matcher that matches Matcher 0 or more times _greedily_ in the SkRecord.
92 // This version stores nothing. It's enabled when Matcher stores nothing. 92 // This version stores nothing. It's enabled when Matcher stores nothing.
93 template <typename Matcher, typename = void> 93 template <typename Matcher, typename = void>
94 class Star { 94 class Star {
95 public: 95 public:
96 void reset() {} 96 void reset() {}
97 97
98 template <typename T> 98 template <typename T>
99 bool match(T* ptr) { return Matcher().match(ptr); } 99 bool operator()(T* ptr) { return Matcher()(ptr); }
100 }; 100 };
101 101
102 // This version stores a list of matches. It's enabled if Matcher stores someth ing. 102 // This version stores a list of matches. It's enabled if Matcher stores someth ing.
103 template <typename Matcher> 103 template <typename Matcher>
104 class Star<Matcher, SK_WHEN(HasType_type<Matcher>, void)> { 104 class Star<Matcher, SK_WHEN(HasType_type<Matcher>, void)> {
105 public: 105 public:
106 typedef SkTDArray<typename Matcher::type*> type; 106 typedef SkTDArray<typename Matcher::type*> type;
107 type* get() { return &fMatches; } 107 type* get() { return &fMatches; }
108 108
109 void reset() { fMatches.rewind(); } 109 void reset() { fMatches.rewind(); }
110 110
111 template <typename T> 111 template <typename T>
112 bool match(T* ptr) { 112 bool operator()(T* ptr) {
113 Matcher matcher; 113 Matcher matcher;
114 if (matcher.match(ptr)) { 114 if (matcher(ptr)) {
115 fMatches.push(matcher.get()); 115 fMatches.push(matcher.get());
116 return true; 116 return true;
117 } 117 }
118 return false; 118 return false;
119 } 119 }
120 120
121 private: 121 private:
122 type fMatches; 122 type fMatches;
123 }; 123 };
124 124
(...skipping 29 matching lines...) Expand all
154 } 154 }
155 155
156 // Once either match or search has succeeded, access the stored data of the first, second, 156 // Once either match or search has succeeded, access the stored data of the first, second,
157 // or third matcher in this pattern. Add as needed for longer patterns. 157 // or third matcher in this pattern. Add as needed for longer patterns.
158 // T is checked statically at compile time; no casting is involved. It's ju st an API wart. 158 // T is checked statically at compile time; no casting is involved. It's ju st an API wart.
159 template <typename T> T* first() { return fHead.get(); } 159 template <typename T> T* first() { return fHead.get(); }
160 template <typename T> T* second() { return fTail.fHead.get(); } 160 template <typename T> T* second() { return fTail.fHead.get(); }
161 template <typename T> T* third() { return fTail.fTail.fHead.get(); } 161 template <typename T> T* third() { return fTail.fTail.fHead.get(); }
162 162
163 private: 163 private:
164 template <typename T>
165 void operator()(T* r) { fHeadMatched = fHead.match(r); }
166
167 // If head isn't a Star, try to match at i once. 164 // If head isn't a Star, try to match at i once.
168 template <typename T> 165 template <typename T>
169 unsigned matchHead(T*, SkRecord* record, unsigned i) { 166 unsigned matchHead(T*, SkRecord* record, unsigned i) {
170 if (i < record->count()) { 167 if (i < record->count()) {
171 fHeadMatched = false; 168 if (record->mutate<bool>(i, fHead)) {
172 record->mutate(i, *this);
173 if (fHeadMatched) {
174 return i+1; 169 return i+1;
175 } 170 }
176 } 171 }
177 return 0; 172 return 0;
178 } 173 }
179 174
180 // If head is a Star, walk i until it doesn't match. 175 // If head is a Star, walk i until it doesn't match.
181 template <typename T> 176 template <typename T>
182 unsigned matchHead(Star<T>*, SkRecord* record, unsigned i) { 177 unsigned matchHead(Star<T>*, SkRecord* record, unsigned i) {
183 fHead.reset(); 178 fHead.reset();
184 while (i < record->count()) { 179 while (i < record->count()) {
185 fHeadMatched = false; 180 if (!record->mutate<bool>(i, fHead)) {
186 record->mutate(i, *this);
187 if (!fHeadMatched) {
188 return i; 181 return i;
189 } 182 }
190 i++; 183 i++;
191 } 184 }
192 return 0; 185 return 0;
193 } 186 }
194 187
195 Matcher fHead; 188 Matcher fHead;
196 Pattern fTail; 189 Pattern fTail;
197 bool fHeadMatched;
198
199 friend class ::SkRecord; // So operator() can otherwise stay private.
200 190
201 // All Cons are friends with each other. This lets first, second, and third work. 191 // All Cons are friends with each other. This lets first, second, and third work.
202 template <typename, typename> friend class Cons; 192 template <typename, typename> friend class Cons;
203 }; 193 };
204 194
205 // Nil is the end of every pattern Cons chain. 195 // Nil is the end of every pattern Cons chain.
206 struct Nil { 196 struct Nil {
207 // Bottoms out recursion down the fTail chain. Just return whatever i the f ront decided on. 197 // Bottoms out recursion down the fTail chain. Just return whatever i the f ront decided on.
208 unsigned match(SkRecord*, unsigned i) { return i; } 198 unsigned match(SkRecord*, unsigned i) { return i; }
209 }; 199 };
210 200
211 // These Pattern# types are syntax sugar over Cons and Nil, just to help elimina te some of the 201 // These Pattern# types are syntax sugar over Cons and Nil, just to help elimina te some of the
212 // template noise. Use these if you can. Feel free to add more for longer patt erns. 202 // template noise. Use these if you can. Feel free to add more for longer patt erns.
213 // All types A, B, C, ... are Matchers. 203 // All types A, B, C, ... are Matchers.
214 template <typename A> 204 template <typename A>
215 struct Pattern1 : Cons<A, Nil> {}; 205 struct Pattern1 : Cons<A, Nil> {};
216 206
217 template <typename A, typename B> 207 template <typename A, typename B>
218 struct Pattern2 : Cons<A, Pattern1<B> > {}; 208 struct Pattern2 : Cons<A, Pattern1<B> > {};
219 209
220 template <typename A, typename B, typename C> 210 template <typename A, typename B, typename C>
221 struct Pattern3 : Cons<A, Pattern2<B, C> > {}; 211 struct Pattern3 : Cons<A, Pattern2<B, C> > {};
222 212
223 } // namespace SkRecords 213 } // namespace SkRecords
224 214
225 #endif//SkRecordPattern_DEFINED 215 #endif//SkRecordPattern_DEFINED
OLDNEW
« no previous file with comments | « src/record/SkRecordOpts.cpp ('k') | tests/RecordOptsTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698