From e99d7b3b7e8712240822cd6694569606eda951be Mon Sep 17 00:00:00 2001 From: bruvzg <7645683+bruvzg@users.noreply.github.com> Date: Sun, 3 Mar 2024 12:47:57 +0200 Subject: [PATCH] [Packed*Array] Add support for initializer lists. (cherry picked from commit 8c98a90f321cebdb70cabb46a84557355918848f) --- binding_generator.py | 15 +++++++++++++++ test/project/main.gd | 1 + test/src/example.cpp | 10 ++++++++++ test/src/example.h | 1 + 4 files changed, 27 insertions(+) diff --git a/binding_generator.py b/binding_generator.py index c5a2a2a..caad05c 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -375,6 +375,10 @@ def generate_builtin_class_header(builtin_api, size, used_classes, fully_used_cl if class_name == "PackedVector3Array": result.append("#include ") + if is_packed_array(class_name): + result.append("#include ") + result.append("#include ") + if class_name == "Array": result.append("#include ") @@ -692,6 +696,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;") diff --git a/test/project/main.gd b/test/project/main.gd index 329c622..9717eca 100644 --- a/test/project/main.gd +++ b/test/project/main.gd @@ -91,6 +91,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)) diff --git a/test/src/example.cpp b/test/src/example.cpp index c8eba28..8094f7d 100644 --- a/test/src/example.cpp +++ b/test/src/example.cpp @@ -148,6 +148,7 @@ void Example::_bind_methods() { ClassDB::bind_method(D_METHOD("test_str_utility"), &Example::test_str_utility); ClassDB::bind_method(D_METHOD("test_string_is_fourty_two"), &Example::test_string_is_fourty_two); 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); @@ -339,6 +340,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; +} + int Example::test_tarray_arg(const TypedArray &p_array) { int sum = 0; for (int i = 0; i < p_array.size(); i++) { diff --git a/test/src/example.h b/test/src/example.h index 5735b96..79c1e4d 100644 --- a/test/src/example.h +++ b/test/src/example.h @@ -127,6 +127,7 @@ public: String test_str_utility() const; bool test_string_is_fourty_two(const String &p_str) 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;