Files
Echoion/include/Core/SingletonManager.hpp
T

31 lines
1.0 KiB
C++

#include <vector>
#include <functional>
#include <algorithm>
#include <mutex>
class SingletonManager {
public:
// Priority: Higher numbers are destroyed LAST (e.g., Logger = 100)
static void registerShutdown(const int priority, const std::function<void()> &shutdownFunc) {
m_registry.push_back({priority, shutdownFunc});
// Sort so that high priority stays at the end (destroyed last)
std::ranges::sort(m_registry, [](const auto& a, const auto& b) {
return a.priority < b.priority;
});
}
static void shutdownAll() {
// Destroy in reverse priority (highest priority first? No, usually reverse order of arrival)
// Here we destroy from lowest priority to highest.
for (auto& [priority, func] : m_registry) {
std::cerr << priority << ": " << &func << std::endl;
func();
}
m_registry.clear();
}
private:
struct Entry { int priority; std::function<void()> func; };
static inline std::vector<Entry> m_registry;
};