C++ 自定义事件机制

来源:互联网 发布:淘宝发布宝贝搜索不到 编辑:程序博客网 时间:2024/06/06 20:18

由于项目中要做到解耦,所以需要用到事件机制。

但是这个事件机制没有解决类成员函数的的问题。

类成员函数的解决在下一篇。

--------------------------------------------------------------------------

代码如下:


global.h

#pragma once#include "myevent.h"typedef void(*EVENTFUN)(MyEvent&);

global.cpp

#include "global.h"

myevent.h

#pragma once#include <string>using namespace std;class MyEvent{public:MyEvent(string type);~MyEvent(); string getType();void setType(string value);private:string _type;};

myevent.cpp

#include "myevent.h"#include <iostream>MyEvent::MyEvent(string type){setType(type);cout << "MyEvent()........" << endl;}MyEvent::~MyEvent(){cout << "~MyEvent()........" << endl;}string MyEvent::getType(){return _type;}void MyEvent::setType(string value){_type = value;}


eventdispatcher.h

#pragma once#include <unordered_map>#include "global.h"#include "myevent.h"#include <memory>using namespace std;class EventDispatcher{public:EventDispatcher();~EventDispatcher();static void addEventListener(string type, EVENTFUN fun);static void dispatchEvent(MyEvent& e);static void dispatchEvent(shared_ptr<MyEvent> e);static bool hasEventListener(string type);static void removeEventListener(string type);private:static unordered_map<string, EVENTFUN> myEvents;};

eventdispatcher.cpp

#include "eventdispatcher.h"#include <iostream>EventDispatcher::EventDispatcher(){}EventDispatcher::~EventDispatcher(){}unordered_map<string, EVENTFUN> EventDispatcher::myEvents;void EventDispatcher::addEventListener(string type, EVENTFUN fun){if (!hasEventListener(type)){myEvents[type] = fun;}else{cout << "exist event type :: " << type << endl;}}void EventDispatcher::dispatchEvent(MyEvent& e){string &type = e.getType();if (hasEventListener(type)){myEvents[type](e);}else{cout << "can not find event type :: " << type << endl;}}void EventDispatcher::dispatchEvent(shared_ptr<MyEvent> e){string &type = e->getType();if (hasEventListener(type)){myEvents[type](*e);}else{cout << "can not find event type :: " << type << endl;}}bool EventDispatcher::hasEventListener(string type){return myEvents[type] != nullptr;}void EventDispatcher::removeEventListener(string type){myEvents.erase(type);}
----------------------

调用::

customevent.h

#pragma once#include "myevent.h"class CustomEvent : public MyEvent{public:CustomEvent(string type);~CustomEvent();int a;int b;string c;private:};

customevent.cpp

#include "customevent.h"#include <iostream>CustomEvent::CustomEvent(string type) :MyEvent(type){a = 10;b = 20;c = "hahah";cout << "CustomEvent()" << endl;}CustomEvent::~CustomEvent(){cout << "~CustomEvent()........." << endl;}

main.cpp

void f(MyEvent& e){CustomEvent& evt = (CustomEvent&)e;cout << "hello" << evt.a << evt.b << evt.c <<endl;}void test(){EventDispatcher d;d.addEventListener("hello", f);shared_ptr<CustomEvent> e = make_shared<CustomEvent>("hello");d.dispatchEvent(e);}int main(){EventDispatcher d;d.addEventListener("hello", f);//MyEvent e("hello");CustomEvent* e1 = new CustomEvent("hello");CustomEvent& e = *e1;d.dispatchEvent(e);system("pause");return 0;}





0 0
原创粉丝点击