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