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; diff --git a/binding_generator.py b/binding_generator.py index 54c56f2d..c39e7edd 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 + "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 "") @@ -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/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/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/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 6d0289f6..01bee09f 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, Dictionary::make(varargs...)); +} }