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: Is()15 Is() : fPtr(NULL) {} 16 17 typedef T type; get()18 type* get() { return fPtr; } 19 operator()20 bool operator()(T* ptr) { 21 fPtr = ptr; 22 return true; 23 } 24 25 template <typename U> operator()26 bool operator()(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: IsDraw()39 IsDraw() : fPaint(NULL) {} 40 41 typedef SkPaint type; get()42 type* get() { return fPaint; } 43 44 template <typename T> SK_WHEN(HasMember_paint<T>,bool)45 SK_WHEN(HasMember_paint<T>, bool) operator()(T* draw) { 46 fPaint = AsPtr(draw->paint); 47 return true; 48 } 49 50 template <typename T> operator()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. operator()57 bool operator()(SaveLayer*) { 58 fPaint = NULL; 59 return false; 60 } 61 62 private: 63 // Abstracts away whether the paint is always part of the command or optional. AsPtr(SkRecords::Optional<T> & x)64 template <typename T> static T* AsPtr(SkRecords::Optional<T>& x) { return x; } AsPtr(T & 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. 71 template <typename Matcher> 72 struct Not { 73 template <typename T> operatorNot74 bool operator()(T* ptr) { return !Matcher()(ptr); } 75 }; 76 77 // Matches if either of A or B does. Stores nothing. 78 template <typename A, typename B> 79 struct Or { 80 template <typename T> operatorOr81 bool operator()(T* ptr) { return A()(ptr) || B()(ptr); } 82 }; 83 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> > {}; 87 88 // Star is a special matcher that greedily matches Matcher 0 or more times. Stores nothing. 89 template <typename Matcher> 90 struct Star { 91 template <typename T> operatorStar92 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. 103 template <typename Matcher, typename Pattern> 104 class Cons { 105 public: 106 // If this pattern matches the SkRecord starting at i, 107 // return the index just past the end of the pattern, otherwise return 0. match(SkRecord * record,unsigned i)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). search(SkRecord * record,unsigned * begin,unsigned * 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. first()128 template <typename T> T* first() { return fHead.get(); } second()129 template <typename T> T* second() { return fTail.fHead.get(); } third()130 template <typename T> T* third() { return fTail.fTail.fHead.get(); } 131 132 private: 133 // If head isn't a Star, try to match at i once. 134 template <typename T> matchHead(T *,SkRecord * record,unsigned i)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> matchHead(Star<T> *,SkRecord * record,unsigned i)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. 164 struct Nil { 165 // Bottoms out recursion down the fTail chain. Just return whatever i the front decided on. matchNil166 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. 172 template <typename A> 173 struct Pattern1 : Cons<A, Nil> {}; 174 175 template <typename A, typename B> 176 struct Pattern2 : Cons<A, Pattern1<B> > {}; 177 178 template <typename A, typename B, typename C> 179 struct Pattern3 : Cons<A, Pattern2<B, C> > {}; 180 181 } // namespace SkRecords 182 183 #endif//SkRecordPattern_DEFINED 184