Merge pull request #108 from danielytics/variadic-functions

Adds variadic template functions for register_signal and emit_signal
pull/109/head
Thomas Herzog 2018-03-07 11:43:12 +01:00 committed by GitHub
commit 779568c67c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 3 deletions

View File

@ -78,6 +78,7 @@ public:
/** For registering signal **/ /** For registering signal **/
// register_signal<SimpleClass>("signal_name"); // register_signal<SimpleClass>("signal_name");
// register_signal<SimpleClass>("signal_name", "string_argument", GODOT_VARIANT_TYPE_STRING)
} }
String _name; String _name;

View File

@ -115,6 +115,7 @@ def generate_class_header(used_classes, c):
source.append("") source.append("")
vararg_templates = ""
# generate the class definition here # generate the class definition here
source.append("class " + class_name + ("" if c["base_class"] == "" else (" : public " + strip_name(c["base_class"])) ) + " {") 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 += "static " if c["singleton"] else ""
method_signature += make_gdnative_type(method["return_type"]) 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 has_default_argument = False
method_arguments = ""
for i, argument in enumerate(method["arguments"]): for i, argument in enumerate(method["arguments"]):
method_signature += "const " + make_gdnative_type(argument["type"]) 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 # default arguments
@ -210,10 +215,13 @@ def generate_class_header(used_classes, c):
if i != len(method["arguments"]) - 1: if i != len(method["arguments"]) - 1:
method_signature += ", " method_signature += ", "
method_arguments += ","
if method["has_varargs"]: if method["has_varargs"]:
if len(method["arguments"]) > 0: if len(method["arguments"]) > 0:
method_signature += ", " method_signature += ", "
method_arguments += ", "
vararg_templates += "\ttemplate <class... Args> " + 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 Array& __var_args = Array()"
method_signature += ")" + (" const" if method["is_const"] and not c["singleton"] else "") 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("\t" + method_signature + ";")
source.append(vararg_templates)
source.append("};") source.append("};")
source.append("") source.append("")

View File

@ -3,6 +3,7 @@
#include <gdnative/array.h> #include <gdnative/array.h>
#include "Defs.hpp"
#include "String.hpp" #include "String.hpp"
namespace godot { namespace godot {
@ -39,6 +40,11 @@ public:
Array(const PoolColorArray& a); Array(const PoolColorArray& a);
template <class... Args>
static Array make(Args... args) {
return helpers::append_all(Array(), args...);
}
Variant& operator [](const int idx); Variant& operator [](const int idx);
Variant operator [](const int idx) const; Variant operator [](const int idx) const;

View File

@ -58,6 +58,42 @@ enum Error {
ERR_WTF = ERR_OMFG_THIS_IS_VERY_VERY_BAD ///< short version of the above ERR_WTF = ERR_OMFG_THIS_IS_VERY_VERY_BAD ///< short version of the above
}; };
namespace helpers {
template <typename T, typename ValueT>
T append_all (T appendable, ValueT value) {
appendable.append(value);
return appendable;
}
template <typename T, typename ValueT, typename... Args>
T append_all (T appendable, ValueT value, Args... args) {
appendable.append(value);
return append_all(appendable, args...);
}
template <typename T>
T append_all (T appendable) {
return appendable;
}
template <typename KV, typename KeyT, typename ValueT>
KV add_all (KV kv, KeyT key, ValueT value) {
kv[key] = value;
return kv;
}
template <typename KV, typename KeyT, typename ValueT, typename... Args>
KV add_all (KV kv, KeyT key, ValueT value, Args... args) {
kv[key] = value;
return add_all(kv, args...);
}
template <typename KV>
KV add_all (KV kv) {
return kv;
}
}
} }
#include <stdio.h> #include <stdio.h>

View File

@ -16,6 +16,11 @@ public:
Dictionary(const Dictionary & other); Dictionary(const Dictionary & other);
Dictionary & operator=(const Dictionary & other); Dictionary & operator=(const Dictionary & other);
template <class... Args>
static Dictionary make(Args... args) {
return helpers::add_all(Dictionary(), args...);
}
void clear(); void clear();
bool empty() const; bool empty() const;

View File

@ -484,6 +484,11 @@ void register_signal(String name, Dictionary args = Dictionary())
} }
} }
template<class T, class... Args>
void register_signal(String name, Args... varargs)
{
register_signal<T>(name, Dictionary::make(varargs...));
}
} }