C | Function pointer and callback function simulation implementation commission and reflection

Home > Sci-Tech

C | Function pointer and callback function simulation implementation commission and reflection

2021-11-28 12:03:42 26 ℃

The function is the core concept of the C language. The main adjustment function (Callee) is a general call relationship. If the modular parameter contains a function pointer, the function pointer can also form a multi-layer call relationship to form a third-party function. Professional terms are called callbacks, and third-party functions called by function pointer parameters are called callback functions. The callback can make the code (herein refer to a function of function pointer to function parameters) more generalized or abstracted, simulating the delegation and reflection grammar of other programming languages.

// C language Simulates the pointer function that needs to be used. The purpose of the function address is achieved by receiving a function address with a pointer function as an address to achieve a method of entrusting other functions. #include typedef void (* fun); // typedef Rename the VOID (*) type to Funvoid Func (FUN); // Tune function void func_1; // callback function 1void func_2; // callback Function 2InT main // Main function is used as an primary function {function (FUNC_1); FUN F = FUNC_2; F; FUNC (FUNC_1); FUNC (FUNC_2); GetChar; Return 0;} Void func (fun f) // Fun F For the address, FUN * F is the amount of address or other {Printf ("funcn"); if (f! = Null) {f;}} void func_1 {printf ("func_1n");} void func_2 {printf ("FUNC_2N");} / * funcfunc_1func_2funcfunc_1funcfunc_2 * /

2 Simple simulated reflection

// C language Simuse reflection // advanced language reflection mechanism, simply, it is possible to obtain a corresponding class or function through a string type. #if 0 # include #include typef void (* callback) (void); type; callback fn;} Callback_t; void f0; Void F1; Callback_t callbacks = {{"cmd0", f0}, {"cmd1", f1},}; void f0 // callback function 0 {Printf ("cmd0");} VOID F1 // callback function 1 {Printf ("cmd1"); } void do_callback (const char * name) {size_t i; for (i = 0; i #define sec __attribute __ (__ section __ ("s"), aligned (sizeof (void *)))) VOID FUNC_1 (INT A , int b) {Printf ("% s% D% DN", __FUNC__, __LINE___, A + B);} Void Func_2 (INT A, INT B) {Printf ("% s% D% DN", __func__, __line__ , A * b);} // The compiler will automatically provide __start_ss, __start_ss flag SS start address extern size_t __start_ss; extern size_t __stop_ss; typef struct {void (* p) (int, int);} node_t; / / Structure Variable A is located in the custom segment sssec node_t a = {.p = func_1,}; sec node_t b = {.p = func_2,}; int main (int Argc, char ** argv) {int a = 3, B = 4; Node_t * p; // Travel Section SS, performs FOR AND __SS; P <(node_t *) & __ start_ss; p <(node_t *) & __ stop_ss; p ++) {P-> P (P <(Node_T *) & __T (P-> P (a) , B); A + = 1; B + = 2;}} / * func_1 6 7func_2 10 24 * / # endif -end-