Debian Codegraph @

core/variant/variant_internal.h

raw symbols SCIP
1 /**************************************************************************/
2 /* variant_internal.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 #pragma once
32
33 #include "type_info.h"
34 #include "variant.h"
35
36 #include "core/templates/simple_type.h"
37
38 // For use when you want to access the internal pointer of a Variant directly.
39 // Use with caution. You need to be sure that the type is correct.
40
41 class RefCounted;
42
43 template <typename T>
44 struct GDExtensionConstPtr;
45
46 template <typename T>
47 struct GDExtensionPtr;
48
49 class VariantInternal {
50 friend class Variant;
51
52 public:
53 // Set type.
54 _FORCE_INLINE_ static void set_type(Variant &v, Variant::Type p_type) {
55 v.type = p_type;
56 }
57
58 _FORCE_INLINE_ static void initialize(Variant *v, Variant::Type p_type) {
59 v->clear();
60 v->type = p_type;
61
62 switch (p_type) {
63 case Variant::STRING:
64 init_string(v);
65 break;
66 case Variant::TRANSFORM2D:
67 init_transform2d(v);
68 break;
69 case Variant::QUATERNION:
70 init_quaternion(v);
71 break;
72 case Variant::AABB:
73 init_aabb(v);
74 break;
75 case Variant::BASIS:
76 init_basis(v);
77 break;
78 case Variant::TRANSFORM3D:
79 init_transform3d(v);
80 break;
81 case Variant::PROJECTION:
82 init_projection(v);
83 break;
84 case Variant::COLOR:
85 init_color(v);
86 break;
87 case Variant::STRING_NAME:
88 init_string_name(v);
89 break;
90 case Variant::NODE_PATH:
91 init_node_path(v);
92 break;
93 case Variant::CALLABLE:
94 init_callable(v);
95 break;
96 case Variant::SIGNAL:
97 init_signal(v);
98 break;
99 case Variant::DICTIONARY:
100 init_dictionary(v);
101 break;
102 case Variant::ARRAY:
103 init_array(v);
104 break;
105 case Variant::PACKED_BYTE_ARRAY:
106 init_byte_array(v);
107 break;
108 case Variant::PACKED_INT32_ARRAY:
109 init_int32_array(v);
110 break;
111 case Variant::PACKED_INT64_ARRAY:
112 init_int64_array(v);
113 break;
114 case Variant::PACKED_FLOAT32_ARRAY:
115 init_float32_array(v);
116 break;
117 case Variant::PACKED_FLOAT64_ARRAY:
118 init_float64_array(v);
119 break;
120 case Variant::PACKED_STRING_ARRAY:
121 init_string_array(v);
122 break;
123 case Variant::PACKED_VECTOR2_ARRAY:
124 init_vector2_array(v);
125 break;
126 case Variant::PACKED_VECTOR3_ARRAY:
127 init_vector3_array(v);
128 break;
129 case Variant::PACKED_COLOR_ARRAY:
130 init_color_array(v);
131 break;
132 case Variant::PACKED_VECTOR4_ARRAY:
133 init_vector4_array(v);
134 break;
135 case Variant::OBJECT:
136 init_object(v);
137 break;
138 default:
139 break;
140 }
141 }
142
143 // Atomic types.
144 _FORCE_INLINE_ static bool *get_bool(Variant *v) { return &v->_data._bool; }
145 _FORCE_INLINE_ static const bool *get_bool(const Variant *v) { return &v->_data._bool; }
146 _FORCE_INLINE_ static int64_t *get_int(Variant *v) { return &v->_data._int; }
147 _FORCE_INLINE_ static const int64_t *get_int(const Variant *v) { return &v->_data._int; }
148 _FORCE_INLINE_ static double *get_float(Variant *v) { return &v->_data._float; }
149 _FORCE_INLINE_ static const double *get_float(const Variant *v) { return &v->_data._float; }
150 _FORCE_INLINE_ static String *get_string(Variant *v) { return reinterpret_cast<String *>(v->_data._mem); }
151 _FORCE_INLINE_ static const String *get_string(const Variant *v) { return reinterpret_cast<const String *>(v->_data._mem); }
152
153 // Math types.
154 _FORCE_INLINE_ static Vector2 *get_vector2(Variant *v) { return reinterpret_cast<Vector2 *>(v->_data._mem); }
155 _FORCE_INLINE_ static const Vector2 *get_vector2(const Variant *v) { return reinterpret_cast<const Vector2 *>(v->_data._mem); }
156 _FORCE_INLINE_ static Vector2i *get_vector2i(Variant *v) { return reinterpret_cast<Vector2i *>(v->_data._mem); }
157 _FORCE_INLINE_ static const Vector2i *get_vector2i(const Variant *v) { return reinterpret_cast<const Vector2i *>(v->_data._mem); }
158 _FORCE_INLINE_ static Rect2 *get_rect2(Variant *v) { return reinterpret_cast<Rect2 *>(v->_data._mem); }
159 _FORCE_INLINE_ static const Rect2 *get_rect2(const Variant *v) { return reinterpret_cast<const Rect2 *>(v->_data._mem); }
160 _FORCE_INLINE_ static Rect2i *get_rect2i(Variant *v) { return reinterpret_cast<Rect2i *>(v->_data._mem); }
161 _FORCE_INLINE_ static const Rect2i *get_rect2i(const Variant *v) { return reinterpret_cast<const Rect2i *>(v->_data._mem); }
162 _FORCE_INLINE_ static Vector3 *get_vector3(Variant *v) { return reinterpret_cast<Vector3 *>(v->_data._mem); }
163 _FORCE_INLINE_ static const Vector3 *get_vector3(const Variant *v) { return reinterpret_cast<const Vector3 *>(v->_data._mem); }
164 _FORCE_INLINE_ static Vector3i *get_vector3i(Variant *v) { return reinterpret_cast<Vector3i *>(v->_data._mem); }
165 _FORCE_INLINE_ static const Vector3i *get_vector3i(const Variant *v) { return reinterpret_cast<const Vector3i *>(v->_data._mem); }
166 _FORCE_INLINE_ static Vector4 *get_vector4(Variant *v) { return reinterpret_cast<Vector4 *>(v->_data._mem); }
167 _FORCE_INLINE_ static const Vector4 *get_vector4(const Variant *v) { return reinterpret_cast<const Vector4 *>(v->_data._mem); }
168 _FORCE_INLINE_ static Vector4i *get_vector4i(Variant *v) { return reinterpret_cast<Vector4i *>(v->_data._mem); }
169 _FORCE_INLINE_ static const Vector4i *get_vector4i(const Variant *v) { return reinterpret_cast<const Vector4i *>(v->_data._mem); }
170 _FORCE_INLINE_ static Transform2D *get_transform2d(Variant *v) { return v->_data._transform2d; }
171 _FORCE_INLINE_ static const Transform2D *get_transform2d(const Variant *v) { return v->_data._transform2d; }
172 _FORCE_INLINE_ static Plane *get_plane(Variant *v) { return reinterpret_cast<Plane *>(v->_data._mem); }
173 _FORCE_INLINE_ static const Plane *get_plane(const Variant *v) { return reinterpret_cast<const Plane *>(v->_data._mem); }
174 _FORCE_INLINE_ static Quaternion *get_quaternion(Variant *v) { return reinterpret_cast<Quaternion *>(v->_data._mem); }
175 _FORCE_INLINE_ static const Quaternion *get_quaternion(const Variant *v) { return reinterpret_cast<const Quaternion *>(v->_data._mem); }
176 _FORCE_INLINE_ static ::AABB *get_aabb(Variant *v) { return v->_data._aabb; }
177 _FORCE_INLINE_ static const ::AABB *get_aabb(const Variant *v) { return v->_data._aabb; }
178 _FORCE_INLINE_ static Basis *get_basis(Variant *v) { return v->_data._basis; }
179 _FORCE_INLINE_ static const Basis *get_basis(const Variant *v) { return v->_data._basis; }
180 _FORCE_INLINE_ static Transform3D *get_transform(Variant *v) { return v->_data._transform3d; }
181 _FORCE_INLINE_ static const Transform3D *get_transform(const Variant *v) { return v->_data._transform3d; }
182 _FORCE_INLINE_ static Projection *get_projection(Variant *v) { return v->_data._projection; }
183 _FORCE_INLINE_ static const Projection *get_projection(const Variant *v) { return v->_data._projection; }
184
185 // Misc types.
186 _FORCE_INLINE_ static Color *get_color(Variant *v) { return reinterpret_cast<Color *>(v->_data._mem); }
187 _FORCE_INLINE_ static const Color *get_color(const Variant *v) { return reinterpret_cast<const Color *>(v->_data._mem); }
188 _FORCE_INLINE_ static StringName *get_string_name(Variant *v) { return reinterpret_cast<StringName *>(v->_data._mem); }
189 _FORCE_INLINE_ static const StringName *get_string_name(const Variant *v) { return reinterpret_cast<const StringName *>(v->_data._mem); }
190 _FORCE_INLINE_ static NodePath *get_node_path(Variant *v) { return reinterpret_cast<NodePath *>(v->_data._mem); }
191 _FORCE_INLINE_ static const NodePath *get_node_path(const Variant *v) { return reinterpret_cast<const NodePath *>(v->_data._mem); }
192 _FORCE_INLINE_ static ::RID *get_rid(Variant *v) { return reinterpret_cast<::RID *>(v->_data._mem); }
193 _FORCE_INLINE_ static const ::RID *get_rid(const Variant *v) { return reinterpret_cast<const ::RID *>(v->_data._mem); }
194 _FORCE_INLINE_ static Callable *get_callable(Variant *v) { return reinterpret_cast<Callable *>(v->_data._mem); }
195 _FORCE_INLINE_ static const Callable *get_callable(const Variant *v) { return reinterpret_cast<const Callable *>(v->_data._mem); }
196 _FORCE_INLINE_ static Signal *get_signal(Variant *v) { return reinterpret_cast<Signal *>(v->_data._mem); }
197 _FORCE_INLINE_ static const Signal *get_signal(const Variant *v) { return reinterpret_cast<const Signal *>(v->_data._mem); }
198 _FORCE_INLINE_ static Dictionary *get_dictionary(Variant *v) { return reinterpret_cast<Dictionary *>(v->_data._mem); }
199 _FORCE_INLINE_ static const Dictionary *get_dictionary(const Variant *v) { return reinterpret_cast<const Dictionary *>(v->_data._mem); }
200 _FORCE_INLINE_ static Array *get_array(Variant *v) { return reinterpret_cast<Array *>(v->_data._mem); }
201 _FORCE_INLINE_ static const Array *get_array(const Variant *v) { return reinterpret_cast<const Array *>(v->_data._mem); }
202
203 // Typed arrays.
204 _FORCE_INLINE_ static PackedByteArray *get_byte_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<uint8_t> *>(v->_data.packed_array)->array; }
205 _FORCE_INLINE_ static const PackedByteArray *get_byte_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<uint8_t> *>(v->_data.packed_array)->array; }
206 _FORCE_INLINE_ static PackedInt32Array *get_int32_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<int32_t> *>(v->_data.packed_array)->array; }
207 _FORCE_INLINE_ static const PackedInt32Array *get_int32_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<int32_t> *>(v->_data.packed_array)->array; }
208 _FORCE_INLINE_ static PackedInt64Array *get_int64_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<int64_t> *>(v->_data.packed_array)->array; }
209 _FORCE_INLINE_ static const PackedInt64Array *get_int64_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<int64_t> *>(v->_data.packed_array)->array; }
210 _FORCE_INLINE_ static PackedFloat32Array *get_float32_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<float> *>(v->_data.packed_array)->array; }
211 _FORCE_INLINE_ static const PackedFloat32Array *get_float32_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<float> *>(v->_data.packed_array)->array; }
212 _FORCE_INLINE_ static PackedFloat64Array *get_float64_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<double> *>(v->_data.packed_array)->array; }
213 _FORCE_INLINE_ static const PackedFloat64Array *get_float64_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<double> *>(v->_data.packed_array)->array; }
214 _FORCE_INLINE_ static PackedStringArray *get_string_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<String> *>(v->_data.packed_array)->array; }
215 _FORCE_INLINE_ static const PackedStringArray *get_string_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<String> *>(v->_data.packed_array)->array; }
216 _FORCE_INLINE_ static PackedVector2Array *get_vector2_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<Vector2> *>(v->_data.packed_array)->array; }
217 _FORCE_INLINE_ static const PackedVector2Array *get_vector2_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<Vector2> *>(v->_data.packed_array)->array; }
218 _FORCE_INLINE_ static PackedVector3Array *get_vector3_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<Vector3> *>(v->_data.packed_array)->array; }
219 _FORCE_INLINE_ static const PackedVector3Array *get_vector3_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<Vector3> *>(v->_data.packed_array)->array; }
220 _FORCE_INLINE_ static PackedColorArray *get_color_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<Color> *>(v->_data.packed_array)->array; }
221 _FORCE_INLINE_ static const PackedColorArray *get_color_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<Color> *>(v->_data.packed_array)->array; }
222 _FORCE_INLINE_ static PackedVector4Array *get_vector4_array(Variant *v) { return &static_cast<Variant::PackedArrayRef<Vector4> *>(v->_data.packed_array)->array; }
223 _FORCE_INLINE_ static const PackedVector4Array *get_vector4_array(const Variant *v) { return &static_cast<const Variant::PackedArrayRef<Vector4> *>(v->_data.packed_array)->array; }
224
225 _FORCE_INLINE_ static Object **get_object(Variant *v) { return (Object **)&v->_get_obj().obj; }
226 _FORCE_INLINE_ static const Object **get_object(const Variant *v) { return (const Object **)&v->_get_obj().obj; }
227
228 _FORCE_INLINE_ static const ObjectID get_object_id(const Variant *v) { return v->_get_obj().id; }
229
230 template <typename T>
231 _FORCE_INLINE_ static void init_generic(Variant *v) {
232 v->type = GetTypeInfo<T>::VARIANT_TYPE;
233 }
234
235 // Should be in the same order as Variant::Type for consistency.
236 // Those primitive and vector types don't need an `init_` method:
237 // Nil, bool, float, Vector2/i, Rect2/i, Vector3/i, Plane, RID.
238 // Object is a special case, handled via `object_reset_data`.
239 _FORCE_INLINE_ static void init_string(Variant *v) {
240 memnew_placement(v->_data._mem, String);
241 v->type = Variant::STRING;
242 }
243 _FORCE_INLINE_ static void init_transform2d(Variant *v) {
244 v->_data._transform2d = (Transform2D *)Variant::Pools::_bucket_small.alloc();
245 memnew_placement(v->_data._transform2d, Transform2D);
246 v->type = Variant::TRANSFORM2D;
247 }
248 _FORCE_INLINE_ static void init_quaternion(Variant *v) {
249 memnew_placement(v->_data._mem, Quaternion);
250 v->type = Variant::QUATERNION;
251 }
252 _FORCE_INLINE_ static void init_aabb(Variant *v) {
253 v->_data._aabb = (AABB *)Variant::Pools::_bucket_small.alloc();
254 memnew_placement(v->_data._aabb, AABB);
255 v->type = Variant::AABB;
256 }
257 _FORCE_INLINE_ static void init_basis(Variant *v) {
258 v->_data._basis = (Basis *)Variant::Pools::_bucket_medium.alloc();
259 memnew_placement(v->_data._basis, Basis);
260 v->type = Variant::BASIS;
261 }
262 _FORCE_INLINE_ static void init_transform3d(Variant *v) {
263 v->_data._transform3d = (Transform3D *)Variant::Pools::_bucket_medium.alloc();
264 memnew_placement(v->_data._transform3d, Transform3D);
265 v->type = Variant::TRANSFORM3D;
266 }
267 _FORCE_INLINE_ static void init_projection(Variant *v) {
268 v->_data._projection = (Projection *)Variant::Pools::_bucket_large.alloc();
269 memnew_placement(v->_data._projection, Projection);
270 v->type = Variant::PROJECTION;
271 }
272 _FORCE_INLINE_ static void init_color(Variant *v) {
273 memnew_placement(v->_data._mem, Color);
274 v->type = Variant::COLOR;
275 }
276 _FORCE_INLINE_ static void init_string_name(Variant *v) {
277 memnew_placement(v->_data._mem, StringName);
278 v->type = Variant::STRING_NAME;
279 }
280 _FORCE_INLINE_ static void init_node_path(Variant *v) {
281 memnew_placement(v->_data._mem, NodePath);
282 v->type = Variant::NODE_PATH;
283 }
284 _FORCE_INLINE_ static void init_callable(Variant *v) {
285 memnew_placement(v->_data._mem, Callable);
286 v->type = Variant::CALLABLE;
287 }
288 _FORCE_INLINE_ static void init_signal(Variant *v) {
289 memnew_placement(v->_data._mem, Signal);
290 v->type = Variant::SIGNAL;
291 }
292 _FORCE_INLINE_ static void init_dictionary(Variant *v) {
293 memnew_placement(v->_data._mem, Dictionary);
294 v->type = Variant::DICTIONARY;
295 }
296 _FORCE_INLINE_ static void init_array(Variant *v) {
297 memnew_placement(v->_data._mem, Array);
298 v->type = Variant::ARRAY;
299 }
300 _FORCE_INLINE_ static void init_byte_array(Variant *v) {
301 v->_data.packed_array = Variant::PackedArrayRef<uint8_t>::create(Vector<uint8_t>());
302 v->type = Variant::PACKED_BYTE_ARRAY;
303 }
304 _FORCE_INLINE_ static void init_int32_array(Variant *v) {
305 v->_data.packed_array = Variant::PackedArrayRef<int32_t>::create(Vector<int32_t>());
306 v->type = Variant::PACKED_INT32_ARRAY;
307 }
308 _FORCE_INLINE_ static void init_int64_array(Variant *v) {
309 v->_data.packed_array = Variant::PackedArrayRef<int64_t>::create(Vector<int64_t>());
310 v->type = Variant::PACKED_INT64_ARRAY;
311 }
312 _FORCE_INLINE_ static void init_float32_array(Variant *v) {
313 v->_data.packed_array = Variant::PackedArrayRef<float>::create(Vector<float>());
314 v->type = Variant::PACKED_FLOAT32_ARRAY;
315 }
316 _FORCE_INLINE_ static void init_float64_array(Variant *v) {
317 v->_data.packed_array = Variant::PackedArrayRef<double>::create(Vector<double>());
318 v->type = Variant::PACKED_FLOAT64_ARRAY;
319 }
320 _FORCE_INLINE_ static void init_string_array(Variant *v) {
321 v->_data.packed_array = Variant::PackedArrayRef<String>::create(Vector<String>());
322 v->type = Variant::PACKED_STRING_ARRAY;
323 }
324 _FORCE_INLINE_ static void init_vector2_array(Variant *v) {
325 v->_data.packed_array = Variant::PackedArrayRef<Vector2>::create(Vector<Vector2>());
326 v->type = Variant::PACKED_VECTOR2_ARRAY;
327 }
328 _FORCE_INLINE_ static void init_vector3_array(Variant *v) {
329 v->_data.packed_array = Variant::PackedArrayRef<Vector3>::create(Vector<Vector3>());
330 v->type = Variant::PACKED_VECTOR3_ARRAY;
331 }
332 _FORCE_INLINE_ static void init_color_array(Variant *v) {
333 v->_data.packed_array = Variant::PackedArrayRef<Color>::create(Vector<Color>());
334 v->type = Variant::PACKED_COLOR_ARRAY;
335 }
336 _FORCE_INLINE_ static void init_vector4_array(Variant *v) {
337 v->_data.packed_array = Variant::PackedArrayRef<Vector4>::create(Vector<Vector4>());
338 v->type = Variant::PACKED_VECTOR4_ARRAY;
339 }
340 _FORCE_INLINE_ static void init_object(Variant *v) {
341 object_reset_data(v);
342 v->type = Variant::OBJECT;
343 }
344
345 _FORCE_INLINE_ static void clear(Variant *v) {
346 v->clear();
347 }
348
349 _FORCE_INLINE_ static void object_assign(Variant *v, const Variant *vo) {
350 v->_get_obj().ref(vo->_get_obj());
351 }
352
353 _FORCE_INLINE_ static void object_assign(Variant *v, Object *o) {
354 v->_get_obj().ref_pointer(o);
355 }
356
357 _FORCE_INLINE_ static void object_assign(Variant *v, const Object *o) {
358 v->_get_obj().ref_pointer(const_cast<Object *>(o));
359 }
360
361 template <typename T>
362 _FORCE_INLINE_ static void object_assign(Variant *v, const Ref<T> &r) {
363 v->_get_obj().ref(r);
364 }
365
366 _FORCE_INLINE_ static void object_reset_data(Variant *v) {
367 v->_get_obj() = Variant::ObjData();
368 }
369
370 _FORCE_INLINE_ static void update_object_id(Variant *v) {
371 const Object *o = v->_get_obj().obj;
372 if (o) {
373 v->_get_obj().id = o->get_instance_id();
374 }
375 }
376
377 _FORCE_INLINE_ static void *get_opaque_pointer(Variant *v) {
378 switch (v->type) {
379 case Variant::NIL:
380 return nullptr;
381 case Variant::BOOL:
382 return get_bool(v);
383 case Variant::INT:
384 return get_int(v);
385 case Variant::FLOAT:
386 return get_float(v);
387 case Variant::STRING:
388 return get_string(v);
389 case Variant::VECTOR2:
390 return get_vector2(v);
391 case Variant::VECTOR2I:
392 return get_vector2i(v);
393 case Variant::VECTOR3:
394 return get_vector3(v);
395 case Variant::VECTOR3I:
396 return get_vector3i(v);
397 case Variant::VECTOR4:
398 return get_vector4(v);
399 case Variant::VECTOR4I:
400 return get_vector4i(v);
401 case Variant::RECT2:
402 return get_rect2(v);
403 case Variant::RECT2I:
404 return get_rect2i(v);
405 case Variant::TRANSFORM3D:
406 return get_transform(v);
407 case Variant::PROJECTION:
408 return get_projection(v);
409 case Variant::TRANSFORM2D:
410 return get_transform2d(v);
411 case Variant::QUATERNION:
412 return get_quaternion(v);
413 case Variant::PLANE:
414 return get_plane(v);
415 case Variant::BASIS:
416 return get_basis(v);
417 case Variant::AABB:
418 return get_aabb(v);
419 case Variant::COLOR:
420 return get_color(v);
421 case Variant::STRING_NAME:
422 return get_string_name(v);
423 case Variant::NODE_PATH:
424 return get_node_path(v);
425 case Variant::RID:
426 return get_rid(v);
427 case Variant::CALLABLE:
428 return get_callable(v);
429 case Variant::SIGNAL:
430 return get_signal(v);
431 case Variant::DICTIONARY:
432 return get_dictionary(v);
433 case Variant::ARRAY:
434 return get_array(v);
435 case Variant::PACKED_BYTE_ARRAY:
436 return get_byte_array(v);
437 case Variant::PACKED_INT32_ARRAY:
438 return get_int32_array(v);
439 case Variant::PACKED_INT64_ARRAY:
440 return get_int64_array(v);
441 case Variant::PACKED_FLOAT32_ARRAY:
442 return get_float32_array(v);
443 case Variant::PACKED_FLOAT64_ARRAY:
444 return get_float64_array(v);
445 case Variant::PACKED_STRING_ARRAY:
446 return get_string_array(v);
447 case Variant::PACKED_VECTOR2_ARRAY:
448 return get_vector2_array(v);
449 case Variant::PACKED_VECTOR3_ARRAY:
450 return get_vector3_array(v);
451 case Variant::PACKED_COLOR_ARRAY:
452 return get_color_array(v);
453 case Variant::PACKED_VECTOR4_ARRAY:
454 return get_vector4_array(v);
455 case Variant::OBJECT:
456 return get_object(v);
457 case Variant::VARIANT_MAX:
458 ERR_FAIL_V(nullptr);
459 }
460 ERR_FAIL_V(nullptr);
461 }
462
463 _FORCE_INLINE_ static const void *get_opaque_pointer(const Variant *v) {
464 switch (v->type) {
465 case Variant::NIL:
466 return nullptr;
467 case Variant::BOOL:
468 return get_bool(v);
469 case Variant::INT:
470 return get_int(v);
471 case Variant::FLOAT:
472 return get_float(v);
473 case Variant::STRING:
474 return get_string(v);
475 case Variant::VECTOR2:
476 return get_vector2(v);
477 case Variant::VECTOR2I:
478 return get_vector2i(v);
479 case Variant::VECTOR3:
480 return get_vector3(v);
481 case Variant::VECTOR3I:
482 return get_vector3i(v);
483 case Variant::VECTOR4:
484 return get_vector4(v);
485 case Variant::VECTOR4I:
486 return get_vector4i(v);
487 case Variant::RECT2:
488 return get_rect2(v);
489 case Variant::RECT2I:
490 return get_rect2i(v);
491 case Variant::TRANSFORM3D:
492 return get_transform(v);
493 case Variant::PROJECTION:
494 return get_projection(v);
495 case Variant::TRANSFORM2D:
496 return get_transform2d(v);
497 case Variant::QUATERNION:
498 return get_quaternion(v);
499 case Variant::PLANE:
500 return get_plane(v);
501 case Variant::BASIS:
502 return get_basis(v);
503 case Variant::AABB:
504 return get_aabb(v);
505 case Variant::COLOR:
506 return get_color(v);
507 case Variant::STRING_NAME:
508 return get_string_name(v);
509 case Variant::NODE_PATH:
510 return get_node_path(v);
511 case Variant::RID:
512 return get_rid(v);
513 case Variant::CALLABLE:
514 return get_callable(v);
515 case Variant::SIGNAL:
516 return get_signal(v);
517 case Variant::DICTIONARY:
518 return get_dictionary(v);
519 case Variant::ARRAY:
520 return get_array(v);
521 case Variant::PACKED_BYTE_ARRAY:
522 return get_byte_array(v);
523 case Variant::PACKED_INT32_ARRAY:
524 return get_int32_array(v);
525 case Variant::PACKED_INT64_ARRAY:
526 return get_int64_array(v);
527 case Variant::PACKED_FLOAT32_ARRAY:
528 return get_float32_array(v);
529 case Variant::PACKED_FLOAT64_ARRAY:
530 return get_float64_array(v);
531 case Variant::PACKED_STRING_ARRAY:
532 return get_string_array(v);
533 case Variant::PACKED_VECTOR2_ARRAY:
534 return get_vector2_array(v);
535 case Variant::PACKED_VECTOR3_ARRAY:
536 return get_vector3_array(v);
537 case Variant::PACKED_COLOR_ARRAY:
538 return get_color_array(v);
539 case Variant::PACKED_VECTOR4_ARRAY:
540 return get_vector4_array(v);
541 case Variant::OBJECT:
542 return get_object(v);
543 case Variant::VARIANT_MAX:
544 ERR_FAIL_V(nullptr);
545 }
546 ERR_FAIL_V(nullptr);
547 }
548
549 // Used internally in GDExtension and Godot's binding system when converting to Variant
550 // from values that may include RequiredParam<T> or RequiredResult<T>.
551 template <typename T>
552 _FORCE_INLINE_ static Variant make(const T &v) {
553 return Variant(v);
554 }
555 template <typename T>
556 _FORCE_INLINE_ static Variant make(const GDExtensionConstPtr<T> &v) {
557 return v.operator Variant();
558 }
559 template <typename T>
560 _FORCE_INLINE_ static Variant make(const GDExtensionPtr<T> &v) {
561 return v.operator Variant();
562 }
563 template <typename T>
564 _FORCE_INLINE_ static Variant make(const RequiredParam<T> &v) {
565 return Variant(v._internal_ptr_dont_use());
566 }
567 template <typename T>
568 _FORCE_INLINE_ static Variant make(const RequiredResult<T> &v) {
569 return Variant(v._internal_ptr_dont_use());
570 }
571 };
572
573 template <typename T, typename = void>
574 struct VariantInternalAccessor;
575
576 template <typename T>
577 struct VariantInternalAccessor<T, std::enable_if_t<!std::is_same_v<T, GetSimpleTypeT<T>>>> : VariantInternalAccessor<GetSimpleTypeT<T>> {};
578
579 template <typename T>
580 struct _VariantInternalAccessorLocal {
581 using declared_when_native_type = void;
582 static constexpr bool is_local = true;
583 static _FORCE_INLINE_ T &get(Variant *v) { return *reinterpret_cast<T *>(v->_data._mem); }
584 static _FORCE_INLINE_ const T &get(const Variant *v) { return *reinterpret_cast<const T *>(v->_data._mem); }
585 static _FORCE_INLINE_ void set(Variant *v, T p_value) { *reinterpret_cast<T *>(v->_data._mem) = std::move(p_value); }
586 };
587
588 template <typename T>
589 struct _VariantInternalAccessorElsewhere {
590 using declared_when_native_type = void;
591 static _FORCE_INLINE_ T &get(Variant *v) { return *reinterpret_cast<T *>(v->_data._ptr); }
592 static _FORCE_INLINE_ const T &get(const Variant *v) { return *reinterpret_cast<const T *>(v->_data._ptr); }
593 static _FORCE_INLINE_ void set(Variant *v, T p_value) { *reinterpret_cast<T *>(v->_data._ptr) = std::move(p_value); }
594 };
595
596 template <typename T>
597 struct _VariantInternalAccessorPackedArrayRef {
598 using declared_when_native_type = void;
599 static _FORCE_INLINE_ Vector<T> &get(Variant *v) { return static_cast<Variant::PackedArrayRef<T> *>(v->_data.packed_array)->array; }
600 static _FORCE_INLINE_ const Vector<T> &get(const Variant *v) { return static_cast<const Variant::PackedArrayRef<T> *>(v->_data.packed_array)->array; }
601 static _FORCE_INLINE_ void set(Variant *v, Vector<T> p_value) { static_cast<Variant::PackedArrayRef<T> *>(v->_data.packed_array)->array = std::move(p_value); }
602 };
603
604 template <typename T>
605 struct VariantInternalAccessor<T *> {
606 static _FORCE_INLINE_ T *get(const Variant *v) { return const_cast<T *>(static_cast<const T *>(*VariantInternal::get_object(v))); }
607 static _FORCE_INLINE_ void set(Variant *v, const T *p_value) { VariantInternal::object_assign(v, p_value); }
608 };
609
610 template <typename T>
611 struct VariantInternalAccessor<const T *> {
612 static _FORCE_INLINE_ const T *get(const Variant *v) { return static_cast<const T *>(*VariantInternal::get_object(v)); }
613 static _FORCE_INLINE_ void set(Variant *v, const T *p_value) { VariantInternal::object_assign(v, p_value); }
614 };
615
616 template <>
617 struct VariantInternalAccessor<IPAddress> {
618 static _FORCE_INLINE_ IPAddress get(const Variant *v) { return IPAddress(*VariantInternal::get_string(v)); }
619 static _FORCE_INLINE_ void set(Variant *v, IPAddress p_value) { *VariantInternal::get_string(v) = String(p_value); }
620 };
621
622 template <typename T>
623 struct VariantInternalAccessor<TypedArray<T>> {
624 static _FORCE_INLINE_ TypedArray<T> get(const Variant *v) { return TypedArray<T>(*VariantInternal::get_array(v)); }
625 static _FORCE_INLINE_ void set(Variant *v, const TypedArray<T> &p_array) { *VariantInternal::get_array(v) = Array(p_array); }
626 };
627
628 template <typename K, typename V>
629 struct VariantInternalAccessor<TypedDictionary<K, V>> {
630 static _FORCE_INLINE_ TypedDictionary<K, V> get(const Variant *v) { return TypedDictionary<K, V>(*VariantInternal::get_dictionary(v)); }
631 static _FORCE_INLINE_ void set(Variant *v, const TypedDictionary<K, V> &p_dictionary) { *VariantInternal::get_dictionary(v) = Dictionary(p_dictionary); }
632 };
633
634 template <>
635 struct VariantInternalAccessor<Object *> {
636 static _FORCE_INLINE_ Object *get(const Variant *v) { return const_cast<Object *>(*VariantInternal::get_object(v)); }
637 static _FORCE_INLINE_ void set(Variant *v, const Object *p_value) { VariantInternal::object_assign(v, p_value); }
638 };
639
640 template <class T>
641 struct VariantInternalAccessor<RequiredParam<T>> {
642 static _FORCE_INLINE_ RequiredParam<T> get(const Variant *v) { return RequiredParam<T>(Object::cast_to<T>(const_cast<Object *>(*VariantInternal::get_object(v)))); }
643 static _FORCE_INLINE_ void set(Variant *v, const RequiredParam<T> &p_value) { VariantInternal::object_assign(v, p_value.ptr()); }
644 };
645
646 template <class T>
647 struct VariantInternalAccessor<const RequiredParam<T> &> {
648 static _FORCE_INLINE_ RequiredParam<T> get(const Variant *v) { return RequiredParam<T>(Object::cast_to<T>(*VariantInternal::get_object(v))); }
649 static _FORCE_INLINE_ void set(Variant *v, const RequiredParam<T> &p_value) { VariantInternal::object_assign(v, p_value.ptr()); }
650 };
651
652 template <class T>
653 struct VariantInternalAccessor<RequiredResult<T>> {
654 static _FORCE_INLINE_ RequiredResult<T> get(const Variant *v) { return RequiredResult<T>(Object::cast_to<T>(const_cast<Object *>(*VariantInternal::get_object(v)))); }
655 static _FORCE_INLINE_ void set(Variant *v, const RequiredResult<T> &p_value) { VariantInternal::object_assign(v, p_value.ptr()); }
656 };
657
658 template <class T>
659 struct VariantInternalAccessor<const RequiredResult<T> &> {
660 static _FORCE_INLINE_ RequiredResult<T> get(const Variant *v) { return RequiredResult<T>(Object::cast_to<T>(*VariantInternal::get_object(v))); }
661 static _FORCE_INLINE_ void set(Variant *v, const RequiredResult<T> &p_value) { VariantInternal::object_assign(v, p_value.ptr()); }
662 };
663
664 template <>
665 struct VariantInternalAccessor<Variant> {
666 static _FORCE_INLINE_ Variant &get(Variant *v) { return *v; }
667 static _FORCE_INLINE_ const Variant &get(const Variant *v) { return *v; }
668 static _FORCE_INLINE_ void set(Variant *v, const Variant &p_value) { *v = p_value; }
669 };
670
671 template <>
672 struct VariantInternalAccessor<Vector<Variant>> {
673 static _FORCE_INLINE_ Vector<Variant> get(const Variant *v) {
674 Vector<Variant> ret;
675 int s = VariantInternal::get_array(v)->size();
676 ret.resize(s);
677 for (int i = 0; i < s; i++) {
678 ret.write[i] = VariantInternal::get_array(v)->get(i);
679 }
680
681 return ret;
682 }
683 static _FORCE_INLINE_ void set(Variant *v, const Vector<Variant> &p_value) {
684 int s = p_value.size();
685 VariantInternal::get_array(v)->resize(s);
686 for (int i = 0; i < s; i++) {
687 VariantInternal::get_array(v)->set(i, p_value[i]);
688 }
689 }
690 };
691
692 template <>
693 struct VariantInternalAccessor<bool> : _VariantInternalAccessorLocal<bool> {};
694
695 template <>
696 struct VariantInternalAccessor<int64_t> : _VariantInternalAccessorLocal<int64_t> {};
697
698 template <>
699 struct VariantInternalAccessor<double> : _VariantInternalAccessorLocal<double> {};
700
701 template <>
702 struct VariantInternalAccessor<String> : _VariantInternalAccessorLocal<String> {};
703
704 template <>
705 struct VariantInternalAccessor<Vector2> : _VariantInternalAccessorLocal<Vector2> {};
706
707 template <>
708 struct VariantInternalAccessor<Vector2i> : _VariantInternalAccessorLocal<Vector2i> {};
709
710 template <>
711 struct VariantInternalAccessor<Rect2> : _VariantInternalAccessorLocal<Rect2> {};
712
713 template <>
714 struct VariantInternalAccessor<Rect2i> : _VariantInternalAccessorLocal<Rect2i> {};
715
716 template <>
717 struct VariantInternalAccessor<Vector3> : _VariantInternalAccessorLocal<Vector3> {};
718
719 template <>
720 struct VariantInternalAccessor<Vector3i> : _VariantInternalAccessorLocal<Vector3i> {};
721
722 template <>
723 struct VariantInternalAccessor<Vector4> : _VariantInternalAccessorLocal<Vector4> {};
724
725 template <>
726 struct VariantInternalAccessor<Vector4i> : _VariantInternalAccessorLocal<Vector4i> {};
727
728 template <>
729 struct VariantInternalAccessor<Transform2D> : _VariantInternalAccessorElsewhere<Transform2D> {};
730
731 template <>
732 struct VariantInternalAccessor<Transform3D> : _VariantInternalAccessorElsewhere<Transform3D> {};
733
734 template <>
735 struct VariantInternalAccessor<Projection> : _VariantInternalAccessorElsewhere<Projection> {};
736
737 template <>
738 struct VariantInternalAccessor<Plane> : _VariantInternalAccessorLocal<Plane> {};
739
740 template <>
741 struct VariantInternalAccessor<Quaternion> : _VariantInternalAccessorLocal<Quaternion> {};
742
743 template <>
744 struct VariantInternalAccessor<::AABB> : _VariantInternalAccessorElsewhere<::AABB> {};
745
746 template <>
747 struct VariantInternalAccessor<Basis> : _VariantInternalAccessorElsewhere<Basis> {};
748
749 template <>
750 struct VariantInternalAccessor<Color> : _VariantInternalAccessorLocal<Color> {};
751
752 template <>
753 struct VariantInternalAccessor<StringName> : _VariantInternalAccessorLocal<StringName> {};
754
755 template <>
756 struct VariantInternalAccessor<NodePath> : _VariantInternalAccessorLocal<NodePath> {};
757
758 template <>
759 struct VariantInternalAccessor<::RID> : _VariantInternalAccessorLocal<::RID> {};
760
761 // template <>
762 // struct VariantInternalAccessor<Variant::ObjData> : _VariantInternalAccessorLocal<Variant::ObjData> {};
763
764 template <>
765 struct VariantInternalAccessor<Callable> : _VariantInternalAccessorLocal<Callable> {};
766
767 template <>
768 struct VariantInternalAccessor<Signal> : _VariantInternalAccessorLocal<Signal> {};
769
770 template <>
771 struct VariantInternalAccessor<Dictionary> : _VariantInternalAccessorLocal<Dictionary> {};
772
773 template <>
774 struct VariantInternalAccessor<Array> : _VariantInternalAccessorLocal<Array> {};
775
776 template <>
777 struct VariantInternalAccessor<PackedByteArray> : _VariantInternalAccessorPackedArrayRef<uint8_t> {};
778
779 template <>
780 struct VariantInternalAccessor<PackedInt32Array> : _VariantInternalAccessorPackedArrayRef<int32_t> {};
781
782 template <>
783 struct VariantInternalAccessor<PackedInt64Array> : _VariantInternalAccessorPackedArrayRef<int64_t> {};
784
785 template <>
786 struct VariantInternalAccessor<PackedFloat32Array> : _VariantInternalAccessorPackedArrayRef<float> {};
787
788 template <>
789 struct VariantInternalAccessor<PackedFloat64Array> : _VariantInternalAccessorPackedArrayRef<double> {};
790
791 template <>
792 struct VariantInternalAccessor<PackedStringArray> : _VariantInternalAccessorPackedArrayRef<String> {};
793
794 template <>
795 struct VariantInternalAccessor<PackedVector2Array> : _VariantInternalAccessorPackedArrayRef<Vector2> {};
796
797 template <>
798 struct VariantInternalAccessor<PackedVector3Array> : _VariantInternalAccessorPackedArrayRef<Vector3> {};
799
800 template <>
801 struct VariantInternalAccessor<PackedColorArray> : _VariantInternalAccessorPackedArrayRef<Color> {};
802
803 template <>
804 struct VariantInternalAccessor<PackedVector4Array> : _VariantInternalAccessorPackedArrayRef<Vector4> {};
805
806 template <typename T, typename = std::void_t<>>
807 struct IsVariantType : std::false_type {};
808
809 template <typename T>
810 struct IsVariantType<T, std::void_t<typename VariantInternalAccessor<T>::declared_when_native_type>> : std::true_type {};
811
812 template <typename T>
813 constexpr bool IsVariantTypeT = IsVariantType<T>::value;
814
815 template <typename T, typename S>
816 struct _VariantInternalAccessorConvert {
817 static _FORCE_INLINE_ T get(const Variant *v) {
818 return T(VariantInternalAccessor<S>::get(v));
819 }
820 static _FORCE_INLINE_ void set(Variant *v, const T p_value) {
821 VariantInternalAccessor<S>::get(v) = S(std::move(p_value));
822 }
823 };
824
825 // Integer types.
826 template <typename T>
827 struct VariantInternalAccessor<T, std::enable_if_t<std::is_integral_v<T> && !std::is_same_v<T, bool> && !std::is_same_v<T, int64_t>>> : _VariantInternalAccessorConvert<T, int64_t> {};
828 template <typename T>
829 struct VariantInternalAccessor<T, std::enable_if_t<std::is_enum_v<T>>> : _VariantInternalAccessorConvert<T, int64_t> {};
830 template <typename T>
831 struct VariantInternalAccessor<BitField<T>, std::enable_if_t<std::is_enum_v<T>>> : _VariantInternalAccessorConvert<BitField<T>, int64_t> {};
832
833 template <>
834 struct VariantInternalAccessor<ObjectID> : _VariantInternalAccessorConvert<ObjectID, int64_t> {};
835
836 // Float types.
837 template <>
838 struct VariantInternalAccessor<float> : _VariantInternalAccessorConvert<float, double> {};
839
840 template <typename T, typename = void>
841 struct VariantInitializer {
842 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_generic<T>(v); }
843 };
844
845 template <typename T>
846 struct VariantInitializer<T, std::enable_if_t<VariantInternalAccessor<T>::is_local>> {
847 static _FORCE_INLINE_ void init(Variant *v) {
848 memnew_placement(&VariantInternalAccessor<T>::get(v), T);
849 VariantInternal::set_type(*v, GetTypeInfo<T>::VARIANT_TYPE);
850 }
851 };
852
853 template <>
854 struct VariantInitializer<Transform2D> {
855 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_transform2d(v); }
856 };
857
858 template <>
859 struct VariantInitializer<AABB> {
860 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_aabb(v); }
861 };
862
863 template <>
864 struct VariantInitializer<Basis> {
865 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_basis(v); }
866 };
867
868 template <>
869 struct VariantInitializer<Transform3D> {
870 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_transform3d(v); }
871 };
872
873 template <>
874 struct VariantInitializer<Projection> {
875 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_projection(v); }
876 };
877
878 template <>
879 struct VariantInitializer<PackedByteArray> {
880 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_byte_array(v); }
881 };
882
883 template <>
884 struct VariantInitializer<PackedInt32Array> {
885 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_int32_array(v); }
886 };
887
888 template <>
889 struct VariantInitializer<PackedInt64Array> {
890 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_int64_array(v); }
891 };
892
893 template <>
894 struct VariantInitializer<PackedFloat32Array> {
895 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_float32_array(v); }
896 };
897
898 template <>
899 struct VariantInitializer<PackedFloat64Array> {
900 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_float64_array(v); }
901 };
902
903 template <>
904 struct VariantInitializer<PackedStringArray> {
905 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_string_array(v); }
906 };
907
908 template <>
909 struct VariantInitializer<PackedVector2Array> {
910 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_vector2_array(v); }
911 };
912
913 template <>
914 struct VariantInitializer<PackedVector3Array> {
915 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_vector3_array(v); }
916 };
917
918 template <>
919 struct VariantInitializer<PackedColorArray> {
920 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_color_array(v); }
921 };
922
923 template <>
924 struct VariantInitializer<PackedVector4Array> {
925 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_vector4_array(v); }
926 };
927
928 template <>
929 struct VariantInitializer<Object *> {
930 static _FORCE_INLINE_ void init(Variant *v) { VariantInternal::init_object(v); }
931 };
932
933 /// Note: This struct assumes that the argument type is already of the correct type.
934 template <typename T, typename = void>
935 struct VariantDefaultInitializer;
936
937 template <typename T>
938 struct VariantDefaultInitializer<T, std::enable_if_t<IsVariantTypeT<T>>> {
939 static _FORCE_INLINE_ void init(Variant *v) {
940 VariantInternalAccessor<T>::get(v) = T();
941 }
942 };
943
944 template <typename T>
945 struct VariantTypeChanger {
946 static _FORCE_INLINE_ void change(Variant *v) {
947 if (v->get_type() != GetTypeInfo<T>::VARIANT_TYPE || GetTypeInfo<T>::VARIANT_TYPE >= Variant::PACKED_BYTE_ARRAY) { //second condition removed by optimizer
948 VariantInternal::clear(v);
949 VariantInitializer<T>::init(v);
950 }
951 }
952 static _FORCE_INLINE_ void change_and_reset(Variant *v) {
953 if (v->get_type() != GetTypeInfo<T>::VARIANT_TYPE || GetTypeInfo<T>::VARIANT_TYPE >= Variant::PACKED_BYTE_ARRAY) { //second condition removed by optimizer
954 VariantInternal::clear(v);
955 VariantInitializer<T>::init(v);
956 }
957
958 VariantDefaultInitializer<T>::init(v);
959 }
960 };
961
962 template <typename T>
963 struct VariantTypeAdjust {
964 _FORCE_INLINE_ static void adjust(Variant *r_ret) {
965 VariantTypeChanger<GetSimpleTypeT<T>>::change(r_ret);
966 }
967 };
968
969 template <>
970 struct VariantTypeAdjust<Variant> {
971 _FORCE_INLINE_ static void adjust(Variant *r_ret) {
972 // Do nothing for variant.
973 }
974 };
975
976 template <>
977 struct VariantTypeAdjust<Object *> {
978 _FORCE_INLINE_ static void adjust(Variant *r_ret) {
979 VariantInternal::clear(r_ret);
980 *r_ret = (Object *)nullptr;
981 }
982 };
983
984 // GDExtension helpers.
985
986 template <typename T>
987 struct VariantTypeConstructor {
988 _FORCE_INLINE_ static void variant_from_type(void *r_variant, void *p_value) {
989 // r_variant is provided by caller as uninitialized memory
990 memnew_placement(r_variant, Variant(*((T *)p_value)));
991 }
992
993 _FORCE_INLINE_ static void type_from_variant(void *r_value, void *p_variant) {
994 // r_value is provided by caller as uninitialized memory
995 memnew_placement(r_value, T(*reinterpret_cast<Variant *>(p_variant)));
996 }
997 };