2026-05-11 23:06:39 +03:00
|
|
|
/*
|
|
|
|
|
* InfiniPaint
|
|
|
|
|
* Copyright (C) 2025-2026 Yousef Khadadeh
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2026-02-28 22:47:35 +03:00
|
|
|
#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...);
|
|
|
|
|
}
|
2026-03-02 16:36:34 +03:00
|
|
|
void clear() {
|
2026-02-28 22:47:35 +03:00
|
|
|
for(Callback* c : funcs)
|
|
|
|
|
delete c;
|
2026-03-02 16:36:34 +03:00
|
|
|
funcs.clear();
|
|
|
|
|
}
|
|
|
|
|
~CallbackManager() {
|
|
|
|
|
clear();
|
2026-02-28 22:47:35 +03:00
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
std::vector<Callback*> funcs;
|
|
|
|
|
};
|