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// Matches if any of A, B, C or D does. Stores nothing. 89template <typename A, typename B, typename C, typename D> 90struct Or4 : Or<A, Or<B, Or<C, D> > > {}; 91 92// Star is a special matcher that greedily matches Matcher 0 or more times. Stores nothing. 93template <typename Matcher> 94struct Star { 95 template <typename T> 96 bool operator()(T* ptr) { return Matcher()(ptr); } 97}; 98 99// Cons builds a list of Matchers. 100// It first matches Matcher (something from above), then Pattern (another Cons or Nil). 101// 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. 105// 106// These Cons build lists analogously to Lisp's "cons". See Pattern# for the "list" equivalent. 107template <typename Matcher, typename Pattern> 108class Cons { 109public: 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); 115 } 116 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); 122 if (*end != 0) { 123 return true; 124 } 125 } 126 return false; 127 } 128 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(); } 135 136private: 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)) { 142 return i+1; 143 } 144 } 145 return 0; 146 } 147 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)) { 153 return i; 154 } 155 i++; 156 } 157 return 0; 158 } 159 160 Matcher fHead; 161 Pattern fTail; 162 163 // All Cons are friends with each other. This lets first, second, and third work. 164 template <typename, typename> friend class Cons; 165}; 166 167// Nil is the end of every pattern Cons chain. 168struct Nil { 169 // Bottoms out recursion down the fTail chain. Just return whatever i the front decided on. 170 unsigned match(SkRecord*, unsigned i) { return i; } 171}; 172 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. 176template <typename A> 177struct Pattern1 : Cons<A, Nil> {}; 178 179template <typename A, typename B> 180struct Pattern2 : Cons<A, Pattern1<B> > {}; 181 182template <typename A, typename B, typename C> 183struct Pattern3 : Cons<A, Pattern2<B, C> > {}; 184 185} // namespace SkRecords 186 187#endif//SkRecordPattern_DEFINED 188