1 
2 /*
3  * Copyright 2006 The Android Open Source Project
4  *
5  * Use of this source code is governed by a BSD-style license that can be
6  * found in the LICENSE file.
7  */
8 
9 
10 #ifndef SkMemberInfo_DEFINED
11 #define SkMemberInfo_DEFINED
12 
13 #if defined SK_BUILD_CONDENSED
14     #define SK_USE_CONDENSED_INFO 0
15 #endif
16 
17 #include "SkDisplayType.h"
18 #include "SkScript.h"
19 #include "SkString.h"
20 #include "SkIntArray.h"
21 #include <utility>
22 
23 class SkAnimateMaker;
24 class SkDisplayable;
25 class SkScriptEngine;
26 
27 // temporary hacks until name change is more complete
28 #define SkFloat SkScalar
29 #define SkInt SkS32
30 
31 struct SkMemberInfo {
32     //!!! alternative:
33     // if fCount == 0, record is member property
34     // then fType can be type, so caller doesn't have to check
35 #if SK_USE_CONDENSED_INFO == 0
36     const char* fName;  // may be nullptr for anonymous functions
37     size_t fOffset; // if negative, is index into member pointer table (for properties and functions)
38     SkDisplayTypes fType;
39     int fCount;         // for properties, actual type (count is always assumed to be 1)
40 #else
41     unsigned char fName;
42     signed char fOffset;
43     unsigned char fType;
44     signed char fCount;
45 #endif
arrayTypeSkMemberInfo46     SkDisplayTypes arrayType() const {
47         SkASSERT(fType == SkType_Array);
48         return (SkDisplayTypes) fCount;  // hack, but worth it?
49     }
functionIndexSkMemberInfo50     int functionIndex() const {
51         SkASSERT(fType == SkType_MemberFunction);
52         return (signed) fOffset > 0 ? -1 + (int) fOffset : -1 - (int) fOffset;
53     }
54     bool getArrayValue(const SkDisplayable* displayable, int index, SkOperand* value) const;
getCountSkMemberInfo55     int getCount() const {
56         return fType == SkType_MemberProperty || fType == SkType_Array ||
57             fType == SkType_MemberFunction ? 1 : fCount;
58     }
59     const SkMemberInfo* getInherited() const;
60     size_t getSize(const SkDisplayable* ) const;
61     void getString(const SkDisplayable* , SkString** string) const;
getTypeSkMemberInfo62     SkDisplayTypes getType() const {
63         return fType == SkType_MemberProperty || fType == SkType_Array ||
64             fType == SkType_MemberFunction ? (SkDisplayTypes) fCount : (SkDisplayTypes) fType;
65     }
66     void getValue(const SkDisplayable* , SkOperand values[], int count) const;
67     bool isEnum() const;
68     const char* mapEnums(const char* match, int* value) const;
memberDataSkMemberInfo69     void* memberData(const SkDisplayable* displayable) const {
70         SkASSERT(fType != SkType_MemberProperty &&  fType != SkType_MemberFunction);
71         return (void*) ((const char*) displayable + fOffset);
72     }
propertyIndexSkMemberInfo73     int propertyIndex() const {
74         SkASSERT(fType == SkType_MemberProperty);
75         return (signed) fOffset > 0 ? -1 + (int) fOffset : -1 - (int) fOffset;
76     }
propertyTypeSkMemberInfo77     SkDisplayTypes propertyType() const {
78         SkASSERT(fType == SkType_MemberProperty || fType == SkType_Array);
79         return (SkDisplayTypes) fCount;  // hack, but worth it?
80     }
setMemberDataSkMemberInfo81     void setMemberData(SkDisplayable* displayable, const void* child, size_t size) const {
82         SkASSERT(fType != SkType_MemberProperty &&  fType != SkType_MemberFunction);
83         memcpy((char*) displayable + fOffset, child, size);
84     }
85     void setString(SkDisplayable* , SkString* ) const;
86     void setValue(SkDisplayable* , const SkOperand values[], int count) const;
87     bool setValue(SkAnimateMaker& , SkTDOperandArray* storage,
88         int storageOffset, int maxStorage, SkDisplayable* ,
89         SkDisplayTypes outType, const char value[], size_t len) const;
90     bool setValue(SkAnimateMaker& , SkTDOperandArray* storage,
91         int storageOffset, int maxStorage, SkDisplayable* ,
92         SkDisplayTypes outType, SkString& str) const;
93 //  void setValue(SkDisplayable* , const char value[], const char name[]) const;
94     bool writeValue(SkDisplayable* displayable, SkTDOperandArray* arrayStorage,
95         int storageOffset, int maxStorage, void* untypedStorage, SkDisplayTypes outType,
96         SkScriptValue& scriptValue) const;
97 #if SK_USE_CONDENSED_INFO == 0
98     static const SkMemberInfo* Find(const SkMemberInfo [], int count, int* index);
99     static const SkMemberInfo* Find(const SkMemberInfo [], int count, const char** name);
100 #else
101     static const SkMemberInfo* Find(SkDisplayTypes type, int* index);
102     static const SkMemberInfo* Find(SkDisplayTypes type, const char** name);
103 #endif
104     static size_t GetSize(SkDisplayTypes type); // size of simple types only
105 //  static bool SetValue(void* value, const char* name, SkDisplayTypes , int count);
106 };
107 
108 #ifndef SK_OFFSETOF
109     // This is offsetof for types which are not standard layout.
110     #define SK_OFFSETOF(type, field) (size_t)((char*)&(((type*)1024)->field) - (char*)1024)
111 #endif
112 
113 #define SK_MEMBER(_member, _type) \
114     { #_member, SK_OFFSETOF(BASE_CLASS, _member), SkType_##_type, \
115     sizeof(std::declval<BASE_CLASS>()._member) / sizeof(SkScalar) }
116 
117 #define SK_MEMBER_ALIAS(_member, _alias, _type) \
118     { #_member, SK_OFFSETOF(BASE_CLASS, _alias), SkType_##_type, \
119     sizeof(std::declval<BASE_CLASS>()._alias) / sizeof(SkScalar) }
120 
121 #define SK_MEMBER_ARRAY(_member, _type) \
122     { #_member, SK_OFFSETOF(BASE_CLASS, _member), SkType_Array, \
123     (int) SkType_##_type }
124 
125 #define SK_MEMBER_INHERITED \
126     { (const char*) INHERITED::fInfo, 0, SkType_BaseClassInfo, INHERITED::fInfoCount }
127 
128 // #define SK_MEMBER_KEY_TYPE(_member, _type)
129 //  {#_member, (size_t) -1, SkType_##_type, 0}
130 
131 #define SK_FUNCTION(_member) \
132     k_##_member##Function
133 
134 #define SK_PROPERTY(_member) \
135     k_##_member##Property
136 
137 #define SK_MEMBER_DYNAMIC_FUNCTION(_member, _type) \
138     {#_member, (size_t) (+1 + SK_FUNCTION(_member)), SkType_MemberFunction, \
139     (int) SkType_##_type }
140 
141 #define SK_MEMBER_DYNAMIC_PROPERTY(_member, _type) \
142     {#_member, (size_t) (1 + SK_PROPERTY(_member)), SkType_MemberProperty, \
143     (int) SkType_##_type }
144 
145 #define SK_MEMBER_FUNCTION(_member, _type) \
146     {#_member, (size_t) (-1 - SK_FUNCTION(_member)), SkType_MemberFunction, \
147     (int) SkType_##_type }
148 
149 #define SK_MEMBER_PROPERTY(_member, _type) \
150     {#_member, (size_t) (-1 - SK_PROPERTY(_member)), SkType_MemberProperty, \
151     (int) SkType_##_type }
152 
153 #if SK_USE_CONDENSED_INFO == 0
154 
155 #define DECLARE_PRIVATE_MEMBER_INFO(_type) \
156 public: \
157     static const SkMemberInfo fInfo[]; \
158     static const int fInfoCount; \
159     const SkMemberInfo* getMember(int index) override; \
160     const SkMemberInfo* getMember(const char name[]) override; \
161     typedef Sk##_type BASE_CLASS
162 
163 #define DECLARE_MEMBER_INFO(_type) \
164 public: \
165     static const SkMemberInfo fInfo[]; \
166     static const int fInfoCount; \
167     const SkMemberInfo* getMember(int index) override; \
168     const SkMemberInfo* getMember(const char name[]) override; \
169     SkDisplayTypes getType() const override { return SkType_##_type; } \
170     typedef Sk##_type BASE_CLASS
171 
172 #define DECLARE_DRAW_MEMBER_INFO(_type) \
173 public: \
174     static const SkMemberInfo fInfo[]; \
175     static const int fInfoCount; \
176     const SkMemberInfo* getMember(int index) override; \
177     const SkMemberInfo* getMember(const char name[]) override; \
178     SkDisplayTypes getType() const override { return SkType_##_type; } \
179     typedef SkDraw##_type BASE_CLASS
180 
181 #define DECLARE_DISPLAY_MEMBER_INFO(_type) \
182 public: \
183     static const SkMemberInfo fInfo[]; \
184     static const int fInfoCount; \
185     const SkMemberInfo* getMember(int index) override; \
186     const SkMemberInfo* getMember(const char name[]) override; \
187     SkDisplayTypes getType() const override { return SkType_##_type; } \
188     typedef SkDisplay##_type BASE_CLASS
189 
190 #define DECLARE_EMPTY_MEMBER_INFO(_type) \
191 public: \
192     SkDisplayTypes getType() const override { return SkType_##_type; }
193 
194 #define DECLARE_EXTRAS_MEMBER_INFO(_type) \
195 public: \
196     static const SkMemberInfo fInfo[]; \
197     static const int fInfoCount; \
198     const SkMemberInfo* getMember(int index) override; \
199     const SkMemberInfo* getMember(const char name[]) override; \
200     SkDisplayTypes fType; \
201     SkDisplayTypes getType() const override { return fType; } \
202     typedef _type BASE_CLASS
203 
204 #define DECLARE_NO_VIRTUALS_MEMBER_INFO(_type) \
205 public: \
206     static const SkMemberInfo fInfo[]; \
207     static const int fInfoCount; \
208     typedef Sk##_type BASE_CLASS
209 
210 #define DEFINE_GET_MEMBER(_class) \
211     const SkMemberInfo* _class::getMember(int index) { \
212         const SkMemberInfo* result = SkMemberInfo::Find(fInfo, SK_ARRAY_COUNT(fInfo), &index); \
213         return result; \
214     } \
215     const SkMemberInfo* _class::getMember(const char name[]) { \
216         const SkMemberInfo* result = SkMemberInfo::Find(fInfo, SK_ARRAY_COUNT(fInfo), &name); \
217         return result; \
218     } \
219     const int _class::fInfoCount = SK_ARRAY_COUNT(fInfo)
220 
221 #define DEFINE_NO_VIRTUALS_GET_MEMBER(_class) \
222     const int _class::fInfoCount = SK_ARRAY_COUNT(fInfo)
223 
224 #else
225 
226 #define DECLARE_PRIVATE_MEMBER_INFO(_type) \
227 public: \
228     typedef Sk##_type BASE_CLASS
229 
230 #define DECLARE_MEMBER_INFO(_type) \
231 public: \
232     virtual const SkMemberInfo* getMember(int index) { \
233         return SkDisplayType::GetMember(nullptr, SkType_##_type, &index); } \
234     virtual const SkMemberInfo* getMember(const char name[]) { \
235         return SkDisplayType::GetMember(nullptr, SkType_##_type, &name); } \
236     virtual SkDisplayTypes getType() const { return SkType_##_type; } \
237     typedef Sk##_type BASE_CLASS
238 
239 #define DECLARE_DRAW_MEMBER_INFO(_type) \
240 public: \
241     virtual const SkMemberInfo* getMember(int index) { \
242         return SkDisplayType::GetMember(nullptr, SkType_##_type, &index); } \
243     virtual const SkMemberInfo* getMember(const char name[]) { \
244         return SkDisplayType::GetMember(nullptr, SkType_##_type, &name); } \
245     virtual SkDisplayTypes getType() const { return SkType_##_type; } \
246     typedef SkDraw##_type BASE_CLASS
247 
248 #define DECLARE_DISPLAY_MEMBER_INFO(_type) \
249 public: \
250     virtual const SkMemberInfo* getMember(int index) { \
251         return SkDisplayType::GetMember(nullptr, SkType_##_type, &index); } \
252     virtual const SkMemberInfo* getMember(const char name[]) { \
253         return SkDisplayType::GetMember(nullptr, SkType_##_type, &name); } \
254     virtual SkDisplayTypes getType() const { return SkType_##_type; } \
255     typedef SkDisplay##_type BASE_CLASS
256 
257 #define DECLARE_EXTRAS_MEMBER_INFO(_type) \
258 public: \
259     virtual const SkMemberInfo* getMember(int index) { \
260         return SkDisplayType::GetMember(nullptr, SkType_##_type, &index); } \
261     virtual const SkMemberInfo* getMember(const char name[]) { \
262         return SkDisplayType::GetMember(nullptr, fType, &name); } \
263     SkDisplayTypes fType; \
264     virtual SkDisplayTypes getType() const { return fType; } \
265     typedef _type BASE_CLASS
266 
267 #define DECLARE_NO_VIRTUALS_MEMBER_INFO(_type) \
268 public: \
269     typedef Sk##_type BASE_CLASS
270 
271 #define DEFINE_GET_MEMBER(_class)
272 #define DEFINE_NO_VIRTUALS_GET_MEMBER(_class)
273 
274 #endif
275 
276 #endif // SkMemberInfo_DEFINED
277