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

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: yet more notes 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 // Star is a special matcher that matches Matcher 0 or more times _greedily_ in the SkRecord.
79 // This version stores nothing. It's enabled when Matcher stores nothing.
80 template <typename Matcher, typename = void>
81 class Star {
82 public:
83 void reset() {}
84
85 template <typename T>
86 bool match(T* ptr) { return Matcher().match(ptr); }
87 };
88
89 // This version stores a list of matches. It's enabled if Matcher stores someth ing.
90 template <typename Matcher>
91 class Star<Matcher, SK_WHEN_TYPE(typename Matcher::type, void)> {
92 public:
93 typedef SkTDArray<typename Matcher::type*> type;
94 type* get() { return &fMatches; }
95
96 void reset() { fMatches.rewind(); }
97
98 template <typename T>
99 bool match(T* ptr) {
100 Matcher matcher;
101 if (matcher.match(ptr)) {
102 fMatches.push(matcher.get());
103 return true;
104 }
105 return false;
106 }
107
108 private:
109 type fMatches;
110 };
111
112
113 // Cons builds a list of Matchers.
114 // It first matches Matcher (something from above), then Pattern (another Cons o r Nil).
115 //
116 // This is the main entry point to pattern matching, and so provides a couple of extra API bits:
117 // - search scans through the record to look for matches;
118 // - first, second, and third return the data stored by their respective matche rs in the pattern.
119 //
120 // These Cons build lists analagously to Lisp's "cons". See Pattern# for the "l ist" equivalent.
121 template <typename Matcher, typename Pattern>
122 class Cons {
123 public:
124 // If this pattern matches the SkRecord starting at i,
125 // return the index just past the end of the pattern, otherwise return 0.
126 SK_ALWAYS_INLINE unsigned match(SkRecord* record, unsigned i) {
127 i = this->matchHead(&fHead, record, i);
128 return i == 0 ? 0 : fTail.match(record, i);
129 }
130
131 // Starting from *end, walk through the SkRecord to find the first span matc hing this pattern.
132 // If there is no such span, return false. If there is, return true and set [*begin, *end).
133 SK_ALWAYS_INLINE bool search(SkRecord* record, unsigned* begin, unsigned* en d) {
134 for (*begin = *end; *begin < record->count(); ++(*begin)) {
135 *end = this->match(record, *begin);
136 if (*end != 0) {
137 return true;
138 }
139 }
140 return false;
141 }
142
143 // Once either match or search has succeeded, access the stored data of the first, second,
144 // or third matcher in this pattern. Add as needed for longer patterns.
145 // T is checked statically at compile time; no casting is involved. It's ju st an API wart.
146 template <typename T> T* first() { return fHead.get(); }
147 template <typename T> T* second() { return fTail.fHead.get(); }
148 template <typename T> T* third() { return fTail.fTail.fHead.get(); }
149
150 private:
151 template <typename T>
152 void operator()(T* r) { fHeadMatched = fHead.match(r); }
153
154 // If head isn't a Star, try to match at i once.
155 template <typename T>
156 unsigned matchHead(T*, SkRecord* record, unsigned i) {
157 if (i < record->count()) {
158 fHeadMatched = false;
159 record->mutate(i, *this);
160 if (fHeadMatched) {
161 return i+1;
162 }
163 }
164 return 0;
165 }
166
167 // If head is a Star, walk i until it doesn't match.
168 template <typename T>
169 unsigned matchHead(Star<T>*, SkRecord* record, unsigned i) {
170 fHead.reset();
171 while (i < record->count()) {
172 fHeadMatched = false;
173 record->mutate(i, *this);
174 if (!fHeadMatched) {
175 return i;
176 }
177 i++;
178 }
179 return 0;
180 }
181
182 Matcher fHead;
183 Pattern fTail;
184 bool fHeadMatched;
185
186 friend class ::SkRecord; // So operator() can otherwise stay private.
187
188 // All Cons are friends with each other. This lets first, second, and third work.
189 template <typename, typename> friend class Cons;
190 };
191
192 // Nil is the end of every pattern Cons chain.
193 struct Nil {
194 // Bottoms out recursion down the fTail chain. Just return whatever i the f ront decided on.
195 unsigned match(SkRecord*, unsigned i) { return i; }
196 };
197
198 // These Pattern# types are syntax sugar over Cons and Nil, just to help elimina te some of the
199 // template noise. Use these if you can. Feel free to add more for longer patt erns.
200 // All types A, B, C, ... are Matchers.
201 template <typename A>
202 struct Pattern1 : Cons<A, Nil> {};
203
204 template <typename A, typename B>
205 struct Pattern2 : Cons<A, Pattern1<B> > {};
206
207 template <typename A, typename B, typename C>
208 struct Pattern3 : Cons<A, Pattern2<B, C> > {};
209
210 } // namespace SkRecords
211
212 #endif//SkRecordPattern_DEFINED
OLDNEW
« no previous file with comments | « src/record/SkRecordOpts.cpp ('k') | src/record/SkRecordTraits.h » ('j') | src/utils/SkTLogic.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698