Send a custom "message" with a callback function How to do
1, define the calorific prototype and callback message
Here we define the parameters of the callback function to be 3, the type is the first one for the primary ID, the second is the sub-ID, the third is the transmitted data. (You can set the number and type of the parameters of the callback function according to your own needs.)
TYPEDEF VOID (* CallbackMessageFunc) (Unsigned Int Imainid,
Unsigned int icholder,
Unsigned int idata = 0);
Define the primary message
// main msg
ENUM Main_MSG
{
Output_info,
EXIT_PROGRAM,
// Other MSG
Main_msg_count
}
Define sub-message
// Child MSG of Output_info
ENUM Child_Output_info
{
Hello_world,
SHOW_ME_THE_MONEY,
// ....
}
2, define the message reception function
This function is a global function or a static member function of the class, and the form is the same as the original form of the callback function. The static member function of this class is used as the message reception function.
Class CMSGReceiver
{
PUBLIC:
Static void onprocessmsg (unsigned int ing)
Unsigned int icholder,
Unsigned int idata = 0);
}
3, define the user of the callback function
Class CMSGSender
{
PUBLIC:
Void RegisterMsgreceiver (CallbackMessageFunc FPMSGReceiver);
Void SendMessage (unsigned Int iMainid,
Unsigned int icholder,
Unsigned int idata = 0);
PRIVATE: CallbackMessageFunc M_FPMSG Shent;
}
4, use example:
CMSG Shender Msg Shender;
Msg Shender.Registermsgreceiver (CMSGReceiver :: onprocessmsg);
Msg Shender.sendMessage (Output_info, Hello_World);
5, code download
code