Assignment 3: In-depth understanding of the Callback function (USTC-High Software)

How the callback function works (Callback)

A callback function is a function called through a function pointer. If a function pointer (address) is passed as a parameter to another function, and when this pointer is used to call the function it points to, it is said to be a callback function. The callback function is not called directly by the implementer of the function, but is called by another party when a specific event or condition occurs to respond to the event or condition.
There are two points in the working mechanism of the callback function. First, using a function pointer, when we want to use another unknown function in a certain function, we can pass the pointer of the unknown function to this function. This approach eliminates the need for the caller to know the specific implementation details of the called function;
second, parameter passing, when calling the Callback function, we can pass parameters to it to provide additional context information. In this way, the called function can perform specific operations based on these parameters, keeping the caller and callee decoupled.

The callback function implemented in Lab5.2 is the SearchLinkTableNode() function in the linktable.c file:

/*
 * Search a LinkTableNode from LinkTable
 * int Condition(tLinkTableNode * pNode, void * args);
 */
tLinkTableNode * SearchLinkTableNode(tLinkTable *pLinkTable, 
                        int Condition(tLinkTableNode * pNode, void * args),
                        void * args)
                        //condition函数作为参数传入,call-in方式函数结构
{
    
    
    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;
}

The function here has a callback function named condition as an incoming parameter, and this function will call this condition function during execution. What is passed in is a condition function pointer. The condition function accepts a pointer of LinkTableNode type and a pointer of void type as parameters.

menu.c

int SearchCondition(tLinkTableNode * pLinkTableNode, void * args)
{
    
       //查询条件
    char * cmd = (char*) args;
    tDataNode * pNode = (tDataNode *)pLinkTableNode;
    if(strcmp(pNode->cmd, cmd) == 0)
    {
    
    
        return  SUCCESS;  
    }
    return FAILURE;	       
}

The SearchCondition function in the menu.c file is passed to the SearchLinkTableNode function as a specific condition function Condition (the use of the callback function is implemented in FindCmd)

tDataNode* FindCmd(tLinkTable * head, char * cmd)
{
    
    
    return  (tDataNode*)SearchLinkTableNode(head, SearchCondition, (void*)cmd);
}

Decoupling via parameters

During the implementation of the above callback function, when the FindCmd function in menu.c calls the function SearchLinkTableNode, the pointer of the function SearchCondition is passed in through the parameter. During the execution of the function Search Link Table Node, the SearchCondition function is called.
The SearchLinkTableNode function does not care about the implementation of SearchCondition, as long as the interface of SearchCondition meets the input and output of the specified type. Therefore, we can modify the incoming function pointer at will without changing the function interface, and implement different functions without changing the SearchLinkTableNode to achieve decoupling.

Guess you like

Origin blog.csdn.net/ZHorcrux/article/details/129926272