Skip to content

Commit

Permalink
Avoid creating objects that Godot is going to use placement new to in…
Browse files Browse the repository at this point in the history
…itialize
  • Loading branch information
dsnopek committed Feb 26, 2024
1 parent 36847f6 commit 50f7123
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 44 deletions.
14 changes: 14 additions & 0 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,8 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("")
result.append("\tstatic struct _MethodBindings {")

result.append("\t\tGDExtensionTypeFromVariantConstructorFunc from_variant_constructor;")

if "constructors" in builtin_api:
for constructor in builtin_api["constructors"]:
result.append(f'\t\tGDExtensionPtrConstructor constructor_{constructor["index"]};')
Expand Down Expand Up @@ -494,6 +496,9 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
result.append("\tstatic void init_bindings();")
result.append("\tstatic void _init_bindings_constructors_destructor();")

result.append("")
result.append(f"\t{class_name}(const Variant *p_variant);")

result.append("")
result.append("public:")

Expand Down Expand Up @@ -818,6 +823,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl

result.append(f"void {class_name}::_init_bindings_constructors_destructor() {{")

result.append(
f"\t_method_bindings.from_variant_constructor = internal::gdextension_interface_get_variant_to_type_constructor({enum_type_name});"
)

if "constructors" in builtin_api:
for constructor in builtin_api["constructors"]:
result.append(
Expand Down Expand Up @@ -899,6 +908,11 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl

copy_constructor_index = -1

result.append(f"{class_name}::{class_name}(const Variant *p_variant) {{")
result.append("\t_method_bindings.from_variant_constructor(&opaque, p_variant->_native_ptr());")
result.append("}")
result.append("")

if "constructors" in builtin_api:
for constructor in builtin_api["constructors"]:
method_signature = f"{class_name}::{class_name}("
Expand Down
3 changes: 1 addition & 2 deletions include/godot_cpp/variant/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ class ObjectID;
class Variant {
uint8_t opaque[GODOT_CPP_VARIANT_SIZE]{ 0 };

_FORCE_INLINE_ GDExtensionVariantPtr _native_ptr() const { return const_cast<uint8_t(*)[GODOT_CPP_VARIANT_SIZE]>(&opaque); }

friend class GDExtensionBinding;
friend class MethodBind;

Expand Down Expand Up @@ -145,6 +143,7 @@ class Variant {
static GDExtensionTypeFromVariantConstructorFunc to_type_constructor[VARIANT_MAX];

public:
_FORCE_INLINE_ GDExtensionVariantPtr _native_ptr() const { return const_cast<uint8_t(*)[GODOT_CPP_VARIANT_SIZE]>(&opaque); }
Variant();
Variant(std::nullptr_t n) :
Variant() {}
Expand Down
56 changes: 14 additions & 42 deletions src/variant/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,7 @@ Variant::operator float() const {
}

Variant::operator String() const {
String result;
to_type_constructor[STRING](result._native_ptr(), _native_ptr());
return result;
return String(this);
}

Variant::operator Vector2() const {
Expand Down Expand Up @@ -447,81 +445,55 @@ Variant::operator ObjectID() const {
}

Variant::operator Callable() const {
Callable result;
to_type_constructor[CALLABLE](result._native_ptr(), _native_ptr());
return result;
return Callable(this);
}

Variant::operator Signal() const {
Signal result;
to_type_constructor[SIGNAL](result._native_ptr(), _native_ptr());
return result;
return Signal(this);
}

Variant::operator Dictionary() const {
Dictionary result;
to_type_constructor[DICTIONARY](result._native_ptr(), _native_ptr());
return result;
return Dictionary(this);
}

Variant::operator Array() const {
Array result;
to_type_constructor[ARRAY](result._native_ptr(), _native_ptr());
return result;
return Array(this);
}

Variant::operator PackedByteArray() const {
PackedByteArray result;
to_type_constructor[PACKED_BYTE_ARRAY](result._native_ptr(), _native_ptr());
return result;
return PackedByteArray(this);
}

Variant::operator PackedInt32Array() const {
PackedInt32Array result;
to_type_constructor[PACKED_INT32_ARRAY](result._native_ptr(), _native_ptr());
return result;
return PackedInt32Array(this);
}

Variant::operator PackedInt64Array() const {
PackedInt64Array result;
to_type_constructor[PACKED_INT64_ARRAY](result._native_ptr(), _native_ptr());
return result;
return PackedInt64Array(this);
}

Variant::operator PackedFloat32Array() const {
PackedFloat32Array result;
to_type_constructor[PACKED_FLOAT32_ARRAY](result._native_ptr(), _native_ptr());
return result;
return PackedFloat32Array(this);
}

Variant::operator PackedFloat64Array() const {
PackedFloat64Array result;
to_type_constructor[PACKED_FLOAT64_ARRAY](result._native_ptr(), _native_ptr());
return result;
return PackedFloat64Array(this);
}

Variant::operator PackedStringArray() const {
PackedStringArray result;
to_type_constructor[PACKED_STRING_ARRAY](result._native_ptr(), _native_ptr());
return result;
return PackedStringArray(this);
}

Variant::operator PackedVector2Array() const {
PackedVector2Array result;
to_type_constructor[PACKED_VECTOR2_ARRAY](result._native_ptr(), _native_ptr());
return result;
return PackedVector2Array(this);
}

Variant::operator PackedVector3Array() const {
PackedVector3Array result;
to_type_constructor[PACKED_VECTOR3_ARRAY](result._native_ptr(), _native_ptr());
return result;
return PackedVector3Array(this);
}

Variant::operator PackedColorArray() const {
PackedColorArray result;
to_type_constructor[PACKED_COLOR_ARRAY](result._native_ptr(), _native_ptr());
return result;
return PackedColorArray(this);
}

Variant &Variant::operator=(const Variant &other) {
Expand Down

0 comments on commit 50f7123

Please sign in to comment.