41 lines
998 B
C++
41 lines
998 B
C++
#include <algorithm>
|
|
#include <functional>
|
|
#include <mutex>
|
|
#include <vector>
|
|
|
|
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;
|
|
};
|