Merge pull request #90866 from Repiteo/core/variant-initalizer-lists

Core: Utilize initalizer lists in Variant constructors
This commit is contained in:
Rémi Verschelde 2024-04-23 19:11:29 +02:00
commit cb1d6140be
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 115 additions and 118 deletions

View file

@ -2373,184 +2373,183 @@ Variant::operator IPAddress() const {
return IPAddress(operator String()); return IPAddress(operator String());
} }
Variant::Variant(bool p_bool) { Variant::Variant(bool p_bool) :
type = BOOL; type(BOOL) {
_data._bool = p_bool; _data._bool = p_bool;
} }
Variant::Variant(int64_t p_int64) { Variant::Variant(int64_t p_int64) :
type = INT; type(INT) {
_data._int = p_int64; _data._int = p_int64;
} }
Variant::Variant(int32_t p_int32) { Variant::Variant(int32_t p_int32) :
type = INT; type(INT) {
_data._int = p_int32; _data._int = p_int32;
} }
Variant::Variant(int16_t p_int16) { Variant::Variant(int16_t p_int16) :
type = INT; type(INT) {
_data._int = p_int16; _data._int = p_int16;
} }
Variant::Variant(int8_t p_int8) { Variant::Variant(int8_t p_int8) :
type = INT; type(INT) {
_data._int = p_int8; _data._int = p_int8;
} }
Variant::Variant(uint64_t p_uint64) { Variant::Variant(uint64_t p_uint64) :
type = INT; type(INT) {
_data._int = p_uint64; _data._int = p_uint64;
} }
Variant::Variant(uint32_t p_uint32) { Variant::Variant(uint32_t p_uint32) :
type = INT; type(INT) {
_data._int = p_uint32; _data._int = p_uint32;
} }
Variant::Variant(uint16_t p_uint16) { Variant::Variant(uint16_t p_uint16) :
type = INT; type(INT) {
_data._int = p_uint16; _data._int = p_uint16;
} }
Variant::Variant(uint8_t p_uint8) { Variant::Variant(uint8_t p_uint8) :
type = INT; type(INT) {
_data._int = p_uint8; _data._int = p_uint8;
} }
Variant::Variant(float p_float) { Variant::Variant(float p_float) :
type = FLOAT; type(FLOAT) {
_data._float = p_float; _data._float = p_float;
} }
Variant::Variant(double p_double) { Variant::Variant(double p_double) :
type = FLOAT; type(FLOAT) {
_data._float = p_double; _data._float = p_double;
} }
Variant::Variant(const ObjectID &p_id) { Variant::Variant(const ObjectID &p_id) :
type = INT; type(INT) {
_data._int = p_id; _data._int = p_id;
} }
Variant::Variant(const StringName &p_string) { Variant::Variant(const StringName &p_string) :
type = STRING_NAME; type(STRING_NAME) {
memnew_placement(_data._mem, StringName(p_string)); memnew_placement(_data._mem, StringName(p_string));
} }
Variant::Variant(const String &p_string) { Variant::Variant(const String &p_string) :
type = STRING; type(STRING) {
memnew_placement(_data._mem, String(p_string)); memnew_placement(_data._mem, String(p_string));
} }
Variant::Variant(const char *const p_cstring) { Variant::Variant(const char *const p_cstring) :
type = STRING; type(STRING) {
memnew_placement(_data._mem, String((const char *)p_cstring)); memnew_placement(_data._mem, String((const char *)p_cstring));
} }
Variant::Variant(const char32_t *p_wstring) { Variant::Variant(const char32_t *p_wstring) :
type = STRING; type(STRING) {
memnew_placement(_data._mem, String(p_wstring)); memnew_placement(_data._mem, String(p_wstring));
} }
Variant::Variant(const Vector3 &p_vector3) { Variant::Variant(const Vector3 &p_vector3) :
type = VECTOR3; type(VECTOR3) {
memnew_placement(_data._mem, Vector3(p_vector3)); memnew_placement(_data._mem, Vector3(p_vector3));
} }
Variant::Variant(const Vector3i &p_vector3i) { Variant::Variant(const Vector3i &p_vector3i) :
type = VECTOR3I; type(VECTOR3I) {
memnew_placement(_data._mem, Vector3i(p_vector3i)); memnew_placement(_data._mem, Vector3i(p_vector3i));
} }
Variant::Variant(const Vector4 &p_vector4) { Variant::Variant(const Vector4 &p_vector4) :
type = VECTOR4; type(VECTOR4) {
memnew_placement(_data._mem, Vector4(p_vector4)); memnew_placement(_data._mem, Vector4(p_vector4));
} }
Variant::Variant(const Vector4i &p_vector4i) { Variant::Variant(const Vector4i &p_vector4i) :
type = VECTOR4I; type(VECTOR4I) {
memnew_placement(_data._mem, Vector4i(p_vector4i)); memnew_placement(_data._mem, Vector4i(p_vector4i));
} }
Variant::Variant(const Vector2 &p_vector2) { Variant::Variant(const Vector2 &p_vector2) :
type = VECTOR2; type(VECTOR2) {
memnew_placement(_data._mem, Vector2(p_vector2)); memnew_placement(_data._mem, Vector2(p_vector2));
} }
Variant::Variant(const Vector2i &p_vector2i) { Variant::Variant(const Vector2i &p_vector2i) :
type = VECTOR2I; type(VECTOR2I) {
memnew_placement(_data._mem, Vector2i(p_vector2i)); memnew_placement(_data._mem, Vector2i(p_vector2i));
} }
Variant::Variant(const Rect2 &p_rect2) { Variant::Variant(const Rect2 &p_rect2) :
type = RECT2; type(RECT2) {
memnew_placement(_data._mem, Rect2(p_rect2)); memnew_placement(_data._mem, Rect2(p_rect2));
} }
Variant::Variant(const Rect2i &p_rect2i) { Variant::Variant(const Rect2i &p_rect2i) :
type = RECT2I; type(RECT2I) {
memnew_placement(_data._mem, Rect2i(p_rect2i)); memnew_placement(_data._mem, Rect2i(p_rect2i));
} }
Variant::Variant(const Plane &p_plane) { Variant::Variant(const Plane &p_plane) :
type = PLANE; type(PLANE) {
memnew_placement(_data._mem, Plane(p_plane)); memnew_placement(_data._mem, Plane(p_plane));
} }
Variant::Variant(const ::AABB &p_aabb) { Variant::Variant(const ::AABB &p_aabb) :
type = AABB; type(AABB) {
_data._aabb = (::AABB *)Pools::_bucket_small.alloc(); _data._aabb = (::AABB *)Pools::_bucket_small.alloc();
memnew_placement(_data._aabb, ::AABB(p_aabb)); memnew_placement(_data._aabb, ::AABB(p_aabb));
} }
Variant::Variant(const Basis &p_matrix) { Variant::Variant(const Basis &p_matrix) :
type = BASIS; type(BASIS) {
_data._basis = (Basis *)Pools::_bucket_medium.alloc(); _data._basis = (Basis *)Pools::_bucket_medium.alloc();
memnew_placement(_data._basis, Basis(p_matrix)); memnew_placement(_data._basis, Basis(p_matrix));
} }
Variant::Variant(const Quaternion &p_quaternion) { Variant::Variant(const Quaternion &p_quaternion) :
type = QUATERNION; type(QUATERNION) {
memnew_placement(_data._mem, Quaternion(p_quaternion)); memnew_placement(_data._mem, Quaternion(p_quaternion));
} }
Variant::Variant(const Transform3D &p_transform) { Variant::Variant(const Transform3D &p_transform) :
type = TRANSFORM3D; type(TRANSFORM3D) {
_data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc(); _data._transform3d = (Transform3D *)Pools::_bucket_medium.alloc();
memnew_placement(_data._transform3d, Transform3D(p_transform)); memnew_placement(_data._transform3d, Transform3D(p_transform));
} }
Variant::Variant(const Projection &pp_projection) { Variant::Variant(const Projection &pp_projection) :
type = PROJECTION; type(PROJECTION) {
_data._projection = (Projection *)Pools::_bucket_large.alloc(); _data._projection = (Projection *)Pools::_bucket_large.alloc();
memnew_placement(_data._projection, Projection(pp_projection)); memnew_placement(_data._projection, Projection(pp_projection));
} }
Variant::Variant(const Transform2D &p_transform) { Variant::Variant(const Transform2D &p_transform) :
type = TRANSFORM2D; type(TRANSFORM2D) {
_data._transform2d = (Transform2D *)Pools::_bucket_small.alloc(); _data._transform2d = (Transform2D *)Pools::_bucket_small.alloc();
memnew_placement(_data._transform2d, Transform2D(p_transform)); memnew_placement(_data._transform2d, Transform2D(p_transform));
} }
Variant::Variant(const Color &p_color) { Variant::Variant(const Color &p_color) :
type = COLOR; type(COLOR) {
memnew_placement(_data._mem, Color(p_color)); memnew_placement(_data._mem, Color(p_color));
} }
Variant::Variant(const NodePath &p_node_path) { Variant::Variant(const NodePath &p_node_path) :
type = NODE_PATH; type(NODE_PATH) {
memnew_placement(_data._mem, NodePath(p_node_path)); memnew_placement(_data._mem, NodePath(p_node_path));
} }
Variant::Variant(const ::RID &p_rid) { Variant::Variant(const ::RID &p_rid) :
type = RID; type(RID) {
memnew_placement(_data._mem, ::RID(p_rid)); memnew_placement(_data._mem, ::RID(p_rid));
} }
Variant::Variant(const Object *p_object) { Variant::Variant(const Object *p_object) :
type = OBJECT; type(OBJECT) {
memnew_placement(_data._mem, ObjData); memnew_placement(_data._mem, ObjData);
if (p_object) { if (p_object) {
@ -2571,76 +2570,74 @@ Variant::Variant(const Object *p_object) {
} }
} }
Variant::Variant(const Callable &p_callable) { Variant::Variant(const Callable &p_callable) :
type = CALLABLE; type(CALLABLE) {
memnew_placement(_data._mem, Callable(p_callable)); memnew_placement(_data._mem, Callable(p_callable));
} }
Variant::Variant(const Signal &p_callable) { Variant::Variant(const Signal &p_callable) :
type = SIGNAL; type(SIGNAL) {
memnew_placement(_data._mem, Signal(p_callable)); memnew_placement(_data._mem, Signal(p_callable));
} }
Variant::Variant(const Dictionary &p_dictionary) { Variant::Variant(const Dictionary &p_dictionary) :
type = DICTIONARY; type(DICTIONARY) {
memnew_placement(_data._mem, Dictionary(p_dictionary)); memnew_placement(_data._mem, Dictionary(p_dictionary));
} }
Variant::Variant(const Array &p_array) { Variant::Variant(const Array &p_array) :
type = ARRAY; type(ARRAY) {
memnew_placement(_data._mem, Array(p_array)); memnew_placement(_data._mem, Array(p_array));
} }
Variant::Variant(const PackedByteArray &p_byte_array) { Variant::Variant(const PackedByteArray &p_byte_array) :
type = PACKED_BYTE_ARRAY; type(PACKED_BYTE_ARRAY) {
_data.packed_array = PackedArrayRef<uint8_t>::create(p_byte_array); _data.packed_array = PackedArrayRef<uint8_t>::create(p_byte_array);
} }
Variant::Variant(const PackedInt32Array &p_int32_array) { Variant::Variant(const PackedInt32Array &p_int32_array) :
type = PACKED_INT32_ARRAY; type(PACKED_INT32_ARRAY) {
_data.packed_array = PackedArrayRef<int32_t>::create(p_int32_array); _data.packed_array = PackedArrayRef<int32_t>::create(p_int32_array);
} }
Variant::Variant(const PackedInt64Array &p_int64_array) { Variant::Variant(const PackedInt64Array &p_int64_array) :
type = PACKED_INT64_ARRAY; type(PACKED_INT64_ARRAY) {
_data.packed_array = PackedArrayRef<int64_t>::create(p_int64_array); _data.packed_array = PackedArrayRef<int64_t>::create(p_int64_array);
} }
Variant::Variant(const PackedFloat32Array &p_float32_array) { Variant::Variant(const PackedFloat32Array &p_float32_array) :
type = PACKED_FLOAT32_ARRAY; type(PACKED_FLOAT32_ARRAY) {
_data.packed_array = PackedArrayRef<float>::create(p_float32_array); _data.packed_array = PackedArrayRef<float>::create(p_float32_array);
} }
Variant::Variant(const PackedFloat64Array &p_float64_array) { Variant::Variant(const PackedFloat64Array &p_float64_array) :
type = PACKED_FLOAT64_ARRAY; type(PACKED_FLOAT64_ARRAY) {
_data.packed_array = PackedArrayRef<double>::create(p_float64_array); _data.packed_array = PackedArrayRef<double>::create(p_float64_array);
} }
Variant::Variant(const PackedStringArray &p_string_array) { Variant::Variant(const PackedStringArray &p_string_array) :
type = PACKED_STRING_ARRAY; type(PACKED_STRING_ARRAY) {
_data.packed_array = PackedArrayRef<String>::create(p_string_array); _data.packed_array = PackedArrayRef<String>::create(p_string_array);
} }
Variant::Variant(const PackedVector2Array &p_vector2_array) { Variant::Variant(const PackedVector2Array &p_vector2_array) :
type = PACKED_VECTOR2_ARRAY; type(PACKED_VECTOR2_ARRAY) {
_data.packed_array = PackedArrayRef<Vector2>::create(p_vector2_array); _data.packed_array = PackedArrayRef<Vector2>::create(p_vector2_array);
} }
Variant::Variant(const PackedVector3Array &p_vector3_array) { Variant::Variant(const PackedVector3Array &p_vector3_array) :
type = PACKED_VECTOR3_ARRAY; type(PACKED_VECTOR3_ARRAY) {
_data.packed_array = PackedArrayRef<Vector3>::create(p_vector3_array); _data.packed_array = PackedArrayRef<Vector3>::create(p_vector3_array);
} }
Variant::Variant(const PackedColorArray &p_color_array) { Variant::Variant(const PackedColorArray &p_color_array) :
type = PACKED_COLOR_ARRAY; type(PACKED_COLOR_ARRAY) {
_data.packed_array = PackedArrayRef<Color>::create(p_color_array); _data.packed_array = PackedArrayRef<Color>::create(p_color_array);
} }
/* helpers */ /* helpers */
Variant::Variant(const Vector<::RID> &p_array) { Variant::Variant(const Vector<::RID> &p_array) :
type = ARRAY; type(ARRAY) {
Array *rid_array = memnew_placement(_data._mem, Array); Array *rid_array = memnew_placement(_data._mem, Array);
rid_array->resize(p_array.size()); rid_array->resize(p_array.size());
@ -2650,9 +2647,8 @@ Variant::Variant(const Vector<::RID> &p_array) {
} }
} }
Variant::Variant(const Vector<Plane> &p_array) { Variant::Variant(const Vector<Plane> &p_array) :
type = ARRAY; type(ARRAY) {
Array *plane_array = memnew_placement(_data._mem, Array); Array *plane_array = memnew_placement(_data._mem, Array);
plane_array->resize(p_array.size()); plane_array->resize(p_array.size());
@ -2662,7 +2658,8 @@ Variant::Variant(const Vector<Plane> &p_array) {
} }
} }
Variant::Variant(const Vector<Face3> &p_face_array) { Variant::Variant(const Vector<Face3> &p_face_array) :
type(NIL) {
PackedVector3Array vertices; PackedVector3Array vertices;
int face_count = p_face_array.size(); int face_count = p_face_array.size();
vertices.resize(face_count * 3); vertices.resize(face_count * 3);
@ -2678,13 +2675,11 @@ Variant::Variant(const Vector<Face3> &p_face_array) {
} }
} }
type = NIL;
*this = vertices; *this = vertices;
} }
Variant::Variant(const Vector<Variant> &p_array) { Variant::Variant(const Vector<Variant> &p_array) :
type = NIL; type(NIL) {
Array arr; Array arr;
arr.resize(p_array.size()); arr.resize(p_array.size());
for (int i = 0; i < p_array.size(); i++) { for (int i = 0; i < p_array.size(); i++) {
@ -2693,8 +2688,8 @@ Variant::Variant(const Vector<Variant> &p_array) {
*this = arr; *this = arr;
} }
Variant::Variant(const Vector<StringName> &p_array) { Variant::Variant(const Vector<StringName> &p_array) :
type = NIL; type(NIL) {
PackedStringArray v; PackedStringArray v;
int len = p_array.size(); int len = p_array.size();
v.resize(len); v.resize(len);
@ -2863,12 +2858,13 @@ void Variant::operator=(const Variant &p_variant) {
} }
} }
Variant::Variant(const IPAddress &p_address) { Variant::Variant(const IPAddress &p_address) :
type = STRING; type(STRING) {
memnew_placement(_data._mem, String(p_address)); memnew_placement(_data._mem, String(p_address));
} }
Variant::Variant(const Variant &p_variant) { Variant::Variant(const Variant &p_variant) :
type(NIL) {
reference(p_variant); reference(p_variant);
} }

View file

@ -165,7 +165,7 @@ private:
// Variant takes 20 bytes when real_t is float, and 36 if double // Variant takes 20 bytes when real_t is float, and 36 if double
// it only allocates extra memory for aabb/matrix. // it only allocates extra memory for aabb/matrix.
Type type = NIL; Type type;
struct ObjData { struct ObjData {
ObjectID id; ObjectID id;
@ -483,8 +483,8 @@ public:
Variant(const IPAddress &p_address); Variant(const IPAddress &p_address);
#define VARIANT_ENUM_CLASS_CONSTRUCTOR(m_enum) \ #define VARIANT_ENUM_CLASS_CONSTRUCTOR(m_enum) \
Variant(m_enum p_value) { \ Variant(m_enum p_value) : \
type = INT; \ type(INT) { \
_data._int = (int64_t)p_value; \ _data._int = (int64_t)p_value; \
} }
@ -788,7 +788,8 @@ public:
static void unregister_types(); static void unregister_types();
Variant(const Variant &p_variant); Variant(const Variant &p_variant);
_FORCE_INLINE_ Variant() {} _FORCE_INLINE_ Variant() :
type(NIL) {}
_FORCE_INLINE_ ~Variant() { _FORCE_INLINE_ ~Variant() {
clear(); clear();
} }