63 lines
1.5 KiB
C++
63 lines
1.5 KiB
C++
#include "singleton_manager.h"
|
|
|
|
#include <cstdint>
|
|
|
|
#include "singleton.h"
|
|
|
|
bool singleton_initliazer::has_init(singleton* s) {
|
|
return std::ranges::find(singletons_, s) != singletons_.end();
|
|
}
|
|
|
|
void singleton_initliazer::init_singleton(singleton* s) {
|
|
if (!has_init(s)) {
|
|
singletons_.push_back(s);
|
|
s->init(*this);
|
|
}
|
|
}
|
|
|
|
bool singleton_release_guard::has_release(singleton* s) {
|
|
return std::ranges::find(release_singletons_, s) != release_singletons_.end();
|
|
}
|
|
|
|
void singleton_release_guard::release_singleton(singleton* s) {
|
|
if (!has_release(s)) {
|
|
release_singletons_.push_back(s);
|
|
if (begin_release_)
|
|
s->begin_release(*this);
|
|
else
|
|
s->release(*this);
|
|
}
|
|
}
|
|
|
|
void singleton_manager::add(singleton* s) {
|
|
singletons_.push_back(s);
|
|
}
|
|
|
|
void singleton_manager::init() {
|
|
singleton_initliazer initliazer;
|
|
for (const auto s : singletons_) {
|
|
initliazer.init_singleton(s);
|
|
}
|
|
for (const auto s : singletons_) {
|
|
s->post_init();
|
|
}
|
|
singletons_ = initliazer.singletons_;
|
|
}
|
|
|
|
void singleton_manager::release() const {
|
|
{
|
|
singleton_release_guard release_guard(true);
|
|
for (int32_t j = singletons_.size() - 1; j >= 0; --j) {
|
|
auto s = singletons_[j];
|
|
release_guard.release_singleton(s);
|
|
}
|
|
}
|
|
{
|
|
singleton_release_guard release_guard(false);
|
|
for (int32_t j = singletons_.size() - 1; j >= 0; --j) {
|
|
auto s = singletons_[j];
|
|
release_guard.release_singleton(s);
|
|
}
|
|
}
|
|
}
|