Implementing dictionary operators
parent
5cacce7a26
commit
b008810c01
|
@ -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"\tconst Variant &operator[](int p_index) const;")
|
||||||
result.append(f"\tVariant &operator[](int p_index);")
|
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("};")
|
result.append("};")
|
||||||
|
|
||||||
if class_name == "String":
|
if class_name == "String":
|
||||||
|
@ -607,7 +611,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
|
||||||
|
|
||||||
# Move constructor.
|
# Move constructor.
|
||||||
result.append(f"{class_name}::{class_name}({class_name} &&other) {{")
|
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("}")
|
||||||
result.append("")
|
result.append("")
|
||||||
|
|
||||||
|
@ -722,7 +729,10 @@ def generate_builtin_class_source(builtin_api, size, used_classes, fully_used_cl
|
||||||
|
|
||||||
# Move assignment.
|
# Move assignment.
|
||||||
result.append(f"{class_name} &{class_name}::operator=({class_name} &&other) {{")
|
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("\treturn *this;")
|
||||||
result.append("}")
|
result.append("}")
|
||||||
|
|
||||||
|
@ -1560,6 +1570,13 @@ def is_packed_array(type_name):
|
||||||
"PackedVector3Array",
|
"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):
|
def is_enum(type_name):
|
||||||
return type_name.startswith("enum::")
|
return type_name.startswith("enum::")
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
Subproject commit 3ada7a309ef1af3285641f905e3ec628f7be0b17
|
Subproject commit cfc1909edf55f21a8822dd088172dcf6c349a1d0
|
|
@ -33,6 +33,7 @@
|
||||||
#include <godot_cpp/godot.hpp>
|
#include <godot_cpp/godot.hpp>
|
||||||
|
|
||||||
#include <godot_cpp/variant/array.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_byte_array.hpp>
|
||||||
#include <godot_cpp/variant/packed_color_array.hpp>
|
#include <godot_cpp/variant/packed_color_array.hpp>
|
||||||
#include <godot_cpp/variant/packed_float32_array.hpp>
|
#include <godot_cpp/variant/packed_float32_array.hpp>
|
||||||
|
@ -135,4 +136,14 @@ Variant &Array::operator[](int p_index) {
|
||||||
return *var;
|
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
|
} // namespace godot
|
||||||
|
|
|
@ -13,7 +13,9 @@ func _ready():
|
||||||
var ref = ExampleRef.new()
|
var ref = ExampleRef.new()
|
||||||
prints("sending ref: ", ref.get_instance_id(), "returned ref: ", $Example.extended_ref_checks(ref).get_instance_id())
|
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("vararg args", $Example.varargs_func("some", "arguments", "to", "test"))
|
||||||
|
|
||||||
prints("test array", $Example.test_array())
|
prints("test array", $Example.test_array())
|
||||||
|
prints("test dictionary", $Example.test_dictionary())
|
||||||
|
|
||||||
# Use properties.
|
# Use properties.
|
||||||
prints("custom position is", $Example.group_subgroup_custom_position)
|
prints("custom position is", $Example.group_subgroup_custom_position)
|
||||||
|
|
|
@ -6,15 +6,12 @@
|
||||||
script = ExtResource( "1_c326s" )
|
script = ExtResource( "1_c326s" )
|
||||||
|
|
||||||
[node name="Example" type="Example" parent="."]
|
[node name="Example" type="Example" parent="."]
|
||||||
script = null
|
|
||||||
|
|
||||||
[node name="Label" type="Label" parent="Example"]
|
[node name="Label" type="Label" parent="Example"]
|
||||||
offset_left = 194.0
|
offset_left = 194.0
|
||||||
offset_top = -2.0
|
offset_top = -2.0
|
||||||
offset_right = 234.0
|
offset_right = 234.0
|
||||||
offset_bottom = 21.0
|
offset_bottom = 21.0
|
||||||
structured_text_bidi_override_options = []
|
|
||||||
script = null
|
|
||||||
__meta__ = {
|
__meta__ = {
|
||||||
"_edit_use_anchors_": false
|
"_edit_use_anchors_": false
|
||||||
}
|
}
|
||||||
|
@ -23,9 +20,6 @@ __meta__ = {
|
||||||
offset_right = 79.0
|
offset_right = 79.0
|
||||||
offset_bottom = 29.0
|
offset_bottom = 29.0
|
||||||
text = "Click me!"
|
text = "Click me!"
|
||||||
script = null
|
|
||||||
__meta__ = {
|
__meta__ = {
|
||||||
"_edit_use_anchors_": false
|
"_edit_use_anchors_": false
|
||||||
}
|
}
|
||||||
|
|
||||||
[connection signal="custom_signal" from="Example" to="." method="_on_Example_custom_signal"]
|
|
||||||
|
|
|
@ -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_something_const"), &Example::return_something_const);
|
||||||
ClassDB::bind_method(D_METHOD("return_extended_ref"), &Example::return_extended_ref);
|
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("extended_ref_checks"), &Example::extended_ref_checks);
|
||||||
|
|
||||||
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
|
ClassDB::bind_method(D_METHOD("test_array"), &Example::test_array);
|
||||||
|
ClassDB::bind_method(D_METHOD("test_dictionary"), &Example::test_dictionary);
|
||||||
|
|
||||||
{
|
{
|
||||||
MethodInfo mi;
|
MethodInfo mi;
|
||||||
|
@ -145,6 +147,15 @@ Array Example::test_array() const {
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dictionary Example::test_dictionary() const {
|
||||||
|
Dictionary dict;
|
||||||
|
|
||||||
|
dict["hello"] = "world";
|
||||||
|
dict["foo"] = "bar";
|
||||||
|
|
||||||
|
return dict;
|
||||||
|
}
|
||||||
|
|
||||||
// Properties.
|
// Properties.
|
||||||
void Example::set_custom_position(const Vector2 &pos) {
|
void Example::set_custom_position(const Vector2 &pos) {
|
||||||
custom_position = pos;
|
custom_position = pos;
|
||||||
|
|
|
@ -88,7 +88,9 @@ public:
|
||||||
Ref<ExampleRef> extended_ref_checks(Ref<ExampleRef> p_ref) const;
|
Ref<ExampleRef> extended_ref_checks(Ref<ExampleRef> p_ref) const;
|
||||||
Variant varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error);
|
Variant varargs_func(const Variant **args, GDNativeInt arg_count, GDNativeCallError &error);
|
||||||
void emit_custom_signal(const String &name, int value);
|
void emit_custom_signal(const String &name, int value);
|
||||||
|
|
||||||
Array test_array() const;
|
Array test_array() const;
|
||||||
|
Dictionary test_dictionary() const;
|
||||||
|
|
||||||
// Property.
|
// Property.
|
||||||
void set_custom_position(const Vector2 &pos);
|
void set_custom_position(const Vector2 &pos);
|
||||||
|
|
Loading…
Reference in New Issue