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

Add vararg call() method to C++ Callable #79341

Merged
merged 1 commit into from
Oct 5, 2023
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
11 changes: 2 additions & 9 deletions core/object/worker_thread_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ void WorkerThreadPool::_process_task(Task *p_task) {
if (p_task->group) {
// Handling a group
bool do_post = false;
Callable::CallError ce;
Variant ret;
Variant arg;
Variant *argptr = &arg;

while (true) {
uint32_t work_index = p_task->group->index.postincrement();
Expand All @@ -91,8 +87,7 @@ void WorkerThreadPool::_process_task(Task *p_task) {
} else if (p_task->template_userdata) {
p_task->template_userdata->callback_indexed(work_index);
} else {
arg = work_index;
p_task->callable.callp((const Variant **)&argptr, 1, ret, ce);
p_task->callable.call(work_index);
}

// This is the only way to ensure posting is done when all tasks are really complete.
Expand Down Expand Up @@ -141,9 +136,7 @@ void WorkerThreadPool::_process_task(Task *p_task) {
p_task->template_userdata->callback();
memdelete(p_task->template_userdata);
} else {
Callable::CallError ce;
Variant ret;
p_task->callable.callp(nullptr, 0, ret, ce);
p_task->callable.call();
}

task_mutex.lock();
Expand Down
2 changes: 2 additions & 0 deletions core/variant/callable.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class Callable {
int expected = 0;
};

template <typename... VarArgs>
Variant call(VarArgs... p_args) const;
void callp(const Variant **p_arguments, int p_argcount, Variant &r_return_value, CallError &r_call_error) const;
void call_deferredp(const Variant **p_arguments, int p_argcount) const;
Variant callv(const Array &p_arguments) const;
Expand Down
14 changes: 14 additions & 0 deletions core/variant/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -833,6 +833,20 @@ String vformat(const String &p_text, const VarArgs... p_args) {
return fmt;
}

template <typename... VarArgs>
Variant Callable::call(VarArgs... p_args) const {
Variant args[sizeof...(p_args) + 1] = { p_args..., 0 }; // +1 makes sure zero sized arrays are also supported.
const Variant *argptrs[sizeof...(p_args) + 1];
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
argptrs[i] = &args[i];
}

Variant ret;
CallError ce;
callp(sizeof...(p_args) == 0 ? nullptr : (const Variant **)argptrs, sizeof...(p_args), ret, ce);
return ret;
}

template <typename... VarArgs>
Callable Callable::bind(VarArgs... p_args) {
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
Expand Down
21 changes: 1 addition & 20 deletions editor/animation_track_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3169,26 +3169,7 @@ AnimationTrackEdit::AnimationTrackEdit() {

AnimationTrackEdit *AnimationTrackEditPlugin::create_value_track_edit(Object *p_object, Variant::Type p_type, const String &p_property, PropertyHint p_hint, const String &p_hint_string, int p_usage) {
if (get_script_instance()) {
Variant args[6] = {
p_object,
p_type,
p_property,
p_hint,
p_hint_string,
p_usage
};

Variant *argptrs[6] = {
&args[0],
&args[1],
&args[2],
&args[3],
&args[4],
&args[5]
};

Callable::CallError ce;
return Object::cast_to<AnimationTrackEdit>(get_script_instance()->callp("create_value_track_edit", (const Variant **)&argptrs, 6, ce).operator Object *());
return Object::cast_to<AnimationTrackEdit>(get_script_instance()->call("create_value_track_edit", p_object, p_type, p_property, p_hint, p_hint_string, p_usage));
}
return nullptr;
}
Expand Down
24 changes: 4 additions & 20 deletions editor/editor_inspector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1726,11 +1726,7 @@ void EditorInspectorArray::_move_element(int p_element_index, int p_to_pos) {
// Call the function.
Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
if (move_function.is_valid()) {
Variant args[] = { undo_redo, object, array_element_prefix, p_element_index, p_to_pos };
const Variant *args_p[] = { &args[0], &args[1], &args[2], &args[3], &args[4] };
Variant return_value;
Callable::CallError call_error;
move_function.callp(args_p, 5, return_value, call_error);
move_function.call(undo_redo, object, array_element_prefix, p_element_index, p_to_pos);
} else {
WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
}
Expand Down Expand Up @@ -1875,11 +1871,7 @@ void EditorInspectorArray::_clear_array() {
// Call the function.
Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
if (move_function.is_valid()) {
Variant args[] = { undo_redo, object, array_element_prefix, i, -1 };
const Variant *args_p[] = { &args[0], &args[1], &args[2], &args[3], &args[4] };
Variant return_value;
Callable::CallError call_error;
move_function.callp(args_p, 5, return_value, call_error);
move_function.call(undo_redo, object, array_element_prefix, i, -1);
} else {
WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
}
Expand Down Expand Up @@ -1929,11 +1921,7 @@ void EditorInspectorArray::_resize_array(int p_size) {
// Call the function.
Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
if (move_function.is_valid()) {
Variant args[] = { undo_redo, object, array_element_prefix, -1, -1 };
const Variant *args_p[] = { &args[0], &args[1], &args[2], &args[3], &args[4] };
Variant return_value;
Callable::CallError call_error;
move_function.callp(args_p, 5, return_value, call_error);
move_function.call(undo_redo, object, array_element_prefix, -1, -1);
} else {
WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
}
Expand All @@ -1948,11 +1936,7 @@ void EditorInspectorArray::_resize_array(int p_size) {
// Call the function.
Callable move_function = EditorNode::get_editor_data().get_move_array_element_function(object->get_class_name());
if (move_function.is_valid()) {
Variant args[] = { undo_redo, object, array_element_prefix, i, -1 };
const Variant *args_p[] = { &args[0], &args[1], &args[2], &args[3], &args[4] };
Variant return_value;
Callable::CallError call_error;
move_function.callp(args_p, 5, return_value, call_error);
move_function.call(undo_redo, object, array_element_prefix, i, -1);
} else {
WARN_PRINT(vformat("Could not find a function to move arrays elements for class %s. Register a move element function using EditorData::add_move_array_element_function", object->get_class_name()));
}
Expand Down
11 changes: 2 additions & 9 deletions editor/import/post_import_plugin_skeleton_renamer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,26 +119,19 @@ void PostImportPluginSkeletonRenamer::_internal_process(InternalImportCategory p

// Rename bones in all Nodes by calling method.
{
Vector<Variant> vargs;
Array vargs;
vargs.push_back(p_base_scene);
vargs.push_back(skeleton);
Dictionary rename_map_dict;
for (HashMap<String, String>::Iterator E = p_rename_map.begin(); E; ++E) {
rename_map_dict[E->key] = E->value;
}
vargs.push_back(rename_map_dict);
const Variant **argptrs = (const Variant **)alloca(sizeof(const Variant **) * vargs.size());
const Variant *args = vargs.ptr();
uint32_t argcount = vargs.size();
for (uint32_t i = 0; i < argcount; i++) {
argptrs[i] = &args[i];
}

TypedArray<Node> nodes = p_base_scene->find_children("*");
while (nodes.size()) {
Node *nd = Object::cast_to<Node>(nodes.pop_back());
Callable::CallError ce;
nd->callp("_notify_skeleton_bones_renamed", argptrs, argcount, ce);
nd->callv("_notify_skeleton_bones_renamed", vargs);
}
}
}
Expand Down
6 changes: 1 addition & 5 deletions editor/plugins/tiles/tiles_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,7 @@ void TilesEditorUtils::_thread() {
Ref<Image> image = viewport->get_texture()->get_image();

// Find the index for the given pattern. TODO: optimize.
Variant args[] = { item.pattern, ImageTexture::create_from_image(image) };
const Variant *args_ptr[] = { &args[0], &args[1] };
Variant r;
Callable::CallError error;
item.callback.callp(args_ptr, 2, r, error);
item.callback.call(item.pattern, ImageTexture::create_from_image(image));

viewport->queue_free();
}
Expand Down
7 changes: 1 addition & 6 deletions modules/navigation/nav_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,7 @@ void NavAgent::dispatch_avoidance_callback() {
}

// Invoke the callback with the new velocity.
Variant args[] = { new_velocity };
const Variant *args_p[] = { &args[0] };
Variant return_value;
Callable::CallError call_error;

avoidance_callback.callp(args_p, 1, return_value, call_error);
avoidance_callback.call(new_velocity);
}

void NavAgent::set_neighbor_distance(real_t p_neighbor_distance) {
Expand Down
18 changes: 4 additions & 14 deletions platform/android/display_server_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,13 +309,10 @@ void DisplayServerAndroid::window_set_drop_files_callback(const Callable &p_call

void DisplayServerAndroid::_window_callback(const Callable &p_callable, const Variant &p_arg, bool p_deferred) const {
if (!p_callable.is_null()) {
const Variant *argp = &p_arg;
Variant ret;
Callable::CallError ce;
if (p_deferred) {
p_callable.callp((const Variant **)&argp, 1, ret, ce);
p_callable.call(p_arg);
} else {
p_callable.call_deferredp((const Variant **)&argp, 1);
p_callable.call_deferred(p_arg);
}
}
}
Expand Down Expand Up @@ -538,16 +535,9 @@ void DisplayServerAndroid::reset_window() {
}

void DisplayServerAndroid::notify_surface_changed(int p_width, int p_height) {
if (rect_changed_callback.is_null()) {
return;
if (rect_changed_callback.is_valid()) {
rect_changed_callback.call(Rect2i(0, 0, p_width, p_height));
}

const Variant size = Rect2i(0, 0, p_width, p_height);
const Variant *sizep = &size;
Variant ret;
Callable::CallError ce;

rect_changed_callback.callp(reinterpret_cast<const Variant **>(&sizep), 1, ret, ce);
}

DisplayServerAndroid::DisplayServerAndroid(const String &p_rendering_driver, DisplayServer::WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Error &r_error) {
Expand Down
5 changes: 1 addition & 4 deletions platform/ios/display_server_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,7 @@

void DisplayServerIOS::_window_callback(const Callable &p_callable, const Variant &p_arg) const {
if (!p_callable.is_null()) {
const Variant *argp = &p_arg;
Variant ret;
Callable::CallError ce;
p_callable.callp((const Variant **)&argp, 1, ret, ce);
p_callable.call(p_arg);
}
}

Expand Down
35 changes: 8 additions & 27 deletions platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3738,15 +3738,8 @@ void DisplayServerX11::_window_changed(XEvent *event) {
}
#endif

if (!wd.rect_changed_callback.is_null()) {
Rect2i r = new_rect;

Variant rect = r;

Variant *rectp = &rect;
Variant ret;
Callable::CallError ce;
wd.rect_changed_callback.callp((const Variant **)&rectp, 1, ret, ce);
if (wd.rect_changed_callback.is_valid()) {
wd.rect_changed_callback.call(new_rect);
}
}

Expand All @@ -3764,19 +3757,14 @@ void DisplayServerX11::_dispatch_input_events(const Ref<InputEvent> &p_event) {
}

void DisplayServerX11::_dispatch_input_event(const Ref<InputEvent> &p_event) {
Variant ev = p_event;
Variant *evp = &ev;
Variant ret;
Callable::CallError ce;

{
List<WindowID>::Element *E = popup_list.back();
if (E && Object::cast_to<InputEventKey>(*p_event)) {
// Redirect keyboard input to active popup.
if (windows.has(E->get())) {
Callable callable = windows[E->get()].input_event_callback;
if (callable.is_valid()) {
callable.callp((const Variant **)&evp, 1, ret, ce);
callable.call(p_event);
}
}
return;
Expand All @@ -3789,27 +3777,24 @@ void DisplayServerX11::_dispatch_input_event(const Ref<InputEvent> &p_event) {
if (windows.has(event_from_window->get_window_id())) {
Callable callable = windows[event_from_window->get_window_id()].input_event_callback;
if (callable.is_valid()) {
callable.callp((const Variant **)&evp, 1, ret, ce);
callable.call(p_event);
}
}
} else {
// Send to all windows.
for (KeyValue<WindowID, WindowData> &E : windows) {
Callable callable = E.value.input_event_callback;
if (callable.is_valid()) {
callable.callp((const Variant **)&evp, 1, ret, ce);
callable.call(p_event);
}
}
}
}

void DisplayServerX11::_send_window_event(const WindowData &wd, WindowEvent p_event) {
if (!wd.event_callback.is_null()) {
if (wd.event_callback.is_valid()) {
RandomShaper marked this conversation as resolved.
Show resolved Hide resolved
Variant event = int(p_event);
Variant *eventp = &event;
Variant ret;
Callable::CallError ce;
wd.event_callback.callp((const Variant **)&eventp, 1, ret, ce);
wd.event_callback.call(event);
}
}

Expand Down Expand Up @@ -4754,11 +4739,7 @@ void DisplayServerX11::process_events() {
}

if (!windows[window_id].drop_files_callback.is_null()) {
Variant v = files;
Variant *vp = &v;
Variant ret;
Callable::CallError ce;
windows[window_id].drop_files_callback.callp((const Variant **)&vp, 1, ret, ce);
windows[window_id].drop_files_callback.call(files);
}

//Reply that all is well.
Expand Down
Loading
Loading