From c7b03c4132f9be5846141fb101f8f89522eb8efa Mon Sep 17 00:00:00 2001 From: danielytics Date: Tue, 6 Mar 2018 10:26:07 +0000 Subject: [PATCH] make register_signal and auto-generated variadic funcitons use variadic templates to streamline their use --- binding_generator.py | 14 +++++++++++--- include/core/Defs.hpp | 36 ++++++++++++++++++++++++++++++++++++ include/core/Godot.hpp | 5 +++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/binding_generator.py b/binding_generator.py index 54c56f2..a7d874d 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -115,6 +115,7 @@ def generate_class_header(used_classes, c): source.append("") + vararg_templates = "" # generate the class definition here source.append("class " + class_name + ("" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {") @@ -157,14 +158,18 @@ def generate_class_header(used_classes, c): method_signature += "static " if c["singleton"] else "" method_signature += make_gdnative_type(method["return_type"]) - method_signature += escape_cpp(method["name"]) + "(" + method_name = escape_cpp(method["name"]) + method_signature += method_name + "(" has_default_argument = False + method_arguments = "" for i, argument in enumerate(method["arguments"]): method_signature += "const " + make_gdnative_type(argument["type"]) - method_signature += escape_cpp(argument["name"]) + argument_name = escape_cpp(argument["name"]) + method_signature += argument_name + method_arguments += argument_name # default arguments @@ -210,10 +215,13 @@ def generate_class_header(used_classes, c): if i != len(method["arguments"]) - 1: method_signature += ", " + method_arguments += "," if method["has_varargs"]: if len(method["arguments"]) > 0: method_signature += ", " + method_arguments += ", " + vararg_templates += "\ttemplate " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "godot::helpers::append_all(Array(), args...));\n\t}\n""" method_signature += "const Array& __var_args = Array()" method_signature += ")" + (" const" if method["is_const"] and not c["singleton"] else "") @@ -221,7 +229,7 @@ def generate_class_header(used_classes, c): source.append("\t" + method_signature + ";") - + source.append(vararg_templates) source.append("};") source.append("") diff --git a/include/core/Defs.hpp b/include/core/Defs.hpp index f8c76d3..0306219 100644 --- a/include/core/Defs.hpp +++ b/include/core/Defs.hpp @@ -58,6 +58,42 @@ enum Error { ERR_WTF = ERR_OMFG_THIS_IS_VERY_VERY_BAD ///< short version of the above }; + namespace helpers { + template + T append_all (T appendable, ValueT value) { + appendable.append(value); + return appendable; + } + + template + T append_all (T appendable, ValueT value, Args... args) { + appendable.append(value); + return append_all(appendable, args...); + } + + template + T append_all (T appendable) { + return appendable; + } + + template + KV add_all (KV kv, KeyT key, ValueT value) { + kv[key] = value; + return kv; + } + + template + KV add_all (KV kv, KeyT key, ValueT value, Args... args) { + kv[key] = value; + return add_all(kv, args...); + } + + template + KV add_all (KV kv) { + return kv; + } + } + } #include diff --git a/include/core/Godot.hpp b/include/core/Godot.hpp index 6d0289f..8c463b6 100644 --- a/include/core/Godot.hpp +++ b/include/core/Godot.hpp @@ -484,6 +484,11 @@ void register_signal(String name, Dictionary args = Dictionary()) } } +template +void register_signal(String name, Args... varargs) +{ + register_signal(name, helpers::add_all(Dictionary(), varargs...)); +} }