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