Skip to content

Instantly share code, notes, and snippets.

@arturoc
Last active August 29, 2015 14:21
Show Gist options
  • Save arturoc/a77375c7934ce4aa1418 to your computer and use it in GitHub Desktop.
Save arturoc/a77375c7934ce4aa1418 to your computer and use it in GitHub Desktop.
#pragma once
#include <vector>
#include <functional>
#include <mutex>
#include <thread>
template<typename T>
class ofEvent{
public:
template<class TObj>
void add(TObj * listener, void (TObj::*method)(T&)){
std::unique_lock<std::mutex> lck(mtx);
functions.push_back(std::bind(method,listener));
}
template<class TObj>
void remove(TObj * listener, void (TObj::*method)(T&)){
std::unique_lock<std::mutex> lck(mtx);
for(size_t i=0; i<functions.size(); i++){
if(functions[i].target<void (TObj::*)(T&)>()==method){
functions.erase(functions.begin() + i);
}
}
}
void notify(T & param){
std::unique_lock<std::mutex> lck(mtx);
for(auto & f: functions){
f(param);
}
}
private:
std::mutex mtx;
std::vector<std::function<void(T&)>> functions;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment