Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Packed*Array] Add support for initializer lists. #1399

Merged
merged 1 commit into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions binding_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
if class_name == "PackedVector3Array":
result.append("#include <godot_cpp/variant/vector3.hpp>")

if is_packed_array(class_name):
result.append("#include <godot_cpp/core/error_macros.hpp>")
result.append("#include <initializer_list>")

if class_name == "Array":
result.append("#include <godot_cpp/variant/array_helpers.hpp>")

Expand Down Expand Up @@ -872,6 +876,17 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl
}
"""
result.append(iterators.replace("$TYPE", return_type))
init_list = """
_FORCE_INLINE_ $CLASS(std::initializer_list<$TYPE> p_init) {
ERR_FAIL_COND(resize(p_init.size()) != 0);

size_t i = 0;
for (const $TYPE &element : p_init) {
set(i++, element);
}
}
"""
result.append(init_list.replace("$TYPE", return_type).replace("$CLASS", class_name))

if class_name == "Array":
result.append("\tconst Variant &operator[](int64_t p_index) const;")
Expand Down
1 change: 1 addition & 0 deletions test/project/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func _ready():

# PackedArray iterators
assert_equal(example.test_vector_ops(), 105)
assert_equal(example.test_vector_init_list(), 105)

# Properties.
assert_equal(example.group_subgroup_custom_position, Vector2(0, 0))
Expand Down
10 changes: 10 additions & 0 deletions test/src/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ void Example::_bind_methods() {
ClassDB::bind_method(D_METHOD("test_string_is_fourty_two"), &Example::test_string_is_fourty_two);
ClassDB::bind_method(D_METHOD("test_string_resize"), &Example::test_string_resize);
ClassDB::bind_method(D_METHOD("test_vector_ops"), &Example::test_vector_ops);
ClassDB::bind_method(D_METHOD("test_vector_init_list"), &Example::test_vector_init_list);

ClassDB::bind_method(D_METHOD("test_object_cast_to_node", "object"), &Example::test_object_cast_to_node);
ClassDB::bind_method(D_METHOD("test_object_cast_to_control", "object"), &Example::test_object_cast_to_control);
Expand Down Expand Up @@ -411,6 +412,15 @@ int Example::test_vector_ops() const {
return ret;
}

int Example::test_vector_init_list() const {
PackedInt32Array arr = { 10, 20, 30, 45 };
int ret = 0;
for (const int32_t &E : arr) {
ret += E;
}
return ret;
}

Callable Example::test_callable_mp() {
return callable_mp(this, &Example::unbound_method1);
}
Expand Down
1 change: 1 addition & 0 deletions test/src/example.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ class Example : public Control {
bool test_string_is_fourty_two(const String &p_str) const;
String test_string_resize(String p_original) const;
int test_vector_ops() const;
int test_vector_init_list() const;

bool test_object_cast_to_node(Object *p_object) const;
bool test_object_cast_to_control(Object *p_object) const;
Expand Down
Loading