mirror of
https://github.com/BotChain-Robots/control.git
synced 2026-03-10 00:32:26 +01:00
Prepare files for public release
This commit is contained in:
50
include/util/Event.h
Normal file
50
include/util/Event.h
Normal file
@@ -0,0 +1,50 @@
|
||||
|
||||
#ifndef CONTROL_EVENT_H
|
||||
#define CONTROL_EVENT_H
|
||||
|
||||
#include <functional>
|
||||
|
||||
class Event;
|
||||
|
||||
class CallbackHandle {
|
||||
public:
|
||||
CallbackHandle(Event *evt, int id) : evt(evt), id(id) {
|
||||
}
|
||||
~CallbackHandle();
|
||||
|
||||
private:
|
||||
Event *evt;
|
||||
int id;
|
||||
};
|
||||
|
||||
class Event {
|
||||
public:
|
||||
using Callback = std::function<void()>;
|
||||
|
||||
CallbackHandle addListener(Callback cb) {
|
||||
int id = nextId++;
|
||||
callbacks[id] = std::move(cb);
|
||||
return CallbackHandle(this, id);
|
||||
}
|
||||
|
||||
void remove(int id) {
|
||||
callbacks.erase(id);
|
||||
}
|
||||
|
||||
void fire() {
|
||||
for (auto &kv : callbacks)
|
||||
kv.second();
|
||||
}
|
||||
|
||||
private:
|
||||
friend class CallbackHandle;
|
||||
std::unordered_map<int, Callback> callbacks;
|
||||
int nextId = 0;
|
||||
};
|
||||
|
||||
CallbackHandle::~CallbackHandle() {
|
||||
if (evt)
|
||||
evt->remove(id);
|
||||
}
|
||||
|
||||
#endif // CONTROL_EVENT_H
|
||||
Reference in New Issue
Block a user