Debian Codegraph @

core/variant/variant.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* variant.cpp */
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 #include "variant.h"
32
33 #include "core/debugger/engine_debugger.h"
34 #include "core/io/json.h"
35 #include "core/io/resource.h"
36 #include "core/math/math_funcs.h"
37 #include "core/variant/variant_parser.h"
38
39 PagedAllocator<Variant::Pools::BucketSmall, true> Variant::Pools::_bucket_small;
40 PagedAllocator<Variant::Pools::BucketMedium, true> Variant::Pools::_bucket_medium;
41 PagedAllocator<Variant::Pools::BucketLarge, true> Variant::Pools::_bucket_large;
42
43 String Variant::get_type_name(Variant::Type p_type) {
44 switch (p_type) {
45 case NIL: {
46 return "Nil";
47 }
48
49 // Atomic types.
50 case BOOL: {
51 return "bool";
52 }
53 case INT: {
54 return "int";
55 }
56 case FLOAT: {
57 return "float";
58 }
59 case STRING: {
60 return "String";
61 }
62
63 // Math types.
64 case VECTOR2: {
65 return "Vector2";
66 }
67 case VECTOR2I: {
68 return "Vector2i";
69 }
70 case RECT2: {
71 return "Rect2";
72 }
73 case RECT2I: {
74 return "Rect2i";
75 }
76 case TRANSFORM2D: {
77 return "Transform2D";
78 }
79 case VECTOR3: {
80 return "Vector3";
81 }
82 case VECTOR3I: {
83 return "Vector3i";
84 }
85 case VECTOR4: {
86 return "Vector4";
87 }
88 case VECTOR4I: {
89 return "Vector4i";
90 }
91 case PLANE: {
92 return "Plane";
93 }
94 case AABB: {
95 return "AABB";
96 }
97 case QUATERNION: {
98 return "Quaternion";
99 }
100 case BASIS: {
101 return "Basis";
102 }
103 case TRANSFORM3D: {
104 return "Transform3D";
105 }
106 case PROJECTION: {
107 return "Projection";
108 }
109
110 // Miscellaneous types.
111 case COLOR: {
112 return "Color";
113 }
114 case RID: {
115 return "RID";
116 }
117 case OBJECT: {
118 return "Object";
119 }
120 case CALLABLE: {
121 return "Callable";
122 }
123 case SIGNAL: {
124 return "Signal";
125 }
126 case STRING_NAME: {
127 return "StringName";
128 }
129 case NODE_PATH: {
130 return "NodePath";
131 }
132 case DICTIONARY: {
133 return "Dictionary";
134 }
135 case ARRAY: {
136 return "Array";
137 }
138
139 // Arrays.
140 case PACKED_BYTE_ARRAY: {
141 return "PackedByteArray";
142 }
143 case PACKED_INT32_ARRAY: {
144 return "PackedInt32Array";
145 }
146 case PACKED_INT64_ARRAY: {
147 return "PackedInt64Array";
148 }
149 case PACKED_FLOAT32_ARRAY: {
150 return "PackedFloat32Array";
151 }
152 case PACKED_FLOAT64_ARRAY: {
153 return "PackedFloat64Array";
154 }
155 case PACKED_STRING_ARRAY: {
156 return "PackedStringArray";
157 }
158 case PACKED_VECTOR2_ARRAY: {
159 return "PackedVector2Array";
160 }
161 case PACKED_VECTOR3_ARRAY: {
162 return "PackedVector3Array";
163 }
164 case PACKED_COLOR_ARRAY: {
165 return "PackedColorArray";
166 }
167 case PACKED_VECTOR4_ARRAY: {
168 return "PackedVector4Array";
169 }
170 default: {
171 }
172 }
173
174 return "";
175 }
176
177 static HashMap<String, Variant::Type> _init_type_name_map() {
178 HashMap<String, Variant::Type> type_names;
179 for (int i = 0; i < Variant::VARIANT_MAX; i++) {
180 type_names[Variant::get_type_name((Variant::Type)i)] = (Variant::Type)i;
181 }
182 return type_names;
183 }
184
185 Variant::Type Variant::get_type_by_name(const String &p_type_name) {
186 static HashMap<String, Type> type_names = _init_type_name_map();
187
188 const Type *ptr = type_names.getptr(p_type_name);
189 return (ptr == nullptr) ? VARIANT_MAX : *ptr;
190 }
191
192 bool Variant::can_convert(Variant::Type p_type_from, Variant::Type p_type_to) {
193 if (p_type_from == p_type_to) {
194 return true;
195 }
196 if (p_type_to == NIL) { //nil can convert to anything
197 return true;
198 }
199
200 if (p_type_from == NIL) {
201 return (p_type_to == OBJECT);
202 }
203
204 const Type *valid_types = nullptr;
205 const Type *invalid_types = nullptr;
206
207 switch (p_type_to) {
208 case BOOL: {
209 static const Type valid[] = {
210 INT,
211 FLOAT,
212 STRING,
213 NIL,
214 };
215
216 valid_types = valid;
217 } break;
218 case INT: {
219 static const Type valid[] = {
220 BOOL,
221 FLOAT,
222 STRING,
223 NIL,
224 };
225
226 valid_types = valid;
227
228 } break;
229 case FLOAT: {
230 static const Type valid[] = {
231 BOOL,
232 INT,
233 STRING,
234 NIL,
235 };
236
237 valid_types = valid;
238
239 } break;
240 case STRING: {
241 static const Type invalid[] = {
242 OBJECT,
243 NIL
244 };
245
246 invalid_types = invalid;
247 } break;
248 case VECTOR2: {
249 static const Type valid[] = {
250 VECTOR2I,
251 NIL,
252 };
253
254 valid_types = valid;
255
256 } break;
257 case VECTOR2I: {
258 static const Type valid[] = {
259 VECTOR2,
260 NIL,
261 };
262
263 valid_types = valid;
264
265 } break;
266 case RECT2: {
267 static const Type valid[] = {
268 RECT2I,
269 NIL,
270 };
271
272 valid_types = valid;
273
274 } break;
275 case RECT2I: {
276 static const Type valid[] = {
277 RECT2,
278 NIL,
279 };
280
281 valid_types = valid;
282
283 } break;
284 case TRANSFORM2D: {
285 static const Type valid[] = {
286 TRANSFORM3D,
287 NIL
288 };
289
290 valid_types = valid;
291 } break;
292 case VECTOR3: {
293 static const Type valid[] = {
294 VECTOR3I,
295 NIL,
296 };
297
298 valid_types = valid;
299
300 } break;
301 case VECTOR3I: {
302 static const Type valid[] = {
303 VECTOR3,
304 NIL,
305 };
306
307 valid_types = valid;
308
309 } break;
310 case VECTOR4: {
311 static const Type valid[] = {
312 VECTOR4I,
313 NIL,
314 };
315
316 valid_types = valid;
317
318 } break;
319 case VECTOR4I: {
320 static const Type valid[] = {
321 VECTOR4,
322 NIL,
323 };
324
325 valid_types = valid;
326
327 } break;
328
329 case QUATERNION: {
330 static const Type valid[] = {
331 BASIS,
332 NIL
333 };
334
335 valid_types = valid;
336
337 } break;
338 case BASIS: {
339 static const Type valid[] = {
340 QUATERNION,
341 NIL
342 };
343
344 valid_types = valid;
345
346 } break;
347 case TRANSFORM3D: {
348 static const Type valid[] = {
349 TRANSFORM2D,
350 QUATERNION,
351 BASIS,
352 PROJECTION,
353 NIL
354 };
355
356 valid_types = valid;
357
358 } break;
359 case PROJECTION: {
360 static const Type valid[] = {
361 TRANSFORM3D,
362 NIL
363 };
364
365 valid_types = valid;
366
367 } break;
368
369 case COLOR: {
370 static const Type valid[] = {
371 STRING,
372 INT,
373 NIL,
374 };
375
376 valid_types = valid;
377
378 } break;
379
380 case RID: {
381 static const Type valid[] = {
382 OBJECT,
383 NIL
384 };
385
386 valid_types = valid;
387 } break;
388 case OBJECT: {
389 static const Type valid[] = {
390 NIL
391 };
392
393 valid_types = valid;
394 } break;
395 case STRING_NAME: {
396 static const Type valid[] = {
397 STRING,
398 NIL
399 };
400
401 valid_types = valid;
402 } break;
403 case NODE_PATH: {
404 static const Type valid[] = {
405 STRING,
406 NIL
407 };
408
409 valid_types = valid;
410 } break;
411 case ARRAY: {
412 static const Type valid[] = {
413 PACKED_BYTE_ARRAY,
414 PACKED_INT32_ARRAY,
415 PACKED_INT64_ARRAY,
416 PACKED_FLOAT32_ARRAY,
417 PACKED_FLOAT64_ARRAY,
418 PACKED_STRING_ARRAY,
419 PACKED_COLOR_ARRAY,
420 PACKED_VECTOR2_ARRAY,
421 PACKED_VECTOR3_ARRAY,
422 PACKED_VECTOR4_ARRAY,
423 NIL
424 };
425
426 valid_types = valid;
427 } break;
428 // arrays
429 case PACKED_BYTE_ARRAY: {
430 static const Type valid[] = {
431 ARRAY,
432 NIL
433 };
434
435 valid_types = valid;
436 } break;
437 case PACKED_INT32_ARRAY: {
438 static const Type valid[] = {
439 ARRAY,
440 NIL
441 };
442 valid_types = valid;
443 } break;
444 case PACKED_INT64_ARRAY: {
445 static const Type valid[] = {
446 ARRAY,
447 NIL
448 };
449 valid_types = valid;
450 } break;
451 case PACKED_FLOAT32_ARRAY: {
452 static const Type valid[] = {
453 ARRAY,
454 NIL
455 };
456
457 valid_types = valid;
458 } break;
459 case PACKED_FLOAT64_ARRAY: {
460 static const Type valid[] = {
461 ARRAY,
462 NIL
463 };
464
465 valid_types = valid;
466 } break;
467 case PACKED_STRING_ARRAY: {
468 static const Type valid[] = {
469 ARRAY,
470 NIL
471 };
472 valid_types = valid;
473 } break;
474 case PACKED_VECTOR2_ARRAY: {
475 static const Type valid[] = {
476 ARRAY,
477 NIL
478 };
479 valid_types = valid;
480
481 } break;
482 case PACKED_VECTOR3_ARRAY: {
483 static const Type valid[] = {
484 ARRAY,
485 NIL
486 };
487 valid_types = valid;
488
489 } break;
490 case PACKED_COLOR_ARRAY: {
491 static const Type valid[] = {
492 ARRAY,
493 NIL
494 };
495
496 valid_types = valid;
497
498 } break;
499 case PACKED_VECTOR4_ARRAY: {
500 static const Type valid[] = {
501 ARRAY,
502 NIL
503 };
504 valid_types = valid;
505
506 } break;
507 default: {
508 }
509 }
510
511 if (valid_types) {
512 int i = 0;
513 while (valid_types[i] != NIL) {
514 if (p_type_from == valid_types[i]) {
515 return true;
516 }
517 i++;
518 }
519
520 } else if (invalid_types) {
521 int i = 0;
522 while (invalid_types[i] != NIL) {
523 if (p_type_from == invalid_types[i]) {
524 return false;
525 }
526 i++;
527 }
528
529 return true;
530 }
531
532 return false;
533 }
534
535 bool Variant::can_convert_strict(Variant::Type p_type_from, Variant::Type p_type_to) {
536 if (p_type_from == p_type_to) {
537 return true;
538 }
539 if (p_type_to == NIL) { //nil can convert to anything
540 return true;
541 }
542
543 if (p_type_from == NIL) {
544 return (p_type_to == OBJECT);
545 }
546
547 const Type *valid_types = nullptr;
548
549 switch (p_type_to) {
550 case BOOL: {
551 static const Type valid[] = {
552 INT,
553 FLOAT,
554 //STRING,
555 NIL,
556 };
557
558 valid_types = valid;
559 } break;
560 case INT: {
561 static const Type valid[] = {
562 BOOL,
563 FLOAT,
564 //STRING,
565 NIL,
566 };
567
568 valid_types = valid;
569
570 } break;
571 case FLOAT: {
572 static const Type valid[] = {
573 BOOL,
574 INT,
575 //STRING,
576 NIL,
577 };
578
579 valid_types = valid;
580
581 } break;
582 case STRING: {
583 static const Type valid[] = {
584 NODE_PATH,
585 STRING_NAME,
586 NIL
587 };
588
589 valid_types = valid;
590 } break;
591 case VECTOR2: {
592 static const Type valid[] = {
593 VECTOR2I,
594 NIL,
595 };
596
597 valid_types = valid;
598
599 } break;
600 case VECTOR2I: {
601 static const Type valid[] = {
602 VECTOR2,
603 NIL,
604 };
605
606 valid_types = valid;
607
608 } break;
609 case RECT2: {
610 static const Type valid[] = {
611 RECT2I,
612 NIL,
613 };
614
615 valid_types = valid;
616
617 } break;
618 case RECT2I: {
619 static const Type valid[] = {
620 RECT2,
621 NIL,
622 };
623
624 valid_types = valid;
625
626 } break;
627 case TRANSFORM2D: {
628 static const Type valid[] = {
629 TRANSFORM3D,
630 NIL
631 };
632
633 valid_types = valid;
634 } break;
635 case VECTOR3: {
636 static const Type valid[] = {
637 VECTOR3I,
638 NIL,
639 };
640
641 valid_types = valid;
642
643 } break;
644 case VECTOR3I: {
645 static const Type valid[] = {
646 VECTOR3,
647 NIL,
648 };
649
650 valid_types = valid;
651
652 } break;
653 case VECTOR4: {
654 static const Type valid[] = {
655 VECTOR4I,
656 NIL,
657 };
658
659 valid_types = valid;
660
661 } break;
662 case VECTOR4I: {
663 static const Type valid[] = {
664 VECTOR4,
665 NIL,
666 };
667
668 valid_types = valid;
669
670 } break;
671
672 case QUATERNION: {
673 static const Type valid[] = {
674 BASIS,
675 NIL
676 };
677
678 valid_types = valid;
679
680 } break;
681 case BASIS: {
682 static const Type valid[] = {
683 QUATERNION,
684 NIL
685 };
686
687 valid_types = valid;
688
689 } break;
690 case TRANSFORM3D: {
691 static const Type valid[] = {
692 TRANSFORM2D,
693 QUATERNION,
694 BASIS,
695 PROJECTION,
696 NIL
697 };
698
699 valid_types = valid;
700
701 } break;
702 case PROJECTION: {
703 static const Type valid[] = {
704 TRANSFORM3D,
705 NIL
706 };
707
708 valid_types = valid;
709
710 } break;
711
712 case COLOR: {
713 static const Type valid[] = {
714 STRING,
715 INT,
716 NIL,
717 };
718
719 valid_types = valid;
720
721 } break;
722
723 case RID: {
724 static const Type valid[] = {
725 OBJECT,
726 NIL
727 };
728
729 valid_types = valid;
730 } break;
731 case OBJECT: {
732 static const Type valid[] = {
733 NIL
734 };
735
736 valid_types = valid;
737 } break;
738 case STRING_NAME: {
739 static const Type valid[] = {
740 STRING,
741 NIL
742 };
743
744 valid_types = valid;
745 } break;
746 case NODE_PATH: {
747 static const Type valid[] = {
748 STRING,
749 NIL
750 };
751
752 valid_types = valid;
753 } break;
754 case ARRAY: {
755 static const Type valid[] = {
756 PACKED_BYTE_ARRAY,
757 PACKED_INT32_ARRAY,
758 PACKED_INT64_ARRAY,
759 PACKED_FLOAT32_ARRAY,
760 PACKED_FLOAT64_ARRAY,
761 PACKED_STRING_ARRAY,
762 PACKED_COLOR_ARRAY,
763 PACKED_VECTOR2_ARRAY,
764 PACKED_VECTOR3_ARRAY,
765 PACKED_VECTOR4_ARRAY,
766 NIL
767 };
768
769 valid_types = valid;
770 } break;
771 // arrays
772 case PACKED_BYTE_ARRAY: {
773 static const Type valid[] = {
774 ARRAY,
775 NIL
776 };
777
778 valid_types = valid;
779 } break;
780 case PACKED_INT32_ARRAY: {
781 static const Type valid[] = {
782 ARRAY,
783 NIL
784 };
785 valid_types = valid;
786 } break;
787 case PACKED_INT64_ARRAY: {
788 static const Type valid[] = {
789 ARRAY,
790 NIL
791 };
792 valid_types = valid;
793 } break;
794 case PACKED_FLOAT32_ARRAY: {
795 static const Type valid[] = {
796 ARRAY,
797 NIL
798 };
799
800 valid_types = valid;
801 } break;
802 case PACKED_FLOAT64_ARRAY: {
803 static const Type valid[] = {
804 ARRAY,
805 NIL
806 };
807
808 valid_types = valid;
809 } break;
810 case PACKED_STRING_ARRAY: {
811 static const Type valid[] = {
812 ARRAY,
813 NIL
814 };
815 valid_types = valid;
816 } break;
817 case PACKED_VECTOR2_ARRAY: {
818 static const Type valid[] = {
819 ARRAY,
820 NIL
821 };
822 valid_types = valid;
823
824 } break;
825 case PACKED_VECTOR3_ARRAY: {
826 static const Type valid[] = {
827 ARRAY,
828 NIL
829 };
830 valid_types = valid;
831
832 } break;
833 case PACKED_COLOR_ARRAY: {
834 static const Type valid[] = {
835 ARRAY,
836 NIL
837 };
838
839 valid_types = valid;
840
841 } break;
842 case PACKED_VECTOR4_ARRAY: {
843 static const Type valid[] = {
844 ARRAY,
845 NIL
846 };
847 valid_types = valid;
848
849 } break;
850 default: {
851 }
852 }
853
854 if (valid_types) {
855 int i = 0;
856 while (valid_types[i] != NIL) {
857 if (p_type_from == valid_types[i]) {
858 return true;
859 }
860 i++;
861 }
862 }
863
864 return false;
865 }
866
867 bool Variant::operator==(const Variant &p_variant) const {
868 return hash_compare(p_variant);
869 }
870
871 bool Variant::operator!=(const Variant &p_variant) const {
872 // Don't use `!hash_compare(p_variant)` given it makes use of OP_EQUAL
873 if (type != p_variant.type) { //evaluation of operator== needs to be more strict
874 return true;
875 }
876 bool v;
877 Variant r;
878 evaluate(OP_NOT_EQUAL, *this, p_variant, r, v);
879 return r;
880 }
881
882 bool Variant::operator<(const Variant &p_variant) const {
883 if (type != p_variant.type) { //if types differ, then order by type first
884 return type < p_variant.type;
885 }
886 bool v;
887 Variant r;
888 evaluate(OP_LESS, *this, p_variant, r, v);
889 return r;
890 }
891
892 bool Variant::is_zero() const {
893 switch (type) {
894 case NIL: {
895 return true;
896 }
897
898 // Atomic types.
899 case BOOL: {
900 return !(_data._bool);
901 }
902 case INT: {
903 return _data._int == 0;
904 }
905 case FLOAT: {
906 return _data._float == 0;
907 }
908 case STRING: {
909 return *reinterpret_cast<const String *>(_data._mem) == String();
910 }
911
912 // Math types.
913 case VECTOR2: {
914 return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2();
915 }
916 case VECTOR2I: {
917 return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i();
918 }
919 case RECT2: {
920 return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2();
921 }
922 case RECT2I: {
923 return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i();
924 }
925 case TRANSFORM2D: {
926 return *_data._transform2d == Transform2D();
927 }
928 case VECTOR3: {
929 return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3();
930 }
931 case VECTOR3I: {
932 return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i();
933 }
934 case VECTOR4: {
935 return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4();
936 }
937 case VECTOR4I: {
938 return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i();
939 }
940 case PLANE: {
941 return *reinterpret_cast<const Plane *>(_data._mem) == Plane();
942 }
943 case AABB: {
944 return *_data._aabb == ::AABB();
945 }
946 case QUATERNION: {
947 return *reinterpret_cast<const Quaternion *>(_data._mem) == Quaternion();
948 }
949 case BASIS: {
950 return *_data._basis == Basis();
951 }
952 case TRANSFORM3D: {
953 return *_data._transform3d == Transform3D();
954 }
955 case PROJECTION: {
956 return *_data._projection == Projection();
957 }
958
959 // Miscellaneous types.
960 case COLOR: {
961 return *reinterpret_cast<const Color *>(_data._mem) == Color();
962 }
963 case RID: {
964 return *reinterpret_cast<const ::RID *>(_data._mem) == ::RID();
965 }
966 case OBJECT: {
967 return get_validated_object() == nullptr;
968 }
969 case CALLABLE: {
970 return reinterpret_cast<const Callable *>(_data._mem)->is_null();
971 }
972 case SIGNAL: {
973 return reinterpret_cast<const Signal *>(_data._mem)->is_null();
974 }
975 case STRING_NAME: {
976 return *reinterpret_cast<const StringName *>(_data._mem) == StringName();
977 }
978 case NODE_PATH: {
979 return reinterpret_cast<const NodePath *>(_data._mem)->is_empty();
980 }
981 case DICTIONARY: {
982 return reinterpret_cast<const Dictionary *>(_data._mem)->is_empty();
983 }
984 case ARRAY: {
985 return reinterpret_cast<const Array *>(_data._mem)->is_empty();
986 }
987
988 // Arrays.
989 case PACKED_BYTE_ARRAY: {
990 return PackedArrayRef<uint8_t>::get_array(_data.packed_array).is_empty();
991 }
992 case PACKED_INT32_ARRAY: {
993 return PackedArrayRef<int32_t>::get_array(_data.packed_array).is_empty();
994 }
995 case PACKED_INT64_ARRAY: {
996 return PackedArrayRef<int64_t>::get_array(_data.packed_array).is_empty();
997 }
998 case PACKED_FLOAT32_ARRAY: {
999 return PackedArrayRef<float>::get_array(_data.packed_array).is_empty();
1000 }
1001 case PACKED_FLOAT64_ARRAY: {
1002 return PackedArrayRef<double>::get_array(_data.packed_array).is_empty();
1003 }
1004 case PACKED_STRING_ARRAY: {
1005 return PackedArrayRef<String>::get_array(_data.packed_array).is_empty();
1006 }
1007 case PACKED_VECTOR2_ARRAY: {
1008 return PackedArrayRef<Vector2>::get_array(_data.packed_array).is_empty();
1009 }
1010 case PACKED_VECTOR3_ARRAY: {
1011 return PackedArrayRef<Vector3>::get_array(_data.packed_array).is_empty();
1012 }
1013 case PACKED_COLOR_ARRAY: {
1014 return PackedArrayRef<Color>::get_array(_data.packed_array).is_empty();
1015 }
1016 case PACKED_VECTOR4_ARRAY: {
1017 return PackedArrayRef<Vector4>::get_array(_data.packed_array).is_empty();
1018 }
1019 default: {
1020 }
1021 }
1022
1023 return false;
1024 }
1025
1026 bool Variant::is_one() const {
1027 switch (type) {
1028 case NIL: {
1029 return true;
1030 }
1031
1032 case BOOL: {
1033 return _data._bool;
1034 }
1035 case INT: {
1036 return _data._int == 1;
1037 }
1038 case FLOAT: {
1039 return _data._float == 1;
1040 }
1041
1042 case VECTOR2: {
1043 return *reinterpret_cast<const Vector2 *>(_data._mem) == Vector2(1, 1);
1044 }
1045 case VECTOR2I: {
1046 return *reinterpret_cast<const Vector2i *>(_data._mem) == Vector2i(1, 1);
1047 }
1048 case RECT2: {
1049 return *reinterpret_cast<const Rect2 *>(_data._mem) == Rect2(1, 1, 1, 1);
1050 }
1051 case RECT2I: {
1052 return *reinterpret_cast<const Rect2i *>(_data._mem) == Rect2i(1, 1, 1, 1);
1053 }
1054 case VECTOR3: {
1055 return *reinterpret_cast<const Vector3 *>(_data._mem) == Vector3(1, 1, 1);
1056 }
1057 case VECTOR3I: {
1058 return *reinterpret_cast<const Vector3i *>(_data._mem) == Vector3i(1, 1, 1);
1059 }
1060 case VECTOR4: {
1061 return *reinterpret_cast<const Vector4 *>(_data._mem) == Vector4(1, 1, 1, 1);
1062 }
1063 case VECTOR4I: {
1064 return *reinterpret_cast<const Vector4i *>(_data._mem) == Vector4i(1, 1, 1, 1);
1065 }
1066 case PLANE: {
1067 return *reinterpret_cast<const Plane *>(_data._mem) == Plane(1, 1, 1, 1);
1068 }
1069
1070 case COLOR: {
1071 return *reinterpret_cast<const Color *>(_data._mem) == Color(1, 1, 1, 1);
1072 }
1073
1074 default: {
1075 return !is_zero();
1076 }
1077 }
1078 }
1079
1080 bool Variant::is_null() const {
1081 if (type == OBJECT && _get_obj().obj) {
1082 return false;
1083 } else {
1084 return true;
1085 }
1086 }
1087
1088 void Variant::ObjData::ref(const ObjData &p_from) {
1089 // Mirrors Ref::ref in refcounted.h
1090 if (p_from.id == id) {
1091 return;
1092 }
1093
1094 ObjData cleanup_ref = *this;
1095
1096 *this = p_from;
1097 if (id.is_ref_counted()) {
1098 RefCounted *reference = static_cast<RefCounted *>(obj);
1099 // Assuming reference is not null because id.is_ref_counted() was true.
1100 if (!reference->reference()) {
1101 *this = ObjData();
1102 }
1103 }
1104
1105 cleanup_ref.unref();
1106 }
1107
1108 void Variant::ObjData::ref_pointer(Object *p_object) {
1109 // Mirrors Ref::ref_pointer in refcounted.h
1110 if (p_object == obj) {
1111 return;
1112 }
1113
1114 ObjData cleanup_ref = *this;
1115
1116 if (p_object) {
1117 *this = ObjData{ p_object->get_instance_id(), p_object };
1118 if (p_object->is_ref_counted()) {
1119 RefCounted *reference = static_cast<RefCounted *>(p_object);
1120 if (!reference->init_ref()) {
1121 *this = ObjData();
1122 }
1123 }
1124 } else {
1125 *this = ObjData();
1126 }
1127
1128 cleanup_ref.unref();
1129 }
1130
1131 void Variant::ObjData::unref() {
1132 // Mirrors Ref::unref in refcounted.h
1133 if (id.is_ref_counted()) {
1134 RefCounted *reference = static_cast<RefCounted *>(obj);
1135 // Assuming reference is not null because id.is_ref_counted() was true.
1136 if (reference->unreference()) {
1137 memdelete(reference);
1138 }
1139 }
1140 *this = ObjData();
1141 }
1142
1143 void Variant::reference(const Variant &p_variant) {
1144 if (type == OBJECT && p_variant.type == OBJECT) {
1145 _get_obj().ref(p_variant._get_obj());
1146 return;
1147 }
1148
1149 clear();
1150
1151 type = p_variant.type;
1152
1153 switch (p_variant.type) {
1154 case NIL: {
1155 // None.
1156 } break;
1157
1158 // Atomic types.
1159 case BOOL: {
1160 _data._bool = p_variant._data._bool;
1161 } break;
1162 case INT: {
1163 _data._int = p_variant._data._int;
1164 } break;
1165 case FLOAT: {
1166 _data._float = p_variant._data._float;
1167 } break;
1168 case STRING: {
1169 memnew_placement(_data._mem, String(*reinterpret_cast<const String *>(p_variant._data._mem)));
1170 } break;
1171
1172 // Math types.
1173 case VECTOR2: {
1174 memnew_placement(_data._mem, Vector2(*reinterpret_cast<const Vector2 *>(p_variant._data._mem)));
1175 } break;
1176 case VECTOR2I: {
1177 memnew_placement(_data._mem, Vector2i(*reinterpret_cast<const Vector2i *>(p_variant._data._mem)));
1178 } break;
1179 case RECT2: {
1180 memnew_placement(_data._mem, Rect2(*reinterpret_cast<const Rect2 *>(p_variant._data._mem)));
1181 } break;
1182 case RECT2I: {
1183 memnew_placement(_data._mem, Rect2i(*reinterpret_cast<const Rect2i *>(p_variant._data._mem)));
1184 } break;
1185 case TRANSFORM2D: {
1186 _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
1187 memnew_placement(_data._transform2d, Transform2D(*p_variant._data._transform2d));
1188 } break;
1189 case VECTOR3: {
1190 memnew_placement(_data._mem, Vector3(*reinterpret_cast<const Vector3 *>(p_variant._data._mem)));
1191 } break;
1192 case VECTOR3I: {
1193 memnew_placement(_data._mem, Vector3i(*reinterpret_cast<const Vector3i *>(p_variant._data._mem)));
1194 } break;
1195 case VECTOR4: {
1196 memnew_placement(_data._mem, Vector4(*reinterpret_cast<const Vector4 *>(p_variant._data._mem)));
1197 } break;
1198 case VECTOR4I: {
1199 memnew_placement(_data._mem, Vector4i(*reinterpret_cast<const Vector4i *>(p_variant._data._mem)));
1200 } break;
1201 case PLANE: {
1202 memnew_placement(_data._mem, Plane(*reinterpret_cast<const Plane *>(p_variant._data._mem)));
1203 } break;
1204 case AABB: {
1205 _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
1206 memnew_placement(_data._aabb, ::AABB(*p_variant._data._aabb));
1207 } break;
1208 case QUATERNION: {
1209 memnew_placement(_data._mem, Quaternion(*reinterpret_cast<const Quaternion *>(p_variant._data._mem)));
1210 } break;
1211 case BASIS: {
1212 _data._basis = (Basis *)Pools::_bucket_medium.alloc();
1213 memnew_placement(_data._basis, Basis(*p_variant._data._basis));
1214 } break;
1215 case TRANSFORM3D: {
1216 _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
1217 memnew_placement(_data._transform3d, Transform3D(*p_variant._data._transform3d));
1218 } break;
1219 case PROJECTION: {
1220 _data._projection = (Projection *)Pools::_bucket_large.alloc();
1221 memnew_placement(_data._projection, Projection(*p_variant._data._projection));
1222 } break;
1223
1224 // Miscellaneous types.
1225 case COLOR: {
1226 memnew_placement(_data._mem, Color(*reinterpret_cast<const Color *>(p_variant._data._mem)));
1227 } break;
1228 case RID: {
1229 memnew_placement(_data._mem, ::RID(*reinterpret_cast<const ::RID *>(p_variant._data._mem)));
1230 } break;
1231 case OBJECT: {
1232 memnew_placement(_data._mem, ObjData);
1233 _get_obj().ref(p_variant._get_obj());
1234 } break;
1235 case CALLABLE: {
1236 memnew_placement(_data._mem, Callable(*reinterpret_cast<const Callable *>(p_variant._data._mem)));
1237 } break;
1238 case SIGNAL: {
1239 memnew_placement(_data._mem, Signal(*reinterpret_cast<const Signal *>(p_variant._data._mem)));
1240 } break;
1241 case STRING_NAME: {
1242 memnew_placement(_data._mem, StringName(*reinterpret_cast<const StringName *>(p_variant._data._mem)));
1243 } break;
1244 case NODE_PATH: {
1245 memnew_placement(_data._mem, NodePath(*reinterpret_cast<const NodePath *>(p_variant._data._mem)));
1246 } break;
1247 case DICTIONARY: {
1248 memnew_placement(_data._mem, Dictionary(*reinterpret_cast<const Dictionary *>(p_variant._data._mem)));
1249 } break;
1250 case ARRAY: {
1251 memnew_placement(_data._mem, Array(*reinterpret_cast<const Array *>(p_variant._data._mem)));
1252 } break;
1253
1254 // Arrays.
1255 case PACKED_BYTE_ARRAY: {
1256 _data.packed_array = static_cast<PackedArrayRef<uint8_t> *>(p_variant._data.packed_array)->reference();
1257 if (!_data.packed_array) {
1258 _data.packed_array = PackedArrayRef<uint8_t>::create();
1259 }
1260 } break;
1261 case PACKED_INT32_ARRAY: {
1262 _data.packed_array = static_cast<PackedArrayRef<int32_t> *>(p_variant._data.packed_array)->reference();
1263 if (!_data.packed_array) {
1264 _data.packed_array = PackedArrayRef<int32_t>::create();
1265 }
1266 } break;
1267 case PACKED_INT64_ARRAY: {
1268 _data.packed_array = static_cast<PackedArrayRef<int64_t> *>(p_variant._data.packed_array)->reference();
1269 if (!_data.packed_array) {
1270 _data.packed_array = PackedArrayRef<int64_t>::create();
1271 }
1272 } break;
1273 case PACKED_FLOAT32_ARRAY: {
1274 _data.packed_array = static_cast<PackedArrayRef<float> *>(p_variant._data.packed_array)->reference();
1275 if (!_data.packed_array) {
1276 _data.packed_array = PackedArrayRef<float>::create();
1277 }
1278 } break;
1279 case PACKED_FLOAT64_ARRAY: {
1280 _data.packed_array = static_cast<PackedArrayRef<double> *>(p_variant._data.packed_array)->reference();
1281 if (!_data.packed_array) {
1282 _data.packed_array = PackedArrayRef<double>::create();
1283 }
1284 } break;
1285 case PACKED_STRING_ARRAY: {
1286 _data.packed_array = static_cast<PackedArrayRef<String> *>(p_variant._data.packed_array)->reference();
1287 if (!_data.packed_array) {
1288 _data.packed_array = PackedArrayRef<String>::create();
1289 }
1290 } break;
1291 case PACKED_VECTOR2_ARRAY: {
1292 _data.packed_array = static_cast<PackedArrayRef<Vector2> *>(p_variant._data.packed_array)->reference();
1293 if (!_data.packed_array) {
1294 _data.packed_array = PackedArrayRef<Vector2>::create();
1295 }
1296 } break;
1297 case PACKED_VECTOR3_ARRAY: {
1298 _data.packed_array = static_cast<PackedArrayRef<Vector3> *>(p_variant._data.packed_array)->reference();
1299 if (!_data.packed_array) {
1300 _data.packed_array = PackedArrayRef<Vector3>::create();
1301 }
1302 } break;
1303 case PACKED_COLOR_ARRAY: {
1304 _data.packed_array = static_cast<PackedArrayRef<Color> *>(p_variant._data.packed_array)->reference();
1305 if (!_data.packed_array) {
1306 _data.packed_array = PackedArrayRef<Color>::create();
1307 }
1308 } break;
1309 case PACKED_VECTOR4_ARRAY: {
1310 _data.packed_array = static_cast<PackedArrayRef<Vector4> *>(p_variant._data.packed_array)->reference();
1311 if (!_data.packed_array) {
1312 _data.packed_array = PackedArrayRef<Vector4>::create();
1313 }
1314 } break;
1315 default: {
1316 }
1317 }
1318 }
1319
1320 void Variant::zero() {
1321 switch (type) {
1322 case NIL:
1323 break;
1324 case BOOL:
1325 _data._bool = false;
1326 break;
1327 case INT:
1328 _data._int = 0;
1329 break;
1330 case FLOAT:
1331 _data._float = 0;
1332 break;
1333
1334 case VECTOR2:
1335 *reinterpret_cast<Vector2 *>(_data._mem) = Vector2();
1336 break;
1337 case VECTOR2I:
1338 *reinterpret_cast<Vector2i *>(_data._mem) = Vector2i();
1339 break;
1340 case RECT2:
1341 *reinterpret_cast<Rect2 *>(_data._mem) = Rect2();
1342 break;
1343 case RECT2I:
1344 *reinterpret_cast<Rect2i *>(_data._mem) = Rect2i();
1345 break;
1346 case VECTOR3:
1347 *reinterpret_cast<Vector3 *>(_data._mem) = Vector3();
1348 break;
1349 case VECTOR3I:
1350 *reinterpret_cast<Vector3i *>(_data._mem) = Vector3i();
1351 break;
1352 case VECTOR4:
1353 *reinterpret_cast<Vector4 *>(_data._mem) = Vector4();
1354 break;
1355 case VECTOR4I:
1356 *reinterpret_cast<Vector4i *>(_data._mem) = Vector4i();
1357 break;
1358 case PLANE:
1359 *reinterpret_cast<Plane *>(_data._mem) = Plane();
1360 break;
1361 case QUATERNION:
1362 *reinterpret_cast<Quaternion *>(_data._mem) = Quaternion();
1363 break;
1364
1365 case COLOR:
1366 *reinterpret_cast<Color *>(_data._mem) = Color();
1367 break;
1368
1369 default:
1370 Type prev_type = type;
1371 clear();
1372 if (type != prev_type) {
1373 // clear() changes type to NIL, so it needs to be restored.
1374 Callable::CallError ce;
1375 Variant::construct(prev_type, *this, nullptr, 0, ce);
1376 }
1377 break;
1378 }
1379 }
1380
1381 void Variant::_clear_internal() {
1382 switch (type) {
1383 case STRING: {
1384 reinterpret_cast<String *>(_data._mem)->~String();
1385 } break;
1386
1387 // Math types.
1388 case TRANSFORM2D: {
1389 if (_data._transform2d) {
1390 _data._transform2d->~Transform2D();
1391 Pools::_bucket_small.free((Pools::BucketSmall *)_data._transform2d);
1392 _data._transform2d = nullptr;
1393 }
1394 } break;
1395 case AABB: {
1396 if (_data._aabb) {
1397 _data._aabb->~AABB();
1398 Pools::_bucket_small.free((Pools::BucketSmall *)_data._aabb);
1399 _data._aabb = nullptr;
1400 }
1401 } break;
1402 case BASIS: {
1403 if (_data._basis) {
1404 _data._basis->~Basis();
1405 Pools::_bucket_medium.free((Pools::BucketMedium *)_data._basis);
1406 _data._basis = nullptr;
1407 }
1408 } break;
1409 case TRANSFORM3D: {
1410 if (_data._transform3d) {
1411 _data._transform3d->~Transform3D();
1412 Pools::_bucket_medium.free((Pools::BucketMedium *)_data._transform3d);
1413 _data._transform3d = nullptr;
1414 }
1415 } break;
1416 case PROJECTION: {
1417 if (_data._projection) {
1418 _data._projection->~Projection();
1419 Pools::_bucket_large.free((Pools::BucketLarge *)_data._projection);
1420 _data._projection = nullptr;
1421 }
1422 } break;
1423
1424 // Miscellaneous types.
1425 case STRING_NAME: {
1426 reinterpret_cast<StringName *>(_data._mem)->~StringName();
1427 } break;
1428 case NODE_PATH: {
1429 reinterpret_cast<NodePath *>(_data._mem)->~NodePath();
1430 } break;
1431 case OBJECT: {
1432 _get_obj().unref();
1433 } break;
1434 case RID: {
1435 // Not much need probably.
1436 // HACK: Can't seem to use destructor + scoping operator, so hack.
1437 typedef ::RID RID_Class;
1438 reinterpret_cast<RID_Class *>(_data._mem)->~RID_Class();
1439 } break;
1440 case CALLABLE: {
1441 reinterpret_cast<Callable *>(_data._mem)->~Callable();
1442 } break;
1443 case SIGNAL: {
1444 reinterpret_cast<Signal *>(_data._mem)->~Signal();
1445 } break;
1446 case DICTIONARY: {
1447 reinterpret_cast<Dictionary *>(_data._mem)->~Dictionary();
1448 } break;
1449 case ARRAY: {
1450 reinterpret_cast<Array *>(_data._mem)->~Array();
1451 } break;
1452
1453 // Arrays.
1454 case PACKED_BYTE_ARRAY: {
1455 PackedArrayRefBase::destroy(_data.packed_array);
1456 } break;
1457 case PACKED_INT32_ARRAY: {
1458 PackedArrayRefBase::destroy(_data.packed_array);
1459 } break;
1460 case PACKED_INT64_ARRAY: {
1461 PackedArrayRefBase::destroy(_data.packed_array);
1462 } break;
1463 case PACKED_FLOAT32_ARRAY: {
1464 PackedArrayRefBase::destroy(_data.packed_array);
1465 } break;
1466 case PACKED_FLOAT64_ARRAY: {
1467 PackedArrayRefBase::destroy(_data.packed_array);
1468 } break;
1469 case PACKED_STRING_ARRAY: {
1470 PackedArrayRefBase::destroy(_data.packed_array);
1471 } break;
1472 case PACKED_VECTOR2_ARRAY: {
1473 PackedArrayRefBase::destroy(_data.packed_array);
1474 } break;
1475 case PACKED_VECTOR3_ARRAY: {
1476 PackedArrayRefBase::destroy(_data.packed_array);
1477 } break;
1478 case PACKED_COLOR_ARRAY: {
1479 PackedArrayRefBase::destroy(_data.packed_array);
1480 } break;
1481 case PACKED_VECTOR4_ARRAY: {
1482 PackedArrayRefBase::destroy(_data.packed_array);
1483 } break;
1484 default: {
1485 // Not needed, there is no point. The following do not allocate memory:
1486 // VECTOR2, VECTOR3, VECTOR4, RECT2, PLANE, QUATERNION, COLOR.
1487 }
1488 }
1489 }
1490
1491 Variant::operator int64_t() const {
1492 return _to_int<int64_t>();
1493 }
1494
1495 Variant::operator int32_t() const {
1496 return _to_int<int32_t>();
1497 }
1498
1499 Variant::operator int16_t() const {
1500 return _to_int<int16_t>();
1501 }
1502
1503 Variant::operator int8_t() const {
1504 return _to_int<int8_t>();
1505 }
1506
1507 Variant::operator uint64_t() const {
1508 return _to_int<uint64_t>();
1509 }
1510
1511 Variant::operator uint32_t() const {
1512 return _to_int<uint32_t>();
1513 }
1514
1515 Variant::operator uint16_t() const {
1516 return _to_int<uint16_t>();
1517 }
1518
1519 Variant::operator uint8_t() const {
1520 return _to_int<uint8_t>();
1521 }
1522
1523 Variant::operator ObjectID() const {
1524 if (type == INT) {
1525 return ObjectID(_data._int);
1526 } else if (type == OBJECT) {
1527 return _get_obj().id;
1528 } else {
1529 return ObjectID();
1530 }
1531 }
1532
1533 Variant::operator char32_t() const {
1534 return operator uint32_t();
1535 }
1536
1537 Variant::operator float() const {
1538 return _to_float<float>();
1539 }
1540
1541 Variant::operator double() const {
1542 return _to_float<double>();
1543 }
1544
1545 Variant::operator StringName() const {
1546 if (type == STRING_NAME) {
1547 return *reinterpret_cast<const StringName *>(_data._mem);
1548 } else if (type == STRING) {
1549 return *reinterpret_cast<const String *>(_data._mem);
1550 }
1551
1552 return StringName();
1553 }
1554
1555 struct _VariantStrPair {
1556 String key;
1557 String value;
1558
1559 bool operator<(const _VariantStrPair &p) const {
1560 return key < p.key;
1561 }
1562 };
1563
1564 Variant::operator String() const {
1565 return stringify(0);
1566 }
1567
1568 String stringify_variant_clean(const Variant &p_variant, int recursion_count) {
1569 String s = p_variant.stringify(recursion_count);
1570
1571 // Wrap strings in quotes to avoid ambiguity.
1572 switch (p_variant.get_type()) {
1573 case Variant::STRING: {
1574 s = s.c_escape().quote();
1575 } break;
1576 case Variant::STRING_NAME: {
1577 s = "&" + s.c_escape().quote();
1578 } break;
1579 case Variant::NODE_PATH: {
1580 s = "^" + s.c_escape().quote();
1581 } break;
1582 default: {
1583 } break;
1584 }
1585
1586 return s;
1587 }
1588
1589 template <typename T>
1590 String stringify_vector(const T &vec, int recursion_count) {
1591 String str("[");
1592 for (int i = 0; i < vec.size(); i++) {
1593 if (i > 0) {
1594 str += ", ";
1595 }
1596
1597 str += stringify_variant_clean(vec[i], recursion_count);
1598 }
1599 str += "]";
1600 return str;
1601 }
1602
1603 String Variant::stringify(int recursion_count) const {
1604 switch (type) {
1605 case NIL:
1606 return "<null>";
1607 case BOOL:
1608 return _data._bool ? "true" : "false";
1609 case INT:
1610 return itos(_data._int);
1611 case FLOAT:
1612 return String::num_real(_data._float, true);
1613 case STRING:
1614 return *reinterpret_cast<const String *>(_data._mem);
1615 case VECTOR2:
1616 return String(operator Vector2());
1617 case VECTOR2I:
1618 return String(operator Vector2i());
1619 case RECT2:
1620 return String(operator Rect2());
1621 case RECT2I:
1622 return String(operator Rect2i());
1623 case TRANSFORM2D:
1624 return String(operator Transform2D());
1625 case VECTOR3:
1626 return String(operator Vector3());
1627 case VECTOR3I:
1628 return String(operator Vector3i());
1629 case VECTOR4:
1630 return String(operator Vector4());
1631 case VECTOR4I:
1632 return String(operator Vector4i());
1633 case PLANE:
1634 return String(operator Plane());
1635 case AABB:
1636 return String(operator ::AABB());
1637 case QUATERNION:
1638 return String(operator Quaternion());
1639 case BASIS:
1640 return String(operator Basis());
1641 case TRANSFORM3D:
1642 return String(operator Transform3D());
1643 case PROJECTION:
1644 return String(operator Projection());
1645 case STRING_NAME:
1646 return operator StringName();
1647 case NODE_PATH:
1648 return String(operator NodePath());
1649 case COLOR:
1650 return String(operator Color());
1651 case DICTIONARY: {
1652 ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "{ ... }", "Maximum dictionary recursion reached!");
1653 recursion_count++;
1654
1655 const Dictionary &d = *reinterpret_cast<const Dictionary *>(_data._mem);
1656
1657 // Add leading and trailing space to Dictionary printing. This distinguishes it
1658 // from array printing on fonts that have similar-looking {} and [] characters.
1659 String str("{ ");
1660
1661 Vector<_VariantStrPair> pairs;
1662
1663 for (const KeyValue<Variant, Variant> &kv : d) {
1664 _VariantStrPair sp;
1665 sp.key = stringify_variant_clean(kv.key, recursion_count);
1666 sp.value = stringify_variant_clean(kv.value, recursion_count);
1667
1668 pairs.push_back(sp);
1669 }
1670
1671 for (int i = 0; i < pairs.size(); i++) {
1672 if (i > 0) {
1673 str += ", ";
1674 }
1675 str += pairs[i].key + ": " + pairs[i].value;
1676 }
1677 str += " }";
1678
1679 return str;
1680 }
1681 // Packed arrays cannot contain recursive structures, the recursion_count increment is not needed.
1682 case PACKED_VECTOR2_ARRAY: {
1683 return stringify_vector(operator PackedVector2Array(), recursion_count);
1684 }
1685 case PACKED_VECTOR3_ARRAY: {
1686 return stringify_vector(operator PackedVector3Array(), recursion_count);
1687 }
1688 case PACKED_COLOR_ARRAY: {
1689 return stringify_vector(operator PackedColorArray(), recursion_count);
1690 }
1691 case PACKED_VECTOR4_ARRAY: {
1692 return stringify_vector(operator PackedVector4Array(), recursion_count);
1693 }
1694 case PACKED_STRING_ARRAY: {
1695 return stringify_vector(operator PackedStringArray(), recursion_count);
1696 }
1697 case PACKED_BYTE_ARRAY: {
1698 return stringify_vector(operator PackedByteArray(), recursion_count);
1699 }
1700 case PACKED_INT32_ARRAY: {
1701 return stringify_vector(operator PackedInt32Array(), recursion_count);
1702 }
1703 case PACKED_INT64_ARRAY: {
1704 return stringify_vector(operator PackedInt64Array(), recursion_count);
1705 }
1706 case PACKED_FLOAT32_ARRAY: {
1707 return stringify_vector(operator PackedFloat32Array(), recursion_count);
1708 }
1709 case PACKED_FLOAT64_ARRAY: {
1710 return stringify_vector(operator PackedFloat64Array(), recursion_count);
1711 }
1712 case ARRAY: {
1713 ERR_FAIL_COND_V_MSG(recursion_count > MAX_RECURSION, "[...]", "Maximum array recursion reached!");
1714 recursion_count++;
1715
1716 return stringify_vector(operator Array(), recursion_count);
1717 }
1718 case OBJECT: {
1719 if (_get_obj().obj) {
1720 if (!_get_obj().id.is_ref_counted() && ObjectDB::get_instance(_get_obj().id) == nullptr) {
1721 return "<Freed Object>";
1722 }
1723
1724 return _get_obj().obj->to_string();
1725 } else {
1726 return "<Object#null>";
1727 }
1728 }
1729 case CALLABLE: {
1730 const Callable &c = *reinterpret_cast<const Callable *>(_data._mem);
1731 return String(c);
1732 }
1733 case SIGNAL: {
1734 const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
1735 return String(s);
1736 }
1737 case RID: {
1738 const ::RID &s = *reinterpret_cast<const ::RID *>(_data._mem);
1739 return "RID(" + itos(s.get_id()) + ")";
1740 }
1741 default: {
1742 return "<" + get_type_name(type) + ">";
1743 }
1744 }
1745 }
1746
1747 String Variant::to_json_string() const {
1748 return JSON::stringify(*this);
1749 }
1750
1751 Variant::operator Vector2() const {
1752 if (type == VECTOR2) {
1753 return *reinterpret_cast<const Vector2 *>(_data._mem);
1754 } else if (type == VECTOR2I) {
1755 return *reinterpret_cast<const Vector2i *>(_data._mem);
1756 } else if (type == VECTOR3) {
1757 return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
1758 } else if (type == VECTOR3I) {
1759 return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
1760 } else if (type == VECTOR4) {
1761 return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
1762 } else if (type == VECTOR4I) {
1763 return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
1764 } else {
1765 return Vector2();
1766 }
1767 }
1768
1769 Variant::operator Vector2i() const {
1770 if (type == VECTOR2I) {
1771 return *reinterpret_cast<const Vector2i *>(_data._mem);
1772 } else if (type == VECTOR2) {
1773 return *reinterpret_cast<const Vector2 *>(_data._mem);
1774 } else if (type == VECTOR3) {
1775 return Vector2(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y);
1776 } else if (type == VECTOR3I) {
1777 return Vector2(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y);
1778 } else if (type == VECTOR4) {
1779 return Vector2(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y);
1780 } else if (type == VECTOR4I) {
1781 return Vector2(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y);
1782 } else {
1783 return Vector2i();
1784 }
1785 }
1786
1787 Variant::operator Rect2() const {
1788 if (type == RECT2) {
1789 return *reinterpret_cast<const Rect2 *>(_data._mem);
1790 } else if (type == RECT2I) {
1791 return *reinterpret_cast<const Rect2i *>(_data._mem);
1792 } else {
1793 return Rect2();
1794 }
1795 }
1796
1797 Variant::operator Rect2i() const {
1798 if (type == RECT2I) {
1799 return *reinterpret_cast<const Rect2i *>(_data._mem);
1800 } else if (type == RECT2) {
1801 return *reinterpret_cast<const Rect2 *>(_data._mem);
1802 } else {
1803 return Rect2i();
1804 }
1805 }
1806
1807 Variant::operator Vector3() const {
1808 if (type == VECTOR3) {
1809 return *reinterpret_cast<const Vector3 *>(_data._mem);
1810 } else if (type == VECTOR3I) {
1811 return *reinterpret_cast<const Vector3i *>(_data._mem);
1812 } else if (type == VECTOR2) {
1813 return Vector3(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
1814 } else if (type == VECTOR2I) {
1815 return Vector3(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
1816 } else if (type == VECTOR4) {
1817 return Vector3(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
1818 } else if (type == VECTOR4I) {
1819 return Vector3(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
1820 } else {
1821 return Vector3();
1822 }
1823 }
1824
1825 Variant::operator Vector3i() const {
1826 if (type == VECTOR3I) {
1827 return *reinterpret_cast<const Vector3i *>(_data._mem);
1828 } else if (type == VECTOR3) {
1829 return *reinterpret_cast<const Vector3 *>(_data._mem);
1830 } else if (type == VECTOR2) {
1831 return Vector3i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0);
1832 } else if (type == VECTOR2I) {
1833 return Vector3i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0);
1834 } else if (type == VECTOR4) {
1835 return Vector3i(reinterpret_cast<const Vector4 *>(_data._mem)->x, reinterpret_cast<const Vector4 *>(_data._mem)->y, reinterpret_cast<const Vector4 *>(_data._mem)->z);
1836 } else if (type == VECTOR4I) {
1837 return Vector3i(reinterpret_cast<const Vector4i *>(_data._mem)->x, reinterpret_cast<const Vector4i *>(_data._mem)->y, reinterpret_cast<const Vector4i *>(_data._mem)->z);
1838 } else {
1839 return Vector3i();
1840 }
1841 }
1842
1843 Variant::operator Vector4() const {
1844 if (type == VECTOR4) {
1845 return *reinterpret_cast<const Vector4 *>(_data._mem);
1846 } else if (type == VECTOR4I) {
1847 return *reinterpret_cast<const Vector4i *>(_data._mem);
1848 } else if (type == VECTOR2) {
1849 return Vector4(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
1850 } else if (type == VECTOR2I) {
1851 return Vector4(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
1852 } else if (type == VECTOR3) {
1853 return Vector4(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y, reinterpret_cast<const Vector3 *>(_data._mem)->z, 0.0);
1854 } else if (type == VECTOR3I) {
1855 return Vector4(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y, reinterpret_cast<const Vector3i *>(_data._mem)->z, 0.0);
1856 } else {
1857 return Vector4();
1858 }
1859 }
1860
1861 Variant::operator Vector4i() const {
1862 if (type == VECTOR4I) {
1863 return *reinterpret_cast<const Vector4i *>(_data._mem);
1864 } else if (type == VECTOR4) {
1865 const Vector4 &v4 = *reinterpret_cast<const Vector4 *>(_data._mem);
1866 return Vector4i(v4.x, v4.y, v4.z, v4.w);
1867 } else if (type == VECTOR2) {
1868 return Vector4i(reinterpret_cast<const Vector2 *>(_data._mem)->x, reinterpret_cast<const Vector2 *>(_data._mem)->y, 0.0, 0.0);
1869 } else if (type == VECTOR2I) {
1870 return Vector4i(reinterpret_cast<const Vector2i *>(_data._mem)->x, reinterpret_cast<const Vector2i *>(_data._mem)->y, 0.0, 0.0);
1871 } else if (type == VECTOR3) {
1872 return Vector4i(reinterpret_cast<const Vector3 *>(_data._mem)->x, reinterpret_cast<const Vector3 *>(_data._mem)->y, reinterpret_cast<const Vector3 *>(_data._mem)->z, 0.0);
1873 } else if (type == VECTOR3I) {
1874 return Vector4i(reinterpret_cast<const Vector3i *>(_data._mem)->x, reinterpret_cast<const Vector3i *>(_data._mem)->y, reinterpret_cast<const Vector3i *>(_data._mem)->z, 0.0);
1875 } else {
1876 return Vector4i();
1877 }
1878 }
1879
1880 Variant::operator Plane() const {
1881 if (type == PLANE) {
1882 return *reinterpret_cast<const Plane *>(_data._mem);
1883 } else {
1884 return Plane();
1885 }
1886 }
1887
1888 Variant::operator ::AABB() const {
1889 if (type == AABB) {
1890 return *_data._aabb;
1891 } else {
1892 return ::AABB();
1893 }
1894 }
1895
1896 Variant::operator Basis() const {
1897 if (type == BASIS) {
1898 return *_data._basis;
1899 } else if (type == QUATERNION) {
1900 return *reinterpret_cast<const Quaternion *>(_data._mem);
1901 } else if (type == TRANSFORM3D) { // unexposed in Variant::can_convert?
1902 return _data._transform3d->basis;
1903 } else {
1904 return Basis();
1905 }
1906 }
1907
1908 Variant::operator Quaternion() const {
1909 if (type == QUATERNION) {
1910 return *reinterpret_cast<const Quaternion *>(_data._mem);
1911 } else if (type == BASIS) {
1912 return *_data._basis;
1913 } else if (type == TRANSFORM3D) {
1914 return _data._transform3d->basis;
1915 } else {
1916 return Quaternion();
1917 }
1918 }
1919
1920 Variant::operator Transform3D() const {
1921 if (type == TRANSFORM3D) {
1922 return *_data._transform3d;
1923 } else if (type == BASIS) {
1924 return Transform3D(*_data._basis, Vector3());
1925 } else if (type == QUATERNION) {
1926 return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
1927 } else if (type == TRANSFORM2D) {
1928 const Transform2D &t = *_data._transform2d;
1929 Transform3D m;
1930 m.basis.rows[0][0] = t.columns[0][0];
1931 m.basis.rows[1][0] = t.columns[0][1];
1932 m.basis.rows[0][1] = t.columns[1][0];
1933 m.basis.rows[1][1] = t.columns[1][1];
1934 m.origin[0] = t.columns[2][0];
1935 m.origin[1] = t.columns[2][1];
1936 return m;
1937 } else if (type == PROJECTION) {
1938 return *_data._projection;
1939 } else {
1940 return Transform3D();
1941 }
1942 }
1943
1944 Variant::operator Projection() const {
1945 if (type == TRANSFORM3D) {
1946 return *_data._transform3d;
1947 } else if (type == BASIS) {
1948 return Transform3D(*_data._basis, Vector3());
1949 } else if (type == QUATERNION) {
1950 return Transform3D(Basis(*reinterpret_cast<const Quaternion *>(_data._mem)), Vector3());
1951 } else if (type == TRANSFORM2D) {
1952 const Transform2D &t = *_data._transform2d;
1953 Transform3D m;
1954 m.basis.rows[0][0] = t.columns[0][0];
1955 m.basis.rows[1][0] = t.columns[0][1];
1956 m.basis.rows[0][1] = t.columns[1][0];
1957 m.basis.rows[1][1] = t.columns[1][1];
1958 m.origin[0] = t.columns[2][0];
1959 m.origin[1] = t.columns[2][1];
1960 return m;
1961 } else if (type == PROJECTION) {
1962 return *_data._projection;
1963 } else {
1964 return Projection();
1965 }
1966 }
1967
1968 Variant::operator Transform2D() const {
1969 if (type == TRANSFORM2D) {
1970 return *_data._transform2d;
1971 } else if (type == TRANSFORM3D) {
1972 const Transform3D &t = *_data._transform3d;
1973 Transform2D m;
1974 m.columns[0][0] = t.basis.rows[0][0];
1975 m.columns[0][1] = t.basis.rows[1][0];
1976 m.columns[1][0] = t.basis.rows[0][1];
1977 m.columns[1][1] = t.basis.rows[1][1];
1978 m.columns[2][0] = t.origin[0];
1979 m.columns[2][1] = t.origin[1];
1980 return m;
1981 } else {
1982 return Transform2D();
1983 }
1984 }
1985
1986 Variant::operator Color() const {
1987 if (type == COLOR) {
1988 return *reinterpret_cast<const Color *>(_data._mem);
1989 } else if (type == STRING) {
1990 return Color(operator String());
1991 } else if (type == INT) {
1992 return Color::hex(operator int());
1993 } else {
1994 return Color();
1995 }
1996 }
1997
1998 Variant::operator NodePath() const {
1999 if (type == NODE_PATH) {
2000 return *reinterpret_cast<const NodePath *>(_data._mem);
2001 } else if (type == STRING) {
2002 return NodePath(operator String());
2003 } else {
2004 return NodePath();
2005 }
2006 }
2007
2008 Variant::operator ::RID() const {
2009 if (type == RID) {
2010 return *reinterpret_cast<const ::RID *>(_data._mem);
2011 } else if (type == OBJECT && _get_obj().obj == nullptr) {
2012 return ::RID();
2013 } else if (type == OBJECT && _get_obj().obj) {
2014 #ifdef DEBUG_ENABLED
2015 if (EngineDebugger::is_active()) {
2016 ERR_FAIL_NULL_V_MSG(ObjectDB::get_instance(_get_obj().id), ::RID(), "Invalid pointer (object was freed).");
2017 }
2018 #endif
2019 Callable::CallError ce;
2020 const Variant ret = _get_obj().obj->callp(CoreStringName(get_rid), nullptr, 0, ce);
2021 if (ce.error == Callable::CallError::CALL_OK && ret.get_type() == Variant::RID) {
2022 return ret;
2023 }
2024 return ::RID();
2025 } else {
2026 return ::RID();
2027 }
2028 }
2029
2030 Variant::operator Object *() const {
2031 if (type == OBJECT) {
2032 return _get_obj().obj;
2033 } else {
2034 return nullptr;
2035 }
2036 }
2037
2038 Object *Variant::get_validated_object_with_check(bool &r_previously_freed) const {
2039 if (type == OBJECT) {
2040 Object *instance = ObjectDB::get_instance(_get_obj().id);
2041 r_previously_freed = !instance && _get_obj().id != ObjectID();
2042 return instance;
2043 } else {
2044 r_previously_freed = false;
2045 return nullptr;
2046 }
2047 }
2048
2049 Object *Variant::get_validated_object() const {
2050 if (type == OBJECT) {
2051 return ObjectDB::get_instance(_get_obj().id);
2052 } else {
2053 return nullptr;
2054 }
2055 }
2056
2057 Variant::operator Dictionary() const {
2058 if (type == DICTIONARY) {
2059 return *reinterpret_cast<const Dictionary *>(_data._mem);
2060 } else {
2061 return Dictionary();
2062 }
2063 }
2064
2065 Variant::operator Callable() const {
2066 if (type == CALLABLE) {
2067 return *reinterpret_cast<const Callable *>(_data._mem);
2068 } else {
2069 return Callable();
2070 }
2071 }
2072
2073 Variant::operator Signal() const {
2074 if (type == SIGNAL) {
2075 return *reinterpret_cast<const Signal *>(_data._mem);
2076 } else {
2077 return Signal();
2078 }
2079 }
2080
2081 template <typename DA, typename SA>
2082 inline DA _convert_array(const SA &p_array) {
2083 DA da;
2084 da.resize(p_array.size());
2085
2086 for (int i = 0; i < p_array.size(); i++) {
2087 da.set(i, Variant(p_array.get(i)));
2088 }
2089
2090 return da;
2091 }
2092
2093 template <typename DA>
2094 inline DA _convert_array_from_variant(const Variant &p_variant) {
2095 switch (p_variant.get_type()) {
2096 case Variant::ARRAY: {
2097 return _convert_array<DA, Array>(p_variant.operator Array());
2098 }
2099 case Variant::PACKED_BYTE_ARRAY: {
2100 return _convert_array<DA, PackedByteArray>(p_variant.operator PackedByteArray());
2101 }
2102 case Variant::PACKED_INT32_ARRAY: {
2103 return _convert_array<DA, PackedInt32Array>(p_variant.operator PackedInt32Array());
2104 }
2105 case Variant::PACKED_INT64_ARRAY: {
2106 return _convert_array<DA, PackedInt64Array>(p_variant.operator PackedInt64Array());
2107 }
2108 case Variant::PACKED_FLOAT32_ARRAY: {
2109 return _convert_array<DA, PackedFloat32Array>(p_variant.operator PackedFloat32Array());
2110 }
2111 case Variant::PACKED_FLOAT64_ARRAY: {
2112 return _convert_array<DA, PackedFloat64Array>(p_variant.operator PackedFloat64Array());
2113 }
2114 case Variant::PACKED_STRING_ARRAY: {
2115 return _convert_array<DA, PackedStringArray>(p_variant.operator PackedStringArray());
2116 }
2117 case Variant::PACKED_VECTOR2_ARRAY: {
2118 return _convert_array<DA, PackedVector2Array>(p_variant.operator PackedVector2Array());
2119 }
2120 case Variant::PACKED_VECTOR3_ARRAY: {
2121 return _convert_array<DA, PackedVector3Array>(p_variant.operator PackedVector3Array());
2122 }
2123 case Variant::PACKED_COLOR_ARRAY: {
2124 return _convert_array<DA, PackedColorArray>(p_variant.operator PackedColorArray());
2125 }
2126 case Variant::PACKED_VECTOR4_ARRAY: {
2127 return _convert_array<DA, PackedVector4Array>(p_variant.operator PackedVector4Array());
2128 }
2129 default: {
2130 return DA();
2131 }
2132 }
2133 }
2134
2135 Variant::operator Array() const {
2136 if (type == ARRAY) {
2137 return *reinterpret_cast<const Array *>(_data._mem);
2138 } else {
2139 return _convert_array_from_variant<Array>(*this);
2140 }
2141 }
2142
2143 Variant::operator PackedByteArray() const {
2144 if (type == PACKED_BYTE_ARRAY) {
2145 return static_cast<PackedArrayRef<uint8_t> *>(_data.packed_array)->array;
2146 } else {
2147 return _convert_array_from_variant<PackedByteArray>(*this);
2148 }
2149 }
2150
2151 Variant::operator PackedInt32Array() const {
2152 if (type == PACKED_INT32_ARRAY) {
2153 return static_cast<PackedArrayRef<int32_t> *>(_data.packed_array)->array;
2154 } else {
2155 return _convert_array_from_variant<PackedInt32Array>(*this);
2156 }
2157 }
2158
2159 Variant::operator PackedInt64Array() const {
2160 if (type == PACKED_INT64_ARRAY) {
2161 return static_cast<PackedArrayRef<int64_t> *>(_data.packed_array)->array;
2162 } else {
2163 return _convert_array_from_variant<PackedInt64Array>(*this);
2164 }
2165 }
2166
2167 Variant::operator PackedFloat32Array() const {
2168 if (type == PACKED_FLOAT32_ARRAY) {
2169 return static_cast<PackedArrayRef<float> *>(_data.packed_array)->array;
2170 } else {
2171 return _convert_array_from_variant<PackedFloat32Array>(*this);
2172 }
2173 }
2174
2175 Variant::operator PackedFloat64Array() const {
2176 if (type == PACKED_FLOAT64_ARRAY) {
2177 return static_cast<PackedArrayRef<double> *>(_data.packed_array)->array;
2178 } else {
2179 return _convert_array_from_variant<PackedFloat64Array>(*this);
2180 }
2181 }
2182
2183 Variant::operator PackedStringArray() const {
2184 if (type == PACKED_STRING_ARRAY) {
2185 return static_cast<PackedArrayRef<String> *>(_data.packed_array)->array;
2186 } else {
2187 return _convert_array_from_variant<PackedStringArray>(*this);
2188 }
2189 }
2190
2191 Variant::operator PackedVector2Array() const {
2192 if (type == PACKED_VECTOR2_ARRAY) {
2193 return static_cast<PackedArrayRef<Vector2> *>(_data.packed_array)->array;
2194 } else {
2195 return _convert_array_from_variant<PackedVector2Array>(*this);
2196 }
2197 }
2198
2199 Variant::operator PackedVector3Array() const {
2200 if (type == PACKED_VECTOR3_ARRAY) {
2201 return static_cast<PackedArrayRef<Vector3> *>(_data.packed_array)->array;
2202 } else {
2203 return _convert_array_from_variant<PackedVector3Array>(*this);
2204 }
2205 }
2206
2207 Variant::operator PackedColorArray() const {
2208 if (type == PACKED_COLOR_ARRAY) {
2209 return static_cast<PackedArrayRef<Color> *>(_data.packed_array)->array;
2210 } else {
2211 return _convert_array_from_variant<PackedColorArray>(*this);
2212 }
2213 }
2214
2215 Variant::operator PackedVector4Array() const {
2216 if (type == PACKED_VECTOR4_ARRAY) {
2217 return static_cast<PackedArrayRef<Vector4> *>(_data.packed_array)->array;
2218 } else {
2219 return _convert_array_from_variant<PackedVector4Array>(*this);
2220 }
2221 }
2222
2223 /* helpers */
2224
2225 Variant::operator Vector<::RID>() const {
2226 Array va = operator Array();
2227 Vector<::RID> rids;
2228 rids.resize(va.size());
2229 for (int i = 0; i < rids.size(); i++) {
2230 rids.write[i] = va[i];
2231 }
2232 return rids;
2233 }
2234
2235 Variant::operator Vector<Plane>() const {
2236 Array va = operator Array();
2237 Vector<Plane> planes;
2238 int va_size = va.size();
2239 if (va_size == 0) {
2240 return planes;
2241 }
2242
2243 planes.resize(va_size);
2244 Plane *w = planes.ptrw();
2245
2246 for (int i = 0; i < va_size; i++) {
2247 w[i] = va[i];
2248 }
2249
2250 return planes;
2251 }
2252
2253 Variant::operator Vector<Face3>() const {
2254 PackedVector3Array va = operator PackedVector3Array();
2255 Vector<Face3> faces;
2256 int va_size = va.size();
2257 if (va_size == 0) {
2258 return faces;
2259 }
2260
2261 faces.resize(va_size / 3);
2262 Face3 *w = faces.ptrw();
2263 const Vector3 *r = va.ptr();
2264
2265 for (int i = 0; i < va_size; i++) {
2266 w[i / 3].vertex[i % 3] = r[i];
2267 }
2268
2269 return faces;
2270 }
2271
2272 Variant::operator Vector<Variant>() const {
2273 Array va = operator Array();
2274 Vector<Variant> variants;
2275 int va_size = va.size();
2276 if (va_size == 0) {
2277 return variants;
2278 }
2279
2280 variants.resize(va_size);
2281 Variant *w = variants.ptrw();
2282 for (int i = 0; i < va_size; i++) {
2283 w[i] = va[i];
2284 }
2285
2286 return variants;
2287 }
2288
2289 Variant::operator Vector<StringName>() const {
2290 PackedStringArray from = operator PackedStringArray();
2291 Vector<StringName> to;
2292 int len = from.size();
2293 to.resize(len);
2294 for (int i = 0; i < len; i++) {
2295 to.write[i] = from[i];
2296 }
2297 return to;
2298 }
2299
2300 Variant::operator IPAddress() const {
2301 if (type == PACKED_FLOAT32_ARRAY || type == PACKED_INT32_ARRAY || type == PACKED_FLOAT64_ARRAY || type == PACKED_INT64_ARRAY || type == PACKED_BYTE_ARRAY) {
2302 Vector<int> addr = operator Vector<int>();
2303 if (addr.size() == 4) {
2304 return IPAddress(addr.get(0), addr.get(1), addr.get(2), addr.get(3));
2305 }
2306 }
2307
2308 return IPAddress(operator String());
2309 }
2310
2311 Variant::Variant(bool p_bool) :
2312 type(BOOL) {
2313 _data._bool = p_bool;
2314 }
2315
2316 Variant::Variant(int64_t p_int64) :
2317 type(INT) {
2318 _data._int = p_int64;
2319 }
2320
2321 Variant::Variant(int32_t p_int32) :
2322 type(INT) {
2323 _data._int = p_int32;
2324 }
2325
2326 Variant::Variant(int16_t p_int16) :
2327 type(INT) {
2328 _data._int = p_int16;
2329 }
2330
2331 Variant::Variant(int8_t p_int8) :
2332 type(INT) {
2333 _data._int = p_int8;
2334 }
2335
2336 Variant::Variant(uint64_t p_uint64) :
2337 type(INT) {
2338 _data._int = int64_t(p_uint64);
2339 }
2340
2341 Variant::Variant(uint32_t p_uint32) :
2342 type(INT) {
2343 _data._int = int64_t(p_uint32);
2344 }
2345
2346 Variant::Variant(uint16_t p_uint16) :
2347 type(INT) {
2348 _data._int = int64_t(p_uint16);
2349 }
2350
2351 Variant::Variant(uint8_t p_uint8) :
2352 type(INT) {
2353 _data._int = int64_t(p_uint8);
2354 }
2355
2356 Variant::Variant(float p_float) :
2357 type(FLOAT) {
2358 _data._float = p_float;
2359 }
2360
2361 Variant::Variant(double p_double) :
2362 type(FLOAT) {
2363 _data._float = p_double;
2364 }
2365
2366 Variant::Variant(const ObjectID &p_id) :
2367 type(INT) {
2368 _data._int = int64_t(p_id);
2369 }
2370
2371 Variant::Variant(const StringName &p_string) :
2372 type(STRING_NAME) {
2373 memnew_placement(_data._mem, StringName(p_string));
2374 static_assert(sizeof(StringName) <= sizeof(_data._mem));
2375 }
2376
2377 Variant::Variant(const String &p_string) :
2378 type(STRING) {
2379 memnew_placement(_data._mem, String(p_string));
2380 static_assert(sizeof(String) <= sizeof(_data._mem));
2381 }
2382
2383 Variant::Variant(const char *const p_cstring) :
2384 type(STRING) {
2385 memnew_placement(_data._mem, String((const char *)p_cstring));
2386 static_assert(sizeof(String) <= sizeof(_data._mem));
2387 }
2388
2389 Variant::Variant(const char32_t *p_wstring) :
2390 type(STRING) {
2391 memnew_placement(_data._mem, String(p_wstring));
2392 static_assert(sizeof(String) <= sizeof(_data._mem));
2393 }
2394
2395 Variant::Variant(const Vector3 &p_vector3) :
2396 type(VECTOR3) {
2397 memnew_placement(_data._mem, Vector3(p_vector3));
2398 static_assert(sizeof(Vector3) <= sizeof(_data._mem));
2399 }
2400
2401 Variant::Variant(const Vector3i &p_vector3i) :
2402 type(VECTOR3I) {
2403 memnew_placement(_data._mem, Vector3i(p_vector3i));
2404 static_assert(sizeof(Vector3i) <= sizeof(_data._mem));
2405 }
2406
2407 Variant::Variant(const Vector4 &p_vector4) :
2408 type(VECTOR4) {
2409 memnew_placement(_data._mem, Vector4(p_vector4));
2410 static_assert(sizeof(Vector4) <= sizeof(_data._mem));
2411 }
2412
2413 Variant::Variant(const Vector4i &p_vector4i) :
2414 type(VECTOR4I) {
2415 memnew_placement(_data._mem, Vector4i(p_vector4i));
2416 static_assert(sizeof(Vector4i) <= sizeof(_data._mem));
2417 }
2418
2419 Variant::Variant(const Vector2 &p_vector2) :
2420 type(VECTOR2) {
2421 memnew_placement(_data._mem, Vector2(p_vector2));
2422 static_assert(sizeof(Vector2) <= sizeof(_data._mem));
2423 }
2424
2425 Variant::Variant(const Vector2i &p_vector2i) :
2426 type(VECTOR2I) {
2427 memnew_placement(_data._mem, Vector2i(p_vector2i));
2428 static_assert(sizeof(Vector2i) <= sizeof(_data._mem));
2429 }
2430
2431 Variant::Variant(const Rect2 &p_rect2) :
2432 type(RECT2) {
2433 memnew_placement(_data._mem, Rect2(p_rect2));
2434 static_assert(sizeof(Rect2) <= sizeof(_data._mem));
2435 }
2436
2437 Variant::Variant(const Rect2i &p_rect2i) :
2438 type(RECT2I) {
2439 memnew_placement(_data._mem, Rect2i(p_rect2i));
2440 static_assert(sizeof(Rect2i) <= sizeof(_data._mem));
2441 }
2442
2443 Variant::Variant(const Plane &p_plane) :
2444 type(PLANE) {
2445 memnew_placement(_data._mem, Plane(p_plane));
2446 static_assert(sizeof(Plane) <= sizeof(_data._mem));
2447 }
2448
2449 Variant::Variant(const ::AABB &p_aabb) :
2450 type(AABB) {
2451 _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
2452 memnew_placement(_data._aabb, ::AABB(p_aabb));
2453 }
2454
2455 Variant::Variant(const Basis &p_matrix) :
2456 type(BASIS) {
2457 _data._basis = (Basis *)Pools::_bucket_medium.alloc();
2458 memnew_placement(_data._basis, Basis(p_matrix));
2459 }
2460
2461 Variant::Variant(const Quaternion &p_quaternion) :
2462 type(QUATERNION) {
2463 memnew_placement(_data._mem, Quaternion(p_quaternion));
2464 static_assert(sizeof(Quaternion) <= sizeof(_data._mem));
2465 }
2466
2467 Variant::Variant(const Transform3D &p_transform) :
2468 type(TRANSFORM3D) {
2469 _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
2470 memnew_placement(_data._transform3d, Transform3D(p_transform));
2471 }
2472
2473 Variant::Variant(const Projection &pp_projection) :
2474 type(PROJECTION) {
2475 _data._projection = (Projection *)Pools::_bucket_large.alloc();
2476 memnew_placement(_data._projection, Projection(pp_projection));
2477 }
2478
2479 Variant::Variant(const Transform2D &p_transform) :
2480 type(TRANSFORM2D) {
2481 _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
2482 memnew_placement(_data._transform2d, Transform2D(p_transform));
2483 }
2484
2485 Variant::Variant(const Color &p_color) :
2486 type(COLOR) {
2487 memnew_placement(_data._mem, Color(p_color));
2488 static_assert(sizeof(Color) <= sizeof(_data._mem));
2489 }
2490
2491 Variant::Variant(const NodePath &p_node_path) :
2492 type(NODE_PATH) {
2493 memnew_placement(_data._mem, NodePath(p_node_path));
2494 static_assert(sizeof(NodePath) <= sizeof(_data._mem));
2495 }
2496
2497 Variant::Variant(const ::RID &p_rid) :
2498 type(RID) {
2499 memnew_placement(_data._mem, ::RID(p_rid));
2500 static_assert(sizeof(::RID) <= sizeof(_data._mem));
2501 }
2502
2503 Variant::Variant(const Object *p_object) :
2504 type(OBJECT) {
2505 _get_obj() = ObjData();
2506 _get_obj().ref_pointer(const_cast<Object *>(p_object));
2507 }
2508
2509 Variant::Variant(const Callable &p_callable) :
2510 type(CALLABLE) {
2511 memnew_placement(_data._mem, Callable(p_callable));
2512 static_assert(sizeof(Callable) <= sizeof(_data._mem));
2513 }
2514
2515 Variant::Variant(const Signal &p_callable) :
2516 type(SIGNAL) {
2517 memnew_placement(_data._mem, Signal(p_callable));
2518 static_assert(sizeof(Signal) <= sizeof(_data._mem));
2519 }
2520
2521 Variant::Variant(const Dictionary &p_dictionary) :
2522 type(DICTIONARY) {
2523 memnew_placement(_data._mem, Dictionary(p_dictionary));
2524 static_assert(sizeof(Dictionary) <= sizeof(_data._mem));
2525 }
2526
2527 Variant::Variant(std::initializer_list<Variant> p_init) :
2528 type(ARRAY) {
2529 memnew_placement(_data._mem, Array(p_init));
2530 }
2531
2532 Variant::Variant(const Array &p_array) :
2533 type(ARRAY) {
2534 memnew_placement(_data._mem, Array(p_array));
2535 static_assert(sizeof(Array) <= sizeof(_data._mem));
2536 }
2537
2538 Variant::Variant(const PackedByteArray &p_byte_array) :
2539 type(PACKED_BYTE_ARRAY) {
2540 _data.packed_array = PackedArrayRef<uint8_t>::create(p_byte_array);
2541 }
2542
2543 Variant::Variant(const PackedInt32Array &p_int32_array) :
2544 type(PACKED_INT32_ARRAY) {
2545 _data.packed_array = PackedArrayRef<int32_t>::create(p_int32_array);
2546 }
2547
2548 Variant::Variant(const PackedInt64Array &p_int64_array) :
2549 type(PACKED_INT64_ARRAY) {
2550 _data.packed_array = PackedArrayRef<int64_t>::create(p_int64_array);
2551 }
2552
2553 Variant::Variant(const PackedFloat32Array &p_float32_array) :
2554 type(PACKED_FLOAT32_ARRAY) {
2555 _data.packed_array = PackedArrayRef<float>::create(p_float32_array);
2556 }
2557
2558 Variant::Variant(const PackedFloat64Array &p_float64_array) :
2559 type(PACKED_FLOAT64_ARRAY) {
2560 _data.packed_array = PackedArrayRef<double>::create(p_float64_array);
2561 }
2562
2563 Variant::Variant(const PackedStringArray &p_string_array) :
2564 type(PACKED_STRING_ARRAY) {
2565 _data.packed_array = PackedArrayRef<String>::create(p_string_array);
2566 }
2567
2568 Variant::Variant(const PackedVector2Array &p_vector2_array) :
2569 type(PACKED_VECTOR2_ARRAY) {
2570 _data.packed_array = PackedArrayRef<Vector2>::create(p_vector2_array);
2571 }
2572
2573 Variant::Variant(const PackedVector3Array &p_vector3_array) :
2574 type(PACKED_VECTOR3_ARRAY) {
2575 _data.packed_array = PackedArrayRef<Vector3>::create(p_vector3_array);
2576 }
2577
2578 Variant::Variant(const PackedColorArray &p_color_array) :
2579 type(PACKED_COLOR_ARRAY) {
2580 _data.packed_array = PackedArrayRef<Color>::create(p_color_array);
2581 }
2582
2583 Variant::Variant(const PackedVector4Array &p_vector4_array) :
2584 type(PACKED_VECTOR4_ARRAY) {
2585 _data.packed_array = PackedArrayRef<Vector4>::create(p_vector4_array);
2586 }
2587
2588 /* helpers */
2589 Variant::Variant(const Vector<::RID> &p_array) :
2590 type(ARRAY) {
2591 Array *rid_array = memnew_placement(_data._mem, Array);
2592
2593 rid_array->resize(p_array.size());
2594
2595 for (int i = 0; i < p_array.size(); i++) {
2596 rid_array->set(i, Variant(p_array[i]));
2597 }
2598 }
2599
2600 Variant::Variant(const Vector<Plane> &p_array) :
2601 type(ARRAY) {
2602 Array *plane_array = memnew_placement(_data._mem, Array);
2603
2604 plane_array->resize(p_array.size());
2605
2606 for (int i = 0; i < p_array.size(); i++) {
2607 plane_array->operator[](i) = Variant(p_array[i]);
2608 }
2609 }
2610
2611 Variant::Variant(const Vector<Face3> &p_face_array) {
2612 PackedVector3Array vertices;
2613 int face_count = p_face_array.size();
2614 vertices.resize(face_count * 3);
2615
2616 if (face_count) {
2617 const Face3 *r = p_face_array.ptr();
2618 Vector3 *w = vertices.ptrw();
2619
2620 for (int i = 0; i < face_count; i++) {
2621 for (int j = 0; j < 3; j++) {
2622 w[i * 3 + j] = r[i].vertex[j];
2623 }
2624 }
2625 }
2626
2627 *this = vertices;
2628 }
2629
2630 Variant::Variant(const Vector<Variant> &p_array) {
2631 Array arr;
2632 arr.resize(p_array.size());
2633 for (int i = 0; i < p_array.size(); i++) {
2634 arr[i] = p_array[i];
2635 }
2636 *this = arr;
2637 }
2638
2639 Variant::Variant(const Vector<StringName> &p_array) {
2640 PackedStringArray v;
2641 int len = p_array.size();
2642 v.resize(len);
2643 for (int i = 0; i < len; i++) {
2644 v.set(i, p_array[i]);
2645 }
2646 *this = v;
2647 }
2648
2649 void Variant::operator=(const Variant &p_variant) {
2650 if (unlikely(this == &p_variant)) {
2651 return;
2652 }
2653
2654 if (unlikely(type != p_variant.type)) {
2655 reference(p_variant);
2656 return;
2657 }
2658
2659 switch (p_variant.type) {
2660 case NIL: {
2661 // none
2662 } break;
2663
2664 // atomic types
2665 case BOOL: {
2666 _data._bool = p_variant._data._bool;
2667 } break;
2668 case INT: {
2669 _data._int = p_variant._data._int;
2670 } break;
2671 case FLOAT: {
2672 _data._float = p_variant._data._float;
2673 } break;
2674 case STRING: {
2675 *reinterpret_cast<String *>(_data._mem) = *reinterpret_cast<const String *>(p_variant._data._mem);
2676 } break;
2677
2678 // math types
2679 case VECTOR2: {
2680 *reinterpret_cast<Vector2 *>(_data._mem) = *reinterpret_cast<const Vector2 *>(p_variant._data._mem);
2681 } break;
2682 case VECTOR2I: {
2683 *reinterpret_cast<Vector2i *>(_data._mem) = *reinterpret_cast<const Vector2i *>(p_variant._data._mem);
2684 } break;
2685 case RECT2: {
2686 *reinterpret_cast<Rect2 *>(_data._mem) = *reinterpret_cast<const Rect2 *>(p_variant._data._mem);
2687 } break;
2688 case RECT2I: {
2689 *reinterpret_cast<Rect2i *>(_data._mem) = *reinterpret_cast<const Rect2i *>(p_variant._data._mem);
2690 } break;
2691 case TRANSFORM2D: {
2692 *_data._transform2d = *(p_variant._data._transform2d);
2693 } break;
2694 case VECTOR3: {
2695 *reinterpret_cast<Vector3 *>(_data._mem) = *reinterpret_cast<const Vector3 *>(p_variant._data._mem);
2696 } break;
2697 case VECTOR3I: {
2698 *reinterpret_cast<Vector3i *>(_data._mem) = *reinterpret_cast<const Vector3i *>(p_variant._data._mem);
2699 } break;
2700 case VECTOR4: {
2701 *reinterpret_cast<Vector4 *>(_data._mem) = *reinterpret_cast<const Vector4 *>(p_variant._data._mem);
2702 } break;
2703 case VECTOR4I: {
2704 *reinterpret_cast<Vector4i *>(_data._mem) = *reinterpret_cast<const Vector4i *>(p_variant._data._mem);
2705 } break;
2706 case PLANE: {
2707 *reinterpret_cast<Plane *>(_data._mem) = *reinterpret_cast<const Plane *>(p_variant._data._mem);
2708 } break;
2709
2710 case AABB: {
2711 *_data._aabb = *(p_variant._data._aabb);
2712 } break;
2713 case QUATERNION: {
2714 *reinterpret_cast<Quaternion *>(_data._mem) = *reinterpret_cast<const Quaternion *>(p_variant._data._mem);
2715 } break;
2716 case BASIS: {
2717 *_data._basis = *(p_variant._data._basis);
2718 } break;
2719 case TRANSFORM3D: {
2720 *_data._transform3d = *(p_variant._data._transform3d);
2721 } break;
2722 case PROJECTION: {
2723 *_data._projection = *(p_variant._data._projection);
2724 } break;
2725
2726 // misc types
2727 case COLOR: {
2728 *reinterpret_cast<Color *>(_data._mem) = *reinterpret_cast<const Color *>(p_variant._data._mem);
2729 } break;
2730 case RID: {
2731 *reinterpret_cast<::RID *>(_data._mem) = *reinterpret_cast<const ::RID *>(p_variant._data._mem);
2732 } break;
2733 case OBJECT: {
2734 _get_obj().ref(p_variant._get_obj());
2735 } break;
2736 case CALLABLE: {
2737 *reinterpret_cast<Callable *>(_data._mem) = *reinterpret_cast<const Callable *>(p_variant._data._mem);
2738 } break;
2739 case SIGNAL: {
2740 *reinterpret_cast<Signal *>(_data._mem) = *reinterpret_cast<const Signal *>(p_variant._data._mem);
2741 } break;
2742
2743 case STRING_NAME: {
2744 *reinterpret_cast<StringName *>(_data._mem) = *reinterpret_cast<const StringName *>(p_variant._data._mem);
2745 } break;
2746 case NODE_PATH: {
2747 *reinterpret_cast<NodePath *>(_data._mem) = *reinterpret_cast<const NodePath *>(p_variant._data._mem);
2748 } break;
2749 case DICTIONARY: {
2750 *reinterpret_cast<Dictionary *>(_data._mem) = *reinterpret_cast<const Dictionary *>(p_variant._data._mem);
2751 } break;
2752 case ARRAY: {
2753 *reinterpret_cast<Array *>(_data._mem) = *reinterpret_cast<const Array *>(p_variant._data._mem);
2754 } break;
2755
2756 // arrays
2757 case PACKED_BYTE_ARRAY: {
2758 _data.packed_array = PackedArrayRef<uint8_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
2759 } break;
2760 case PACKED_INT32_ARRAY: {
2761 _data.packed_array = PackedArrayRef<int32_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
2762 } break;
2763 case PACKED_INT64_ARRAY: {
2764 _data.packed_array = PackedArrayRef<int64_t>::reference_from(_data.packed_array, p_variant._data.packed_array);
2765 } break;
2766 case PACKED_FLOAT32_ARRAY: {
2767 _data.packed_array = PackedArrayRef<float>::reference_from(_data.packed_array, p_variant._data.packed_array);
2768 } break;
2769 case PACKED_FLOAT64_ARRAY: {
2770 _data.packed_array = PackedArrayRef<double>::reference_from(_data.packed_array, p_variant._data.packed_array);
2771 } break;
2772 case PACKED_STRING_ARRAY: {
2773 _data.packed_array = PackedArrayRef<String>::reference_from(_data.packed_array, p_variant._data.packed_array);
2774 } break;
2775 case PACKED_VECTOR2_ARRAY: {
2776 _data.packed_array = PackedArrayRef<Vector2>::reference_from(_data.packed_array, p_variant._data.packed_array);
2777 } break;
2778 case PACKED_VECTOR3_ARRAY: {
2779 _data.packed_array = PackedArrayRef<Vector3>::reference_from(_data.packed_array, p_variant._data.packed_array);
2780 } break;
2781 case PACKED_COLOR_ARRAY: {
2782 _data.packed_array = PackedArrayRef<Color>::reference_from(_data.packed_array, p_variant._data.packed_array);
2783 } break;
2784 case PACKED_VECTOR4_ARRAY: {
2785 _data.packed_array = PackedArrayRef<Vector4>::reference_from(_data.packed_array, p_variant._data.packed_array);
2786 } break;
2787 default: {
2788 }
2789 }
2790 }
2791
2792 Variant::Variant(const IPAddress &p_address) :
2793 type(STRING) {
2794 memnew_placement(_data._mem, String(p_address));
2795 }
2796
2797 Variant::Variant(const Variant &p_variant) {
2798 reference(p_variant);
2799 }
2800
2801 uint32_t Variant::hash() const {
2802 return recursive_hash(0);
2803 }
2804
2805 uint32_t Variant::recursive_hash(int recursion_count) const {
2806 switch (type) {
2807 case NIL: {
2808 return 0;
2809 } break;
2810 case BOOL: {
2811 return _data._bool ? 1 : 0;
2812 } break;
2813 case INT: {
2814 return hash_one_uint64((uint64_t)_data._int);
2815 } break;
2816 case FLOAT: {
2817 return hash_murmur3_one_double(_data._float);
2818 } break;
2819 case STRING: {
2820 return reinterpret_cast<const String *>(_data._mem)->hash();
2821 } break;
2822
2823 // math types
2824 case VECTOR2: {
2825 return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2 *>(_data._mem));
2826 } break;
2827 case VECTOR2I: {
2828 return HashMapHasherDefault::hash(*reinterpret_cast<const Vector2i *>(_data._mem));
2829 } break;
2830 case RECT2: {
2831 return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2 *>(_data._mem));
2832 } break;
2833 case RECT2I: {
2834 return HashMapHasherDefault::hash(*reinterpret_cast<const Rect2i *>(_data._mem));
2835 } break;
2836 case TRANSFORM2D: {
2837 uint32_t h = HASH_MURMUR3_SEED;
2838 const Transform2D &t = *_data._transform2d;
2839 h = hash_murmur3_one_real(t[0].x, h);
2840 h = hash_murmur3_one_real(t[0].y, h);
2841 h = hash_murmur3_one_real(t[1].x, h);
2842 h = hash_murmur3_one_real(t[1].y, h);
2843 h = hash_murmur3_one_real(t[2].x, h);
2844 h = hash_murmur3_one_real(t[2].y, h);
2845
2846 return hash_fmix32(h);
2847 } break;
2848 case VECTOR3: {
2849 return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3 *>(_data._mem));
2850 } break;
2851 case VECTOR3I: {
2852 return HashMapHasherDefault::hash(*reinterpret_cast<const Vector3i *>(_data._mem));
2853 } break;
2854 case VECTOR4: {
2855 return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4 *>(_data._mem));
2856 } break;
2857 case VECTOR4I: {
2858 return HashMapHasherDefault::hash(*reinterpret_cast<const Vector4i *>(_data._mem));
2859 } break;
2860 case PLANE: {
2861 uint32_t h = HASH_MURMUR3_SEED;
2862 const Plane &p = *reinterpret_cast<const Plane *>(_data._mem);
2863 h = hash_murmur3_one_real(p.normal.x, h);
2864 h = hash_murmur3_one_real(p.normal.y, h);
2865 h = hash_murmur3_one_real(p.normal.z, h);
2866 h = hash_murmur3_one_real(p.d, h);
2867 return hash_fmix32(h);
2868 } break;
2869 case AABB: {
2870 return HashMapHasherDefault::hash(*_data._aabb);
2871 } break;
2872 case QUATERNION: {
2873 uint32_t h = HASH_MURMUR3_SEED;
2874 const Quaternion &q = *reinterpret_cast<const Quaternion *>(_data._mem);
2875 h = hash_murmur3_one_real(q.x, h);
2876 h = hash_murmur3_one_real(q.y, h);
2877 h = hash_murmur3_one_real(q.z, h);
2878 h = hash_murmur3_one_real(q.w, h);
2879 return hash_fmix32(h);
2880 } break;
2881 case BASIS: {
2882 uint32_t h = HASH_MURMUR3_SEED;
2883 const Basis &b = *_data._basis;
2884 h = hash_murmur3_one_real(b[0].x, h);
2885 h = hash_murmur3_one_real(b[0].y, h);
2886 h = hash_murmur3_one_real(b[0].z, h);
2887 h = hash_murmur3_one_real(b[1].x, h);
2888 h = hash_murmur3_one_real(b[1].y, h);
2889 h = hash_murmur3_one_real(b[1].z, h);
2890 h = hash_murmur3_one_real(b[2].x, h);
2891 h = hash_murmur3_one_real(b[2].y, h);
2892 h = hash_murmur3_one_real(b[2].z, h);
2893 return hash_fmix32(h);
2894 } break;
2895 case TRANSFORM3D: {
2896 uint32_t h = HASH_MURMUR3_SEED;
2897 const Transform3D &t = *_data._transform3d;
2898 h = hash_murmur3_one_real(t.basis[0].x, h);
2899 h = hash_murmur3_one_real(t.basis[0].y, h);
2900 h = hash_murmur3_one_real(t.basis[0].z, h);
2901 h = hash_murmur3_one_real(t.basis[1].x, h);
2902 h = hash_murmur3_one_real(t.basis[1].y, h);
2903 h = hash_murmur3_one_real(t.basis[1].z, h);
2904 h = hash_murmur3_one_real(t.basis[2].x, h);
2905 h = hash_murmur3_one_real(t.basis[2].y, h);
2906 h = hash_murmur3_one_real(t.basis[2].z, h);
2907 h = hash_murmur3_one_real(t.origin.x, h);
2908 h = hash_murmur3_one_real(t.origin.y, h);
2909 h = hash_murmur3_one_real(t.origin.z, h);
2910 return hash_fmix32(h);
2911 } break;
2912 case PROJECTION: {
2913 uint32_t h = HASH_MURMUR3_SEED;
2914 const Projection &t = *_data._projection;
2915 h = hash_murmur3_one_real(t.columns[0].x, h);
2916 h = hash_murmur3_one_real(t.columns[0].y, h);
2917 h = hash_murmur3_one_real(t.columns[0].z, h);
2918 h = hash_murmur3_one_real(t.columns[0].w, h);
2919 h = hash_murmur3_one_real(t.columns[1].x, h);
2920 h = hash_murmur3_one_real(t.columns[1].y, h);
2921 h = hash_murmur3_one_real(t.columns[1].z, h);
2922 h = hash_murmur3_one_real(t.columns[1].w, h);
2923 h = hash_murmur3_one_real(t.columns[2].x, h);
2924 h = hash_murmur3_one_real(t.columns[2].y, h);
2925 h = hash_murmur3_one_real(t.columns[2].z, h);
2926 h = hash_murmur3_one_real(t.columns[2].w, h);
2927 h = hash_murmur3_one_real(t.columns[3].x, h);
2928 h = hash_murmur3_one_real(t.columns[3].y, h);
2929 h = hash_murmur3_one_real(t.columns[3].z, h);
2930 h = hash_murmur3_one_real(t.columns[3].w, h);
2931 return hash_fmix32(h);
2932 } break;
2933 // misc types
2934 case COLOR: {
2935 uint32_t h = HASH_MURMUR3_SEED;
2936 const Color &c = *reinterpret_cast<const Color *>(_data._mem);
2937 h = hash_murmur3_one_float(c.r, h);
2938 h = hash_murmur3_one_float(c.g, h);
2939 h = hash_murmur3_one_float(c.b, h);
2940 h = hash_murmur3_one_float(c.a, h);
2941 return hash_fmix32(h);
2942 } break;
2943 case RID: {
2944 return hash_one_uint64(reinterpret_cast<const ::RID *>(_data._mem)->get_id());
2945 } break;
2946 case OBJECT: {
2947 return hash_one_uint64(reinterpret_cast<uint64_t>(_get_obj().obj));
2948 } break;
2949 case STRING_NAME: {
2950 return reinterpret_cast<const StringName *>(_data._mem)->hash();
2951 } break;
2952 case NODE_PATH: {
2953 return reinterpret_cast<const NodePath *>(_data._mem)->hash();
2954 } break;
2955 case DICTIONARY: {
2956 return reinterpret_cast<const Dictionary *>(_data._mem)->recursive_hash(recursion_count);
2957
2958 } break;
2959 case CALLABLE: {
2960 return reinterpret_cast<const Callable *>(_data._mem)->hash();
2961
2962 } break;
2963 case SIGNAL: {
2964 const Signal &s = *reinterpret_cast<const Signal *>(_data._mem);
2965 uint32_t hash = s.get_name().hash();
2966 return hash_murmur3_one_64(s.get_object_id(), hash);
2967 } break;
2968 case ARRAY: {
2969 const Array &arr = *reinterpret_cast<const Array *>(_data._mem);
2970 return arr.recursive_hash(recursion_count);
2971
2972 } break;
2973 case PACKED_BYTE_ARRAY: {
2974 const PackedByteArray &arr = PackedArrayRef<uint8_t>::get_array(_data.packed_array);
2975 int len = arr.size();
2976 if (likely(len)) {
2977 const uint8_t *r = arr.ptr();
2978 return hash_murmur3_buffer((uint8_t *)&r[0], len);
2979 } else {
2980 return hash_murmur3_one_64(0);
2981 }
2982
2983 } break;
2984 case PACKED_INT32_ARRAY: {
2985 const PackedInt32Array &arr = PackedArrayRef<int32_t>::get_array(_data.packed_array);
2986 int len = arr.size();
2987 if (likely(len)) {
2988 const int32_t *r = arr.ptr();
2989 return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int32_t));
2990 } else {
2991 return hash_murmur3_one_64(0);
2992 }
2993
2994 } break;
2995 case PACKED_INT64_ARRAY: {
2996 const PackedInt64Array &arr = PackedArrayRef<int64_t>::get_array(_data.packed_array);
2997 int len = arr.size();
2998 if (likely(len)) {
2999 const int64_t *r = arr.ptr();
3000 return hash_murmur3_buffer((uint8_t *)&r[0], len * sizeof(int64_t));
3001 } else {
3002 return hash_murmur3_one_64(0);
3003 }
3004
3005 } break;
3006 case PACKED_FLOAT32_ARRAY: {
3007 const PackedFloat32Array &arr = PackedArrayRef<float>::get_array(_data.packed_array);
3008 int len = arr.size();
3009
3010 if (likely(len)) {
3011 const float *r = arr.ptr();
3012 uint32_t h = HASH_MURMUR3_SEED;
3013 for (int32_t i = 0; i < len; i++) {
3014 h = hash_murmur3_one_float(r[i], h);
3015 }
3016 return hash_fmix32(h);
3017 } else {
3018 return hash_murmur3_one_float(0.0);
3019 }
3020
3021 } break;
3022 case PACKED_FLOAT64_ARRAY: {
3023 const PackedFloat64Array &arr = PackedArrayRef<double>::get_array(_data.packed_array);
3024 int len = arr.size();
3025
3026 if (likely(len)) {
3027 const double *r = arr.ptr();
3028 uint32_t h = HASH_MURMUR3_SEED;
3029 for (int32_t i = 0; i < len; i++) {
3030 h = hash_murmur3_one_double(r[i], h);
3031 }
3032 return hash_fmix32(h);
3033 } else {
3034 return hash_murmur3_one_double(0.0);
3035 }
3036
3037 } break;
3038 case PACKED_STRING_ARRAY: {
3039 uint32_t hash = HASH_MURMUR3_SEED;
3040 const PackedStringArray &arr = PackedArrayRef<String>::get_array(_data.packed_array);
3041 int len = arr.size();
3042
3043 if (likely(len)) {
3044 const String *r = arr.ptr();
3045
3046 for (int i = 0; i < len; i++) {
3047 hash = hash_murmur3_one_32(r[i].hash(), hash);
3048 }
3049 hash = hash_fmix32(hash);
3050 }
3051
3052 return hash;
3053 } break;
3054 case PACKED_VECTOR2_ARRAY: {
3055 uint32_t hash = HASH_MURMUR3_SEED;
3056 const PackedVector2Array &arr = PackedArrayRef<Vector2>::get_array(_data.packed_array);
3057 int len = arr.size();
3058
3059 if (likely(len)) {
3060 const Vector2 *r = arr.ptr();
3061
3062 for (int i = 0; i < len; i++) {
3063 hash = hash_murmur3_one_real(r[i].x, hash);
3064 hash = hash_murmur3_one_real(r[i].y, hash);
3065 }
3066 hash = hash_fmix32(hash);
3067 }
3068
3069 return hash;
3070 } break;
3071 case PACKED_VECTOR3_ARRAY: {
3072 uint32_t hash = HASH_MURMUR3_SEED;
3073 const PackedVector3Array &arr = PackedArrayRef<Vector3>::get_array(_data.packed_array);
3074 int len = arr.size();
3075
3076 if (likely(len)) {
3077 const Vector3 *r = arr.ptr();
3078
3079 for (int i = 0; i < len; i++) {
3080 hash = hash_murmur3_one_real(r[i].x, hash);
3081 hash = hash_murmur3_one_real(r[i].y, hash);
3082 hash = hash_murmur3_one_real(r[i].z, hash);
3083 }
3084 hash = hash_fmix32(hash);
3085 }
3086
3087 return hash;
3088 } break;
3089 case PACKED_COLOR_ARRAY: {
3090 uint32_t hash = HASH_MURMUR3_SEED;
3091 const PackedColorArray &arr = PackedArrayRef<Color>::get_array(_data.packed_array);
3092 int len = arr.size();
3093
3094 if (likely(len)) {
3095 const Color *r = arr.ptr();
3096
3097 for (int i = 0; i < len; i++) {
3098 hash = hash_murmur3_one_float(r[i].r, hash);
3099 hash = hash_murmur3_one_float(r[i].g, hash);
3100 hash = hash_murmur3_one_float(r[i].b, hash);
3101 hash = hash_murmur3_one_float(r[i].a, hash);
3102 }
3103 hash = hash_fmix32(hash);
3104 }
3105
3106 return hash;
3107 } break;
3108 case PACKED_VECTOR4_ARRAY: {
3109 uint32_t hash = HASH_MURMUR3_SEED;
3110 const PackedVector4Array &arr = PackedArrayRef<Vector4>::get_array(_data.packed_array);
3111 int len = arr.size();
3112
3113 if (likely(len)) {
3114 const Vector4 *r = arr.ptr();
3115
3116 for (int i = 0; i < len; i++) {
3117 hash = hash_murmur3_one_real(r[i].x, hash);
3118 hash = hash_murmur3_one_real(r[i].y, hash);
3119 hash = hash_murmur3_one_real(r[i].z, hash);
3120 hash = hash_murmur3_one_real(r[i].w, hash);
3121 }
3122 hash = hash_fmix32(hash);
3123 }
3124
3125 return hash;
3126 } break;
3127 default: {
3128 }
3129 }
3130
3131 return 0;
3132 }
3133
3134 #define hash_compare_scalar_base(p_lhs, p_rhs, semantic_comparison) \
3135 (((p_lhs) == (p_rhs)) || (semantic_comparison && Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
3136
3137 #define hash_compare_scalar(p_lhs, p_rhs) \
3138 (hash_compare_scalar_base(p_lhs, p_rhs, true))
3139
3140 #define hash_compare_vector2(p_lhs, p_rhs) \
3141 (p_lhs).is_same(p_rhs)
3142
3143 #define hash_compare_vector3(p_lhs, p_rhs) \
3144 (p_lhs).is_same(p_rhs)
3145
3146 #define hash_compare_vector4(p_lhs, p_rhs) \
3147 (p_lhs).is_same(p_rhs)
3148
3149 #define hash_compare_quaternion(p_lhs, p_rhs) \
3150 (p_lhs).is_same(p_rhs)
3151
3152 #define hash_compare_color(p_lhs, p_rhs) \
3153 (p_lhs).is_same(p_rhs)
3154
3155 #define hash_compare_packed_array(p_lhs, p_rhs, p_type, p_compare_func) \
3156 const Vector<p_type> &l = PackedArrayRef<p_type>::get_array(p_lhs); \
3157 const Vector<p_type> &r = PackedArrayRef<p_type>::get_array(p_rhs); \
3158 \
3159 if (l.size() != r.size()) \
3160 return false; \
3161 \
3162 const p_type *lr = l.ptr(); \
3163 const p_type *rr = r.ptr(); \
3164 \
3165 for (int i = 0; i < l.size(); ++i) { \
3166 if (!p_compare_func((lr[i]), (rr[i]))) \
3167 return false; \
3168 } \
3169 \
3170 return true
3171
3172 bool Variant::hash_compare(const Variant &p_variant, int recursion_count, bool semantic_comparison) const {
3173 if (type != p_variant.type) {
3174 return false;
3175 }
3176
3177 switch (type) {
3178 case INT: {
3179 return _data._int == p_variant._data._int;
3180 } break;
3181
3182 case FLOAT: {
3183 return hash_compare_scalar_base(_data._float, p_variant._data._float, semantic_comparison);
3184 } break;
3185
3186 case STRING: {
3187 return *reinterpret_cast<const String *>(_data._mem) == *reinterpret_cast<const String *>(p_variant._data._mem);
3188 } break;
3189
3190 case STRING_NAME: {
3191 return *reinterpret_cast<const StringName *>(_data._mem) == *reinterpret_cast<const StringName *>(p_variant._data._mem);
3192 } break;
3193
3194 case VECTOR2: {
3195 const Vector2 *l = reinterpret_cast<const Vector2 *>(_data._mem);
3196 const Vector2 *r = reinterpret_cast<const Vector2 *>(p_variant._data._mem);
3197
3198 return hash_compare_vector2(*l, *r);
3199 } break;
3200 case VECTOR2I: {
3201 const Vector2i *l = reinterpret_cast<const Vector2i *>(_data._mem);
3202 const Vector2i *r = reinterpret_cast<const Vector2i *>(p_variant._data._mem);
3203 return *l == *r;
3204 } break;
3205
3206 case RECT2: {
3207 const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
3208 const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
3209
3210 return hash_compare_vector2(l->position, r->position) &&
3211 hash_compare_vector2(l->size, r->size);
3212 } break;
3213 case RECT2I: {
3214 const Rect2i *l = reinterpret_cast<const Rect2i *>(_data._mem);
3215 const Rect2i *r = reinterpret_cast<const Rect2i *>(p_variant._data._mem);
3216
3217 return *l == *r;
3218 } break;
3219
3220 case TRANSFORM2D: {
3221 Transform2D *l = _data._transform2d;
3222 Transform2D *r = p_variant._data._transform2d;
3223
3224 return l->is_same(*r);
3225 } break;
3226
3227 case VECTOR3: {
3228 const Vector3 *l = reinterpret_cast<const Vector3 *>(_data._mem);
3229 const Vector3 *r = reinterpret_cast<const Vector3 *>(p_variant._data._mem);
3230
3231 return hash_compare_vector3(*l, *r);
3232 } break;
3233 case VECTOR3I: {
3234 const Vector3i *l = reinterpret_cast<const Vector3i *>(_data._mem);
3235 const Vector3i *r = reinterpret_cast<const Vector3i *>(p_variant._data._mem);
3236
3237 return *l == *r;
3238 } break;
3239 case VECTOR4: {
3240 const Vector4 *l = reinterpret_cast<const Vector4 *>(_data._mem);
3241 const Vector4 *r = reinterpret_cast<const Vector4 *>(p_variant._data._mem);
3242
3243 return hash_compare_vector4(*l, *r);
3244 } break;
3245 case VECTOR4I: {
3246 const Vector4i *l = reinterpret_cast<const Vector4i *>(_data._mem);
3247 const Vector4i *r = reinterpret_cast<const Vector4i *>(p_variant._data._mem);
3248
3249 return *l == *r;
3250 } break;
3251
3252 case PLANE: {
3253 const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
3254 const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
3255
3256 return l->is_same(*r);
3257 } break;
3258
3259 case AABB: {
3260 const ::AABB *l = _data._aabb;
3261 const ::AABB *r = p_variant._data._aabb;
3262
3263 return l->is_same(*r);
3264 } break;
3265
3266 case QUATERNION: {
3267 const Quaternion *l = reinterpret_cast<const Quaternion *>(_data._mem);
3268 const Quaternion *r = reinterpret_cast<const Quaternion *>(p_variant._data._mem);
3269
3270 return hash_compare_quaternion(*l, *r);
3271 } break;
3272
3273 case BASIS: {
3274 const Basis *l = _data._basis;
3275 const Basis *r = p_variant._data._basis;
3276
3277 return l->is_same(*r);
3278 } break;
3279
3280 case TRANSFORM3D: {
3281 const Transform3D *l = _data._transform3d;
3282 const Transform3D *r = p_variant._data._transform3d;
3283
3284 return l->is_same(*r);
3285 } break;
3286 case PROJECTION: {
3287 const Projection *l = _data._projection;
3288 const Projection *r = p_variant._data._projection;
3289
3290 return l->is_same(*r);
3291 } break;
3292
3293 case COLOR: {
3294 const Color *l = reinterpret_cast<const Color *>(_data._mem);
3295 const Color *r = reinterpret_cast<const Color *>(p_variant._data._mem);
3296
3297 return hash_compare_color(*l, *r);
3298 } break;
3299
3300 case ARRAY: {
3301 const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
3302 const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
3303
3304 if (!l.recursive_equal(r, recursion_count + 1)) {
3305 return false;
3306 }
3307
3308 return true;
3309 } break;
3310
3311 case DICTIONARY: {
3312 const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
3313 const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
3314
3315 if (!l.recursive_equal(r, recursion_count + 1)) {
3316 return false;
3317 }
3318
3319 return true;
3320 } break;
3321
3322 // This is for floating point comparisons only.
3323 case PACKED_FLOAT32_ARRAY: {
3324 hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, float, hash_compare_scalar);
3325 } break;
3326
3327 case PACKED_FLOAT64_ARRAY: {
3328 hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, double, hash_compare_scalar);
3329 } break;
3330
3331 case PACKED_VECTOR2_ARRAY: {
3332 hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector2, hash_compare_vector2);
3333 } break;
3334
3335 case PACKED_VECTOR3_ARRAY: {
3336 hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector3, hash_compare_vector3);
3337 } break;
3338
3339 case PACKED_COLOR_ARRAY: {
3340 hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Color, hash_compare_color);
3341 } break;
3342
3343 case PACKED_VECTOR4_ARRAY: {
3344 hash_compare_packed_array(_data.packed_array, p_variant._data.packed_array, Vector4, hash_compare_vector4);
3345 } break;
3346
3347 default:
3348 bool v;
3349 Variant r;
3350 evaluate(OP_EQUAL, *this, p_variant, r, v);
3351 return r;
3352 }
3353 }
3354
3355 bool Variant::identity_compare(const Variant &p_variant) const {
3356 if (type != p_variant.type) {
3357 return false;
3358 }
3359
3360 switch (type) {
3361 case OBJECT: {
3362 return _get_obj().id == p_variant._get_obj().id;
3363 } break;
3364
3365 case DICTIONARY: {
3366 const Dictionary &l = *(reinterpret_cast<const Dictionary *>(_data._mem));
3367 const Dictionary &r = *(reinterpret_cast<const Dictionary *>(p_variant._data._mem));
3368 return l.id() == r.id();
3369 } break;
3370
3371 case ARRAY: {
3372 const Array &l = *(reinterpret_cast<const Array *>(_data._mem));
3373 const Array &r = *(reinterpret_cast<const Array *>(p_variant._data._mem));
3374 return l.id() == r.id();
3375 } break;
3376
3377 case PACKED_BYTE_ARRAY:
3378 case PACKED_INT32_ARRAY:
3379 case PACKED_INT64_ARRAY:
3380 case PACKED_FLOAT32_ARRAY:
3381 case PACKED_FLOAT64_ARRAY:
3382 case PACKED_STRING_ARRAY:
3383 case PACKED_VECTOR2_ARRAY:
3384 case PACKED_VECTOR3_ARRAY:
3385 case PACKED_COLOR_ARRAY:
3386 case PACKED_VECTOR4_ARRAY: {
3387 return _data.packed_array == p_variant._data.packed_array;
3388 } break;
3389
3390 default: {
3391 return hash_compare(p_variant);
3392 }
3393 }
3394 }
3395
3396 bool StringLikeVariantComparator::compare(const Variant &p_lhs, const Variant &p_rhs) {
3397 if (p_lhs.hash_compare(p_rhs)) {
3398 return true;
3399 }
3400 if (p_lhs.get_type() == Variant::STRING && p_rhs.get_type() == Variant::STRING_NAME) {
3401 return *VariantInternal::get_string(&p_lhs) == *VariantInternal::get_string_name(&p_rhs);
3402 }
3403 if (p_lhs.get_type() == Variant::STRING_NAME && p_rhs.get_type() == Variant::STRING) {
3404 return *VariantInternal::get_string_name(&p_lhs) == *VariantInternal::get_string(&p_rhs);
3405 }
3406 return false;
3407 }
3408
3409 bool StringLikeVariantOrder::compare(const Variant &p_lhs, const Variant &p_rhs) {
3410 if (p_lhs.get_type() == Variant::STRING) {
3411 const String &lhs = *VariantInternal::get_string(&p_lhs);
3412 if (p_rhs.get_type() == Variant::STRING) {
3413 return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string(&p_rhs));
3414 } else if (p_rhs.get_type() == Variant::STRING_NAME) {
3415 return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string_name(&p_rhs));
3416 }
3417 } else if (p_lhs.get_type() == Variant::STRING_NAME) {
3418 const StringName &lhs = *VariantInternal::get_string_name(&p_lhs);
3419 if (p_rhs.get_type() == Variant::STRING) {
3420 return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string(&p_rhs));
3421 } else if (p_rhs.get_type() == Variant::STRING_NAME) {
3422 return StringName::AlphCompare::compare(lhs, *VariantInternal::get_string_name(&p_rhs));
3423 }
3424 }
3425
3426 return p_lhs < p_rhs;
3427 }
3428
3429 bool Variant::is_ref_counted() const {
3430 return type == OBJECT && _get_obj().id.is_ref_counted();
3431 }
3432
3433 bool Variant::is_type_shared(Variant::Type p_type) {
3434 switch (p_type) {
3435 case OBJECT:
3436 case ARRAY:
3437 case DICTIONARY:
3438 return true;
3439 default: {
3440 }
3441 }
3442
3443 return false;
3444 }
3445
3446 bool Variant::is_shared() const {
3447 return is_type_shared(type);
3448 }
3449
3450 bool Variant::is_read_only() const {
3451 switch (type) {
3452 case ARRAY:
3453 return reinterpret_cast<const Array *>(_data._mem)->is_read_only();
3454 case DICTIONARY:
3455 return reinterpret_cast<const Dictionary *>(_data._mem)->is_read_only();
3456 default:
3457 return false;
3458 }
3459 }
3460
3461 void Variant::_variant_call_error(const String &p_method, Callable::CallError &error) {
3462 switch (error.error) {
3463 case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
3464 String err = "Invalid type for argument #" + itos(error.argument) + ", expected '" + Variant::get_type_name(Variant::Type(error.expected)) + "'.";
3465 ERR_PRINT(err.utf8().get_data());
3466
3467 } break;
3468 case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
3469 String err = "Invalid method '" + p_method + "' for type '" + Variant::get_type_name(type) + "'.";
3470 ERR_PRINT(err.utf8().get_data());
3471 } break;
3472 case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS: {
3473 String err = "Too many arguments for method '" + p_method + "'";
3474 ERR_PRINT(err.utf8().get_data());
3475 } break;
3476 default: {
3477 }
3478 }
3479 }
3480
3481 void Variant::construct_from_string(const String &p_string, Variant &r_value, ObjectConstruct p_obj_construct, void *p_construct_ud) {
3482 r_value = Variant();
3483 }
3484
3485 String Variant::get_construct_string() const {
3486 String vars;
3487 VariantWriter::write_to_string(*this, vars);
3488
3489 return vars;
3490 }
3491
3492 String Variant::get_call_error_text(const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
3493 return get_call_error_text(nullptr, p_method, p_argptrs, p_argcount, ce);
3494 }
3495
3496 String Variant::get_call_error_text(Object *p_base, const StringName &p_method, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
3497 String err_text;
3498
3499 if (ce.error == Callable::CallError::CALL_ERROR_INVALID_ARGUMENT) {
3500 int errorarg = ce.argument;
3501 if (p_argptrs) {
3502 err_text = "Cannot convert argument " + itos(errorarg + 1) + " from " + Variant::get_type_name(p_argptrs[errorarg]->get_type()) + " to " + Variant::get_type_name(Variant::Type(ce.expected));
3503 } else {
3504 err_text = "Cannot convert argument " + itos(errorarg + 1) + " from [missing argptr, type unknown] to " + Variant::get_type_name(Variant::Type(ce.expected));
3505 }
3506 } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS) {
3507 err_text = "Method expected " + itos(ce.expected) + " argument(s), but called with " + itos(p_argcount);
3508 } else if (ce.error == Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS) {
3509 err_text = "Method expected " + itos(ce.expected) + " argument(s), but called with " + itos(p_argcount);
3510 } else if (ce.error == Callable::CallError::CALL_ERROR_INVALID_METHOD) {
3511 err_text = "Method not found";
3512 } else if (ce.error == Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL) {
3513 err_text = "Instance is null";
3514 } else if (ce.error == Callable::CallError::CALL_ERROR_METHOD_NOT_CONST) {
3515 err_text = "Method not const in const instance";
3516 } else if (ce.error == Callable::CallError::CALL_OK) {
3517 return "Call OK";
3518 }
3519
3520 String base_text;
3521 if (p_base) {
3522 base_text = p_base->get_class();
3523 Ref<Resource> script = p_base->get_script();
3524 if (script.is_valid() && script->get_path().is_resource_file()) {
3525 base_text += "(" + script->get_path().get_file() + ")";
3526 }
3527 base_text += "::";
3528 }
3529 return "'" + base_text + String(p_method) + "': " + err_text;
3530 }
3531
3532 String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
3533 Vector<Variant> binds;
3534 p_callable.get_bound_arguments_ref(binds);
3535
3536 int args_unbound = p_callable.get_unbound_arguments_count();
3537
3538 if (p_argcount - args_unbound < 0) {
3539 return "Callable unbinds " + itos(args_unbound) + " arguments, but called with " + itos(p_argcount);
3540 } else {
3541 Vector<const Variant *> argptrs;
3542 argptrs.resize(p_argcount - args_unbound + binds.size());
3543 for (int i = 0; i < p_argcount - args_unbound; i++) {
3544 argptrs.write[i] = p_argptrs[i];
3545 }
3546 for (int i = 0; i < binds.size(); i++) {
3547 argptrs.write[i + p_argcount - args_unbound] = &binds[i];
3548 }
3549 return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
3550 }
3551 }
3552
3553 void Variant::register_types() {
3554 _register_variant_operators();
3555 _register_variant_methods();
3556 _register_variant_setters_getters();
3557 _register_variant_constructors();
3558 _register_variant_destructors();
3559 _register_variant_utility_functions();
3560 }
3561 void Variant::unregister_types() {
3562 _unregister_variant_operators();
3563 _unregister_variant_methods();
3564 _unregister_variant_setters_getters();
3565 _unregister_variant_destructors();
3566 _unregister_variant_utility_functions();
3567 }