dimanche 28 juillet 2019

Unpack C++ array to arguments on a function call

I'm implementing a library capable of understanding Godot's RPC requests.

From the RPC request, I have made an array of arguments of arbitrary size. The arguments are all of the same type, Variant. Each Variant contains a type enum and casting overloading to return the proper type, be it an int or a const char*, etc.

Now, I'd like to have the library accept registrations of function points, or std::function instances, etc. The library should be able to unpack this array Variant* to arguments on the registered function.

i.e.

std::function<void(int, float, const char*)> foo;


Variant* args = ...;
size_t numArgs = ...;

/**
 * For example,
 * if (args[0].type == VAR_INT) {
 *     int arg_int = (int)args[0];
 * }
 * would be valid.
 */

foo(args...);

Is there a way to make this possible?

Thanks,

Aucun commentaire:

Enregistrer un commentaire