1 | /* |
2 | * Copyright 2015 Google Inc. |
3 | * |
4 | * Use of this source code is governed by a BSD-style license that can be |
5 | * found in the LICENSE file. |
6 | */ |
7 | |
8 | #ifndef SkRecordPattern_DEFINED |
9 | #define SkRecordPattern_DEFINED |
10 | |
11 | #include "include/private/SkTLogic.h" |
12 | #include "src/core/SkRecord.h" |
13 | |
14 | namespace SkRecords { |
15 | |
16 | // First, some matchers. These match a single command in the SkRecord, |
17 | // and may hang onto some data from it. If so, you can get the data by calling .get(). |
18 | |
19 | // Matches a command of type T, and stores that command. |
20 | template <typename T> |
21 | class Is { |
22 | public: |
23 | Is() : fPtr(nullptr) {} |
24 | |
25 | typedef T type; |
26 | type* get() { return fPtr; } |
27 | |
28 | bool operator()(T* ptr) { |
29 | fPtr = ptr; |
30 | return true; |
31 | } |
32 | |
33 | template <typename U> |
34 | bool operator()(U*) { |
35 | fPtr = nullptr; |
36 | return false; |
37 | } |
38 | |
39 | private: |
40 | type* fPtr; |
41 | }; |
42 | |
43 | // Matches any command that draws, and stores its paint. |
44 | class IsDraw { |
45 | public: |
46 | IsDraw() : fPaint(nullptr) {} |
47 | |
48 | typedef SkPaint type; |
49 | type* get() { return fPaint; } |
50 | |
51 | template <typename T> |
52 | std::enable_if_t<(T::kTags & kDrawWithPaint_Tag) == kDrawWithPaint_Tag, bool> |
53 | operator()(T* draw) { |
54 | fPaint = AsPtr(draw->paint); |
55 | return true; |
56 | } |
57 | |
58 | template <typename T> |
59 | std::enable_if_t<(T::kTags & kDrawWithPaint_Tag) == kDraw_Tag, bool> operator()(T* draw) { |
60 | fPaint = nullptr; |
61 | return true; |
62 | } |
63 | |
64 | template <typename T> |
65 | std::enable_if_t<!(T::kTags & kDraw_Tag), bool> operator()(T* draw) { |
66 | fPaint = nullptr; |
67 | return false; |
68 | } |
69 | |
70 | private: |
71 | // Abstracts away whether the paint is always part of the command or optional. |
72 | template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; } |
73 | template <typename T> static T* AsPtr(T& x) { return &x; } |
74 | |
75 | type* fPaint; |
76 | }; |
77 | |
78 | // Matches if Matcher doesn't. Stores nothing. |
79 | template <typename Matcher> |
80 | struct Not { |
81 | template <typename T> |
82 | bool operator()(T* ptr) { return !Matcher()(ptr); } |
83 | }; |
84 | |
85 | // Matches if any of First or Rest... does. Stores nothing. |
86 | template <typename First, typename... Rest> |
87 | struct Or { |
88 | template <typename T> |
89 | bool operator()(T* ptr) { return First()(ptr) || Or<Rest...>()(ptr); } |
90 | }; |
91 | template <typename First> |
92 | struct Or<First> { |
93 | template <typename T> |
94 | bool operator()(T* ptr) { return First()(ptr); } |
95 | }; |
96 | |
97 | |
98 | // Greedy is a special matcher that greedily matches Matcher 0 or more times. Stores nothing. |
99 | template <typename Matcher> |
100 | struct Greedy { |
101 | template <typename T> |
102 | bool operator()(T* ptr) { return Matcher()(ptr); } |
103 | }; |
104 | |
105 | // Pattern matches each of its matchers in order. |
106 | // |
107 | // This is the main entry point to pattern matching, and so provides a couple of extra API bits: |
108 | // - search scans through the record to look for matches; |
109 | // - first, second, third, ... return the data stored by their respective matchers in the pattern. |
110 | |
111 | template <typename... Matchers> class Pattern; |
112 | |
113 | template <> class Pattern<> { |
114 | public: |
115 | // Bottoms out recursion. Just return whatever i the front decided on. |
116 | int match(SkRecord*, int i) { return i; } |
117 | }; |
118 | |
119 | template <typename First, typename... Rest> |
120 | class Pattern<First, Rest...> { |
121 | public: |
122 | // If this pattern matches the SkRecord starting from i, |
123 | // return the index just past the end of the pattern, otherwise return 0. |
124 | SK_ALWAYS_INLINE int match(SkRecord* record, int i) { |
125 | i = this->matchFirst(&fFirst, record, i); |
126 | return i > 0 ? fRest.match(record, i) : 0; |
127 | } |
128 | |
129 | // Starting from *end, walk through the SkRecord to find the first span matching this pattern. |
130 | // If there is no such span, return false. If there is, return true and set [*begin, *end). |
131 | SK_ALWAYS_INLINE bool search(SkRecord* record, int* begin, int* end) { |
132 | for (*begin = *end; *begin < record->count(); ++(*begin)) { |
133 | *end = this->match(record, *begin); |
134 | if (*end != 0) { |
135 | return true; |
136 | } |
137 | } |
138 | return false; |
139 | } |
140 | |
141 | // TODO: some sort of smart get<i>() |
142 | template <typename T> T* first() { return fFirst.get(); } |
143 | template <typename T> T* second() { return fRest.template first<T>(); } |
144 | template <typename T> T* third() { return fRest.template second<T>(); } |
145 | template <typename T> T* fourth() { return fRest.template third<T>(); } |
146 | |
147 | private: |
148 | // If first isn't a Greedy, try to match at i once. |
149 | template <typename T> |
150 | int matchFirst(T* first, SkRecord* record, int i) { |
151 | if (i < record->count()) { |
152 | if (record->mutate(i, *first)) { |
153 | return i+1; |
154 | } |
155 | } |
156 | return 0; |
157 | } |
158 | |
159 | // If first is a Greedy, walk i until it doesn't match. |
160 | template <typename T> |
161 | int matchFirst(Greedy<T>* first, SkRecord* record, int i) { |
162 | while (i < record->count()) { |
163 | if (!record->mutate(i, *first)) { |
164 | return i; |
165 | } |
166 | i++; |
167 | } |
168 | return 0; |
169 | } |
170 | |
171 | First fFirst; |
172 | Pattern<Rest...> fRest; |
173 | }; |
174 | |
175 | } // namespace SkRecords |
176 | |
177 | #endif//SkRecordPattern_DEFINED |
178 | |