Online check 1, a little experience, especially to share and discussion.


SECTION 1


The callback function

Callbacks are a concept we hear all the time, such as the WinProc function we encountered during Windows API programming, which is the function we wrote and called by the operating system. Now, we need to document this problem slowly and in detail.

Library and consumer issues

Before we begin, let’s imagine a scenario where a large software company develops a software library and makes it available to customers. In this sentence, there are two opposites, one is the software company, the other is the user. Obviously, software companies are powerful, and they’re powerful because they’re smart. See the picture below:

The library developer is the provider, they don’t know what the user wants to do, the library developer’s information to the user is unknown, because the people on both sides have never met each other, just like a person in the United States and a person in China. They are clever in that they can solve a user’s problem in a universal way, even if they don’t know what the user is doing. In other words, the library developer can adapt to whatever the user does. That’s the magic of the problem, of course, in order to achieve this effect, there has to be a rule.

The rules

Now rules, which are easier to understand, are constraints that two parties agree on. For example, a and B, a said to B, as long as you provide me with a plastic ball, I can color it. The constraint here is the plastic ball, (of course, the constraint here is very general to simplify the problem), at this point, if b gives him a crystal ball, or gives him a car, it will not do, because it is beyond the constraint. Therefore, the library developer can adapt to the various changes of the user is that he also gives the user a certain constraint, is under the constraint of adaptation, this is also self-evident.

There are no secrets under the code

The following code perfectly illustrates the above problem:

#include <stdio.h>

typedef int student_id;

typedef int student_age;

typedef struct _Student{

student_id id;

student_age age;

}Student;

// Type pedef bool (*pFun)(Student, Student);

/ / — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — / / bubble sort method: You can sort by AGE or ID, With the same function / / — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — void sort (Student stu [], const int num, pFun fun) {Student temp; for(int i = 0; i < num; ++i)

{ for(int j = 0; j < num – i -1; ++j) { if((*fun)(stu[j],stu[j+1])) { temp = stu[j]; stu[j] = stu[j+1]; stu[j+1] = temp; }}}}

/ / — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — / / callback function: Comparison of age / / — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — bool CompareAge (Student stu1, Student stu2) {/ / changes from big to small, or the sequence of the growing up, We just do the reverse. if(stu1.age < stu2.age) return true; return false; } / / — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — / / callback function: Compare id / / — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — — bool CompareId (Student stu1, Student stu2) {/ / changes from big to small, or the sequence of the growing up, We just do the reverse. if(stu1.id < stu2.id) return true; return false; }

Int main () {Student stu [] = {{1103, 24}, {1102, 23}, {1104, 2}, {1107}, {1105,}}; pFun fun = CompareAge; int size = sizeof(stu)/sizeof(Student); sort(stu,size,fun); for(int i = 0; i < size; ++i){ printf(“%d %d\n”,stu[i].id,stu[i].age); } return 0; }

 

Through the code, we must distinguish which is the library provider and which is the user:

sort(…) The method is provided by the library developer, but it is written in a file because of simulation problems. We can assume that it is stored in some static library, lib, so there is no boundary with it.

Library developer provides method sort(…) The parameter also contains a pointer to a function that calls the user-written function, the callback function. So it provides rules for what the parameters of the callback function are and what the return value is. If the user writes a function without knowing anything about the parameters and return value of the callback function, does sort accept that? Obviously not.

So how does the user know the parameters and return values of the callback function? The library developer will tell, or state in its documentation, that the parameters in the WinProc() function, as provided in Windows API development, must be identical. (This is where interfaces are involved.)

What is a return?

From the code above, we know that the library developer provided a method with a function pointer to the parameter that the user wrote and called, and called the function in its implementation. From the standpoint of the library developer, we can sum it up: the library developer calls the user’s function (the callback function).

Now, moving to the point of view of the callback function, it is called. However, it also has parameters, and the parameters passed in are the data provided by the library developer. Here we can sum up: the callback function calls the library developer’s data.

This is the meaning of “call back”, you call me and I call you, both sides call each other.

What is the point of the callback function?

Based on the above analysis, I found that the callback function is not difficult to understand, I think it is important to make clear who is the library developer (also known as provider, caller), and who is the user.


SECTION 2


tip1

Imagine that part of your function is outsourced to someone else. Don’t worry about how it’s implemented. Your function has a complete function, but some functions can be customized. See for_each in STL

tip2

To put it simply, the user is the implementer, and the implementer needs to call the function A (). However, in order for the function A () to be universal, the implementer needs to call the function cbB () provided by the implementer according to the will of the implementer, where cbB () is the callback function. Callbacks are very useful in Windows programming.


SECTION 3


A callback function is a function called via a function pointer: passing the function’s pointer (address) as an argument to another function is called a callback function when the pointer is used to call the function to which it points. A callback function is not called directly by the implementers of the function, but is called by another party when a particular event or condition occurs in response to that event or condition.

 

In plain English, if C of class B is called from class A, and D of class A is called from class B, D is the callback function.

 

For example:

We think of type A as A person and call him little A; Treat type B as a person and call him little B;

The process of using the callback function D can be understood as follows:

Little A encountered A trouble in the development process, and only little B could solve this trouble, so little A found little B to ask him for help, but because they were not familiar with little B, so they brought their business card. Small A to small B after the trouble, it happened that small B is busy with other things, so small B took A small A business card, tell small A to go back and other news. Because this trouble is not solved can not continue to develop, so back to wait for the news of small A had to do other things first. After A while the little B busy hands, solves the small A trouble, find out the phone number of the small A business card to dial the past, told the A, trouble has been done (small B just follow the card tell the solution to give little A, while does not care about their solution in the small A there will be how to use). After little A put down the phone, he continued to develop the solution given to him by little B.

In short, LITTLE A brought his business card with D and asked little B for help through C. Little B could not solve the problem immediately, so he accepted his business card D. One day after little B solved the problem of little A, he told little A the solution through his business card D.

That is:

Class A calls the C method of class B, with D as A function pointer as an argument to the C method.

Class B can not be handled immediately, so it is marked with callback function first.

At some point in the future, when the trigger condition is met (after the problem is solved)

Pass the message back to class A via the callback function D (tell the result via the business card)

 

Here is an example of the above process (see note for explanation) :

  1. #include
  1. typedef void (*Fun)(int); // Defines a function pointer type
  1. Fun p = NULL; // with Fun Define a variable p , it points to a return value with a null parameter as int The function of
  1. void caller(Fun pCallback)
  1. {
  2. p = pCallback;
  1. // After a certain condition is reached, pass the card (function pointer p ), returns the result
  1. int result = 1;
  1. (*p)(result);
  2. }
  1. void callback(int a) // The callback function
  1. {
  1. std::cout << “callback result = ” << a << std::endl;
  1. }
  1. int main(int argc, char* argv[])
  1. {
  2. caller(callback);
  3. getchar();
  1. return 0;
  1. }