1 #ifndef SkRecordPattern_DEFINED
2 #define SkRecordPattern_DEFINED
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().
11 // Matches a command of type T, and stores that command.
18 type* get() { return fPtr; }
20 bool operator()(T* ptr) {
35 // Matches any command that draws, and stores its paint.
37 SK_CREATE_MEMBER_DETECTOR(paint);
39 IsDraw() : fPaint(NULL) {}
42 type* get() { return fPaint; }
45 SK_WHEN(HasMember_paint<T>, bool) operator()(T* draw) {
46 fPaint = AsPtr(draw->paint);
51 SK_WHEN(!HasMember_paint<T>, bool) operator()(T*) {
56 // SaveLayer has an SkPaint named paint, but it's not a draw.
57 bool operator()(SaveLayer*) {
63 // Abstracts away whether the paint is always part of the command or optional.
64 template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; }
65 template <typename T> static T* AsPtr(T& x) { return &x; }
70 // Matches if Matcher doesn't. Stores nothing.
71 template <typename Matcher>
74 bool operator()(T* ptr) { return !Matcher()(ptr); }
77 // Matches if either of A or B does. Stores nothing.
78 template <typename A, typename B>
81 bool operator()(T* ptr) { return A()(ptr) || B()(ptr); }
84 // Matches if any of A, B or C does. Stores nothing.
85 template <typename A, typename B, typename C>
86 struct Or3 : Or<A, Or<B, C> > {};
88 // Matches if any of A, B, C or D does. Stores nothing.
89 template <typename A, typename B, typename C, typename D>
90 struct Or4 : Or<A, Or<B, Or<C, D> > > {};
92 // Star is a special matcher that greedily matches Matcher 0 or more times. Stores nothing.
93 template <typename Matcher>
96 bool operator()(T* ptr) { return Matcher()(ptr); }
99 // Cons builds a list of Matchers.
100 // It first matches Matcher (something from above), then Pattern (another Cons or Nil).
102 // This is the main entry point to pattern matching, and so provides a couple of extra API bits:
103 // - search scans through the record to look for matches;
104 // - first, second, and third return the data stored by their respective matchers in the pattern.
106 // These Cons build lists analogously to Lisp's "cons". See Pattern# for the "list" equivalent.
107 template <typename Matcher, typename Pattern>
110 // If this pattern matches the SkRecord starting at i,
111 // return the index just past the end of the pattern, otherwise return 0.
112 SK_ALWAYS_INLINE unsigned match(SkRecord* record, unsigned i) {
113 i = this->matchHead(&fHead, record, i);
114 return i == 0 ? 0 : fTail.match(record, i);
117 // Starting from *end, walk through the SkRecord to find the first span matching this pattern.
118 // If there is no such span, return false. If there is, return true and set [*begin, *end).
119 SK_ALWAYS_INLINE bool search(SkRecord* record, unsigned* begin, unsigned* end) {
120 for (*begin = *end; *begin < record->count(); ++(*begin)) {
121 *end = this->match(record, *begin);
129 // Once either match or search has succeeded, access the stored data of the first, second,
130 // or third matcher in this pattern. Add as needed for longer patterns.
131 // T is checked statically at compile time; no casting is involved. It's just an API wart.
132 template <typename T> T* first() { return fHead.get(); }
133 template <typename T> T* second() { return fTail.fHead.get(); }
134 template <typename T> T* third() { return fTail.fTail.fHead.get(); }
137 // If head isn't a Star, try to match at i once.
138 template <typename T>
139 unsigned matchHead(T*, SkRecord* record, unsigned i) {
140 if (i < record->count()) {
141 if (record->mutate<bool>(i, fHead)) {
148 // If head is a Star, walk i until it doesn't match.
149 template <typename T>
150 unsigned matchHead(Star<T>*, SkRecord* record, unsigned i) {
151 while (i < record->count()) {
152 if (!record->mutate<bool>(i, fHead)) {
163 // All Cons are friends with each other. This lets first, second, and third work.
164 template <typename, typename> friend class Cons;
167 // Nil is the end of every pattern Cons chain.
169 // Bottoms out recursion down the fTail chain. Just return whatever i the front decided on.
170 unsigned match(SkRecord*, unsigned i) { return i; }
173 // These Pattern# types are syntax sugar over Cons and Nil, just to help eliminate some of the
174 // template noise. Use these if you can. Feel free to add more for longer patterns.
175 // All types A, B, C, ... are Matchers.
176 template <typename A>
177 struct Pattern1 : Cons<A, Nil> {};
179 template <typename A, typename B>
180 struct Pattern2 : Cons<A, Pattern1<B> > {};
182 template <typename A, typename B, typename C>
183 struct Pattern3 : Cons<A, Pattern2<B, C> > {};
185 } // namespace SkRecords
187 #endif//SkRecordPattern_DEFINED