Start switching to callbacks on SDL input events
This commit is contained in:
parent
ae88521b32
commit
8c11141ac5
11 changed files with 235 additions and 95 deletions
27
include/Helpers/CallbackManager.hpp
Normal file
27
include/Helpers/CallbackManager.hpp
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#pragma once
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
template <typename... Args> class CallbackManager {
|
||||
public:
|
||||
typedef std::function<void(const Args&...)> Callback;
|
||||
Callback* register_callback(const Callback& func) {
|
||||
Callback* toRet = new Callback(func);
|
||||
funcs.emplace_back(toRet);
|
||||
return toRet;
|
||||
}
|
||||
void deregister_callback(Callback* callbackToRemove) {
|
||||
if(std::erase(funcs, callbackToRemove) > 0)
|
||||
delete callbackToRemove;
|
||||
}
|
||||
void run_callbacks(const Args&... a) {
|
||||
for(auto& f : funcs)
|
||||
(*f)(a...);
|
||||
}
|
||||
~CallbackManager() {
|
||||
for(Callback* c : funcs)
|
||||
delete c;
|
||||
}
|
||||
private:
|
||||
std::vector<Callback*> funcs;
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue