[Reusability program design] -- event triggering

Source: Internet
Author: User

First define a simplified Event Data Structure

Typedef struct {

    unsigned char event_flg;
    unsigned char event_cnt_buf[event_flg_wide];
    void (*pEventCallback[event_flg_wide])(void);
} Event;
If your events are complex, you can continue to add object members to the data structure, but I will not do this. I will abstract an event class and then derive richer child classes by inheritance.

Event triggering is implemented as follows:
Event. cpp
# Include <iostream> # include "event. H "using namespace STD; event msg_event; void msg_event_init (void) {}// register the message response function void event_install_callback (unsigned char fn_index, void PFN (void )) {If (fn_index <event_flg_wide) {response [fn_index] = PFN ;}// uninstall the message response function void event_uninstall_callback (unsigned char fn_index) {If (fn_index <event_flg_wide) {msg_event.peventcallback [fn_index] = NULL ;}} void Close_all_msg (event event_t) {unsigned char I = 0; event_t.event_flg = 0; for (I = 0; I <event_flg_wide; I ++) event_t.event_cnt_buf [I] = 0; for (I = 0; I <event_flg_wide; I ++) event_t.peventcallback [I] = NULL;} void msg_event_rountine (void) {cout <"msg_event_rountine! "<Endl;} // message event main thread void msg_event_main_thread (void) {unsigned char I; // If (msg_event.event_flg) {// accumulated message entries for (I = 0; I <event_flg_wide; I ++) {If (msg_event.event_flg & (1 <I) {msg_event.event_flg & = (~ (1 <I); msg_event.event_cnt_buf [I] ++ ;}}} else {// turn off the load} // execute the message response for (I = 0; I <event_flg_wide; I ++) {If (msg_event.event_cnt_buf [I]) {If (msg_event.peventcallback! = NULL) {msg_event.peventcallback [I] (); msg_event.event_cnt_buf [I] --; break ;}}}}
 

Header file event. h

#ifndef EVENT_H#define EVENT_H#ifndef FALSE  #define  FALSE  0x00u#endif#ifndef TRUE  #define  TRUE   0x01u#endif#ifndef NULL  #define  NULL   0x00u#endif#define event_flg_wide  8typedef struct{    unsigned char event_flg;    unsigned char event_cnt_buf[event_flg_wide];    void (*pEventCallback[event_flg_wide])(void);}event;extern event msg_event;void msg_event_init(void);void msg_event_main_thread(void);void event_install_callback(unsigned char fn_index, void pfn(void));void event_uninstall_callback(unsigned char fn_index);void msg_event_rountine(void);void close_all_msg(event event_t );#endif // EVENT_H

 

Register two events in the main function and trigger it.

Msg_event_init ();

    event_install_callback(0,msg_event_rountine);
    event_install_callback(1,msg_event_rountine);
    msg_event.event_flg |= (1<<0)|(1<<1);
    while(1){
    msg_event_main_thread();
    }

 

Test Engineering placement on Network Disk http://pan.baidu.com/s/1gdy2dhT

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.