From b008810c0121e0a365a41615c48b40934aae9a29 Mon Sep 17 00:00:00 2001 From: Bastiaan Olij Date: Thu, 18 Nov 2021 14:05:14 +1100 Subject: [PATCH] Implementing dictionary operators --- binding_generator.py | 21 +++++++++++++++++++-- godot-headers | 2 +- src/variant/packed_arrays.cpp | 11 +++++++++++ test/demo/main.gd | 2 ++ test/demo/main.tscn | 6 ------ test/src/example.cpp | 11 +++++++++++ test/src/example.h | 2 ++ 7 files changed, 46 insertions(+), 9 deletions(-) diff --git a/binding_generator.py b/binding_generator.py index 89a58c93..6d50292f 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -448,6 +448,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl result.append(f"\tconst Variant &operator[](int p_index) const;") result.append(f"\tVariant &operator[](int p_index);") + if class_name == "Dictionary": + result.append(f"\tconst Variant &operator[](const Variant &p_key) const;") + result.append(f"\tVariant &operator[](const Variant &p_key);") + result.append("};") if class_name == "String": @@ -607,7 +611,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl # Move constructor. result.append(f"{class_name}::{class_name}({class_name} &&other) {{") - result.append("\tstd::swap(opaque, other.opaque);") + if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0: + result.append(f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);") + else: + result.append("\tstd::swap(opaque, other.opaque);") result.append("}") result.append("") @@ -722,7 +729,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl # Move assignment. result.append(f"{class_name} &{class_name}::operator=({class_name} &&other) {{") - result.append("\tstd::swap(opaque, other.opaque);") + if needs_copy_instead_of_move(class_name) and copy_constructor_index >= 0: + result.append(f"\tinternal::_call_builtin_constructor(_method_bindings.constructor_{copy_constructor_index}, &opaque, &other);") + else: + result.append("\tstd::swap(opaque, other.opaque);") result.append("\treturn *this;") result.append("}") @@ -1560,6 +1570,13 @@ def is_packed_array(type_name): "PackedVector3Array", ] +def needs_copy_instead_of_move(type_name): + """ + Those are types which need initialised data or we'll get warning spam so need a copy instead of move. + """ + return type_name in [ + "Dictionary", + ] def is_enum(type_name): return type_name.startswith("enum::") diff --git a/godot-headers b/godot-headers index 3ada7a30..cfc1909e 160000 --- a/godot-headers +++ b/godot-headers @@ -1 +1 @@ -Subproject commit 3ada7a309ef1af3285641f905e3ec628f7be0b17 +Subproject commit cfc1909edf55f21a8822dd088172dcf6c349a1d0 diff --git a/src/variant/packed_arrays.cpp b/src/variant/packed_arrays.cpp index 2081bfaa..bb6bba77 100644 --- a/src/variant/packed_arrays.cpp +++ b/src/variant/packed_arrays.cpp @@ -33,6 +33,7 @@ #include #include +#include #include #include #include @@ -135,4 +136,14 @@ Variant &Array::operator[](int p_index) { return *var; } +const Variant &Dictionary::operator[](const Variant &p_key) const { + const Variant *var = (const Variant *)internal::gdn_interface->dictionary_operator_index_const((GDNativeTypePtr *)this, (GDNativeVariantPtr)&p_key); + return *var; +} + +Variant &Dictionary::operator[](const Variant &p_key) { + Variant *var = (Variant *)internal::gdn_interface->dictionary_operator_index((GDNativeTypePtr *)this, (GDNativeVariantPtr)&p_key); + return *var; +} + } // namespace godot diff --git a/test/demo/main.gd b/test/demo/main.gd index a489c36b..16be87cb 100644 --- a/test/demo/main.gd +++ b/test/demo/main.gd @@ -13,7 +13,9 @@ func _ready(): var ref = ExampleRef.new() prints("sending ref: ", ref.get_instance_id(), "returned ref: ", $Example.extended_ref_checks(ref).get_instance_id()) prints("vararg args", $Example.varargs_func("some", "arguments", "to", "test")) + prints("test array", $Example.test_array()) + prints("test dictionary", $Example.test_dictionary()) # Use properties. prints("custom position is", $Example.group_subgroup_custom_position) diff --git a/test/demo/main.tscn b/test/demo/main.tscn index 125eae03..c0e57fba 100644 --- a/test/demo/main.tscn +++ b/test/demo/main.tscn @@ -6,15 +6,12 @@ script = ExtResource( "1_c326s" ) [node name="Example" type="Example" parent="."] -script = null [node name="Label" type="Label" parent="Example"] offset_left = 194.0 offset_top = -2.0 offset_right = 234.0 offset_bottom = 21.0 -structured_text_bidi_override_options = [] -script = null __meta__ = { "_edit_use_anchors_": false } @@ -23,9 +20,6 @@ __meta__ = { offset_right = 79.0 offset_bottom = 29.0 text = "Click me!" -script = null __meta__ = { "_edit_use_anchors_": false } - -[connection signal="custom_signal" from="Example" to="." method="_on_Example_custom_signal"] diff --git a/test/src/example.cpp b/test/src/example.cpp index e2a6ceb6..ad82103a 100644 --- a/test/src/example.cpp +++ b/test/src/example.cpp @@ -54,7 +54,9 @@ void Example::_bind_methods() { ClassDB::bind_method(D_METHOD("return_something_const"), &Example::return_something_const); ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref); ClassDB::bind_method(D_METHOD("extended_ref_checks"), &Example::extended_ref_checks); + ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array); + ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary); { MethodInfo mi; @@ -145,6 +147,15 @@ Array Example::test_array() const { return arr; } +Dictionary Example::test_dictionary() const { + Dictionary dict; + + dict["hello"] = "world"; + dict["foo"] = "bar"; + + return dict; +} + // Properties. void Example::set_custom_position(const Vector2 &pos) { custom_position = pos; diff --git a/test/src/example.h b/test/src/example.h index 79bcdd91..214b0766 100644 --- a/test/src/example.h +++ b/test/src/example.h @@ -88,7 +88,9 @@ public: Ref extended_ref_checks(Ref p_ref) const; Variant varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error); void emit_custom_signal(const String &name, int value); + Array test_array() const; + Dictionary test_dictionary() const; // Property. void set_custom_position(const Vector2 &pos);