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

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

Issue 263063002: Add pattern matchers for SkRecord (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: ben 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
OLDNEW
(Empty)
1 #ifndef SkRecordPattern_DEFINED
2 #define SkRecordPattern_DEFINED
3
4 #include "SkTLogic.h"
5
6 namespace SkRecords {
7
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().
10
11 // Matches a command of type T, and stores that command.
12 template <typename T>
13 class Is {
14 public:
15 typedef T type;
16 type* get() { return fPtr; }
17
18 bool match(T* ptr) {
19 fPtr = ptr;
20 return true;
21 }
22
23 template <typename U>
24 bool match(U*) {
25 fPtr = NULL;
26 return false;
27 }
28
29 private:
30 type* fPtr;
31 };
32
33 // Matches any command that draws, and stores its paint.
34 class IsDraw {
35 SK_CREATE_MEMBER_DETECTOR(paint);
36 public:
37 typedef SkPaint type;
38 type* get() { return fPaint; }
39
40 template <typename T>
41 SK_WHEN(HasMember_paint<T>, bool) match(T* draw) {
42 fPaint = AsPtr(draw->paint);
43 return true;
44 }
45
46 template <typename T>
47 SK_WHEN(!HasMember_paint<T>, bool) match(T*) {
48 fPaint = NULL;
49 return false;
50 }
51
52 private:
53 // Abstracts away whether the paint is always part of the command or optiona l.
54 template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; }
55 template <typename T> static T* AsPtr(T& x) { return &x; }
56
57 type* fPaint;
58 };
59
60 // Matches if Matcher doesn't. Stores nothing.
61 template <typename Matcher>
62 struct Not {
63 template <typename T>
64 bool match(T* ptr) { return !Matcher().match(ptr); }
65 };
66
67 // Matches if either of A or B does. Stores nothing.
68 template <typename A, typename B>
69 struct Or {
70 template <typename T>
71 bool match(T* ptr) { return A().match(ptr) || B().match(ptr); }
72 };
73
74 // Matches if any of A, B or C does. Stores nothing.
75 template <typename A, typename B, typename C>
76 struct Or3 : Or<A, Or<B, C> > {};
77
78 // We'll use this to choose which implementation of Star suits each Matcher.
79 SK_CREATE_TYPE_DETECTOR(type);
80
81 // Star is a special matcher that matches Matcher 0 or more times _greedily_ in the SkRecord.
82 // This version stores nothing. It's enabled when Matcher stores nothing.
83 template <typename Matcher, typename = void>
84 class Star {
85 public:
86 void reset() {}
87
88 template <typename T>
89 bool match(T* ptr) { return Matcher().match(ptr); }
90 };
91
92 // This version stores a list of matches. It's enabled if Matcher stores someth ing.
93 template <typename Matcher>
94 class Star<Matcher, SK_WHEN(HasType_type<Matcher>, void)> {
95 public:
96 typedef SkTDArray<typename Matcher::type*> type;
97 type* get() { return &fMatches; }
98
99 void reset() { fMatches.rewind(); }
100
101 template <typename T>
102 bool match(T* ptr) {
103 Matcher matcher;
104 if (matcher.match(ptr)) {
105 fMatches.push(matcher.get());
106 return true;
107 }
108 return false;
109 }
110
111 private:
112 type fMatches;
113 };
114
115
116 // Cons builds a list of Matchers.
117 // It first matches Matcher (something from above), then Pattern (another Cons o r Nil).
118 //
119 // This is the main entry point to pattern matching, and so provides a couple of extra API bits:
120 // - search scans through the record to look for matches;
121 // - first, second, and third return the data stored by their respective matche rs in the pattern.
122 //
123 // These Cons build lists analagously to Lisp's "cons". See Pattern# for the "l ist" equivalent.
f(malita) 2014/05/05 20:52:59 "analogously"
mtklein 2014/05/05 21:43:31 Done.
124 template <typename Matcher, typename Pattern>
125 class Cons {
126 public:
127 // If this pattern matches the SkRecord starting at i,
128 // return the index just past the end of the pattern, otherwise return 0.
129 SK_ALWAYS_INLINE unsigned match(SkRecord* record, unsigned i) {
130 i = this->matchHead(&fHead, record, i);
131 return i == 0 ? 0 : fTail.match(record, i);
132 }
133
134 // Starting from *end, walk through the SkRecord to find the first span matc hing this pattern.
135 // If there is no such span, return false. If there is, return true and set [*begin, *end).
136 SK_ALWAYS_INLINE bool search(SkRecord* record, unsigned* begin, unsigned* en d) {
137 for (*begin = *end; *begin < record->count(); ++(*begin)) {
138 *end = this->match(record, *begin);
139 if (*end != 0) {
140 return true;
141 }
142 }
143 return false;
144 }
145
146 // Once either match or search has succeeded, access the stored data of the first, second,
147 // or third matcher in this pattern. Add as needed for longer patterns.
148 // T is checked statically at compile time; no casting is involved. It's ju st an API wart.
149 template <typename T> T* first() { return fHead.get(); }
150 template <typename T> T* second() { return fTail.fHead.get(); }
151 template <typename T> T* third() { return fTail.fTail.fHead.get(); }
152
153 private:
154 template <typename T>
155 void operator()(T* r) { fHeadMatched = fHead.match(r); }
156
157 // If head isn't a Star, try to match at i once.
158 template <typename T>
159 unsigned matchHead(T*, SkRecord* record, unsigned i) {
160 if (i < record->count()) {
161 fHeadMatched = false;
162 record->mutate(i, *this);
163 if (fHeadMatched) {
164 return i+1;
165 }
166 }
167 return 0;
168 }
169
170 // If head is a Star, walk i until it doesn't match.
171 template <typename T>
172 unsigned matchHead(Star<T>*, SkRecord* record, unsigned i) {
173 fHead.reset();
174 while (i < record->count()) {
175 fHeadMatched = false;
176 record->mutate(i, *this);
177 if (!fHeadMatched) {
178 return i;
179 }
180 i++;
181 }
182 return 0;
183 }
184
185 Matcher fHead;
186 Pattern fTail;
187 bool fHeadMatched;
188
189 friend class ::SkRecord; // So operator() can otherwise stay private.
190
191 // All Cons are friends with each other. This lets first, second, and third work.
192 template <typename, typename> friend class Cons;
193 };
194
195 // Nil is the end of every pattern Cons chain.
196 struct Nil {
197 // Bottoms out recursion down the fTail chain. Just return whatever i the f ront decided on.
198 unsigned match(SkRecord*, unsigned i) { return i; }
199 };
200
201 // These Pattern# types are syntax sugar over Cons and Nil, just to help elimina te some of the
202 // template noise. Use these if you can. Feel free to add more for longer patt erns.
203 // All types A, B, C, ... are Matchers.
204 template <typename A>
205 struct Pattern1 : Cons<A, Nil> {};
206
207 template <typename A, typename B>
208 struct Pattern2 : Cons<A, Pattern1<B> > {};
209
210 template <typename A, typename B, typename C>
211 struct Pattern3 : Cons<A, Pattern2<B, C> > {};
212
213 } // namespace SkRecords
214
215 #endif//SkRecordPattern_DEFINED
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698