码迷,mamicode.com
首页 > 其他好文 > 详细

【可复用性程序设计】——事件触发

时间:2014-08-12 18:20:04      阅读:218      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   文件   数据   

先定义一个最简化的事件数据结构

typedef struct{

    unsigned char event_flg;
    unsigned char event_cnt_buf[event_flg_wide];
    void (*pEventCallback[event_flg_wide])(void);
}event;
如果你的事件比较复杂,你可以继续往数据结构里添加对象成员,但是我不会这么干,我会抽象出一个事件类,然后通过继承派生出更丰富的子类。

事件触发具体实现如下:
event.cpp
#include <iostream>
#include "event.h"
using namespace std;

event msg_event;

void msg_event_init(void)
{}

//注册消息响应函数
void event_install_callback(unsigned char fn_index, void pfn(void))
{
  if(fn_index < event_flg_wide)
  {
    msg_event.pEventCallback[fn_index] = pfn;
  }
}

//卸载消息响应函数
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;
}

//消息事件主线程
void msg_event_main_thread(void)
{
    unsigned char i;
    //监测到事件消息
    if(msg_event.event_flg)
    {
        //累计消息条目
        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
    {
        //关掉负载
    }

    //执行消息响应
    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;
            }
        }
    }
}

头文件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  8

typedef 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

 

在main函数中注册两个事件,触发一下试试

      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();
    }

bubuko.com,布布扣

 

测试工程放在网盘http://pan.baidu.com/s/1gdy2dhT

【可复用性程序设计】——事件触发,布布扣,bubuko.com

【可复用性程序设计】——事件触发

标签:style   blog   http   color   os   io   文件   数据   

原文地址:http://www.cnblogs.com/dong1/p/3907512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!