Skip to content

Instantly share code, notes, and snippets.

@peted70
Created June 4, 2018 11:32
Show Gist options
  • Save peted70/9156ae3f9f77114ae678e6d257d682f9 to your computer and use it in GitHub Desktop.
Save peted70/9156ae3f9f77114ae678e6d257d682f9 to your computer and use it in GitHub Desktop.
#include <map>
#include <functional>
#include "sub_token.h"
using namespace std::placeholders;
using namespace std;
template <typename... Args>
class subject
{
public:
void notify(Args... arg)
{
for (auto &kv : _map)
{
kv.second(arg...);
}
}
sub_token subscribe(function<void(Args...)> fn)
{
sub_token tk;
_map[tk] = fn;
return tk;
}
void unsubscribe(sub_token tk)
{
_map.erase(_map.find(tk));
}
private:
map<sub_token, function<void(Args...)>> _map;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment