Those things callback function (reprint)

https://www.cnblogs.com/ioleon13/archive/2010/03/02/1676621.html

Practical work for the callback function has been I do not want to touch something that is due to a lot of people do a very deep mystery that is difficult to understand the technology, and secondly in general by including another category pointer it is possible to solve the problem, so they do not want to study this stuff, most recently a project manager is forced to use a callback function, and really entity will be to its advantage, the relationship between the code in the class not to be so complicated class a class B wanted to tell a thing no longer need pointer defined in a, B, and you can define the callback function. I understand the following write callback function out, throw a brick, one taste you want more practice you spectators can appreciate it.

1, Basics

The so-called correction, that is, through a function module A module B b () complete certain functions, but the function b () are unable to achieve full functionality requires far as to call a function in module A of a () to complete this a () is the callback function. As shown below

 

① agreed interface specification. Interface specification must be agreed in module B, is defined callback functions a () function prototype

Is defined herein callback function prototype is best to follow typedef void (* SCT_XXX) (LPVOID lp, const CBParamStruct & cbNode); SCT_XXX callback function name, LP is a callback context, CBParamStruct callback parameter, generally due to the callback parameter is more than one, so definition of a structure convenient.

② registered callback function. In order for the callback function module B know they were going to be used, there must be a function or statement to register a callback function

Registration is defined callback function follows the void RCF_XXX (SCT_XXX pfn, LPVOID lp); RCF_XXX is a registered name of the function, pfn is a callback function name (pointer), lp is the callback context. A module generally complete after initialization module B calls the callback function address assignment defined in the module A to pfn, lp assignment is this. 

③ do things in module A:

First, the callback function declared as static, static void CF_XXX (LPVOID lp, const CBParamStruct & cbNode); function of the parameters must be consistent with the callback parameter module B function prototype.

B initialization module calls the function module address register A declared CF_XXX passed to the callback function PFN, i.e. pfn = CF_XXX; (CF_XXX function name is actually a pointer to the address of the callback function).

 2, for example

The callback function uses the first scene: MFC interface programming. There is such a demand, the left main interface is a tree list, the right of the plot area is used to display a list of content items on the left side, double-popup drawing area for editing. The general practice is the main dialog when the dialog is initialized or the drawing area list pointer passed in a tree, double-click event handling box in the drawing area, call the main event in the list or tree out of the pointers in the completion of the function update operation. Such dialog between the main dialog class and class drawing area there have been included in relation to each other, the callback function can flourish this time, the main dialog box only needs to contain the plot area of ​​the header file and declare a drawing area dialog the objects can be. Specifically: the definition in the drawing area dialog callback function prototypes and registered callback, and handle mouse double-click events, issued a notice in the event callback function. The main dialog callback function defined by the prototype, to complete the update of tree list in the callback function.

The second scenario callback function: network programming. Network programming, in order to reflect the modular generally to communications and data processing division open, i.e., the communication protocol module is responsible for the definition, a data transceiver, the data processing module is responsible for data transmission and reception is only parsed and packaged, if the communication module opens a thread continuously receive data, this time the question is, what it means to the data submitted by the data processing module in the hands of it? Each time data is received, the data to get the pointer to the relevant processing module to complete the operation, so that there are made two errors class pointer points to another, but also destroy the independence of the two modules. Use callback these problems are solved, the following pseudo-code is given part:

Communication Module
void typedef (* DataReceiveCBFunc) (ReceiveParam & recvParam); //  callback function prototype is defined  
 
// 开始接收,数据处理模块调用,相对于注册回调函数
static BOOL StartReceive(DataReceiveCBFunc pfnData, LPVOID lpContext,……);
// 接收数据的线程,一收到数据就通知回调
static UINT TH_Receive(LPVOID lp);

 

 
数据处理模块
// 开始接收数据,开启监听线程,调用上面的StartReceive函数
int StartReceiveInfo(int nListenPort, std::string strLocalIP);
// 数据接收回调函数,被CUdpEx::TH_Receive()回调
static void RecvInfoCallback(ReceiveParam &recvParam);  

Guess you like

Origin www.cnblogs.com/CodeWorkerLiMing/p/11221723.html