I have a list of arguments of different types:
value_info args_array[10];
value_info
is defined as follows:
typedef int int_type;
typedef float float_type;
typedef double double_type;
typedef bool bool_type;
typedef char *string_type;
typedef enum {
INT_TYPE,
FLOAT_TYPE,
DOUBLE_TYPE,
BOOL_TYPE,
STRING_TYPE,
} data_type;
typedef struct {
data_type type;
union {
int_type val_int;
float_type val_float;
double_type val_double;
bool_type val_bool;
string_type val_string;
function_info val_function;
} value;
} value_info;
I need to call functions from the following table:
#define MAX_ARGS 10
typedef struct {
union {
int (*f_int)();
float (*f_float)();
double (*f_double)();
bool (*f_bool)();
char *(*f_string)();
} function;
int argc;
data_type argv[MAX_ARGS];
data_type ret_type;
} function_info;
struct init {
char *fname;
function_info fnct;
};
struct init arith_fncts[] = {
{"sin", {.function.f_double = sin, 1, {DOUBLE_TYPE}, DOUBLE_TYPE}},
{"cos", {.function.f_double = cos, 1, {DOUBLE_TYPE}, DOUBLE_TYPE}},
{"tan", {.function.f_double = tan, 1, {DOUBLE_TYPE}, DOUBLE_TYPE}}};
I woud like to call the function by string from the arith_fncts
passing the correct number of arguments from the value_info
array to the function.
I have tried using the _Generic
feature of C and writing a macro like this:
#define run(func, ...) \
_Generic(&(__VA_ARGS__), \
int(*)[3]: _Generic((func), void (*)(int, int, int): (func)))( \
__VA_ARGS__[0], __VA_ARGS__[1], __VA_ARGS__[2])
But this only works for array with 3 arguments, i would like it to be generic and aplicable to as many arguments as I want. If that is not possible, at least for 10 arguments would be sufficient. How can I achieve this in C?