Void** 回调混乱
我正在使用一个开源映射工具,它能够使用 g_io_add_watch 来监视文件描述符。我正在尝试观察标准输入文件描述符,并且当标准输入接收数据时我已经成功地能够调用回调。但是我无法读取这些数据。我正在尝试修改回调以接收开源 api 处理程序中描述的信息。
编辑** 我的问题首先是什么是 void** 以及在这种情况下如何使用它。我希望向我的回调函数提供为您在下面看到的 readUserInput_two 函数描述的参数,并且我需要通过下面描述的回调系统传递它。我很困惑我需要传递什么作为我的 void** 以允许当前实现传递这些参数。
看起来像这样,
readUserInput_two(GIOChannel *ioch, GIOCondition cond, gpointer data)
我想将此指针存储到回调中,该回调定义为在
struct callback {
void (*func)();
int pcount;
enum attr_type type;
void *p[0];
};
用于实例化回调的函数中,您提供以下参数
callback_new(void (*func)(void), int pcount, void **p)
在此调用中,pcount是函数指针应具有的参数数量,我相信void**p 是这些参数的类型或类似的东西,但我似乎无法弄清楚。
当回调被触发时,它会调用在定义语句中定义的函数,看起来就像这样。
#define callback_call_3(cb,p1,p2,p3) callback_call_args(cb, 3, p1, p2, p3)
这个函数是为各种数量的 p 的 ex 定义的。 callback_call_1(cb,p1) 也存在。任何人都可以解释我如何让我的回调函数接收这些数据字段。调用callback_call_3的中间回调触发函数如下所示。
static gboolean
navfocus_call_watch(GIOChannel * iochan, GIOCondition condition, gpointer t)
{
struct callback *cb=t;
if(cb->pcount == 1)//I added this if before it only had callback_call_0
{
callback_call_0(cb);
}
else if(cb->pcount == 4)
{
callback_call_3(cb,iochan,condition,t);
}
return TRUE;
}
I am working with a open source mapping tool and it has the ability to use g_io_add_watch to watch file descriptors. I am trying to watch the stdin file descriptor and I have successfully be able to have the callback be called when the stdin recieves data. I am not able to read this data however. I am trying to modify the callback to recieve the infromation described int the handler for the open source api.
EDIT** My question is first off what is a void** and how would it be used in this case. I want my call back function to be provided the parameters that are described for the readUserInput_two function you see below, and I need to pass it through the callback system described below. I am confused as to what I would need to pass as my void** to allow the current implementation to pass those parameters.
It looks like this
readUserInput_two(GIOChannel *ioch, GIOCondition cond, gpointer data)
I want to store this pointer into the callback which is defined as
struct callback {
void (*func)();
int pcount;
enum attr_type type;
void *p[0];
};
In the function that is used to instantiate a callback you provide the following parameters
callback_new(void (*func)(void), int pcount, void **p)
In this call pcount is the number of parameters your function pointer should have, and i believe void**p is either the type of those parameters or something of the sort but I cant seem to figure it out.
When the callback is fired it calls a function defined in a define statement that looks as such.
#define callback_call_3(cb,p1,p2,p3) callback_call_args(cb, 3, p1, p2, p3)
This function is defined for a variety of number of p's ex. callback_call_1(cb,p1) also exists. Can anyone explain how I might go about getting my callback function to recieve those data fields. the intermediate callback fire function that calls callback_call_3 looks like this .
static gboolean
navfocus_call_watch(GIOChannel * iochan, GIOCondition condition, gpointer t)
{
struct callback *cb=t;
if(cb->pcount == 1)//I added this if before it only had callback_call_0
{
callback_call_0(cb);
}
else if(cb->pcount == 4)
{
callback_call_3(cb,iochan,condition,t);
}
return TRUE;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
void** 是指向 void 类型数据(无类型)的指针。 void* 用作泛型类型来指向任何类型变量的基地址。在这种情况下, void** 期望保存多个 void* 变量。您可以将其称为空数组。
根据从callback_new() 发送的参数数量,可以调用各种函数。为方便起见,编写了宏。
您需要将这些参数嵌入到 void** p 中。您可以按如下方式为 void** p 数组分配内存:
您需要按照调用的最终函数所需的相同顺序嵌入它们。
对于 3 个参数函数来说:
现在将 void** p 发送到callback_new(theFunctionPointer, 3, p);正如您所解释的,将根据参数的数量调用适当的函数。
void** is a pointer to pointer of a void type data (no type). void* is used as a generic type to point to the base address of any type variable. In this case, the void** expects to hold multiple void* variables. You can call it as void array.
There are various functions to be called which are based on the number of parameters sent from the callback_new(). There are macros written for convenience.
You need to embed those parameters into void** p. You can allocate the memory for the void** p array as follows:
You need to embed them in the same order required by the final function that is called.
Say for 3 parameter function:
Now send the void** p to callback_new(theFunctionPointer, 3, p); and as you explained, appropriate function will be called based on number of parameters.
void**
是指向 void 的指针的类型。至于如何在您的情况下使用它,您必须查阅文档。我无法从您发布的代码片段中看出,变量名“p”也没有真正的帮助。
编辑:至于向您提供原始数据的回调,回调函数没有任何参数。该函数如何获取您的指针?
Edit2:我明白了,这与 pcount 有关。但是回调的声明是错误的,因为它声明回调不接受任何参数......
A
void**
is a type for a pointer to a pointer to void.As to how to use it in your case, you'll have to consult the documentation. I can't tell from the snippet of code you posted and a variable name of "p" doens't really help either.
Edit: As to your call back being provided your original data, the callback function doesn't have any arguments. How does the function get your pointer?
Edit2: I see, it's to do with pcount. But then the declaration of the callback is wrong as it states that the callback does not take any arguments...