C ++ encapsulation of callback Functions

Source: Internet
Author: User

Some callback functions are often used during software development. These functions are called callback functions. Use
Callback functions can improve the software structure and reusability. For example, in a large software project
The source or relatively independent processing module is encapsulated in the dynamic connection library (DLL), and then these resources are used in different scenarios through the callback function.
And modules. Callback functions can also be used for complex communication between programs to implement some notification functions. In some cases, callback functions are more effective than messages.
A suitable method; in some special cases, callback functions are more irreplaceable. The Win32 API contains many callback functions.
This function is often used in software design, and sometimes you need to write your own callback function. Therefore
It is necessary to call the function principle and master its basic usage.

C ++ is the most widely used language in the contemporary era, from embedded systems to mainframe systems, from Linux to windows, in Large-Scale Systems
Compilation is everywhere. It has gained the trust of many senior programmers with high efficiency and ease of programming. Developed in DirectX play
In the process, you often need to use the callback function. It is complicated and troublesome to directly use the callback function. c ++ is used to encapsulate the callback function,
It makes the callback function easy and practical, and makes sense for DirectX play and other programming.

A callback function is a function pointer. Write a function and pass the function address to the function pointer.
What is the influence of the original form of the callback function on the use of the callback function by C ++ member functions?

To write a callback function, the original function is consistent. It is easy to ensure that the input and output parameters of the function are consistent. Note
Consistent call type. There are several types of function parameter passing. If the parameter passing method is incorrect, the system must run incorrectly. Generally
It is the parameter passing method of winapi. Pay attention to the differences between the member functions of C ++ classes and General C functions. C ++ class uses this rule
Delivery Function. When using a class member function as the callback function, the member function must be known as a static member function and pay attention to the function sound.
The parameter passing rules must be declared at the same time.

1. static member functions call non-static member functions

Because the existence of static member functions is mentioned in every C ++ book before the generation of class instances, static member functions can only
Call the static member function and use the static member of the class. However, if you write a class that is all static members, it seems that C ++ is lost.
Class. It perfectly encapsulates the class, so that the class cannot be instantiated. For example, compile the callback function for window message processing.
There are countless windows in the system. If you write a class that is all static members, these windows cannot be described. How are static members?
What about non-static members of the class used in the function?

You can pass in parameters to static member functions. Generally, a data block pointer is provided as an input for well-designed callback functions.

Parameter. This structure can be used at this time.
Callback Function Type void fun (void 3 pdata, uint umsg)
Class cfun
{
Static void staticfun (void 3 pdata, uint umsg)
{
Cfun 3 pthisobject = (cfun 3) pdata;
Pthisobject-> fun (umsg); // (1)
}
Void fun (uint nmsg)
{
}
};
In this way, the callback function and the C ++ class form a perfect system. Because (1) the cfun member function is used, this class generates

An instance can be any other function member or data member of the category. In this way, the callback function only connects the C ++ class to the callback function.
Start. However, it has not yet made full use of C ++'s advantages. Now let's change the fun function declaration.

2. Distribution and re-processing of functions

Class cfun

{

Static void staticfun (void 3 pdata, uint umsg );

Protected:

Vitual void fun (uint nmsg );

};

Write a derived class at the same time,

Class csubfun: Public cfun

{

Void fun (uint nmsg );

Void subfun1 ();

Void subfun2 ();

..

}

At the same time, this function is implemented as follows:

Voidcsubfun: Fun (uintnmsg)

{

Switch (nmsg)

{

Case 1: subfun1 (); break;

Case 2: subfun2 (); break;

...

}

}

 

Now we can see that due to the use of virtual functions, C ++'s derived classes also provide function processing capabilities. That is to say,
Classes encapsulated by callback functions can be defined in the base class for implementation. However, if the base class does not meet the requirements for input parameters of the callback function,
You can modify it at any time according to our requirements.

The preceding modification to the base class is implemented as required. The handler of the base class is not called, so it is discarded.
The processing routine of the base class is very undesirable. You can re-design the class. Modify the function fun of a derived class as follows:
Bool csubfun: Fun (uint nmsg)

{

Bhandle = false;
Switch (nmsg)
{
Case1:

Bhandle = subfun1 ();

Break;

Case 2:

Bhandle = subfun2 ();

Break;

...
}
If (bhandle)

Return true;

Else

Returncfun: Fun (nmsg );
}

 

So far, the C ++ class has finally been perfectly integrated with the callback function. All the advantages of C ++ combination inheritance are reproduced. Derived
Class can decide whether to process the message, or even continue to require the base class to process the message after it is processed.

 

3. Simplified Callback Function Distribution

Second, if the number of nmsg is very large, it would seem unpleasant to process the function. You can define several
Macro processing:

# Define declare-fun-table ()/

Virtual void fun (uint nmsg );

# Definebegin-fun-table (theclass, baseclass)

Bool theclass: Fun (uint nmsg ){/

Bhandle = false ;/

Switch (nmsg)

{

# Define on-fun (nmsg, function )/

Case nmsg: bhandle = funcion (); break;

# Define end-fun-table (theclass, baseclass )}/

If (bhandle)

Return true ;/

Else

Return baseclass: Fun (nmsg );

}

 

As a result, the base class remains unchanged, and the derived class changes as follows:

Class csubfun: Public cfun

{

Bool subfun1 ();

Bool sunfun2 ();

...

Declare-fun-table ()

}

Begin-fun-table (csubfun, cfun)

On-fun (1, subfun1)

On-fun (2, subfun2)

End-fun-table (csubfun, cfun)

 

Similar macro structures can be seen in the message ing mechanism of MFC, but they are not implemented using virtual functions. MFC considers virtual machines
This mechanism is too inefficient. There are thousands of messages in windows, and the callback function of window messages is called very frequently. However
The call frequency of a function is much lower than that of the message ing mechanism, and the number of messages is also small. You can use this mechanism. If you want to continue
To upgrade the encapsulation mechanism, you can refer to the Message ing mechanism of the following MFC and the function distribution mechanism of the ATL.

 

Related Article

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.