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;}
#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
- 自定义事件机制
- C++ 自定义事件机制
- FLEX 事件机制-自定义事件
- Java事件机制---自定义事件
- Java事件机制---自定义事件
- C#事件处理机制
- C# 事件机制
- C++ 自定义事件机制2
- $.event.special自定义事件机制
- cocos Creator自定义事件机制
- c.sharp自定义事件
- C#-控件-自定义事件
- C#:自定义事件
- java事件处理机制—自定义事件
- Java事件处理机制-自定义事件
- java事件处理机制 自定义事件
- java事件机制及自定义事件处理
- java事件处理机制(自定义事件)
- 10年转型:移动互联网时代真的来临了吗?
- LeetCode | Pascal's Triangle
- Pro Android学习笔记(一六七):联系人API(10):在Personal Profile中添加raw contact
- 解决 iOS 8 WebView 不能呼叫客户端的问题
- SAP SD 如何把几张delivery合并成一张发票invoice
- C++ 自定义事件机制
- android 项目引用另外一个库项目,如何导入该库项目
- 关于设计模式的一些链接
- php 原生的文件上传
- Java中的堆和栈的区别
- freebsd环境变量设置
- 引用Android Design包出现的错误
- 免费网盘运营商著作权责任探析
- C#高级编程十三天----方法的重载