Linktable增加Callback方式的接口 作者:258

回调函数介绍

回调函数是一种函数,它作为参数传递给另一个函数,并在该函数完成后被调用。回调函数通常用于异步编程,例如处理事件或执行异步操作,可以在操作完成后通知调用者。回调函数是一种常见的编程模式,例如在JavaScript中,我们常常使用回调函数来处理异步操作。

回调函数的基本思想是将一个函数作为参数传递给另一个函数,使得后者可以在适当的时候调用前者。例如,在JavaScript中,我们可以将一个函数作为回调函数传递给setTimeout函数,以便在一定时间后调用该函数。在回调函数中,我们可以执行一些任务,例如更新页面或者处理数据。回调函数还可以用于处理事件,例如点击按钮或者滚动页面。在这种情况下,我们可以将一个函数作为事件处理函数,当事件发生时,该函数会被调用,以便处理事件。

回调函数可以使编程更加灵活和高效,但也容易导致代码复杂和难以维护。因此,在使用回调函数时,需要考虑如何合理地组织代码,避免出现回调地狱(Callback Hell)等问题。为了避免回调地狱,我们可以使用Promise、async/await等技术来改进异步编程。

回调函数不是实现该函数的软件模块直接调用,而是在特定的事件或条件发生时由另外的软件模块通过函数指针的方式调用,用于对该事件或条件进行响应,是一种下层软件模块调用上层软件模块的特殊方式

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

SearchLinkTableNode函数接口对应的代码如下:

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

menu菜单项目中采用Callback方式的函数接口来查询链表的应用示例代码如下:回调函数用于判断cmd的指令和链表节点所存入的指令是否相同。

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 *类型,以便于今后其他业务模块复用此模块。

项目完整源码如下:

linktableInternal.h文件

#include <pthread.h>
 
/*
 * LinkTable Node Type
 */
struct LinkTableNode
{
    struct LinkTableNode * pNext;
};
 
/*
 * LinkTable Type
 */
struct LinkTable
{
    struct LinkTableNode *pHead;
    struct LinkTableNode *pTail;
    int SumOfNode;
    pthread_mutex_t mutex;
};

linktable.h文件

#ifndef _LINK_TABLE_H_
#define _LINK_TABLE_H_
 
#include "linktableInternal.h"
 
#define SUCCESS 0
#define FAILURE (-1)
 
/*
 * LinkTable Node Type
 */
typedef struct LinkTableNode tLinkTableNode;
 
/*
 * LinkTable Type
 */
typedef struct LinkTable tLinkTable;
 
/*
 * Create a LinkTable
 */
tLinkTable * CreateLinkTable();
 
/*
 * Delete a LinkTable
 */
int DeleteLinkTable(tLinkTable *pLinkTable);
 
/*
 * Add a LinkTableNode to LinkTable
 */
int AddLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode);
 
/*
 * Delete a LinkTableNode from LinkTable
 */
int DelLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode);
 
/*
 * Serach a LinkTableNode from LinkTable
 * int Condition(tLinkTableNode *pNode, void * args);
 */
tLinkTableNode * SearchLinkTableNode(tLinkTable *pLinkTable, int Condition(tLinkTableNode *pNode, void *args), void * args);
 
/*
 * get LinkTableHead
 */
tLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable);
 
/*
 * get next LinkTableNode
 */
tLinkTableNode *GetNextLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode);
 
#endif /* _LINK_TABLE_H_ */

linktable.c文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "linktable.h"
 
/*
 * Create a LinkTable
 */
tLinkTable * CreateLinkTable()
{
    tLinkTable * pLinkTable = (tLinkTable *)malloc(sizeof(tLinkTable));
    if (pLinkTable == NULL)
    {
        return NULL;
    }
    pLinkTable->pHead = NULL;
    pLinkTable->pTail = NULL;
    pLinkTable->SumOfNode = 0;
    pthread_mutex_init(&(pLinkTable->mutex), NULL);
    return pLinkTable;
}
 
/*
 * Delete a LinkTable
 */
int DeleteLinkTable(tLinkTable *pLinkTable)
{
    if (pLinkTable == NULL)
    {
        return FAILURE;
    }
    while (pLinkTable->pHead != NULL)
    {
        tLinkTableNode *p = pLinkTable->pHead;
        pthread_mutex_lock(&(pLinkTable->mutex));
        pLinkTable->pHead = pLinkTable->pHead->pNext;
        pLinkTable->SumOfNode -= 1;
        pthread_mutex_unlock(&(pLinkTable->mutex));
        free(p);
    }
    pLinkTable->pHead = NULL;
    pLinkTable->pTail = NULL;
    pLinkTable->SumOfNode = 0;
    pthread_mutex_destroy(&(pLinkTable->mutex));
    free(pLinkTable);
    return SUCCESS;
}
 
/*
 * Add a LinkTableNode to LinkTable
 */
int AddLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode)
{
    if (pLinkTable == NULL || pNode == NULL)
    {
        return FAILURE;
    }
    pNode->pNext = NULL;
    pthread_mutex_lock(&(pLinkTable->mutex));
    if (pLinkTable->pHead == NULL)
    {
        pLinkTable->pHead = pNode;
    }
    if (pLinkTable->pTail == NULL)
    {
        pLinkTable->pTail = pNode;
    }
    else
    {
        pLinkTable->pTail->pNext = pNode;
        pLinkTable->pTail = pNode;
    }
    pLinkTable->SumOfNode += 1;
    pthread_mutex_unlock(&(pLinkTable->mutex));
    return SUCCESS;
}
 
/*
 * Delete a LinkTableNode from LinkTable
 */
int DelLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode)
{
    if (pLinkTable == NULL || pNode == NULL)
    {
        return FAILURE;
    }
    pthread_mutex_lock(&(pLinkTable->mutex));
    if (pLinkTable->pHead == pNode)
    {
        pLinkTable->pHead = pLinkTable->pHead->pNext;
        pLinkTable->SumOfNode -= 1;
        if (pLinkTable->SumOfNode == 0)
        {
            pLinkTable->pTail = NULL;
        }
        pthread_mutex_unlock(&(pLinkTable->mutex));
        return SUCCESS;
    }
    tLinkTableNode * pTempNode = pLinkTable->pHead;
    while (pTempNode != NULL)
    {
        if (pTempNode->pNext == pNode)
        {
            pTempNode->pNext = pTempNode->pNext->pNext;
            pLinkTable->SumOfNode -= 1;
            if (pLinkTable->SumOfNode == 0)
            {
                pLinkTable->pTail = NULL;
            }
            pthread_mutex_unlock(&(pLinkTable->mutex));
            return SUCCESS;
        }
        pTempNode = pTempNode->pNext;
    }
    pthread_mutex_unlock(&(pLinkTable->mutex));
    return FAILURE;
}
 
/*
 * 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;
}
 
/*
 * get LinkTableHead
 */
tLinkTableNode * GetLinkTableHead(tLinkTable *pLinkTable)
{
    if (pLinkTable == NULL)
    {
        return NULL;
    }
    return pLinkTable->pHead;
}
 
/*
 * get next LinkTableNode
 */
tLinkTableNode *GetNextLinkTableNode(tLinkTable *pLinkTable, tLinkTableNode *pNode)
{
    if (pLinkTable == NULL || pNode == NULL)
    {
        return NULL;
    }
    tLinkTableNode * pTempNode = pLinkTable->pHead;
    while (pTempNode != NULL)
    {
        if (pTempNode == pNode)
        {
            return pTempNode->pNext;
        }
        pTempNode = pTempNode->pNext;
    }
    return NULL;
}

menu.c文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "linktable.h"
 
int Help();
int Quit();
 
#define CMD_MAX_LEN 128
#define DESC_LEN 1024
#define CMD_NUM 10
 
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);
}
 
/* show all cmd in listlist */
int ShowAllCmd(tLinkTable * head)
{
    tDataNode * pNode = (tDataNode *)GetLinkTableHead(head);
    while (pNode != NULL)
    {
        printf("%s - %s\n", pNode->cmd, pNode->desc);
        pNode = (tDataNode *)GetNextLinkTableNode(head, (tLinkTableNode *)pNode);
    }
    return 0;
}
 
int InitMenuData(tLinkTable ** ppLinktable)
{
    *ppLinktable = CreateLinkTable();
    tDataNode* pNode = (tDataNode *)malloc(sizeof(tDataNode));
    pNode->cmd = "help";
    pNode->desc = "Menu List:";
    pNode->handler = Help;
    AddLinkTableNode(*ppLinktable, (tLinkTableNode *)pNode);
 
    pNode = (tDataNode *)malloc(sizeof(tDataNode));
    pNode->cmd = "version";
    pNode->desc = "Menu Program V1.0";
    pNode->handler = NULL;
    AddLinkTableNode(*ppLinktable, (tLinkTableNode *)pNode);
 
    pNode = (tDataNode *)malloc(sizeof(tDataNode));
    pNode->cmd = "quit";
    pNode->desc = "Quit from Menu Program V1.0";
    pNode->handler = Quit;
    AddLinkTableNode(*ppLinktable, (tLinkTableNode *)pNode);
 
    return 0;
}
 
/* menu program */
 
tLinkTable * head = NULL;
 
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;
}
 
int Help()
{
    printf("This is help can do for you:\n");
    ShowAllCmd(head);
    return 0;
}
 
int Quit()
{
    printf("Bye Bye\n");
    exit(0);
}

作者:258

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值