From c7b03c4132f9be5846141fb101f8f89522eb8efa Mon Sep 17 00:00:00 2001 From: danielytics Date: Tue, 6 Mar 2018 10:26:07 +0000 Subject: [PATCH 1/3] 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 54c56f2d..a7d874df 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 f8c76d3e..0306219c 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 6d0289f6..8c463b69 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...)); +} } From 01db553c49345b683ff9da3283bc92a8c2933af2 Mon Sep 17 00:00:00 2001 From: danielytics Date: Wed, 7 Mar 2018 10:27:34 +0000 Subject: [PATCH 2/3] adds Array::make and Dictionary::make static methods and has variadic template functions use those --- binding_generator.py | 2 +- include/core/Array.hpp | 6 ++++++ include/core/Dictionary.hpp | 5 +++++ include/core/Godot.hpp | 2 +- 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/binding_generator.py b/binding_generator.py index a7d874df..c39e7edd 100644 --- a/binding_generator.py +++ b/binding_generator.py @@ -221,7 +221,7 @@ def generate_class_header(used_classes, c): 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""" + vararg_templates += "\ttemplate " + method_signature + "Args... args){\n\t\treturn " + method_name + "(" + method_arguments + "Array::make(args...));\n\t}\n""" method_signature += "const Array& __var_args = Array()" method_signature += ")" + (" const" if method["is_const"] and not c["singleton"] else "") diff --git a/include/core/Array.hpp b/include/core/Array.hpp index 23205686..ead3ba3b 100644 --- a/include/core/Array.hpp +++ b/include/core/Array.hpp @@ -3,6 +3,7 @@ #include +#include "Defs.hpp" #include "String.hpp" namespace godot { @@ -39,6 +40,11 @@ public: Array(const PoolColorArray& a); + template + static Array make(Args... args) { + return helpers::append_all(Array(), args...); + } + Variant& operator [](const int idx); Variant operator [](const int idx) const; diff --git a/include/core/Dictionary.hpp b/include/core/Dictionary.hpp index ec496af6..613d6ce3 100644 --- a/include/core/Dictionary.hpp +++ b/include/core/Dictionary.hpp @@ -16,6 +16,11 @@ public: Dictionary(const Dictionary & other); Dictionary & operator=(const Dictionary & other); + template + static Dictionary make(Args... args) { + return helpers::add_all(Dictionary(), args...); + } + void clear(); bool empty() const; diff --git a/include/core/Godot.hpp b/include/core/Godot.hpp index 8c463b69..01bee09f 100644 --- a/include/core/Godot.hpp +++ b/include/core/Godot.hpp @@ -487,7 +487,7 @@ void register_signal(String name, Dictionary args = Dictionary()) template void register_signal(String name, Args... varargs) { - register_signal(name, helpers::add_all(Dictionary(), varargs...)); + register_signal(name, Dictionary::make(varargs...)); } } From 85e73de2e0a19160b67430162bd7ec7c3c672b89 Mon Sep 17 00:00:00 2001 From: danielytics Date: Wed, 7 Mar 2018 10:34:25 +0000 Subject: [PATCH 3/3] adds an example of registering signal with arguments to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 4766a8ed..e2d4163b 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ public: /** For registering signal **/ // register_signal("signal_name"); + // register_signal("signal_name", "string_argument", GODOT_VARIANT_TYPE_STRING) } String _name;