Testing some event stuff

This commit is contained in:
2023-11-17 22:40:08 -06:00
parent ce926c5f17
commit 437b67ad06
20 changed files with 260 additions and 30 deletions

View File

@ -11,13 +11,20 @@ namespace Dawn {
class Event {
private:
int32_t nextId = 0;
std::unordered_map<int32, std::function<void(A...)>> listeners;
std::map<int32_t, std::function<void(A...)>> listeners;
public:
/**
* Constructs a new Event.
*/
Event() {
}
/**
* Emits the event.
* @param args The arguments to pass to the listeners.
*/
void emit(A ...args) {
auto copy = listeners;
for(auto &pair : copy) {
@ -25,6 +32,11 @@ namespace Dawn {
}
}
/**
* Listens to the event.
* @param listener The listener to add.
* @returns A function that can be called to remove the listener.
*/
std::function<void()> listen(const std::function<void(A...)> listener) {
int32_t id = nextId++;
listeners[id] = listener;
@ -33,8 +45,12 @@ namespace Dawn {
};
}
/**
* Removes a listener from the event.
* @param id The id of the listener to remove.
*/
virtual ~Event() {
listeners.clear();
}
}
};
}