Skip to content

Instantly share code, notes, and snippets.

@wtrsltnk
Last active January 9, 2022 21:59
Show Gist options
  • Save wtrsltnk/9f2412263d42a2d53d5fee7700efada7 to your computer and use it in GitHub Desktop.
Save wtrsltnk/9f2412263d42a2d53d5fee7700efada7 to your computer and use it in GitHub Desktop.
Event delegate handler in c++
#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