Created
February 21, 2020 06:52
-
-
Save MikuroXina/23addb543084db50541075e125240f63 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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