0

I have several functions:

int do_one_thing(struct my_struct *s, struct other_struct *os);
int do_another_thing(struct third_struct *ts, struct fourth_struct *s, int i);
int do_even_more_stuff(float *f, struct fourth_struct *s, int i);

I would like to wrap all these with a single function like this

/* 
this is basically: 
int wrapper_function(<function name>, <wrapped_function>, <arguments for wrapped function>)
*/
int wrapper_function(const char *fname, function *fun, ... )
{
    int fun_ret = 0;
    fun_ret = fun(b, args);

    printf("Function %s returned %d\n", fname, fun_ret);
    return fun_ret;
}

and obviously, all function calls to do_one_thing, do_another_thing, or do_even_more_stuff will be replaced with a call to wrapper_function.

So, how do I do that?

The challenge is passing variable arguments and function signature.

I do not want to use macros.

feeling_lonely
  • 6,665
  • 4
  • 27
  • 53
  • Just one question before yours: https://stackoverflow.com/questions/54410894/alternative-to-function-overloading-in-c – Eugene Sh. Jan 28 '19 at 22:46
  • I dont think that _Generic will work with variable number arguments – 0___________ Jan 28 '19 at 22:55
  • You cannot, in general, call a different function with the same name depending on the arguments in `C`. Specifically, a function with a variable number of arguments needs a way to tell how many arguments and what they are. – Neil Jan 29 '19 at 00:26
  • Look into `stdarg`, which features macros for dealing with variable argument lists: `va_start`, `va_arg`, `va_end`. Note, however, that when using variable argument lists, you lose all the parameter presence and type checking provided by the compiler. Officially, these are called [variadic functions](https://en.wikipedia.org/wiki/Variadic_function#Example_in_C). – Perette Jan 29 '19 at 00:52

1 Answers1

0

See http://c-faq.com/varargs/varargs1.html.

#include <stdarg.h>
#include <stdio.h>  /* printf */

struct my_struct {
    int i;
};

struct other_struct {
    char a;
};

struct third_struct {
    float f;
};

struct fourth_struct {
    double d;
};

static int do_one_thing(struct my_struct *s, struct other_struct *os) {
    printf("do one thing %d %c.\n", s->i, os->a);
    return s->i;
}

static int do_another_thing(struct third_struct *ts, struct fourth_struct *s,
    int i) {
    printf("do another thing %f %f %d.\n", ts->f, s->d, i);
    return i;
}

static int do_even_more_stuff(float *f, struct fourth_struct *s, int i) {
    printf("do even more stuff %f %f %d.\n", *f, s->d, i);
    return i;
}

enum Fn { ONE, ANOTHER, STUFF };

static int wrapper_function(const enum Fn fn, ... ) {
    va_list argp;
    int fun_ret = 0;
    struct my_struct *s;
    struct other_struct *os;
    struct third_struct *ts;
    struct fourth_struct *fs;
    int i;
    float *f;

    va_start(argp, fn);

    switch(fn) {
        case ONE:
            s = va_arg(argp, struct my_struct *);
            os = va_arg(argp, struct other_struct *);
            fun_ret = do_one_thing(s, os);
            break;
        case ANOTHER:
            ts = va_arg(argp, struct third_struct *);
            fs = va_arg(argp, struct fourth_struct *);
            i = va_arg(argp, int);
            fun_ret = do_another_thing(ts, fs, i);
            break;
        case STUFF:
            f = va_arg(argp, float *);
            fs = va_arg(argp, struct fourth_struct *);
            i = va_arg(argp, int);
            fun_ret = do_even_more_stuff(f, fs, i);
            break;
    }

    va_end(argp);

    return fun_ret;
}

int main(void) {
    struct my_struct s = { 42 };
    struct other_struct os = { 'z' };
    struct third_struct ts = { 3.14f };
    struct fourth_struct fs = { 0.9999 };
    float f = 2.001;
    int r0, r1, r2;
    printf("Function one returned %d.\n"
        "Function another returned %d.\n"
        "Function stuff returned %d.\n",
        r0 = wrapper_function(ONE, &s, &os),
        r1 = wrapper_function(ANOTHER, &ts, &fs, 99),
        r2 = wrapper_function(STUFF, &f, &fs, -3));
    return 0;
}

However, I don't really see what the advantage is; one is increasing overhead and basically turning off type-checking.

Neil
  • 1,767
  • 2
  • 16
  • 22