Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 SkReflected_DEFINED |
| 9 | #define SkReflected_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkRefCnt.h" |
| 13 | #include "include/private/SkTArray.h" |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 14 | |
Mike Klein | 334a642 | 2019-08-27 08:12:15 -0500 | [diff] [blame] | 15 | #include <functional> // std::function |
Brian Osman | e5d532e | 2019-02-26 14:58:40 -0500 | [diff] [blame] | 16 | #include <string.h> |
| 17 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 18 | class SkFieldVisitor; |
Brian Osman | d0c1bd4 | 2019-03-06 16:56:58 -0500 | [diff] [blame] | 19 | struct SkPoint; |
| 20 | class SkString; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 21 | |
Brian Osman | 23b96c0 | 2019-02-19 13:04:40 -0500 | [diff] [blame] | 22 | /** |
| 23 | * Classes and macros for a lightweight reflection system. |
| 24 | * |
| 25 | * Classes that derive from SkReflected have several features: |
| 26 | * - Access to an SkReflected::Type instance, via static GetType() or virtual getType() |
| 27 | * The Type instance can be used to create additional instances (fFactory), get the name |
| 28 | * of the type, and answer queries of the form "is X derived from Y". |
| 29 | * - Given a string containing a type name, SkReflected can create an instance of that type. |
Brian Osman | b77d502 | 2019-03-06 11:08:48 -0500 | [diff] [blame] | 30 | * - SkReflected::VisitTypes can be used to enumerate all Types. |
Brian Osman | 23b96c0 | 2019-02-19 13:04:40 -0500 | [diff] [blame] | 31 | * |
| 32 | * Together, this simplifies the implementation of serialization and other dynamic type factories. |
| 33 | * |
| 34 | * Finally, all SkReflected-derived types must implement visitFields, which provides field-level |
| 35 | * reflection, in conjunction with SkFieldVisitor. See SkFieldVisitor, below. |
| 36 | * |
| 37 | * To create a new reflected class: |
| 38 | * - Derive the class (directly or indirectly) from SkReflected. |
| 39 | * - Ensure that the class can be default constructed. |
| 40 | * - In the public area of the class declaration, add REFLECTED(<ClassName>, <BaseClassName>). |
| 41 | * If the class is abstract, use REFLECTED_ABSTRACT(<ClassName>, <BaseClassName>) instead. |
| 42 | * - Add a one-time call to REGISTER_REFLECTED(<ClassName>) at initialization time. |
| 43 | * - Implement visitFields(), as described below. |
| 44 | */ |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 45 | class SkReflected : public SkRefCnt { |
| 46 | public: |
| 47 | typedef sk_sp<SkReflected>(*Factory)(); |
| 48 | struct Type { |
| 49 | const char* fName; |
| 50 | const Type* fBase; |
| 51 | Factory fFactory; |
Brian Osman | 93c47cc | 2019-03-06 15:00:36 -0500 | [diff] [blame] | 52 | bool fRegistered = false; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 53 | |
| 54 | bool isDerivedFrom(const Type* t) const { |
| 55 | const Type* base = fBase; |
| 56 | while (base) { |
| 57 | if (base == t) { |
| 58 | return true; |
| 59 | } |
| 60 | base = base->fBase; |
| 61 | } |
| 62 | return false; |
| 63 | } |
| 64 | }; |
| 65 | |
| 66 | virtual const Type* getType() const = 0; |
| 67 | static const Type* GetType() { |
| 68 | static Type gType{ "SkReflected", nullptr, nullptr }; |
Brian Osman | 93c47cc | 2019-03-06 15:00:36 -0500 | [diff] [blame] | 69 | RegisterOnce(&gType); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 70 | return &gType; |
| 71 | } |
| 72 | |
| 73 | bool isOfType(const Type* t) const { |
| 74 | const Type* thisType = this->getType(); |
| 75 | return thisType == t || thisType->isDerivedFrom(t); |
| 76 | } |
| 77 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 78 | static sk_sp<SkReflected> CreateInstance(const char* name) { |
| 79 | for (const Type* type : gTypes) { |
| 80 | if (0 == strcmp(name, type->fName)) { |
| 81 | return type->fFactory(); |
| 82 | } |
| 83 | } |
| 84 | return nullptr; |
| 85 | } |
| 86 | |
| 87 | virtual void visitFields(SkFieldVisitor*) = 0; |
| 88 | |
Brian Osman | b77d502 | 2019-03-06 11:08:48 -0500 | [diff] [blame] | 89 | static void VisitTypes(std::function<void(const Type*)> visitor); |
| 90 | |
Brian Osman | 93c47cc | 2019-03-06 15:00:36 -0500 | [diff] [blame] | 91 | protected: |
| 92 | static void RegisterOnce(Type* type) { |
| 93 | if (!type->fRegistered) { |
| 94 | gTypes.push_back(type); |
| 95 | type->fRegistered = true; |
| 96 | } |
| 97 | } |
| 98 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 99 | private: |
| 100 | static SkSTArray<16, const Type*, true> gTypes; |
| 101 | }; |
| 102 | |
| 103 | #define REFLECTED(TYPE, BASE) \ |
| 104 | static sk_sp<SkReflected> CreateProc() { \ |
| 105 | return sk_sp<SkReflected>(new TYPE()); \ |
| 106 | } \ |
| 107 | static const Type* GetType() { \ |
| 108 | static Type gType{ #TYPE, BASE::GetType(), CreateProc }; \ |
Brian Osman | 93c47cc | 2019-03-06 15:00:36 -0500 | [diff] [blame] | 109 | RegisterOnce(&gType); \ |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 110 | return &gType; \ |
| 111 | } \ |
| 112 | const Type* getType() const override { return GetType(); } |
| 113 | |
| 114 | #define REFLECTED_ABSTRACT(TYPE, BASE) \ |
| 115 | static const Type* GetType() { \ |
| 116 | static Type gType{ #TYPE, BASE::GetType(), nullptr }; \ |
Brian Osman | 93c47cc | 2019-03-06 15:00:36 -0500 | [diff] [blame] | 117 | RegisterOnce(&gType); \ |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 118 | return &gType; \ |
| 119 | } \ |
| 120 | const Type* getType() const override { return GetType(); } |
| 121 | |
Brian Osman | 93c47cc | 2019-03-06 15:00:36 -0500 | [diff] [blame] | 122 | #define REGISTER_REFLECTED(TYPE) TYPE::GetType() |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 123 | |
| 124 | /////////////////////////////////////////////////////////////////////////////// |
| 125 | |
Brian Osman | 23b96c0 | 2019-02-19 13:04:40 -0500 | [diff] [blame] | 126 | /** |
| 127 | * SkFieldVisitor is an interface that can be implemented by any class to visit all fields of |
| 128 | * SkReflected types, and of types that implement the visitFields() function. |
| 129 | * |
| 130 | * Classes implementing the interface must supply implementations of virtual functions that visit |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 131 | * basic types (float, int, bool, SkString), as well as helper methods for entering the scope of |
| 132 | * an object or array. |
Brian Osman | 23b96c0 | 2019-02-19 13:04:40 -0500 | [diff] [blame] | 133 | * |
| 134 | * All visit functions supply a field name, and a non-constant reference to an actual field. |
| 135 | * This allows visitors to serialize or deserialize collections of objects, or perform edits on |
| 136 | * existing objects. |
| 137 | * |
| 138 | * Classes that implement visitFields (typically derived from SkReflected) should simply call |
| 139 | * visit() for each of their fields, passing a (unique) field name, and the actual field. If your |
| 140 | * class has derived fields, it's best to only visit() the fields that you would serialize, then |
| 141 | * enforce any constraints afterwards. |
| 142 | * |
| 143 | * See SkParticleSerialization.h for example visitors that perform serialization to and from JSON. |
| 144 | */ |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 145 | class SkFieldVisitor { |
| 146 | public: |
| 147 | virtual ~SkFieldVisitor() {} |
| 148 | |
Brian Osman | 23b96c0 | 2019-02-19 13:04:40 -0500 | [diff] [blame] | 149 | // Visit functions for primitive types, to be implemented by derived visitors. |
Brian Osman | 2991cbe | 2019-02-19 10:45:56 -0500 | [diff] [blame] | 150 | virtual void visit(const char*, float&) = 0; |
| 151 | virtual void visit(const char*, int&) = 0; |
| 152 | virtual void visit(const char*, bool&) = 0; |
| 153 | virtual void visit(const char*, SkString&) = 0; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 154 | |
Brian Osman | 23b96c0 | 2019-02-19 13:04:40 -0500 | [diff] [blame] | 155 | // Specialization for SkTArrays. In conjunction with the enterArray/exitArray virtuals, this |
| 156 | // allows visitors to resize an array (for deserialization), and apply a single edit operation |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 157 | // (remove a single element). Each element of the array is visited as normal. |
Brian Osman | 5de7ea4 | 2019-02-14 13:23:51 -0500 | [diff] [blame] | 158 | template <typename T, bool MEM_MOVE> |
| 159 | void visit(const char* name, SkTArray<T, MEM_MOVE>& arr) { |
| 160 | arr.resize_back(this->enterArray(name, arr.count())); |
| 161 | for (int i = 0; i < arr.count(); ++i) { |
| 162 | this->visit(nullptr, arr[i]); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 163 | } |
Brian Osman | 5de7ea4 | 2019-02-14 13:23:51 -0500 | [diff] [blame] | 164 | this->exitArray().apply(arr); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 165 | } |
| 166 | |
Brian Osman | 23b96c0 | 2019-02-19 13:04:40 -0500 | [diff] [blame] | 167 | // Specialization for sk_sp pointers to types derived from SkReflected. Those types are known |
| 168 | // to implement visitFields. This allows the visitor to modify the contents of the object, or |
| 169 | // even replace it with an entirely new object. The virtual function uses SkReflected as a |
| 170 | // common type, but uses SkReflected::Type to communicate the required base-class. In this way, |
| 171 | // the new object can be verified to match the type of the original (templated) pointer. |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 172 | template <typename T> |
| 173 | void visit(const char* name, sk_sp<T>& obj) { |
| 174 | this->enterObject(name); |
| 175 | |
| 176 | sk_sp<SkReflected> newObj = obj; |
| 177 | this->visit(newObj, T::GetType()); |
| 178 | if (newObj != obj) { |
| 179 | if (!newObj || newObj->isOfType(T::GetType())) { |
| 180 | obj.reset(static_cast<T*>(newObj.release())); |
| 181 | } else { |
| 182 | obj.reset(); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | if (obj) { |
| 187 | obj->visitFields(this); |
| 188 | } |
| 189 | this->exitObject(); |
| 190 | } |
| 191 | |
| 192 | protected: |
Brian Osman | 23b96c0 | 2019-02-19 13:04:40 -0500 | [diff] [blame] | 193 | // Helper struct to allow exitArray to specify a single operation performed on the array. |
Brian Osman | 5de7ea4 | 2019-02-14 13:23:51 -0500 | [diff] [blame] | 194 | struct ArrayEdit { |
| 195 | enum class Verb { |
| 196 | kNone, |
| 197 | kRemove, |
Brian Osman | 5de7ea4 | 2019-02-14 13:23:51 -0500 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | Verb fVerb = Verb::kNone; |
| 201 | int fIndex = 0; |
| 202 | |
| 203 | template <typename T, bool MEM_MOVE> |
| 204 | void apply(SkTArray<T, MEM_MOVE>& arr) const { |
| 205 | switch (fVerb) { |
| 206 | case Verb::kNone: |
| 207 | break; |
| 208 | case Verb::kRemove: |
| 209 | for (int i = fIndex; i < arr.count() - 1; ++i) { |
| 210 | arr[i] = arr[i + 1]; |
| 211 | } |
| 212 | arr.pop_back(); |
| 213 | break; |
Brian Osman | 5de7ea4 | 2019-02-14 13:23:51 -0500 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | }; |
| 217 | |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 218 | private: |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 219 | virtual void enterObject(const char* name) = 0; |
| 220 | virtual void exitObject() = 0; |
Brian Osman | 5de7ea4 | 2019-02-14 13:23:51 -0500 | [diff] [blame] | 221 | |
| 222 | virtual int enterArray(const char* name, int oldCount) = 0; |
| 223 | virtual ArrayEdit exitArray() = 0; |
| 224 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 225 | virtual void visit(sk_sp<SkReflected>&, const SkReflected::Type* baseType) = 0; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | #endif // SkReflected_DEFINED |