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

来源:互联网 发布:matlab m文件读取数据 编辑:程序博客网 时间:2024/05/18 00:51

最近开始搞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;};// 测试类TSGameclass 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;};//测试类TSAppclass 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;}


原创粉丝点击