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: 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 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: 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 62 private: 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. 71 template <typename Matcher> 72 struct 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. 78 template <typename A, typename B> 79 struct 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. 85 template <typename A, typename B, typename C> 86 struct Or3 : Or<A, Or<B, C> > {}; 87 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> > > {}; 91 92 // Star is a special matcher that greedily matches Matcher 0 or more times. Stores nothing. 93 template <typename Matcher> 94 struct 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. 107 template <typename Matcher, typename Pattern> 108 class Cons { 109 public: 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 template <typename T> T* fourth() { return fTail.fTail.fTail.fHead.get(); } 136 137 private: 138 // If head isn't a Star, try to match at i once. 139 template <typename T> 140 unsigned matchHead(T*, SkRecord* record, unsigned i) { 141 if (i < record->count()) { 142 if (record->mutate<bool>(i, fHead)) { 143 return i+1; 144 } 145 } 146 return 0; 147 } 148 149 // If head is a Star, walk i until it doesn't match. 150 template <typename T> 151 unsigned matchHead(Star<T>*, SkRecord* record, unsigned i) { 152 while (i < record->count()) { 153 if (!record->mutate<bool>(i, fHead)) { 154 return i; 155 } 156 i++; 157 } 158 return 0; 159 } 160 161 Matcher fHead; 162 Pattern fTail; 163 164 // All Cons are friends with each other. This lets first, second, and third work. 165 template <typename, typename> friend class Cons; 166 }; 167 168 // Nil is the end of every pattern Cons chain. 169 struct Nil { 170 // Bottoms out recursion down the fTail chain. Just return whatever i the front decided on. 171 unsigned match(SkRecord*, unsigned i) { return i; } 172 }; 173 174 // These Pattern# types are syntax sugar over Cons and Nil, just to help eliminate some of the 175 // template noise. Use these if you can. Feel free to add more for longer patterns. 176 // All types A, B, C, ... are Matchers. 177 template <typename A> 178 struct Pattern1 : Cons<A, Nil> {}; 179 180 template <typename A, typename B> 181 struct Pattern2 : Cons<A, Pattern1<B> > {}; 182 183 template <typename A, typename B, typename C> 184 struct Pattern3 : Cons<A, Pattern2<B, C> > {}; 185 186 template <typename A, typename B, typename C, typename D> 187 struct Pattern4 : Cons<A, Pattern3<B, C, D> > {}; 188 189 template <typename A, typename B, typename C, typename D, typename E> 190 struct Pattern5 : Cons<A, Pattern4<B, C, D, E> > {}; 191 192 template <typename A, typename B, typename C, typename D, typename E, typename F> 193 struct Pattern6 : Cons<A, Pattern5<B, C, D, E, F> > {}; 194 195 template <typename A, typename B, typename C, typename D, typename E, typename F, typename G> 196 struct Pattern7 : Cons<A, Pattern6<B, C, D, E, F, G> > {}; 197 198 } // namespace SkRecords 199 200 #endif//SkRecordPattern_DEFINED 201