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