1/**************************************************************************/
2/* variant_setget.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef VARIANT_SETGET_H
32#define VARIANT_SETGET_H
33
34#include "variant.h"
35
36#include "core/core_string_names.h"
37#include "core/debugger/engine_debugger.h"
38#include "core/object/class_db.h"
39#include "core/templates/local_vector.h"
40#include "core/variant/variant_internal.h"
41
42/**** NAMED SETTERS AND GETTERS ****/
43
44#define SETGET_STRUCT(m_base_type, m_member_type, m_member) \
45 struct VariantSetGet_##m_base_type##_##m_member { \
46 static void get(const Variant *base, Variant *member) { \
47 VariantTypeAdjust<m_member_type>::adjust(member); \
48 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
49 } \
50 static inline void validated_get(const Variant *base, Variant *member) { \
51 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
52 } \
53 static void ptr_get(const void *base, void *member) { \
54 PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_member, member); \
55 } \
56 static void set(Variant *base, const Variant *value, bool &valid) { \
57 if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
58 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
59 valid = true; \
60 } else { \
61 valid = false; \
62 } \
63 } \
64 static inline void validated_set(Variant *base, const Variant *value) { \
65 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
66 } \
67 static void ptr_set(void *base, const void *member) { \
68 m_base_type b = PtrToArg<m_base_type>::convert(base); \
69 b.m_member = PtrToArg<m_member_type>::convert(member); \
70 PtrToArg<m_base_type>::encode(b, base); \
71 } \
72 static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
73 };
74
75#define SETGET_NUMBER_STRUCT(m_base_type, m_member_type, m_member) \
76 struct VariantSetGet_##m_base_type##_##m_member { \
77 static void get(const Variant *base, Variant *member) { \
78 VariantTypeAdjust<m_member_type>::adjust(member); \
79 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
80 } \
81 static inline void validated_get(const Variant *base, Variant *member) { \
82 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member; \
83 } \
84 static void ptr_get(const void *base, void *member) { \
85 PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_member, member); \
86 } \
87 static void set(Variant *base, const Variant *value, bool &valid) { \
88 if (value->get_type() == Variant::FLOAT) { \
89 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<double>::get_ptr(value); \
90 valid = true; \
91 } else if (value->get_type() == Variant::INT) { \
92 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<int64_t>::get_ptr(value); \
93 valid = true; \
94 } else { \
95 valid = false; \
96 } \
97 } \
98 static inline void validated_set(Variant *base, const Variant *value) { \
99 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_member = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
100 } \
101 static void ptr_set(void *base, const void *member) { \
102 m_base_type b = PtrToArg<m_base_type>::convert(base); \
103 b.m_member = PtrToArg<m_member_type>::convert(member); \
104 PtrToArg<m_base_type>::encode(b, base); \
105 } \
106 static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
107 };
108
109#define SETGET_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \
110 struct VariantSetGet_##m_base_type##_##m_member { \
111 static void get(const Variant *base, Variant *member) { \
112 VariantTypeAdjust<m_member_type>::adjust(member); \
113 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
114 } \
115 static inline void validated_get(const Variant *base, Variant *member) { \
116 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
117 } \
118 static void ptr_get(const void *base, void *member) { \
119 PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_custom, member); \
120 } \
121 static void set(Variant *base, const Variant *value, bool &valid) { \
122 if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
123 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
124 valid = true; \
125 } else { \
126 valid = false; \
127 } \
128 } \
129 static inline void validated_set(Variant *base, const Variant *value) { \
130 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
131 } \
132 static void ptr_set(void *base, const void *member) { \
133 m_base_type b = PtrToArg<m_base_type>::convert(base); \
134 b.m_custom = PtrToArg<m_member_type>::convert(member); \
135 PtrToArg<m_base_type>::encode(b, base); \
136 } \
137 static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
138 };
139
140#define SETGET_NUMBER_STRUCT_CUSTOM(m_base_type, m_member_type, m_member, m_custom) \
141 struct VariantSetGet_##m_base_type##_##m_member { \
142 static void get(const Variant *base, Variant *member) { \
143 VariantTypeAdjust<m_member_type>::adjust(member); \
144 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
145 } \
146 static inline void validated_get(const Variant *base, Variant *member) { \
147 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom; \
148 } \
149 static void ptr_get(const void *base, void *member) { \
150 PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_custom, member); \
151 } \
152 static void set(Variant *base, const Variant *value, bool &valid) { \
153 if (value->get_type() == Variant::FLOAT) { \
154 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<double>::get_ptr(value); \
155 valid = true; \
156 } else if (value->get_type() == Variant::INT) { \
157 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<int64_t>::get_ptr(value); \
158 valid = true; \
159 } else { \
160 valid = false; \
161 } \
162 } \
163 static inline void validated_set(Variant *base, const Variant *value) { \
164 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_custom = *VariantGetInternalPtr<m_member_type>::get_ptr(value); \
165 } \
166 static void ptr_set(void *base, const void *member) { \
167 m_base_type b = PtrToArg<m_base_type>::convert(base); \
168 b.m_custom = PtrToArg<m_member_type>::convert(member); \
169 PtrToArg<m_base_type>::encode(b, base); \
170 } \
171 static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
172 };
173
174#define SETGET_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \
175 struct VariantSetGet_##m_base_type##_##m_member { \
176 static void get(const Variant *base, Variant *member) { \
177 VariantTypeAdjust<m_member_type>::adjust(member); \
178 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
179 } \
180 static inline void validated_get(const Variant *base, Variant *member) { \
181 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
182 } \
183 static void ptr_get(const void *base, void *member) { \
184 PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(), member); \
185 } \
186 static void set(Variant *base, const Variant *value, bool &valid) { \
187 if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
188 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
189 valid = true; \
190 } else { \
191 valid = false; \
192 } \
193 } \
194 static inline void validated_set(Variant *base, const Variant *value) { \
195 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
196 } \
197 static void ptr_set(void *base, const void *member) { \
198 m_base_type b = PtrToArg<m_base_type>::convert(base); \
199 b.m_setter(PtrToArg<m_member_type>::convert(member)); \
200 PtrToArg<m_base_type>::encode(b, base); \
201 } \
202 static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
203 };
204
205#define SETGET_NUMBER_STRUCT_FUNC(m_base_type, m_member_type, m_member, m_setter, m_getter) \
206 struct VariantSetGet_##m_base_type##_##m_member { \
207 static void get(const Variant *base, Variant *member) { \
208 VariantTypeAdjust<m_member_type>::adjust(member); \
209 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
210 } \
211 static inline void validated_get(const Variant *base, Variant *member) { \
212 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(); \
213 } \
214 static void ptr_get(const void *base, void *member) { \
215 PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(), member); \
216 } \
217 static void set(Variant *base, const Variant *value, bool &valid) { \
218 if (value->get_type() == Variant::FLOAT) { \
219 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<double>::get_ptr(value)); \
220 valid = true; \
221 } else if (value->get_type() == Variant::INT) { \
222 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<int64_t>::get_ptr(value)); \
223 valid = true; \
224 } else { \
225 valid = false; \
226 } \
227 } \
228 static inline void validated_set(Variant *base, const Variant *value) { \
229 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(*VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
230 } \
231 static void ptr_set(void *base, const void *member) { \
232 m_base_type b = PtrToArg<m_base_type>::convert(base); \
233 b.m_setter(PtrToArg<m_member_type>::convert(member)); \
234 PtrToArg<m_base_type>::encode(b, base); \
235 } \
236 static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
237 };
238
239#define SETGET_STRUCT_FUNC_INDEX(m_base_type, m_member_type, m_member, m_setter, m_getter, m_index) \
240 struct VariantSetGet_##m_base_type##_##m_member { \
241 static void get(const Variant *base, Variant *member) { \
242 VariantTypeAdjust<m_member_type>::adjust(member); \
243 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(m_index); \
244 } \
245 static inline void validated_get(const Variant *base, Variant *member) { \
246 *VariantGetInternalPtr<m_member_type>::get_ptr(member) = VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_getter(m_index); \
247 } \
248 static void ptr_get(const void *base, void *member) { \
249 PtrToArg<m_member_type>::encode(PtrToArg<m_base_type>::convert(base).m_getter(m_index), member); \
250 } \
251 static void set(Variant *base, const Variant *value, bool &valid) { \
252 if (value->get_type() == GetTypeInfo<m_member_type>::VARIANT_TYPE) { \
253 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(m_index, *VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
254 valid = true; \
255 } else { \
256 valid = false; \
257 } \
258 } \
259 static inline void validated_set(Variant *base, const Variant *value) { \
260 VariantGetInternalPtr<m_base_type>::get_ptr(base)->m_setter(m_index, *VariantGetInternalPtr<m_member_type>::get_ptr(value)); \
261 } \
262 static void ptr_set(void *base, const void *member) { \
263 m_base_type b = PtrToArg<m_base_type>::convert(base); \
264 b.m_setter(m_index, PtrToArg<m_member_type>::convert(member)); \
265 PtrToArg<m_base_type>::encode(b, base); \
266 } \
267 static Variant::Type get_type() { return GetTypeInfo<m_member_type>::VARIANT_TYPE; } \
268 };
269
270SETGET_NUMBER_STRUCT(Vector2, double, x)
271SETGET_NUMBER_STRUCT(Vector2, double, y)
272
273SETGET_NUMBER_STRUCT(Vector2i, int64_t, x)
274SETGET_NUMBER_STRUCT(Vector2i, int64_t, y)
275
276SETGET_NUMBER_STRUCT(Vector3, double, x)
277SETGET_NUMBER_STRUCT(Vector3, double, y)
278SETGET_NUMBER_STRUCT(Vector3, double, z)
279
280SETGET_NUMBER_STRUCT(Vector3i, int64_t, x)
281SETGET_NUMBER_STRUCT(Vector3i, int64_t, y)
282SETGET_NUMBER_STRUCT(Vector3i, int64_t, z)
283
284SETGET_NUMBER_STRUCT(Vector4, double, x)
285SETGET_NUMBER_STRUCT(Vector4, double, y)
286SETGET_NUMBER_STRUCT(Vector4, double, z)
287SETGET_NUMBER_STRUCT(Vector4, double, w)
288
289SETGET_NUMBER_STRUCT(Vector4i, int64_t, x)
290SETGET_NUMBER_STRUCT(Vector4i, int64_t, y)
291SETGET_NUMBER_STRUCT(Vector4i, int64_t, z)
292SETGET_NUMBER_STRUCT(Vector4i, int64_t, w)
293
294SETGET_STRUCT(Rect2, Vector2, position)
295SETGET_STRUCT(Rect2, Vector2, size)
296SETGET_STRUCT_FUNC(Rect2, Vector2, end, set_end, get_end)
297
298SETGET_STRUCT(Rect2i, Vector2i, position)
299SETGET_STRUCT(Rect2i, Vector2i, size)
300SETGET_STRUCT_FUNC(Rect2i, Vector2i, end, set_end, get_end)
301
302SETGET_STRUCT(AABB, Vector3, position)
303SETGET_STRUCT(AABB, Vector3, size)
304SETGET_STRUCT_FUNC(AABB, Vector3, end, set_end, get_end)
305
306SETGET_STRUCT_CUSTOM(Transform2D, Vector2, x, columns[0])
307SETGET_STRUCT_CUSTOM(Transform2D, Vector2, y, columns[1])
308SETGET_STRUCT_CUSTOM(Transform2D, Vector2, origin, columns[2])
309
310SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, x, normal.x)
311SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, y, normal.y)
312SETGET_NUMBER_STRUCT_CUSTOM(Plane, double, z, normal.z)
313SETGET_STRUCT(Plane, Vector3, normal)
314SETGET_NUMBER_STRUCT(Plane, double, d)
315
316SETGET_NUMBER_STRUCT(Quaternion, double, x)
317SETGET_NUMBER_STRUCT(Quaternion, double, y)
318SETGET_NUMBER_STRUCT(Quaternion, double, z)
319SETGET_NUMBER_STRUCT(Quaternion, double, w)
320
321SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, x, set_column, get_column, 0)
322SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, y, set_column, get_column, 1)
323SETGET_STRUCT_FUNC_INDEX(Basis, Vector3, z, set_column, get_column, 2)
324
325SETGET_STRUCT(Transform3D, Basis, basis)
326SETGET_STRUCT(Transform3D, Vector3, origin)
327
328SETGET_STRUCT_CUSTOM(Projection, Vector4, x, columns[0])
329SETGET_STRUCT_CUSTOM(Projection, Vector4, y, columns[1])
330SETGET_STRUCT_CUSTOM(Projection, Vector4, z, columns[2])
331SETGET_STRUCT_CUSTOM(Projection, Vector4, w, columns[3])
332
333SETGET_NUMBER_STRUCT(Color, double, r)
334SETGET_NUMBER_STRUCT(Color, double, g)
335SETGET_NUMBER_STRUCT(Color, double, b)
336SETGET_NUMBER_STRUCT(Color, double, a)
337
338SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, r8, set_r8, get_r8)
339SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, g8, set_g8, get_g8)
340SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, b8, set_b8, get_b8)
341SETGET_NUMBER_STRUCT_FUNC(Color, int64_t, a8, set_a8, get_a8)
342
343SETGET_NUMBER_STRUCT_FUNC(Color, double, h, set_h, get_h)
344SETGET_NUMBER_STRUCT_FUNC(Color, double, s, set_s, get_s)
345SETGET_NUMBER_STRUCT_FUNC(Color, double, v, set_v, get_v)
346
347SETGET_NUMBER_STRUCT_FUNC(Color, double, ok_hsl_h, set_ok_hsl_h, get_ok_hsl_h)
348SETGET_NUMBER_STRUCT_FUNC(Color, double, ok_hsl_s, set_ok_hsl_s, get_ok_hsl_s)
349SETGET_NUMBER_STRUCT_FUNC(Color, double, ok_hsl_l, set_ok_hsl_l, get_ok_hsl_l)
350
351#endif // VARIANT_SETGET_H
352