How to build a message connection between C ++

zhaozj2021-02-11  216

How to create a message of connection between objects in C : Qinling Published: 2001/01/02

Thesis:

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.

text:

How to build a message connection between C

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 function are supported by the Callback class proposed in this article support 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 the specific implementation of this class, please refer to the list of procedures attached to the article.) Tune Object Event Object Name Tune Object Pointer Pointer Pointer "Event" Pointercbo Pointertocbf or Pointertocbsf - - - ---addcallback: Registration Event Name and Point Tune Function, Pointer CallCallback: In the reconversion table, retrieve the registration to the callback command for the event Event to handle the event EVENT, the CallBack class, the call, the call, the CallbackList, the member function of the callbacklist, the member function inherited by the callback, the event Event. 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. The member function addCallback of the other member function Callback class in the callback object is used to register the callback function to the callback table of the event object.

It has two overload versions: void callback :: addcallback (char * Event, callbackfunction cbf, callback * p); void callback :: addcallback (char * Event, callbackstaticfunction CBSF); where the first addCallback is used to The member function of the callback object is registered in the callback table of the event object. 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 from a callback object SomeObject, transfer member function pointer should be formatted as follows: (CallBackFunction) & SomeObject :: MemberFunctionName; transmitting a static class member function pointer SomeObject format should be used: (CallBackStaticFunction) & SomeObject :: FunctionName; transfer When a normal function pointer is a general function pointer, you only need to pass a function name. 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 overloaded versions were: void CallBack :: RemoveCallBack (char * event, CallBackFunction cbf, CallBack * p); void CallBack :: RemoveCallBack (char * event, CallBackStaticFunction cbsf); void CallBack :: RemoveCallBack (char * Event); where Event, CBF, CBSF, P and other parameters are the same as those in 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 uses the Callback class, which can be performed 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.

// Test program file: Test.cpp #include "callback.h" // "Speaker" Class Speaker: PUBLIC CALLBACK {Private: int volume; public: Speaker (int V): Volume (V) {} void Increasevolume INT V) // Add volume member function {Volume = v; if (Volume> 20) {// "Volume greater than 20" event has occurred // Call registration The callback function on the two events CallCallback ("Volume change" CallCallback ("Volume greater than 20", & volume);}} Void DecreaseVolume (INT V) // Decrease Volume Member Function {Volume - = V; IF (Volume <5) {// "The volume is less than 5" incident happened // Call the callback function of the registration in two events CallCallback ("Volume change"); CallCallback ("Volume less than 5", & volume);}}}; // "Ear" class class ear: public callback {public: static Void Response (CallData CallData) changes to the "volume change" react {cout << "volume changed." << Endl;} void highvoiceeresponse (calldata calldata) // Tripya response {cout << "Hey! Too Noisy! Now the volume is: "<< *) CallData) << Endl;} void lowvoiceeresponse (calldata calldata) // 低音 低 的 {{cout <<"! I can't hear it. YES: "<< *) CallData) << Endl;}}; void main (void) {Speaker S (10); // Today's volume is 10 EAR E; / / Register a callback function for event object S S.AddCallback ("Volume greater than 20", (CallbackFunction) & Ear :: Highvoiceeresponse, & E); S.Addcallback ("CallbackFunction) & Ear :: lowvoiceerestsponse, & e); S.AddCallback ("Volume Change", (CallbackStaticFunction) & Ear :: Response); S.IncReaseVolume (12); // Add volume 12, now volume bit 22 s.Decreasevolume (20); // Volume reduction 20, now the volume bit 2} Run results: volume changes. Hey! It's noisy! The volume is now: 22 volume changes. A! 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 .. Salvation is registered three events: "Volume change", "volume is greater than 20", "volume 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.

Contact: fei_xiang@263.net attached: Program list (this program is compiled in MS VC 5.0 and TC 3.0) // Tune 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; // pointer to callback data type definition typedef void (callBack :: * callBackFunction (CallData); // Pointer TypeDef Void (CallbackStaticFunction) (CallBackStaticFunction) (CallData) (CallBackStaticFunction) (CallData), pointing to a static member function, or a normal function: char * EventName; // Tune Name Callback * PointerTocbo; // Pointer to the Pointer // Pointer Pointer Pointer Pointer and Pointer Pointer Pointer Pointer Pointer Pointer Union {CallbackFunction Pointertocbf; CallbackStaticFunction Pointertocbsf;}; public: EventRecord (VOID); / / Event Record Class Default Configuration Function / / Constructing Event Record EventRecord (Char * Ename, Callback * PCBO, CallbackFunction PCBF); // Constructing an event record with static member functions or normal functions EventRecord (char * ename , CallbackStaticFunction PCBSF); ~ EventRecord (void); // Destructure Event Record Void Operator = (Const EventRecord & ER); // Reewage Assault Operator // Determines whether the current event record is ename int operator == ( Char * ename) const; // Judging whether the current event log is related to the specified event record Waiting for int operator == (const EventRecord & ER) const; void flush (void); // Clear the current event record CONST; / / Judgment Event Record is empty (ie, whether the event name is empty) Friend Class Callback; // Let the Callback class access to the private member of EventRecord;}; class callback {private: EventRecord * callbacklist; // callback event table int curpos; // Current event record location int lastpos; // Tune in the last idle Location int size; // The size of the adjustment table voidfirst (void) {curpos = 0;} // Set the current record as the first record void MoveNext (Void) // Set the next record to the current record {IF ( Curpos == lastpos) Return; CURPOS ;} // Judgment the callback watch is traversed int endoflist (void) const {return curpos == lastpos;} public: callback (void); // Constructor Callback (Const Callback & Cb) ;

// Copy constructor ~ Callback (void); // Destructor Void Operator = (const callback & cb); // Overload assignment operator // will call the member function of the callback object, static member function (or ordinary function) / / registration void addCallBack (char * event, callBackFunction cbf, callBack * p) event callback function object; void addCallBack (char * event, CallBackStaticFunction cbsf); // delete registered on the specified event callback function void removeCallBack (char * Event, CallbackFunction CBF, Callback * P); Void RemoveCallback (Char * Event, CallbackStaticFunction CBSF); Void RemoveCallback (CHAR * Event); // Remove all of the transcription // executing all the callback functions registered on a certain event Void CallCallback (Cal * Event, CallData CallData = NULL);}; #ENDIF // Toning class Implementation: callback.cpp #include "callback.h" // EventRecord class Implement EventRecord :: EventRecord (void) {eventname = NULL; pointerToCBO = NULL; // 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, CALLBACKSTAICFUNCOCBSF): Pointertocbo (NULL), Pointertocbsf (PC BSF) {eventName = strdup (ename);} 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 {if ((eventName == NULL) || ename == NULL) return eventName == ename; else return strcmp (eventName, ename) == 0;} int EventRecord :: operator == (const EventRecord & er) const {return (er == eventName) / * er exchange position and can not eventname * / && (Pointertocbo ==

er.pointerToCBO) && (? pointerToCBO (pointerToCBF == er.pointerToCBF): (pointerToCBSF == er.pointerToCBSF));} void EventRecord :: Flush (void) {if (eventName) {delete eventName; eventName = NULL;} Pointertocbo = NULL; POINTERTOCBF = NULL;} int eventRecord :: ISempty (void) const {if (eventname == null) Return 1; Else Return 0;} // Callback class Implement Callback :: Callback (void) {//// with the initial size of the memory space allocated callback table callBackList = new EventRecord [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.Size) {CallbackList = New EventRecord [Size]; if (! CallbackList {CERR << "Callback: Memory Allocation Error." << Endl; exit (1);} // One-one copy of each event record for (INT i = 0; i

) IF (callbacklist [start] {callbacklist [start] = EventRecord (Event, PCBO, PCBF); Break;} if (start

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

New Post(0)