Created
June 4, 2018 11:32
-
-
Save peted70/9156ae3f9f77114ae678e6d257d682f9 to your computer and use it in GitHub Desktop.
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 <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