Last active
August 29, 2015 14:21
-
-
Save arturoc/a77375c7934ce4aa1418 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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