C ++ registration and callback

Registered callback action

  Registered in design mode callback method is called callback mode. In the SDK development in order to enhance the versatility of the developer's SDK, or some sort algorithm logic requires the user to be written. This time we need to pass a callback to the SDK.
Register callback can take the initiative to communicate with the lower top. Thus avoiding the upper kept asking underlying pattern.

Registering the callback process

  Interface Format SDK provides a callback function is registered, to regulate the callback function, such as the return value, parameters and the like. Users write callback function of a fixed format, to call the registered callback function SDK. Of course, the c-registered callback function pointer by implementation, in c ++ may be implemented by function and bind.

Example 1

  This is a simple function call, assume that print () is the sdk api, we directly use the time.

#include<iostream>
#include<cstdio>
using namespace std;
void print(string str);
//----------使用者模块-----------------
int main()
{
	print("hello word");
	system("pause");
	return 0;
}
//----------SDK模块-----------------
void print(string str)
{
	printf(str.c_str());
} 

Example 2

  The use of function pointers to indirectly call

#include<iostream>
#include<cstdio>
using namespace std;
void print(string str);
//----------使用者模块-----------------
int main()
{
	void(*p) (string str);
	p = print;
	p("hello word");
	system("pause");
	return 0;
}
//----------SDK模块-----------------
void print(string str)
{
	printf(str.c_str());
}

  

Example 3

This is a simple callback mode. sdk provide callback function is registered users to provide a callback function.

#include <the iostream> 
#include <cstdio> 
the using namespace STD; 
typedef void (* P) (String S); // use the function pointer to generally typedef 
P P = nullptr a; // create a function object pointer SDK module 
void print (string str); // user module create a callback function 
void print_test (string, P p) ; // register a callback function 
// user module ---------- ---------- ------- 
int main () 
{ 
	print_test ( "Hello Word", Print); 
	System ( "PAUSE"); 
	return 0; 
} 
// --- ---------- callback ------- 
void Print (String str) 
{ 
	printf (str.c_str ()); 
	printf ( "just write"); 
} 
// ---- ---------- SDK module ------------- 
void print_test (String STR, P PRIN) // callback function is registered 
{ 
	P = PRIN; 
	P (STR); 
}

  

Example 4

  Of course, in actual use, the function is often placed on the upper communicating a thread loop waiting for incident response, so communication is not a function and registration function to write together, can not pass the function pointer in the communications function. You need to register separately.

//sdk.h 
typedef void (* REC_CALLBACK) (Long, char *, char *, char *); // call the function format 
REC_CALLBACK record_callback; // create an instance of 
//.cpp 
int register_callback (REC_CALLBACK P) // register a callback function 
{ 
	rec_callback = P; 
	rec_callback_state = to true; 
	return 0; 
} 

init_record () 
{ 
the while (to true) 
{ 
  .......... 
IF (== rec_callback1_state to true) 
	{ 
		rec_callback (Card, Time, card_io, State ); // call the callback function 
	} 
the else 
	{ 
	} 
} 
}

  User Module

print (long, char *, char *, char *) // callback function 
{ 
   printf ( "XXXXX" Long, char ......); 
} 

int main () 
{ 
    register_callback (Print) // registration before use 
    Thread T1 :: STD (init_record); 
    t1.join (); 

}

  

Guess you like

Origin www.cnblogs.com/wjcoding/p/11118421.html