Realize event callback mechanisms like Borland VCL components using C ++ member pointers (below)

zhaozj2021-02-16  54

Further common

The ERROR_NOTIFY_EVENT type defined above is only available for class B, which is not compatible with other classes, but other classes may require the same error notification service, so it is necessary to make it a versatility, and can achieve this with C , some people The function mode may be thought of because the function template is not a C type, so it cannot be instantiated, and the class template can specify the type of instantiation. The next type of template has enabled the package of the callback function pointer type.

Template CLASS CNOTIFYEVENT

{

Private:

T * Object;

Typedef void (int notify_code); // Defines member function pointer type

Event_type method;

PUBLIC:

CNotifyEvent (T * Object, Event_Type Method)

{

This-> Object = Object;

This-> method = method;

}

Int donotify ()

{

(Object -> * method) (); // trigger a callback, performing event notifications.

}

}

Below is the example code code in the VC:

Class CsourcesClass;

Class CTargetClass;

Class csourceclass: public cobject // This is a class that triggers event notification

{

Private:

CNotifyEvent * m_onnotify; // Suppose needs to be notified to the CTARGETCLASS instance

protected:

voiddonotify ()

{

IF (m_onnotify) m_onnotify-> donotify (); // Trigger event notification

}

PUBLIC:

CSourceClass ()

{

m_onnotify = NULL;

}

~ Csourceclass ()

{

}

// Set the event notification attribute via setnoityEvent

Void setnoityEvent (CNotifyEvent * Event_sink)

{

m_onnotify = Event_sink;

}

}

Class CTargetClass: public cobject // This is a class that requires event notification

{

Private:

CNotifyEvent * m_callback;

protected:

Void freenotifyeventsink ()

{

IF (m_callback) {

Delete m_callback;

m_callback = NULL;

}

}

Void newNotifyEventSink () // Create a new event notification class template instance

{

M_Callback = New CNOTIFYEVENT

this, / / ​​indicate an instance of objects that need to be called the event notification

& CtargetClass :: removeNotify // Indicates class members function types for callback events

);

}

PUBLIC:

CTargetClass ()

{

NewNotifyEventsink ();

}

~ CtargetClass ()

{

FreeNotifyEventsink ();

}

Void ReceiveNotify (int notify_code)

{

// Treat event notification

}

CNotifyEvent * getNotifySink ()

{

Return m_callback;}

}

CsourceClass SourceObj;

CTARGETCLASS TARGETOBJ;

SourceObj.setnoityEvent (targetobj.getNotiSink ()); // Event Attribute assignment

It can be seen that after a generic transformation of the class template, CNOTIFYEVENT can be applied to different class instances. Various types of event callback types can be defined by changing the parameter structure of Event_Type in CNotifyEvent.

summary

This paper introduces a method of comparing mutual callback between class instances in C . This method mainly uses the following characteristics of C :

1. Member pointer, this is a characteristic of C , is another reflection of the high flexibility in C ; the purpose of using member pointers is to implement the member function of the reference object instance, so that other classes directly call a member function of an object instance. may. The VCL is based on this implementation of the event callback mechanism.

2. Template Template, the purpose of using the template is to enable the defined event callback mechanism to have a universal type, which can be targeted for different class types, which is equivalent to the type of event defined in the VCL, is actually a function pointer type, in C to achieve The best selection of versatility uses templates (generic).

Applying this method to make interactions between classes and classes more convenient and intuitive. When writing your own class libraries, you can use this method to build a class interaction framework for class libraries, and improve the ease of use of class libraries.

references

VC 6.0 Help Document

"C efficient programming"

2004-03-02

转载请注明原文地址:https://www.9cbs.com/read-21959.html

New Post(0)