Last active
January 9, 2022 21:59
-
-
Save wtrsltnk/9f2412263d42a2d53d5fee7700efada7 to your computer and use it in GitHub Desktop.
Event delegate handler in c++
This file contains 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
#include <functional> | |
#include <iostream> | |
#include <vector> | |
template <class TArgs> | |
class Delegate | |
{ | |
std::vector<std::function<void(const TArgs &)>> _handlers; | |
public: | |
Delegate() | |
{} | |
void operator+=(std::function<void(const TArgs &)> handler) | |
{ | |
_handlers.push_back(handler); | |
} | |
void operator()(const TArgs &args) | |
{ | |
Invoke(args); | |
} | |
void Invoke(const TArgs &args) | |
{ | |
for (auto handler : _handlers) | |
{ | |
handler(args); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment