Implementing dictionary operators

pull/656/head
Bastiaan Olij 2021-11-18 14:05:14 +11:00
parent 5cacce7a26
commit b008810c01
7 changed files with 46 additions and 9 deletions

View File

@ -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::")

@ -1 +1 @@
Subproject commit 3ada7a309ef1af3285641f905e3ec628f7be0b17
Subproject commit cfc1909edf55f21a8822dd088172dcf6c349a1d0

View File

@ -33,6 +33,7 @@
#include <godot_cpp/godot.hpp>
#include <godot_cpp/variant/array.hpp>
#include <godot_cpp/variant/dictionary.hpp>
#include <godot_cpp/variant/packed_byte_array.hpp>
#include <godot_cpp/variant/packed_color_array.hpp>
#include <godot_cpp/variant/packed_float32_array.hpp>
@ -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

View File

@ -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)

View File

@ -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"]

View File

@ -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;

View File

@ -88,7 +88,9 @@ public:
Ref<ExampleRef> extended_ref_checks(Ref<ExampleRef> 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);