Debian Codegraph @

core/variant/variant.h

raw symbols SCIP
1 /**************************************************************************/
2 /* variant.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 "core/core_string_names.h"
34 #include "core/input/input_enums.h"
35 #include "core/io/ip_address.h"
36 #include "core/math/aabb.h"
37 #include "core/math/basis.h"
38 #include "core/math/color.h"
39 #include "core/math/face3.h"
40 #include "core/math/plane.h"
41 #include "core/math/projection.h"
42 #include "core/math/quaternion.h"
43 #include "core/math/rect2.h"
44 #include "core/math/rect2i.h"
45 #include "core/math/transform_2d.h"
46 #include "core/math/transform_3d.h"
47 #include "core/math/vector2.h"
48 #include "core/math/vector2i.h"
49 #include "core/math/vector3.h"
50 #include "core/math/vector3i.h"
51 #include "core/math/vector4.h"
52 #include "core/math/vector4i.h"
53 #include "core/object/object_id.h"
54 #include "core/os/keyboard.h"
55 #include "core/string/node_path.h"
56 #include "core/string/ustring.h"
57 #include "core/templates/bit_field.h"
58 #include "core/templates/list.h"
59 #include "core/templates/paged_allocator.h"
60 #include "core/templates/rid.h"
61 #include "core/variant/array.h"
62 #include "core/variant/callable.h"
63 #include "core/variant/dictionary.h"
64 #include "core/variant/variant_deep_duplicate.h"
65
66 class Object;
67 class RefCounted;
68
69 template <typename T>
70 class Ref;
71 template <typename T>
72 class BitField;
73 template <typename T>
74 class TypedArray;
75 template <typename K, typename V>
76 class TypedDictionary;
77
78 struct PropertyInfo;
79 struct MethodInfo;
80
81 typedef Vector<uint8_t> PackedByteArray;
82 typedef Vector<int32_t> PackedInt32Array;
83 typedef Vector<int64_t> PackedInt64Array;
84 typedef Vector<float> PackedFloat32Array;
85 typedef Vector<double> PackedFloat64Array;
86 typedef Vector<real_t> PackedRealArray;
87 typedef Vector<String> PackedStringArray;
88 typedef Vector<Vector2> PackedVector2Array;
89 typedef Vector<Vector3> PackedVector3Array;
90 typedef Vector<Color> PackedColorArray;
91 typedef Vector<Vector4> PackedVector4Array;
92
93 class Variant {
94 public:
95 // If this changes the table in variant_op must be updated
96 enum Type {
97 NIL,
98
99 // atomic types
100 BOOL,
101 INT,
102 FLOAT,
103 STRING,
104
105 // math types
106 VECTOR2,
107 VECTOR2I,
108 RECT2,
109 RECT2I,
110 VECTOR3,
111 VECTOR3I,
112 TRANSFORM2D,
113 VECTOR4,
114 VECTOR4I,
115 PLANE,
116 QUATERNION,
117 AABB,
118 BASIS,
119 TRANSFORM3D,
120 PROJECTION,
121
122 // misc types
123 COLOR,
124 STRING_NAME,
125 NODE_PATH,
126 RID,
127 OBJECT,
128 CALLABLE,
129 SIGNAL,
130 DICTIONARY,
131 ARRAY,
132
133 // typed arrays
134 PACKED_BYTE_ARRAY,
135 PACKED_INT32_ARRAY,
136 PACKED_INT64_ARRAY,
137 PACKED_FLOAT32_ARRAY,
138 PACKED_FLOAT64_ARRAY,
139 PACKED_STRING_ARRAY,
140 PACKED_VECTOR2_ARRAY,
141 PACKED_VECTOR3_ARRAY,
142 PACKED_COLOR_ARRAY,
143 PACKED_VECTOR4_ARRAY,
144
145 VARIANT_MAX
146 };
147
148 enum {
149 // Maximum recursion depth allowed when serializing variants.
150 MAX_RECURSION_DEPTH = 1024,
151 };
152
153 private:
154 struct Pools {
155 union BucketSmall {
156 BucketSmall() {}
157 ~BucketSmall() {}
158 Transform2D _transform2d;
159 ::AABB _aabb;
160 };
161 union BucketMedium {
162 BucketMedium() {}
163 ~BucketMedium() {}
164 Basis _basis;
165 Transform3D _transform3d;
166 };
167 union BucketLarge {
168 BucketLarge() {}
169 ~BucketLarge() {}
170 Projection _projection;
171 };
172
173 static PagedAllocator<BucketSmall, true> _bucket_small;
174 static PagedAllocator<BucketMedium, true> _bucket_medium;
175 static PagedAllocator<BucketLarge, true> _bucket_large;
176 };
177
178 friend struct _VariantCall;
179 friend class VariantInternal;
180 template <typename>
181 friend struct _VariantInternalAccessorLocal;
182 template <typename>
183 friend struct _VariantInternalAccessorElsewhere;
184 template <typename>
185 friend struct _VariantInternalAccessorPackedArrayRef;
186 // Variant takes 24 bytes when real_t is float, and 40 bytes if double.
187 // It only allocates extra memory for AABB/Transform2D (24, 48 if double),
188 // Basis/Transform3D (48, 96 if double), Projection (64, 128 if double),
189 // and PackedArray/Array/Dictionary (platform-dependent).
190
191 Type type = NIL;
192
193 struct ObjData {
194 ObjectID id;
195 Object *obj = nullptr;
196
197 void ref(const ObjData &p_from);
198 void ref_pointer(Object *p_object);
199 void ref_pointer(RefCounted *p_object);
200 void unref();
201
202 template <typename T>
203 _ALWAYS_INLINE_ void ref(const Ref<T> &p_from) {
204 if (p_from.is_valid()) {
205 ref(ObjData{ p_from->get_instance_id(), p_from.ptr() });
206 } else {
207 unref();
208 }
209 }
210 };
211
212 /* array helpers */
213 struct PackedArrayRefBase {
214 SafeRefCount refcount;
215 _FORCE_INLINE_ PackedArrayRefBase *reference() {
216 if (refcount.ref()) {
217 return this;
218 } else {
219 return nullptr;
220 }
221 }
222 static _FORCE_INLINE_ PackedArrayRefBase *reference_from(PackedArrayRefBase *p_base, PackedArrayRefBase *p_from) {
223 if (p_base == p_from) {
224 return p_base; //same thing, do nothing
225 }
226
227 if (p_from->reference()) {
228 if (p_base->refcount.unref()) {
229 memdelete(p_base);
230 }
231 return p_from;
232 } else {
233 return p_base; //keep, could not reference new
234 }
235 }
236 static _FORCE_INLINE_ void destroy(PackedArrayRefBase *p_array) {
237 if (p_array->refcount.unref()) {
238 memdelete(p_array);
239 }
240 }
241 _FORCE_INLINE_ virtual ~PackedArrayRefBase() {} //needs virtual destructor, but make inline
242 };
243
244 template <typename T>
245 struct PackedArrayRef : public PackedArrayRefBase {
246 Vector<T> array;
247 static _FORCE_INLINE_ PackedArrayRef<T> *create() {
248 return memnew(PackedArrayRef<T>);
249 }
250 static _FORCE_INLINE_ PackedArrayRef<T> *create(const Vector<T> &p_from) {
251 return memnew(PackedArrayRef<T>(p_from));
252 }
253
254 static _FORCE_INLINE_ const Vector<T> &get_array(PackedArrayRefBase *p_base) {
255 return static_cast<PackedArrayRef<T> *>(p_base)->array;
256 }
257 static _FORCE_INLINE_ Vector<T> *get_array_ptr(const PackedArrayRefBase *p_base) {
258 return &const_cast<PackedArrayRef<T> *>(static_cast<const PackedArrayRef<T> *>(p_base))->array;
259 }
260
261 _FORCE_INLINE_ PackedArrayRef(const Vector<T> &p_from) {
262 array = p_from;
263 refcount.init();
264 }
265 _FORCE_INLINE_ PackedArrayRef() {
266 refcount.init();
267 }
268 };
269
270 /* end of array helpers */
271 _ALWAYS_INLINE_ ObjData &_get_obj();
272 _ALWAYS_INLINE_ const ObjData &_get_obj() const;
273
274 union {
275 bool _bool;
276 int64_t _int;
277 double _float;
278 Transform2D *_transform2d;
279 ::AABB *_aabb;
280 Basis *_basis;
281 Transform3D *_transform3d;
282 Projection *_projection;
283 PackedArrayRefBase *packed_array;
284 void *_ptr; //generic pointer
285 uint8_t _mem[sizeof(ObjData) > (sizeof(real_t) * 4) ? sizeof(ObjData) : (sizeof(real_t) * 4)]{ 0 };
286 } _data alignas(8);
287
288 void reference(const Variant &p_variant);
289
290 void _clear_internal();
291
292 static constexpr bool needs_deinit[Variant::VARIANT_MAX] = {
293 false, //NIL,
294 false, //BOOL,
295 false, //INT,
296 false, //FLOAT,
297 true, //STRING,
298 false, //VECTOR2,
299 false, //VECTOR2I,
300 false, //RECT2,
301 false, //RECT2I,
302 false, //VECTOR3,
303 false, //VECTOR3I,
304 true, //TRANSFORM2D,
305 false, //VECTOR4,
306 false, //VECTOR4I,
307 false, //PLANE,
308 false, //QUATERNION,
309 true, //AABB,
310 true, //BASIS,
311 true, //TRANSFORM,
312 true, //PROJECTION,
313
314 // misc types
315 false, //COLOR,
316 true, //STRING_NAME,
317 true, //NODE_PATH,
318 false, //RID,
319 true, //OBJECT,
320 true, //CALLABLE,
321 true, //SIGNAL,
322 true, //DICTIONARY,
323 true, //ARRAY,
324
325 // typed arrays
326 true, //PACKED_BYTE_ARRAY,
327 true, //PACKED_INT32_ARRAY,
328 true, //PACKED_INT64_ARRAY,
329 true, //PACKED_FLOAT32_ARRAY,
330 true, //PACKED_FLOAT64_ARRAY,
331 true, //PACKED_STRING_ARRAY,
332 true, //PACKED_VECTOR2_ARRAY,
333 true, //PACKED_VECTOR3_ARRAY,
334 true, //PACKED_COLOR_ARRAY,
335 true, //PACKED_VECTOR4_ARRAY,
336 };
337
338 _FORCE_INLINE_ void clear() {
339 if (unlikely(needs_deinit[type])) { // Make it fast for types that don't need deinit.
340 _clear_internal();
341 }
342 type = NIL;
343 }
344
345 static void _register_variant_operators();
346 static void _unregister_variant_operators();
347 static void _register_variant_methods();
348 static void _unregister_variant_methods();
349 static void _register_variant_setters_getters();
350 static void _unregister_variant_setters_getters();
351 static void _register_variant_constructors();
352 static void _unregister_variant_destructors();
353 static void _register_variant_destructors();
354 static void _unregister_variant_constructors();
355 static void _register_variant_utility_functions();
356 static void _unregister_variant_utility_functions();
357
358 void _variant_call_error(const String &p_method, Callable::CallError &error);
359
360 template <typename T>
361 _ALWAYS_INLINE_ T _to_int() const {
362 switch (get_type()) {
363 case NIL:
364 return 0;
365 case BOOL:
366 return _data._bool ? 1 : 0;
367 case INT:
368 return T(_data._int);
369 case FLOAT:
370 return T(_data._float);
371 case STRING:
372 return reinterpret_cast<const String *>(_data._mem)->to_int();
373 default: {
374 return 0;
375 }
376 }
377 }
378
379 template <typename T>
380 _ALWAYS_INLINE_ T _to_float() const {
381 switch (type) {
382 case NIL:
383 return 0;
384 case BOOL:
385 return _data._bool ? 1 : 0;
386 case INT:
387 return T(_data._int);
388 case FLOAT:
389 return T(_data._float);
390 case STRING:
391 return reinterpret_cast<const String *>(_data._mem)->to_float();
392 default: {
393 return 0;
394 }
395 }
396 }
397
398 // Avoid accidental conversion. If you reached this point, it's because you most likely forgot to dereference
399 // a Variant pointer (so add * like this: *variant_pointer).
400
401 Variant(const Variant *) {}
402 Variant(const Variant **) {}
403
404 public:
405 _FORCE_INLINE_ Type get_type() const {
406 return type;
407 }
408 static String get_type_name(Variant::Type p_type);
409 static Variant::Type get_type_by_name(const String &p_type_name);
410 static bool can_convert(Type p_type_from, Type p_type_to);
411 static bool can_convert_strict(Type p_type_from, Type p_type_to);
412 static bool is_type_shared(Variant::Type p_type);
413
414 bool is_ref_counted() const;
415 _FORCE_INLINE_ bool is_num() const {
416 return type == INT || type == FLOAT;
417 }
418 _FORCE_INLINE_ bool is_string() const {
419 return type == STRING || type == STRING_NAME;
420 }
421 _FORCE_INLINE_ bool is_array() const {
422 return type >= ARRAY;
423 }
424 bool is_shared() const;
425 bool is_zero() const;
426 bool is_one() const;
427 bool is_null() const;
428 bool is_read_only() const;
429
430 // Make sure Variant is not implicitly cast when accessing it with bracket notation (GH-49469).
431 Variant &operator[](const Variant &p_key) = delete;
432 const Variant &operator[](const Variant &p_key) const = delete;
433
434 operator bool() const;
435 operator int64_t() const;
436 operator int32_t() const;
437 operator int16_t() const;
438 operator int8_t() const;
439 operator uint64_t() const;
440 operator uint32_t() const;
441 operator uint16_t() const;
442 operator uint8_t() const;
443
444 operator ObjectID() const;
445
446 operator char32_t() const;
447 operator float() const;
448 operator double() const;
449 operator String() const;
450 operator StringName() const;
451 operator Vector2() const;
452 operator Vector2i() const;
453 operator Rect2() const;
454 operator Rect2i() const;
455 operator Vector3() const;
456 operator Vector3i() const;
457 operator Vector4() const;
458 operator Vector4i() const;
459 operator Plane() const;
460 operator ::AABB() const;
461 operator Quaternion() const;
462 operator Basis() const;
463 operator Transform2D() const;
464 operator Transform3D() const;
465 operator Projection() const;
466
467 operator Color() const;
468 operator NodePath() const;
469 operator ::RID() const;
470
471 operator Object *() const;
472
473 operator Callable() const;
474 operator Signal() const;
475
476 operator Dictionary() const;
477 operator Array() const;
478
479 operator PackedByteArray() const;
480 operator PackedInt32Array() const;
481 operator PackedInt64Array() const;
482 operator PackedFloat32Array() const;
483 operator PackedFloat64Array() const;
484 operator PackedStringArray() const;
485 operator PackedVector3Array() const;
486 operator PackedVector2Array() const;
487 operator PackedColorArray() const;
488 operator PackedVector4Array() const;
489
490 operator Vector<::RID>() const;
491 operator Vector<Plane>() const;
492 operator Vector<Face3>() const;
493 operator Vector<Variant>() const;
494 operator Vector<StringName>() const;
495
496 operator IPAddress() const;
497
498 template <typename T, std::enable_if_t<std::is_enum_v<T>, int> = 0>
499 _FORCE_INLINE_ operator T() const { return static_cast<T>(operator int64_t()); }
500 template <typename T>
501 _FORCE_INLINE_ operator BitField<T>() const { return static_cast<T>(operator uint64_t()); }
502 template <typename T>
503 _FORCE_INLINE_ operator TypedArray<T>() const { return operator Array(); }
504 template <typename K, typename V>
505 _FORCE_INLINE_ operator TypedDictionary<K, V>() const { return operator Dictionary(); }
506
507 Object *get_validated_object() const;
508 Object *get_validated_object_with_check(bool &r_previously_freed) const;
509
510 Variant(bool p_bool);
511 Variant(int64_t p_int64);
512 Variant(int32_t p_int32);
513 Variant(int16_t p_int16);
514 Variant(int8_t p_int8);
515 Variant(uint64_t p_uint64);
516 Variant(uint32_t p_uint32);
517 Variant(uint16_t p_uint16);
518 Variant(uint8_t p_uint8);
519 Variant(float p_float);
520 Variant(double p_double);
521 Variant(const ObjectID &p_id);
522 Variant(const String &p_string);
523 Variant(const StringName &p_string);
524 Variant(const char *const p_cstring);
525 Variant(const char32_t *p_wstring);
526 Variant(const Vector2 &p_vector2);
527 Variant(const Vector2i &p_vector2i);
528 Variant(const Rect2 &p_rect2);
529 Variant(const Rect2i &p_rect2i);
530 Variant(const Vector3 &p_vector3);
531 Variant(const Vector3i &p_vector3i);
532 Variant(const Vector4 &p_vector4);
533 Variant(const Vector4i &p_vector4i);
534 Variant(const Plane &p_plane);
535 Variant(const ::AABB &p_aabb);
536 Variant(const Quaternion &p_quat);
537 Variant(const Basis &p_matrix);
538 Variant(const Transform2D &p_transform);
539 Variant(const Transform3D &p_transform);
540 Variant(const Projection &p_projection);
541 Variant(const Color &p_color);
542 Variant(const NodePath &p_node_path);
543 Variant(const ::RID &p_rid);
544 Variant(const Object *p_object);
545 Variant(const Callable &p_callable);
546 Variant(const Signal &p_signal);
547 Variant(const Dictionary &p_dictionary);
548
549 Variant(std::initializer_list<Variant> p_init);
550 Variant(const Array &p_array);
551 Variant(const PackedByteArray &p_byte_array);
552 Variant(const PackedInt32Array &p_int32_array);
553 Variant(const PackedInt64Array &p_int64_array);
554 Variant(const PackedFloat32Array &p_float32_array);
555 Variant(const PackedFloat64Array &p_float64_array);
556 Variant(const PackedStringArray &p_string_array);
557 Variant(const PackedVector2Array &p_vector2_array);
558 Variant(const PackedVector3Array &p_vector3_array);
559 Variant(const PackedColorArray &p_color_array);
560 Variant(const PackedVector4Array &p_vector4_array);
561
562 Variant(const Vector<::RID> &p_array); // helper
563 Variant(const Vector<Plane> &p_array); // helper
564 Variant(const Vector<Face3> &p_face_array);
565 Variant(const Vector<Variant> &p_array);
566 Variant(const Vector<StringName> &p_array);
567
568 Variant(const IPAddress &p_address);
569
570 template <typename T, std::enable_if_t<std::is_enum_v<T>, int> = 0>
571 _FORCE_INLINE_ Variant(T p_enum) :
572 Variant(static_cast<int64_t>(p_enum)) {}
573 template <typename T>
574 _FORCE_INLINE_ Variant(BitField<T> p_bitfield) :
575 Variant(static_cast<uint64_t>(p_bitfield)) {}
576 template <typename T>
577 _FORCE_INLINE_ Variant(const TypedArray<T> &p_typed_array) :
578 Variant(static_cast<const Array &>(p_typed_array)) {}
579 template <typename K, typename V>
580 _FORCE_INLINE_ Variant(const TypedDictionary<K, V> &p_typed_dictionary) :
581 Variant(static_cast<const Dictionary &>(p_typed_dictionary)) {}
582
583 // If this changes the table in variant_op must be updated
584 enum Operator {
585 //comparison
586 OP_EQUAL,
587 OP_NOT_EQUAL,
588 OP_LESS,
589 OP_LESS_EQUAL,
590 OP_GREATER,
591 OP_GREATER_EQUAL,
592 //mathematic
593 OP_ADD,
594 OP_SUBTRACT,
595 OP_MULTIPLY,
596 OP_DIVIDE,
597 OP_NEGATE,
598 OP_POSITIVE,
599 OP_MODULE,
600 OP_POWER,
601 //bitwise
602 OP_SHIFT_LEFT,
603 OP_SHIFT_RIGHT,
604 OP_BIT_AND,
605 OP_BIT_OR,
606 OP_BIT_XOR,
607 OP_BIT_NEGATE,
608 //logic
609 OP_AND,
610 OP_OR,
611 OP_XOR,
612 OP_NOT,
613 //containment
614 OP_IN,
615 OP_MAX
616
617 };
618
619 static String get_operator_name(Operator p_op);
620 static void evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b, Variant &r_ret, bool &r_valid);
621 static _FORCE_INLINE_ Variant evaluate(const Operator &p_op, const Variant &p_a, const Variant &p_b) {
622 bool valid = true;
623 Variant res;
624 evaluate(p_op, p_a, p_b, res, valid);
625 return res;
626 }
627
628 static Variant::Type get_operator_return_type(Operator p_operator, Type p_type_a, Type p_type_b);
629 typedef void (*ValidatedOperatorEvaluator)(const Variant *left, const Variant *right, Variant *r_ret);
630 static ValidatedOperatorEvaluator get_validated_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
631 typedef void (*PTROperatorEvaluator)(const void *left, const void *right, void *r_ret);
632 static PTROperatorEvaluator get_ptr_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
633
634 void zero();
635 Variant duplicate(bool p_deep = false) const;
636 Variant duplicate_deep(ResourceDeepDuplicateMode p_deep_subresources_mode = RESOURCE_DEEP_DUPLICATE_INTERNAL) const;
637 Variant recursive_duplicate(bool p_deep, ResourceDeepDuplicateMode p_deep_subresources_mode, int recursion_count) const;
638
639 /* Built-In Methods */
640
641 typedef void (*ValidatedBuiltInMethod)(Variant *base, const Variant **p_args, int p_argcount, Variant *r_ret);
642 typedef void (*PTRBuiltInMethod)(void *p_base, const void **p_args, void *r_ret, int p_argcount);
643
644 static bool has_builtin_method(Variant::Type p_type, const StringName &p_method);
645
646 static ValidatedBuiltInMethod get_validated_builtin_method(Variant::Type p_type, const StringName &p_method);
647 static PTRBuiltInMethod get_ptr_builtin_method(Variant::Type p_type, const StringName &p_method);
648 static PTRBuiltInMethod get_ptr_builtin_method_with_compatibility(Variant::Type p_type, const StringName &p_method, uint32_t p_hash);
649
650 static MethodInfo get_builtin_method_info(Variant::Type p_type, const StringName &p_method);
651 static int get_builtin_method_argument_count(Variant::Type p_type, const StringName &p_method);
652 static Variant::Type get_builtin_method_argument_type(Variant::Type p_type, const StringName &p_method, int p_argument);
653 static String get_builtin_method_argument_name(Variant::Type p_type, const StringName &p_method, int p_argument);
654 static Vector<Variant> get_builtin_method_default_arguments(Variant::Type p_type, const StringName &p_method);
655 static bool has_builtin_method_return_value(Variant::Type p_type, const StringName &p_method);
656 static Variant::Type get_builtin_method_return_type(Variant::Type p_type, const StringName &p_method);
657 static bool is_builtin_method_const(Variant::Type p_type, const StringName &p_method);
658 static bool is_builtin_method_static(Variant::Type p_type, const StringName &p_method);
659 static bool is_builtin_method_vararg(Variant::Type p_type, const StringName &p_method);
660 static void get_builtin_method_list(Variant::Type p_type, List<StringName> *p_list);
661 static int get_builtin_method_count(Variant::Type p_type);
662 static uint32_t get_builtin_method_hash(Variant::Type p_type, const StringName &p_method);
663 static Vector<uint32_t> get_builtin_method_compatibility_hashes(Variant::Type p_type, const StringName &p_method);
664
665 void callp(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error);
666
667 template <typename... VarArgs>
668 Variant call(const StringName &p_method, VarArgs... p_args) {
669 Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
670 const Variant *argptrs[sizeof...(p_args) + 1];
671 for (uint32_t i = 0; i < sizeof...(p_args); i++) {
672 argptrs[i] = &args[i];
673 }
674 Callable::CallError cerr;
675 Variant ret;
676 callp(p_method, sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), ret, cerr);
677 if (cerr.error != Callable::CallError::CALL_OK) {
678 _variant_call_error(p_method, cerr);
679 }
680 return ret;
681 }
682
683 void call_const(const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error);
684 static void call_static(Variant::Type p_type, const StringName &p_method, const Variant **p_args, int p_argcount, Variant &r_ret, Callable::CallError &r_error);
685
686 static String get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
687 static String get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
688 static String get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce);
689
690 //dynamic (includes Object)
691 void get_method_list(List<MethodInfo> *p_list) const;
692 bool has_method(const StringName &p_method) const;
693
694 /* Constructors */
695
696 typedef void (*ValidatedConstructor)(Variant *r_base, const Variant **p_args);
697 typedef void (*PTRConstructor)(void *base, const void **p_args);
698
699 static int get_constructor_count(Variant::Type p_type);
700 static ValidatedConstructor get_validated_constructor(Variant::Type p_type, int p_constructor);
701 static PTRConstructor get_ptr_constructor(Variant::Type p_type, int p_constructor);
702 static int get_constructor_argument_count(Variant::Type p_type, int p_constructor);
703 static Variant::Type get_constructor_argument_type(Variant::Type p_type, int p_constructor, int p_argument);
704 static String get_constructor_argument_name(Variant::Type p_type, int p_constructor, int p_argument);
705 static void construct(Variant::Type, Variant &base, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
706
707 static void get_constructor_list(Type p_type, List<MethodInfo> *r_list); //convenience
708
709 /* Destructors */
710
711 // Only ptrcall is available.
712 typedef void (*PTRDestructor)(void *base);
713
714 static PTRDestructor get_ptr_destructor(Variant::Type p_type);
715 static bool has_destructor(Variant::Type p_type);
716
717 /* Properties */
718
719 void set_named(const StringName &p_member, const Variant &p_value, bool &r_valid);
720 Variant get_named(const StringName &p_member, bool &r_valid) const;
721
722 typedef void (*ValidatedSetter)(Variant *base, const Variant *value);
723 typedef void (*ValidatedGetter)(const Variant *base, Variant *value);
724
725 static bool has_member(Variant::Type p_type, const StringName &p_member);
726 static Variant::Type get_member_type(Variant::Type p_type, const StringName &p_member);
727 static void get_member_list(Type p_type, List<StringName> *r_members);
728 static int get_member_count(Type p_type);
729
730 static ValidatedSetter get_member_validated_setter(Variant::Type p_type, const StringName &p_member);
731 static ValidatedGetter get_member_validated_getter(Variant::Type p_type, const StringName &p_member);
732
733 typedef void (*PTRSetter)(void *base, const void *value);
734 typedef void (*PTRGetter)(const void *base, void *value);
735
736 static PTRSetter get_member_ptr_setter(Variant::Type p_type, const StringName &p_member);
737 static PTRGetter get_member_ptr_getter(Variant::Type p_type, const StringName &p_member);
738
739 /* Indexing */
740
741 static bool has_indexing(Variant::Type p_type);
742 static Variant::Type get_indexed_element_type(Variant::Type p_type);
743 static uint32_t get_indexed_element_usage(Variant::Type p_type);
744
745 typedef void (*ValidatedIndexedSetter)(Variant *base, int64_t index, const Variant *value, bool *oob);
746 typedef void (*ValidatedIndexedGetter)(const Variant *base, int64_t index, Variant *value, bool *oob);
747
748 static ValidatedIndexedSetter get_member_validated_indexed_setter(Variant::Type p_type);
749 static ValidatedIndexedGetter get_member_validated_indexed_getter(Variant::Type p_type);
750
751 typedef void (*PTRIndexedSetter)(void *base, int64_t index, const void *value);
752 typedef void (*PTRIndexedGetter)(const void *base, int64_t index, void *value);
753
754 static PTRIndexedSetter get_member_ptr_indexed_setter(Variant::Type p_type);
755 static PTRIndexedGetter get_member_ptr_indexed_getter(Variant::Type p_type);
756
757 void set_indexed(int64_t p_index, const Variant &p_value, bool &r_valid, bool &r_oob);
758 Variant get_indexed(int64_t p_index, bool &r_valid, bool &r_oob) const;
759
760 uint64_t get_indexed_size() const;
761
762 /* Keying */
763
764 static bool is_keyed(Variant::Type p_type);
765
766 typedef void (*ValidatedKeyedSetter)(Variant *base, const Variant *key, const Variant *value, bool *valid);
767 typedef void (*ValidatedKeyedGetter)(const Variant *base, const Variant *key, Variant *value, bool *valid);
768 typedef bool (*ValidatedKeyedChecker)(const Variant *base, const Variant *key, bool *valid);
769
770 static ValidatedKeyedSetter get_member_validated_keyed_setter(Variant::Type p_type);
771 static ValidatedKeyedGetter get_member_validated_keyed_getter(Variant::Type p_type);
772 static ValidatedKeyedChecker get_member_validated_keyed_checker(Variant::Type p_type);
773
774 typedef void (*PTRKeyedSetter)(void *base, const void *key, const void *value);
775 typedef void (*PTRKeyedGetter)(const void *base, const void *key, void *value);
776 typedef uint32_t (*PTRKeyedChecker)(const void *base, const void *key);
777
778 static PTRKeyedSetter get_member_ptr_keyed_setter(Variant::Type p_type);
779 static PTRKeyedGetter get_member_ptr_keyed_getter(Variant::Type p_type);
780 static PTRKeyedChecker get_member_ptr_keyed_checker(Variant::Type p_type);
781
782 void set_keyed(const Variant &p_key, const Variant &p_value, bool &r_valid);
783 Variant get_keyed(const Variant &p_key, bool &r_valid) const;
784 bool has_key(const Variant &p_key, bool &r_valid) const;
785
786 /* Generic */
787 enum VariantSetError {
788 SET_OK,
789 SET_KEYED_ERR,
790 SET_NAMED_ERR,
791 SET_INDEXED_ERR
792 };
793 enum VariantGetError {
794 GET_OK,
795 GET_KEYED_ERR,
796 GET_NAMED_ERR,
797 GET_INDEXED_ERR
798 };
799 void set(const Variant &p_index, const Variant &p_value, bool *r_valid = nullptr, VariantSetError *err_code = nullptr);
800 Variant get(const Variant &p_index, bool *r_valid = nullptr, VariantGetError *err_code = nullptr) const;
801 bool in(const Variant &p_index, bool *r_valid = nullptr) const;
802
803 bool iter_init(Variant &r_iter, bool &r_valid) const;
804 bool iter_next(Variant &r_iter, bool &r_valid) const;
805 Variant iter_get(const Variant &r_iter, bool &r_valid) const;
806
807 void get_property_list(List<PropertyInfo> *p_list) const;
808
809 static void call_utility_function(const StringName &p_name, Variant *r_ret, const Variant **p_args, int p_argcount, Callable::CallError &r_error);
810 static bool has_utility_function(const StringName &p_name);
811
812 typedef void (*ValidatedUtilityFunction)(Variant *r_ret, const Variant **p_args, int p_argcount);
813 typedef void (*PTRUtilityFunction)(void *r_ret, const void **p_args, int p_argcount);
814
815 static ValidatedUtilityFunction get_validated_utility_function(const StringName &p_name);
816 static PTRUtilityFunction get_ptr_utility_function(const StringName &p_name);
817
818 enum UtilityFunctionType {
819 UTILITY_FUNC_TYPE_MATH,
820 UTILITY_FUNC_TYPE_RANDOM,
821 UTILITY_FUNC_TYPE_GENERAL,
822 };
823
824 static UtilityFunctionType get_utility_function_type(const StringName &p_name);
825
826 static MethodInfo get_utility_function_info(const StringName &p_name);
827 static int get_utility_function_argument_count(const StringName &p_name);
828 static Variant::Type get_utility_function_argument_type(const StringName &p_name, int p_arg);
829 static String get_utility_function_argument_name(const StringName &p_name, int p_arg);
830 static bool has_utility_function_return_value(const StringName &p_name);
831 static Variant::Type get_utility_function_return_type(const StringName &p_name);
832 static bool is_utility_function_vararg(const StringName &p_name);
833 static uint32_t get_utility_function_hash(const StringName &p_name);
834
835 static void get_utility_function_list(List<StringName> *r_functions);
836 static int get_utility_function_count();
837
838 //argsVariant call()
839
840 bool operator==(const Variant &p_variant) const;
841 bool operator!=(const Variant &p_variant) const;
842 bool operator<(const Variant &p_variant) const;
843 uint32_t hash() const;
844 uint32_t recursive_hash(int recursion_count) const;
845
846 // By default, performs a semantic comparison. Otherwise, numeric/binary comparison (if appropriate).
847 bool hash_compare(const Variant &p_variant, int recursion_count = 0, bool semantic_comparison = true) const;
848 bool identity_compare(const Variant &p_variant) const;
849 bool booleanize() const;
850 String stringify(int recursion_count = 0) const;
851 String to_json_string() const;
852
853 static void get_constants_for_type(Variant::Type p_type, List<StringName> *p_constants);
854 static int get_constants_count_for_type(Variant::Type p_type);
855 static bool has_constant(Variant::Type p_type, const StringName &p_value);
856 static Variant get_constant_value(Variant::Type p_type, const StringName &p_value, bool *r_valid = nullptr);
857
858 static void get_enums_for_type(Variant::Type p_type, List<StringName> *p_enums);
859 static void get_enumerations_for_enum(Variant::Type p_type, const StringName &p_enum_name, List<StringName> *p_enumerations);
860 static int get_enum_value(Variant::Type p_type, const StringName &p_enum_name, const StringName &p_enumeration, bool *r_valid = nullptr);
861 static bool has_enum(Variant::Type p_type, const StringName &p_enum_name);
862 static StringName get_enum_for_enumeration(Variant::Type p_type, const StringName &p_enumeration);
863
864 typedef String (*ObjectDeConstruct)(const Variant &p_object, void *ud);
865 typedef void (*ObjectConstruct)(const String &p_text, void *ud, Variant &r_value);
866
867 String get_construct_string() const;
868 static void construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct = nullptr, void *p_construct_ud = nullptr);
869
870 void operator=(const Variant &p_variant); // only this is enough for all the other types
871 void operator=(Variant &&p_variant) {
872 if (unlikely(this == &p_variant)) {
873 return;
874 }
875 clear();
876 type = p_variant.type;
877 _data = p_variant._data;
878 p_variant.type = NIL;
879 }
880
881 static void register_types();
882 static void unregister_types();
883
884 Variant(const Variant &p_variant);
885 Variant(Variant &&p_variant) {
886 type = p_variant.type;
887 _data = p_variant._data;
888 p_variant.type = NIL;
889 }
890 _FORCE_INLINE_ Variant() {}
891 _FORCE_INLINE_ ~Variant() {
892 if (unlikely(needs_deinit[type])) { // Make it fast for types that don't need deinit.
893 _clear_internal();
894 }
895 }
896 };
897
898 template <typename... VarArgs>
899 Vector<Variant> varray(VarArgs... p_args) {
900 return Vector<Variant>{ p_args... };
901 }
902
903 template <>
904 struct HashMapComparatorDefault<Variant> {
905 static bool compare(const Variant &p_lhs, const Variant &p_rhs) { return p_lhs.hash_compare(p_rhs); }
906 };
907
908 struct StringLikeVariantComparator {
909 static bool compare(const Variant &p_lhs, const Variant &p_rhs);
910 };
911
912 struct StringLikeVariantOrder {
913 static bool compare(const Variant &p_lhs, const Variant &p_rhs);
914
915 _ALWAYS_INLINE_ bool operator()(const Variant &p_lhs, const Variant &p_rhs) const {
916 return compare(p_lhs, p_rhs);
917 }
918 };
919
920 Variant::ObjData &Variant::_get_obj() {
921 return *reinterpret_cast<ObjData *>(&_data._mem[0]);
922 }
923
924 const Variant::ObjData &Variant::_get_obj() const {
925 return *reinterpret_cast<const ObjData *>(&_data._mem[0]);
926 }
927
928 template <typename... VarArgs>
929 String vformat(const String &p_text, const VarArgs... p_args) {
930 Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
931
932 bool error = false;
933 String fmt = p_text.sprintf(Span(args, sizeof...(p_args)), &error);
934
935 ERR_FAIL_COND_V_MSG(error, String(), String("Formatting error in string \"") + p_text + "\": " + fmt + ".");
936
937 return fmt;
938 }
939
940 template <typename... VarArgs>
941 Variant Callable::call(VarArgs... p_args) const {
942 Variant args[sizeof...(p_args) + 1] = { p_args..., 0 }; // +1 makes sure zero sized arrays are also supported.
943 const Variant *argptrs[sizeof...(p_args) + 1];
944 for (uint32_t i = 0; i < sizeof...(p_args); i++) {
945 argptrs[i] = &args[i];
946 }
947
948 Variant ret;
949 CallError ce;
950 callp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), ret, ce);
951 return ret;
952 }
953
954 template <typename... VarArgs>
955 Callable Callable::bind(VarArgs... p_args) const {
956 Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
957 const Variant *argptrs[sizeof...(p_args) + 1];
958 for (uint32_t i = 0; i < sizeof...(p_args); i++) {
959 argptrs[i] = &args[i];
960 }
961 return bindp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args));
962 }
963
964 Variant &Array::Iterator::operator*() const {
965 if (unlikely(read_only)) {
966 *read_only = *element_ptr;
967 return *read_only;
968 }
969 return *element_ptr;
970 }
971
972 Variant *Array::Iterator::operator->() const {
973 if (unlikely(read_only)) {
974 *read_only = *element_ptr;
975 return read_only;
976 }
977 return element_ptr;
978 }
979
980 Array::Iterator &Array::Iterator::operator++() {
981 element_ptr++;
982 return *this;
983 }
984
985 Array::Iterator &Array::Iterator::operator--() {
986 element_ptr--;
987 return *this;
988 }
989
990 Array::ConstIterator &Array::ConstIterator::operator++() {
991 element_ptr++;
992 return *this;
993 }
994
995 Array::ConstIterator &Array::ConstIterator::operator--() {
996 element_ptr--;
997 return *this;
998 }
999
1000 // Zero-constructing Variant results in NULL.
1001 template <>
1002 struct is_zero_constructible<Variant> : std::true_type {};