1#ifndef SkRecordPattern_DEFINED
2#define SkRecordPattern_DEFINED
3
4#include "SkTLogic.h"
5
6namespace 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.
12template <typename T>
13class Is {
14public:
15    Is() : fPtr(NULL) {}
16
17    typedef T type;
18    type* get() { return fPtr; }
19
20    bool operator()(T* ptr) {
21        fPtr = ptr;
22        return true;
23    }
24
25    template <typename U>
26    bool operator()(U*) {
27        fPtr = NULL;
28        return false;
29    }
30
31private:
32    type* fPtr;
33};
34
35// Matches any command that draws, and stores its paint.
36class IsDraw {
37    SK_CREATE_MEMBER_DETECTOR(paint);
38public:
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) operator()(T* draw) {
46        fPaint = AsPtr(draw->paint);
47        return true;
48    }
49
50    template <typename T>
51    SK_WHEN(!HasMember_paint<T>, bool) operator()(T*) {
52        fPaint = NULL;
53        return false;
54    }
55
56    // SaveLayer has an SkPaint named paint, but it's not a draw.
57    bool operator()(SaveLayer*) {
58        fPaint = NULL;
59        return false;
60    }
61
62private:
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; }
66
67    type* fPaint;
68};
69
70// Matches if Matcher doesn't.  Stores nothing.
71template <typename Matcher>
72struct Not {
73    template <typename T>
74    bool operator()(T* ptr) { return !Matcher()(ptr); }
75};
76
77// Matches if either of A or B does.  Stores nothing.
78template <typename A, typename B>
79struct Or {
80    template <typename T>
81    bool operator()(T* ptr) { return A()(ptr) || B()(ptr); }
82};
83
84// Matches if any of A, B or C does.  Stores nothing.
85template <typename A, typename B, typename C>
86struct Or3 : Or<A, Or<B, C> > {};
87
88// Star is a special matcher that greedily matches Matcher 0 or more times.  Stores nothing.
89template <typename Matcher>
90struct Star {
91    template <typename T>
92    bool operator()(T* ptr) { return Matcher()(ptr); }
93};
94
95// Cons builds a list of Matchers.
96// It first matches Matcher (something from above), then Pattern (another Cons or Nil).
97//
98// This is the main entry point to pattern matching, and so provides a couple of extra API bits:
99//  - search scans through the record to look for matches;
100//  - first, second, and third return the data stored by their respective matchers in the pattern.
101//
102// These Cons build lists analogously to Lisp's "cons".  See Pattern# for the "list" equivalent.
103template <typename Matcher, typename Pattern>
104class Cons {
105public:
106    // If this pattern matches the SkRecord starting at i,
107    // return the index just past the end of the pattern, otherwise return 0.
108    SK_ALWAYS_INLINE unsigned match(SkRecord* record, unsigned i) {
109        i = this->matchHead(&fHead, record, i);
110        return i == 0 ? 0 : fTail.match(record, i);
111    }
112
113    // Starting from *end, walk through the SkRecord to find the first span matching this pattern.
114    // If there is no such span, return false.  If there is, return true and set [*begin, *end).
115    SK_ALWAYS_INLINE bool search(SkRecord* record, unsigned* begin, unsigned* end) {
116        for (*begin = *end; *begin < record->count(); ++(*begin)) {
117            *end = this->match(record, *begin);
118            if (*end != 0) {
119                return true;
120            }
121        }
122        return false;
123    }
124
125    // Once either match or search has succeeded, access the stored data of the first, second,
126    // or third matcher in this pattern.  Add as needed for longer patterns.
127    // T is checked statically at compile time; no casting is involved.  It's just an API wart.
128    template <typename T> T* first()  { return fHead.get(); }
129    template <typename T> T* second() { return fTail.fHead.get(); }
130    template <typename T> T* third()  { return fTail.fTail.fHead.get(); }
131
132private:
133    // If head isn't a Star, try to match at i once.
134    template <typename T>
135    unsigned matchHead(T*, SkRecord* record, unsigned i) {
136        if (i < record->count()) {
137            if (record->mutate<bool>(i, fHead)) {
138                return i+1;
139            }
140        }
141        return 0;
142    }
143
144    // If head is a Star, walk i until it doesn't match.
145    template <typename T>
146    unsigned matchHead(Star<T>*, SkRecord* record, unsigned i) {
147        while (i < record->count()) {
148            if (!record->mutate<bool>(i, fHead)) {
149                return i;
150            }
151            i++;
152        }
153        return 0;
154    }
155
156    Matcher fHead;
157    Pattern fTail;
158
159    // All Cons are friends with each other.  This lets first, second, and third work.
160    template <typename, typename> friend class Cons;
161};
162
163// Nil is the end of every pattern Cons chain.
164struct Nil {
165    // Bottoms out recursion down the fTail chain.  Just return whatever i the front decided on.
166    unsigned match(SkRecord*, unsigned i) { return i; }
167};
168
169// These Pattern# types are syntax sugar over Cons and Nil, just to help eliminate some of the
170// template noise.  Use these if you can.  Feel free to add more for longer patterns.
171// All types A, B, C, ... are Matchers.
172template <typename A>
173struct Pattern1 : Cons<A, Nil> {};
174
175template <typename A, typename B>
176struct Pattern2 : Cons<A, Pattern1<B> > {};
177
178template <typename A, typename B, typename C>
179struct Pattern3 : Cons<A, Pattern2<B, C> > {};
180
181}  // namespace SkRecords
182
183#endif//SkRecordPattern_DEFINED
184