asyncevent-listenereventtimeoutevent-loopevent-drivenasynchronous-programmingasynctaskintervaltimersasync-taskmultithreadinterval-timertimeout-control
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
796 B
40 lines
796 B
7 months ago
|
#ifndef _EVENT_
|
||
|
#define _EVENT_
|
||
|
|
||
|
#include <iostream>
|
||
|
#include <map>
|
||
|
#include <string>
|
||
|
#include <functional>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
namespace marcelb {
|
||
|
|
||
|
class event {
|
||
|
map<string, function<void(const tuple<>&)>> events;
|
||
|
public:
|
||
|
|
||
|
template <typename... Args>
|
||
|
void on(const string& key, function<void(Args...)> f) {
|
||
|
// events[key] = [f](Args... args) {
|
||
|
// f(args...);
|
||
|
// };
|
||
|
}
|
||
|
|
||
|
template <typename... Args>
|
||
|
void emit(const string& key, Args&&... args) {
|
||
|
if (events.find(key) == events.end()) {
|
||
|
cout << "No defined listener for event: " << key << endl;
|
||
|
return;
|
||
|
}
|
||
|
else {
|
||
|
for (auto& func : events[key]) {
|
||
|
func(forward<Args>(args)...);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
}
|
||
|
|
||
|
#endif
|