System Method for establishing a message connection between C ++

xiaoxiao2021-03-02  60

Users who have used C over-object-oriented programming are known, and the objects in the program rarely exist. Almost impossible to consider the interaction between the objects. Therefore, identifying the relationship between the object or the message connection between the establishment of the object is an important task for object-oriented programming. This paper focuses on the perspective of C programming, proposes a practical method for establishing a message connection between an object. If you want to learn more about the object programming technology in detail, please refer to the monograph.

Everyone knows that the object is a package of data and methods. In C , they behave separately as data members and member functions. Program designers change the status of the object by performing the various methods of the object (ie, change the properties data of the object). This makes some "events" in this object. When an object occurs an event, it usually needs to send "messages" to other related objects, requesting them to make some processing. At this time, an event occurs and processed to other objects is called an "event object", and the object of the handling event is called "callback object". The processing of the callback object is called "callback function".

In C , this process is equivalent to: When an event object occurs, call some member functions of the callback object. The usual method is to transfer the object pointer to the event object to the event object. But this method is not universal. In order to reduce the workload of programming, this paper proposes a system method for establishing a message connection between an object. Its idea is: Abstract "Events → Request Handling → Perform Processing" into a "callback" class. By inheritance, users can easily obtain the mechanism of establishing a message connection between the object.

First, the data structure of the callback class and its member functions

The Callback class proposed in this article supports three callback functions. They are: the member function in the callback object, belongs to the static member function of the callback class and the normal C function. The Callbackle class contains a modular function table CallbackList. It is used to record the event name, pointing to the pointer to the callback function and the callback object. Each node of the table is an event record EventRecord. Each event record contains three domains: Event Name Pointer EventName, pointer Pointertocbo, pointing to the callback object Pointertocbf or Pointertocbsf (where Pointertocbf points to the member function of the callback object, Pointertocbsf points to the static member function of the callback class or ordinary Function. They are in a usual use body). The callback mechanism provided by the Callback class is this: the callback function in the registration of the callback object is registered on the event object; when the event occurs, the event object retrieves and executes the callback function in its callback table. This makes the message connection of the two. (For specific implementations of this class, please refer to the list of procedures attached to the text) Tune Object

Event object

Event name callback object pointer callback function pointer

"Event" Pointercbo Pointertocbf or

Pointertocbsf

- - - - - -

AddCallback: Register event name and pointing to the callback function, pointer to the callback object

CallCallback: In the reconversion table, retrieve the registration to call the callback function on the specified event and call them

When the event occurs, call the CallCallback function.

Member function for the event EVENT

The callback watch inherited from the Callback class CallbackList, member functions addCallback and CallCallback.

Pointertocbo is NULL when the callback function is a static member function or a normal C function.

The event name is the retrieval key in callback, CallbackLis.

Other member functions in the callback object

The member function addCallback class of the Callback class is used to register the callback function into the callback table of the event object. It has two overload versions:

Void Callback :: AddCallback (Char * Event, Callback * p); void callback :: addcallback (char * Event, callbackstaticfunction CBSF); where the first addCallback is used to register a member function of a callback object to an event The object's callback table. The second addCallback is used to register or call the static member function of a call to register in the callback table of the event object. In the top parameter table, Event is a pointer to the event name string, and P is a pointer to the callback object, and the CBF and CBSF are pointers that point to member functions and static member functions (or ordinary functions). When the callback function comes from a callback object SomeObject, the transfer member function pointer should adopt the following format:

(CallbackFunction) & someobject :: MEMBERFUNCTIONNAME

The member function void callback :: CallCallback (char * ename, calldata calldata = null) is used to call all callback functions registered on the event ename. Among them, CallData is a data pointer (CallData is actually void *, see a list of procedures). Event objects can pass useful data to the callback object. The member function is typically called in the member function of the event object, as usual only if the member function of the event object can change the internal data of the object, thereby causing some events.

Member function RemoveCallback is used to delete the callback function registered on the event object. Its three overload versions are:

Void Callback :: RemoveCallback (Char * Event, CallbackFunction CBF, Callback * P);

Void Callback :: RemoveCallback (Char * Event, CallbackStaticFunction CBSF);

Void Callback :: RemoveCallback (Char * Event);

Among them, Event, CBF, CBSF, P or other parameters are the same as the member function addCallback. The first RemoveCallback is used to delete a member function that registers a callback object on the event Event. The second RemoveCallback is used to delete a static member function that registers a normal function or a tempering class on an event Event. The third REMOVALLBACK is used to delete all the callback functions registered on the event EVENT.

Second, the use of the Callback class

With the Callback class, you can do it as follows:

1. Determine which objects in the program have a relationship, requiring a message connection. And determine which object is an event object in each particular message connection relationship, which object is a callback object.

2. Event object classes and callback object classes must be inherited from the Callback class to get a callback support.

3. Register the callback data for the event object. Including: event name, callback function name, pointing to the pointer to the callback object.

4. When you are interested in incident, call the CallCallback function in the member function of the event object class.

Here is a specific example. By it will learn more about the use of the Callback class.

File: // Test Program File: Test.cpp

#include "callback.h" file: // "Speaker" class

Class Speaker: Public Callback {Private: INT VOLUME; Public: Speaker (INT V): Volume (V) {} Void IncreaseVolume (INT V) File: // Add Volume Member Function {Volume = V; IF (Volume> 20 ) {file: // "Volume greater than 20" event has occurred file: // Call the callback function of the registration in two events CallCallback ("Volume change"); CallCallback ("volume greater than 20", & volume);}}

Void DecreaseVolume (INT V) File: / / Decrease Volume Member Function {Volume - = V; IF (Volume <5) {file: // "The volume is less than 5" incident fila: // Call registration in two events The callback function CallCallback ("volume change"); CallCallback ("The volume is less than 5", & volume);}}};

File: // "Ear" class

Class Ear: public callback {public: static void response (calldata calldata) file: // The response of "volume change" changed. "<< Endl;} void highvoiceresponse (calldata calldata) // Tao Zone's reaction {cout << "Hey! Too noisy! Now the volume is:" << * (int *) calldata) << Endl;} void lowvoiceeresponse (calldata calldata) // For bass reactions {COUT << "Ah! I can't hear it. The volume is:" << *) calldata) << end1;}};

Void main (void) {Speaker S (10); file: // Today's volume is 10 Ear E; file: // Registered a callback function S.AddCallback ("CallbackFunction) & Ear :: HighvoicesResponse, & E); S.addCallback (CallbackFunction) & Ear :: LowvoicesResponse, & E); S.AddCallback ("Volume Change", (CallbackStaticFunction) & Ear :: Response); S.IncReasevolume (12 ); // increase the volume 12, now the volume bit 22 s.Decreasevolume (20); // Reduce the volume 20, now volume bit 2}

operation result:

The volume changed.

Hey! It's noisy! The volume is: 22

The volume changed.

what! I can't hear it. The volume is: 2

In the above example, the speaker object S is an event object, the ear object e is a callback object. . Some of the three events were registered: "The volume changed", "volume is greater than 20", "volume is less than 5". The callback function is: Ear :: Response, Ear :: Highvoiceerestsponse, Ear :: lowvoiceerestsponse. When the speaker S changes the volume through its member functions Increasevolume and DecReaseVolume, the callback object E will automatically react. It can be seen that the establishment of a message connection between the objects has become a simple and beautiful work by using the Callback class. Because the author's level is limited, the design of this class must be imperfect. If you are interested in it, the author can discuss such issues with you C players. Attachment: Program list (this program is compiled in MS VC 5.0 and TC 3.0)

File: // Toned class structure: Callback.h

#ifndef _callback_h

#define _callback_h

#include

#include

#include

#define callbacklist_init_size 10

#define callbacklist_increment 5

Class Callback;

TypeDef void * calldata; // Tune Data Pointer Type Definition

TypeDef void (callback :: * callbackfunction); file: // Pointer to the callback member function

Typedef void (* callbackstaticfunction) (CallData); file: // Pointer type definition points to static member functions or normal functions

Class EventRecord {private: char * eventname; file: // Tune event name Callback * Pointertocbo; // Pointer Pointer File: // Pointer to the member function and point to a static member function (or normal function) pointer union {callBackFunction pointerToCBF; CallBackStaticFunction pointerToCBSF;}; public: EventRecord (void); file: // default constructor File event record class: // event structure including member functions recording EventRecord (char * ename, CallBack * pCBO, CallbackFunction PCBF); File: // Constructs an event record containing a static member function or a normal function Record EventRecord (Char * Ename, CallbackStaticFunction PCBSF); ~ EventRecord (void); // Destructure Event Record Void Operator = (Const EventRecord & Er); // Overload assignment operator File: // Determine whether the current event record is ename

INT Operator == (char * ename) Const;

File: // Determine whether the current event record is equal to the specified event record

INT Operator == (Const EventRecord & Er) Const;

Void flush (void); file: // Clear the current event record

INT ISEMPTY (VOID) const; // Judgment Event Record is empty (ie, whether the event name is empty)

Friend Class Callback; File: / / Let the Callback class access to the private member of EventRecord;

Class Callback {private: EventRecord * CallbackList; File: // Tonance Event Table INT CURPOS; File: // Current event record location int lastpos; file: // Tune in the last idle position int size; file: // Tune the size of

Void MoveFirst (void) {curpos = 0;} // Set the current record as the first record Void MoveNext (void) file: // Set the next record to the current record {if (curpos == lastpos) Return; CURPOS }

File: // Decision of the callback watch is traversed

INT endoflist (void) const {return curpos == lastpos;} public: callback (void); // Configuration function Callback (const callback & cb); // copy constructor ~ callback (void); // destructor

Void Operator = (const callback & cb); // Overload assignment operator

FILE: / / Winning the callback object, static member function (or normal function) file: // Register as the callback function of the event object

Void AddCallback (Char * Event, CallbackFunction CBF, Callback * P); Void AddCallback (Callback); File: // Delete the callback function registered on the specified event

Void RemoveCallback (Char * Event, CallbackFunction CBF, Callback * p); Void RemoveCallback (Char * Event, CallbackStaticFunction CBSF); Void RemoveCallback (Char * Event); // Remove all of an event full record

FILE: / / Perform all callback functions registered on a certain event

Void CallCallback (Char * Event, CallData CallData = NULL);

}

#ENDIF

File: // Realization of Call Tune: Callback.cpp

#include "callback.h"

File: // EventRecord class implementation

EventRecord :: EventRecord (void) {eventname = null; pointertocbo = null; file: // Because sizeof (callbackfunction)> sizeof (callbackstaticfunction) Pointertocbf = null;

EventRecord :: EventRecord (Char * ENAME, CALLBACK * PCBO, CallbackFunction PCBF): Pointertocbo (PCBO), Pointertocbf (PCBF) {EventName = strDUp (ENAME);

EventRecord :: EventRecord (Char * Ename, CallbackStaticFunction PCBSF): Pointertocbo (NULL), Pointertocbsf (PCBSF) {EventName = strdup (entAme);}

EventRecord :: ~ EventRecord (void) {if (eventName) delete eventName;} void EventRecord :: operator = (const EventRecord & er) {if (er.eventName) eventName = strdup (er.eventName); else eventName = NULL; pointerToCBO = Er.Pointertocbo; Pointertocbf = Er.Pointertocbf;}

INT EVENTRECORD :: Operator == (CHAR * ENAME) const {i ((eventname == null) || ename == null) Return EventName == ENAME; Else Return Strcmp (Eventname, ENAME) == 0;}

int EventRecord :: operator == (const EventRecord & er) const {return (er == eventName) / * er and eventname not exchange position * / && (pointerToCBO == er.pointerToCBO) && (pointerToCBO? (pointerToCBF == er. Pointertocbf): (Pointertocbsf == Er.Pointertocbsf);}

Void EventRecord :: flush (void) {if (eventname) {delete Eventname; EventName = null; Pointertocbo = NULL;

INT EventRecord :: ISempty (void) const {if (eventname == null) Return 1; else return 0;}

File: // Callback Class implementation Callback :: Callback (void) {file: // Allocate memory space to the callback watch

Callbacklist = new evenetrecord [callbacklist_init_size];

IF (! callbacklist) {CERR << "Callback: Memory Allocation error." << endl; exit (1);} size = callbacklist_init_size; lastpos = 0; curpos = 0;}

Callback :: Callback (Const Callback & Cb): Curpos (CB.Curpos), Lastpos (CB.lastpos), Size (Cb.size) {CallbackList = New EventRecord [Size]; if (! Callbacklist) {CERR << "Callback : Memory Allocation Error. "<< Endl; exit (1);} file: // One-one copy of each event log (INT i = 0; i

Void Callback :: Operator = (const callback & cb) {curpos = cb.curpos; limited = cb.size; delete [] callbacklist; // Delete old callback table CallbackList = new eventRecord [size]; / / Re-allocate memory space if (! Callbacklist) {CERR << "Callback: Memory Allocation error." << endl; exit (1);} file: // one-one copy

For (int i = 0; i

Callback :: ~ Callback (void) {delete [] callbacklist;

Void Callback :: AddCallback (Char * Event, Callback * PCBO) {file: // If the event name is empty, exit if ((Event == NULL)? 1: (Strlen (Event) == 0)) Return; file: // Look for the first idle position generated by deleting event records, and fill in new event logs for (int start = 0; start

If (! callbacklist) {CERR << "Callback: Memory Allocation error." << endl; exit (1);} for (int i = 0; i

FILE: / / Delete Member Functions Registered on Specified Events

Void Callback :: RemoveCallback (Cal * Event, CallbackFunction PCBF, Callback * PCBO) {IF ((Event == NULL)? 1: (Event) == 0)) Return; EventRecord Er (Event, PCBO, PCBF) ;

For (int i = 0; i

File: // Delete Static member functions or normal functions registered on specified events

Void Callback :: RemoveCallback (Char * Event, CallbackStaticFunction PCBSF) {IF ((Event == NULL)? 1: (Event) == 0)) Return; EventRecord Er (Event, PCBSF);

For (int i = 0; i

File: // Delete all the callback functions registered on the specified event

Void Callback :: RemoveCallback (CHAR * EVENT) {IF ((Event == NULL)? 1: (Strlen (Event) == 0)) Return; for (int i = 0; i

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

New Post(0)