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