TSEvent消息系统.c++版小样

最近开始搞cocos2dx的相关游戏..发现cocos2dx里面的很多东西用起来Q不爽的...哎~.为了更好的和更方便的移植代码.所以搞了个事件系统的小样.boost又不好移植.还是自己动手丰衣足食吧.. 写这个东东想了好长时间..始终没办法 摆脱TSObject这个基类..如果有大神可以把这个基类摘掉..那么就太谢谢了哈..

//
//  main.m
//  CTest
//
//  Created by TSEnel on 13-5-4.
//  Copyright (c) 2013年 TSEnel. All rights reserved.
//
#include <iostream>
#include <string>
#include <map>

using namespace std;

// 全局基类
class TSObject{};

// 全局回调类型定义
typedef void (TSObject::*TpInstFun)(string sBuffer);

// TS事件系统
class TSEvent : public TSObject{
public:
    void RegistEvent(string sEventKey, TSObject* pInst, TpInstFun pFun) {
        m_MapEvent[sEventKey][pInst] = pFun;
    }
    
    void UnRegistEvent(string sEventKey, TSObject* pInst) {
        m_MapEvent[sEventKey].erase(pInst);
    }
    
    void UnRegistEvent(string sEventKey) {
        m_MapEvent.erase(sEventKey);
    }
    
    void SendMessage(string sEventKey, string sBuffer) {
        if (m_MapEvent.count(sEventKey)) {
            map<TSObject*, TpInstFun>& pInstMap = m_MapEvent[sEventKey];

            map<TSObject*, TpInstFun>::iterator iter = pInstMap.begin();
            for (; iter != pInstMap.end(); iter++) {
                TSObject* pInst = iter->first;
                TpInstFun pFun = iter->second;
                (pInst->*pFun)(sBuffer);
            }
        }
        else {
            cout << "没有消息码 : " << sEventKey << endl;
        }
    }
    
public:
    map<string, map<TSObject*, TpInstFun> > m_MapEvent;
};

// 测试类TSGame
class TSGame : public TSObject{
public:
    TSGame(string name) {
        m_Name = name;
    }
    
    void EventLogin(string sBuffer){
        cout <<  m_Name << " : EventLogin : " << sBuffer << endl;
    }
    
    void EventExit(string sBuffer){
        cout << m_Name << " : EventExit : " << sBuffer << endl;
    }
    
public:
    string m_Name;
};

//测试类TSApp
class TSApp : public TSObject{
public:
    TSApp(string name) {
        m_Name = name;
    }
    
    void EventStart(string sBuffer) {
        cout << m_Name << " : " << sBuffer << endl;
    }
    
public:
    string m_Name;
};

// 声明全局事件系统
TSEvent G_EventSys;

// 入口函数
int main(int argc, const char * argv[])
{
    TSGame pG("TS1");
    TSGame pG2("TS2");
    G_EventSys.RegistEvent("Login", &pG, (TpInstFun)&TSGame::EventLogin);
    G_EventSys.RegistEvent("Login", &pG2,(TpInstFun)&TSGame::EventLogin);
    
    TSApp pA("APP1");
    G_EventSys.RegistEvent("Start", &pA, (TpInstFun)&TSApp::EventStart);
    
    G_EventSys.RegistEvent("Exit", &pG, (TpInstFun)&TSGame::EventExit);
    
    G_EventSys.SendMessage("Login", "WWWWW!!!!!***(FD)SFDS!");
    G_EventSys.SendMessage("Start", "WQL!");
    G_EventSys.SendMessage("Exit", "");
    
    G_EventSys.UnRegistEvent("Exit");
    G_EventSys.UnRegistEvent("Exit");
    G_EventSys.SendMessage("Exit", "");

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值