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

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

Powered by Google App Engine
This is Rietveld 408576698