TSEvent訊息系統.c++版小樣

來源:互聯網
上載者:User

最近開始搞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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.