gpt4 book ai didi

C:将任何函数传递给函数

转载 作者:行者123 更新时间:2023-12-04 10:53:40 25 4
gpt4 key购买 nike

我想知道我将如何将任何函数传递给一个函数,就像在一个可以接受任何函数的通用函数指针中一样,这样做的目标是建立一个析构系统,所以基本上存储函数和用它的参数调用它也存储在后面,

类似于:

Defer(SDL_DestroyWindow, Window); 

我已经处理了参数,但我不知道如何管理其中的函数指针部分,谢谢!

编辑:我添加了更多信息......

typedef struct {
void** args;
} IM_Defer_Resource;
/* Defer & Whatnot */

IM_Stack* IM_Defer_Stack;

void IM_Defer_Init() {
IM_Defer_Stack = IM_Stack_Init();
}


void IM_Defer(/* What to put here? */) {

}

void IM_Defer_All() {
while(IM_Defer_Stack->size) {
IM_Defer_Resource* resource = IM_Stack_Pop(IM_Defer_Stack);
if(!resource) continue;
/* What to do */
}
}

我没有 defer 的实际功能,但我确实将每个参数复制到堆栈中并可以成功弹出它们,但我不知道如何实现可变函数调用

编辑2:收到一些意见后:我认为这会更可行:推迟(SDL_DestroyWindow,“SDL_Window *”,窗口);我正在集思广益,但我希望能提供一些意见

编辑3:

/* Defer & Whatnot */
typedef struct {
char** types;
void** args;
int count;
} IM_Defer_Resource;


IM_Stack* IM_Defer_Stack;

void IM_Defer_Init() {
IM_Defer_Stack = IM_Stack_Init(IM_Get_Stack_Type(IM_Defer_Resource));
}

void IM_Defer_Internal(void* var, int n, ...) {
char* type;
void* arg;

va_list args;
va_start(args, n);

IM_Defer_Resource resource;
int count = n / 2;
resource->types = calloc(count, sizeof(char*));
resource->args = calloc(count, sizeof(void*));
resource->count = count;

for(count > 0; n -= 1) {
type = va_arg(args, char*);
resource->types[count-1] = type;
arg = va_arg(args, void*);
resource->args[count-1] = arg;
}

IM_Stack_Push(IM_Defer_Stack, &resource);
}

void IM_Defer_All() {
while(IM_Defer_Stack->size) {
IM_Defer_Resource* resource = IM_Stack_Pop(IM_Defer_Stack);
if(!resource) continue;
/* I have a char* and a void* to the resource, Now what? */
free(resource->types);
free(resource->args);
}
}

这是我想出的,但我想知道如何将 char* 转换为类型...

最佳答案

正如我在评论中所说,一个大问题是当声明可变参数函数时,未声明的参数受制于默认参数提升。这意味着您会发现传递的参数与函数预期的参数不同,这最终会导致异常。你想做的是可行的,但真的很复杂。

一个解决方案,但由于需要大量编码而受到限制,可能是:

#include <stdarg.h>
#include <stdio.h>

typedef enum { fn1, fn2, fn3, /*....*/} e_fn;

void multi_fun(e_fn fn, ...)
{
va_list ap;
int j;

va_start(ap, fn); /* Requires the last fixed parameter (to get the address) */

switch(fn)
{
case fn1:
{
//suppose prototype for fn1 to be void fn1_fn(int, float, struct mystruct *);
int this_int = va_arg(ap, int);
float this_float = va_arg(ap, float);
struct mystruct *this_struct = va_arg(ap, struct mystruct *);
fn1_fn(this_int, this_float, this_struct);
break;
}

case fn2:
{
...
}
}
va_end(ap);
}

关于C:将任何函数传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45000435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com