Callback 函数的工作机制

首先我们要明确什么是回调函数,以及回调函数什么时候发生作用。

回调函数是通过函数指针调用的函数。把函数的指针(地址)作为参数传递给另一个函数,当这个指针调用其所指向的函数时,就称这是回调函数。回调函数不是实现该函数的软件模块直接调用,而是在特定的事件或条件发生时由另外的软件模块通过函数指针的方式调用,用于对该事件或条件进行响应,是一种下层软件模块调用上层软件模块的特殊方式。

给Linktable增加Callback方式的接口,需要两个函数接口,一个是call-in方式函数,如SearchLinkTableNode函数,其中有一个函数作为参数,这个作为参数的函数就是callback函数,即代码中的Conditon函数。由于我们在工程化C编程中追求松散耦合,所以给call-in方式的函数接口SearchLinkTableNode增加了一个参数args,callback函数Conditon也增加了一个参数args。args用于传递用户输入的菜单命令(help、version和quit)。

/*
 * Serach a LinkTableNode from LinkTable
 * int Condition(tLinkTableNode *pNode, void * args);
 */
tLinkTableNode * SearchLinkTableNode(tLinkTable *pLinkTable, int Condition(tLinkTableNode *pNode, void *args), void * args);

该接口对应的实现代码如下:

/*
 * Serach a LinkTableNode from LinkTable
 * int Condition(tLinkTableNode *pNode, void * args);
 */
tLinkTableNode * SearchLinkTableNode(tLinkTable *pLinkTable, 
                        int Condition(tLinkTableNode *pNode, void *args), 
                        void * args)
{
    if (pLinkTable == NULL || Condition == NULL)
    {
        return NULL;
    }
    tLinkTableNode * pNode = pLinkTable->pHead;
    while (pNode != NULL)
    {
        if (Condition(pNode, args) == SUCCESS)
        {
            return pNode;
        }
        pNode = pNode->pNext;
    }
    return NULL;
}

显然该实现代码中没有实现Condition函数,而Condition是用户代码传进来的一个函数指针,只是我们在回调函数接口定义时规定了传进来的函数指针的规格。

再来看menu菜单项目中采用Callback方式的函数接口来查询链表的应用示例代码。

typedef struct DataNode
{
    tLinkTableNode head;
    char* cmd;
    char* desc;
    int (*handler)();
}tDataNode;
 
int SearchCondition(tLinkTableNode *pLinkTableNode, void *args)
{
    char * cmd = (char*) args;
    tDataNode * pNode = (tDataNode *)pLinkTableNode;
    if (strcmp(pNode->cmd, cmd) == 0)
    {
        return SUCCESS;
    }
    return FAILURE;
}
 
/* find a cmd in the linklist and return the datanode pointer */
tDataNode* FindCmd(tLinkTable * head, char * cmd)
{
    return (tDataNode *)SearchLinkTableNode(head, SearchCondition, (void *)cmd);
}

 main函数中调用FindCmd函数如下:

int main()
{
    InitMenuData(&head);
    /* cmd line begins */
    while (1)
    {
        char cmd[CMD_MAX_LEN];
        printf("Input cmd > ");
        scanf("%s", cmd);
        tDataNode *p = FindCmd(head, cmd);
        if (p == NULL)
        {
            printf("This is a wrong cmd!\n");
            continue;
        }
        printf("%s - %s\n", p->cmd, p->desc); 
        if (p->handler != NULL)
        {
            p->handler();
        }
    }
 
    return 0;
}

由于在具体业务模块,我们已经明确传入的args参数为用户输入的指令(如help、version或quit),所以我们可以将void *args强制类型转换为char *类型,然后根据char *类型进行后续的处理。

但是在通用软件模块,我们为了保证此模块的通用性,需要将传入的参数args定义为void *类型,以便于今后其他业务模块复用此模块。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值