Callback function Design Method __ function

Source: Internet
Author: User
Tags function prototype static class

Original link: Click to open link

Introduction:        your monitor is not lit, you do not know how to do it, then you ask the big it cousin, your big cousin told you to repair the method, and then need you to operate.      your cousin knows how to do it, but he doesn't get it, but you do it. In other words, your older cousin realizes how to fix your monitor, but he doesn't call it by himself, but by you. So your cousin told you the way to repair the machine is the callback function.      In this analogy, you as the keynote party, there are practical needs-repair the display, but there is no way to ask cousin, cousin to your method is a function address, when you follow the method of the big cousin execution, is the implementation of a callback function.   in engineering design, especially in the design of the underlying library, many times, the library's developers do not predict the future use of this code programmers need this function to do specific work, this time, you need to use the callback design.     c and C + + all provide this kind of callback support, C's suggestion is to use function pointers, callback implementation, C + + through the inheritance of the base class, the base class in the design of virtual functions to achieve. However, according to the author's experience, in this way, C is more agile than C + +, and more flexible. Therefore, in the author's project development, the general use of callback function design, do not use the virtual function mechanism.      callback function is actually the application of function pointers, in C, all the data can be pointer, the function itself can also, when we call a function pointer in the correct configuration, the effect and directly call the function itself, exactly the same.      In addition, due to the modern operating system of C-intimacy, many operating system-level API design can see callback functions, such as our common thread function, even the process itself, is actually the operating system's callback function, Beginthread This kind of initiation thread's call, generally is the specified thread function pointer, in the system's thread table, registers a new table item, the system next round time cycle, automatically according to this table item, callback this pointer, then realizes the application thread to the time slice the acquisition. And, this process, generally is pure C, and C + + Independent.      in a sense, modern parallel computing is based on the C callback model. As a programmer, there should be a deep understanding of callback functions and proficiency in application.      callback functions are very simple to design, but first of all, we need to figure out two bodies, one is the designer of the callback function, one is the user, but both are programmers.      in the following article, use the callback model Designer and user to distinguish between these two identities.  Callback Model Designer:As the designer of the callback model, you first need to define a callback function configuration, because the C language, even if flexible, also need to know what the function prototype is, in order to ensure that the user is properly invoked to avoid crashes.

typedef void (*_app_info_out_callback) (char* szinfo,void* pcallparam);
1, typedef, this is we explicitly define a new variable type, this variable type, is the type of this callback function pointer. Designers and users of this pointer can use the _APP_INFO_OUT_CALLBACK variable type to define their own pointer variables. 2. This callback function uses void as the return value because of this particular application. In fact, a lot of times, there is a convention, the general callback function uses bool as the return value, which in some loops traversal, when the user feel that their data has been found, the loop does not need to continue, you can return false, the designer knows, can no longer loop. This shows that the user is not completely passive to accept the callback, or it can affect the logic of the callback initiator by the return value. 3, char* Szinfo This is the business data, here no longer elaborate. 4, void* Pcallparam, this is very critical, all the designers of the callback function, must help the user to pass a void* pointer, and through each callback call. Example: Create a class that supports callbacks
Classcstultzlowdebug
{public
:
cstultzlowdebug (char* szpathname,
char* szappname,
// The constructor passes in the callback function and parameters, which can be null
_app_info_out_callback pinfooutcallback=null,
void* pinfooutcallbackparam=null);
Save in the object inside, convenient Debug and other function functions call
_app_info_out_callback m_pinfooutcallback;
void* m_pinfooutcallbackparam;
};

The concrete implementation of the constructor function:

Cstultzlowdebug::cstultzlowdebug (char* szpathname,
char* szappname,
_app_info_out_callback Pinfooutcallback,
void* pinfooutcallbackparam)
{
m_pinfooutcallback=pinfooutcallback;//callback function pointer save
m_pinfooutcallbackparam=pinfooutcallbackparam;//parameter pointer save
//...
}

How the designer invokes the callback function

Intcstultzlowdebug::D ebug2file (Char*szformat,...)
{
///...
if (m_pinfooutcallback)//Standard notation, first determine the pointer validity
{
m_pinfooutcallback (szinfoout,//like a function called
m_ Pinfooutcallbackparam)//Here is helping to pass the pointer
}
///...
}
Summarizes the design features of the callback function: 1, first define the callback function prototype, and by the way define a new type of pointer variable. 2. The designer defines a new variable with the pointer variable type of the callback function to implement the parameter passing and data saving. 3, before the call to check the effectiveness of the pointer, to avoid jumping to the null pointer, resulting in a crash. Callback Model consumerAs a user, if the callback function designer is based on the above method design, the call design can also form simple rules and routines. The user must first construct a function in the form of a callback function, which is the future callback function entity, where the designer's module will jump to run. Within this function, the user directly uses the variable szinfo, which is the output of each Debug module.
void Applicationinfomationoutcallback (char* szinfo,void* pcallparam);
But a little note, if it is C, you can declare and implement the function directly. However, in C + + classes, this cannot be written directly. This is because the C + + compiler, for each class member function, provides a default implied pointer this as a parameter, pointing to the object of this instantiation, whose type is the class itself. Therefore, as described below, this function is not correct.
Classcstultzlowdebug
{
private:
    voidapplicationinfomationoutcallback (char* szinfo,void* pcallparam);
The callback function prototype at this point, because it is a class member function with an implied pointer, is equivalent to the following prototype:
Voidapplicationinfomationoutcallback (
cstultzlowdebug*this,//This is the char* szinfo that the C + + compiler forcibly added at compile time
,
void* Pcallparam);
We then compared the callback function prototype and found one more this pointer. Two functions are not a configuration, the function pointer type does not match, the call will fail. As a result, all callback functions, once written in the class, must be decorated with static to be statically member functions.
Classcstultzlowdebug
{
private:
//Please note that here the static modifier
    static void Applicationinfomationoutcallback ( char* szinfo,void* pcallparam);
C + + stipulates that for static class member functions, the implied this pointer will not be provided, so the function's compiled ontology is exactly the same as the writing declaration, so that this function can be used as a callback function. But this brings another problem, that is, without the this pointer, it is inconvenient to use. We know that in the object-oriented design of C + +, the core definition of the object is "a collection of data and all the methods for that data." This is the essence of object-oriented programming. The member function method of a class, generally, and the data contained in the object instantiated by this class related, the program needs to constantly access the object's member variables or other member functions, you need to frequently access this object pointer this. so in C + +, we can have the following solution, namely To pass the argument. and callback function DesignerHave the obligation to pass through a void* parameter pointer to the user when actually used, the this pointer is passed as a parameter to the callback function, so you can use this directly to manipulate the data in the class. In addition, if you have more than one parameter to pass, you need to use the way you passed the struct pointer.







void Applicationinfomationoutcallback (char* szinfo,void* pcallparam);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.