A method to resolve the problem of C + + Mutual header file--callback function

Source: Internet
Author: User

Recently wrote two communication protocol program, are the power system in the question-and-answer transmission specification, one is based on the TCP transmission file 102 Protocol Server program, one is based on the serial port data acquisition 102 The protocol client program. There were other communication protocols, and recently more intense expectations were able to abstract out Some of the common parts of these protocol programs, so that when new protocols came in, I could work less. In general, I divide these communication programs into the following categories:

1.CommunicationAgreement class, mainly responsible forfollow a certain transfer protocol toassemble the message, disassemble the message, including the entire character's message (master, sub-station)
2. Communication Controlclasses, including sub-classes of various communication controls:TCP, serial, etc., are mainly responsible for establishing Communication, closeCommunication, sending messages, receiving messages
3, the server logic class, mainly on the service side of the role of processing logic, with 1,2 Two types of member variables
4, the client logic class, mainly standing on the client's role processing logic, with 1,2 Two types of member variables

5. interface classes, which are responsible for interacting with users and logical classes

Sounds very beautiful, do logic class time also is smooth, to do interface class and logic class interaction when the tangled ~ I have obsessive-compulsive disorder, if the structure is too cumbersome on the heart plug ~ before writing other agreements, Because just contact also do not know business, time is tight, had to endure the logic class and interface class kneading together, this said what also want to separate them! Unfortunately, the interface class can be easily output log to the interface, but can not send and receive messages directly, and the logical class is the opposite, although the control of the message, for the output message to the interface is short legs ... And I don't want these two classes to include header files with each other ... Well, obsessive-compulsive disorder is a ==| | .....

After the little master's point, I used a callback function to solve the problem. Roughly as follows:

First, add a static callback function to the interface class:

static void CALLBACK Printlogcallback (void* pinstance, const char* str_log, int len);


Do not underestimate the first parameter, he can use the interface class in the non-static strength of the winning magic weapon Ah! This can be written when implementing this function:

void Ccom102dlg::P rintlogcallback (void* pinstance, const char* str_log, int len) {ccom102dlg* Pcom102dlg = (ccom102dlg*    ) Pinstance; And then take Pcom102dlg this bingfu, what do you want to do?//For example, you can call a custom print log method like this://pcom102dlg->appendlog (Str_log, Len);}


This completes the first step, and the second step is to add the interface to the logical class that invokes the callback function, as follows:

typedef void (CALLBACK *printlogcallback) (void* pinstance, const char* str_log, int len); Set the callback function required to interact with the interface class, save the function pointer void setinterfaceengine (void *pinstance, Printlogcallback printlog);//    A struct struct interfaceengine{void *pinstance that holds information about a callback function; Printlogcallback Printlog;};i Nterfaceengine m_engine;//Logic class print log function void Printlog (const char* buf, int len);


Then, when you need to use the Print log function of the interface class, find your saved function pointer for you to do it!

void Cclasslogic::setinterfaceengine (void *pinstance, Printlogcallback printlog) {m_engine.pinstance= pInstance; M_engine.printlog = Printlog;}


void Cclasslogic::P rintlog (const char* buf, int len) {if (m_engine.pinstance!= NULL) {M_engine.printlog (m_en    Gine.pinstance, buf, Len); }}


so now you just need to include the header file of the logical class in the interface class. ~ Declare a C member variables for classlogic M_clogic , and then call the interface that sets the callback function to see the effect:

M_clogic.setinterfaceengine (This, Com102dlg::P rintlogcallback);



This article is from the "Little Luo la la" blog, please be sure to keep this source http://smallclouds.blog.51cto.com/2191034/1596100

A method to resolve the problem of C + + Mutual header file--callback function

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.