c - 将多个具有不同参数计数的函数包装在包装函数中

标签 c

我有几个功能:

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);

我想用一个像这样的函数来包装所有这些

/* 
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;
}

显然,所有对 do_one_thing、do_another_thing 或 do_even_more_stuff 的函数调用都将替换为对wrapper_function 的调用。

那么,我该怎么做呢?

挑战在于传递变量参数和函数签名。

我不想使用宏。

最佳答案

参见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;
}

但是,我真的不明白有什么好处;一是增加开销并基本上关闭类型检查。

关于c - 将多个具有不同参数计数的函数包装在包装函数中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54411081/

相关文章:

c++ - 在 GCC 中使用 -O3 有什么缺点吗?

c - 写一个文件并使其只读

c - RAND_MAX 的问题

C 编译器差异? Char 中的 NUL 控制字符

C预处理器: dynamic const char

c - 线程程序和结构将值更改为垃圾

c - 帮助2个C宏

python - 我的 c 应用程序的 GUI 开发。如何进行?

c - 避免使用 malloc 的方法?

冲突类型错误之前的声明 ___ 在这里