Skip to content

Instantly share code, notes, and snippets.

@MikuroXina
Created February 21, 2020 06:52
Show Gist options
  • Select an option

  • Save MikuroXina/23addb543084db50541075e125240f63 to your computer and use it in GitHub Desktop.

Select an option

Save MikuroXina/23addb543084db50541075e125240f63 to your computer and use it in GitHub Desktop.
#ifndef EVENT_SOURCE_HPP
#define EVENT_SOURCE_HPP
#include <functional>
#include <vector>
template<class ...Args>
class EventSource {
public:
using Listener = std::function<void(Args...)>;
private:
std::vector<Listener> listeners;
public:
void on(Listener &&listener) {
listeners.emplace_back(std::forward<Listener&&>(listener));
}
void emit(Args &&...args) const {
for (auto &e : listeners) {
e(args...);
}
}
};
#endif // EVENT_SOURCE_HPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment