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