* Singleton<int> b([] { return new int(4); });
*
*/
- singletonWarnDoubleRegistrationAndAbort(type_);
+ singletonWarnDoubleRegistrationAndAbort(type());
}
create_ = std::move(c);
template <typename T>
void SingletonHolder<T>::registerSingletonMock(CreateFunc c, TeardownFunc t) {
if (state_ == SingletonHolderState::NotRegistered) {
- LOG(FATAL)
- << "Registering mock before singleton was registered: " << type_.name();
+ LOG(FATAL) << "Registering mock before singleton was registered: "
+ << type().name();
}
destroyInstance();
+ {
+ RWSpinLock::WriteHolder wh(&vault_.mutex_);
+
+ auto it = std::find(
+ vault_.creation_order_.begin(), vault_.creation_order_.end(), type());
+ if (it != vault_.creation_order_.end()) {
+ vault_.creation_order_.erase(it);
+ }
+ }
+
std::lock_guard<std::mutex> entry_lock(mutex_);
create_ = std::move(c);
throw std::runtime_error(
"Raw pointer to a singleton requested after its destruction."
" Singleton type is: " +
- type_.name());
+ type().name());
}
return instance_ptr_;
return instance_weak_fast_.lock();
}
-template <typename T>
-TypeDescriptor SingletonHolder<T>::type() {
- return type_;
-}
-
template <typename T>
bool SingletonHolder<T>::hasLiveInstance() {
return !instance_weak_.expired();
state_ = SingletonHolderState::Dead;
instance_.reset();
if (destroy_baton_) {
+ constexpr std::chrono::seconds kDestroyWaitTime{5};
auto wait_result = destroy_baton_->timed_wait(
std::chrono::steady_clock::now() + kDestroyWaitTime);
if (!wait_result) {
print_destructor_stack_trace_->store(true);
- LOG(ERROR) << "Singleton of type " << type_.name() << " has a "
+ LOG(ERROR) << "Singleton of type " << type().name() << " has a "
<< "living reference at destroyInstances time; beware! Raw "
<< "pointer is " << instance_ptr_ << ". It is very likely "
<< "that some other singleton is holding a shared_ptr to it. "
}
template <typename T>
-SingletonHolder<T>::SingletonHolder(TypeDescriptor type__,
- SingletonVault& vault) :
- type_(type__), vault_(vault) {
-}
+SingletonHolder<T>::SingletonHolder(
+ TypeDescriptor typeDesc,
+ SingletonVault& vault)
+ : SingletonHolderBase(typeDesc), vault_(vault) {}
template <typename T>
bool SingletonHolder<T>::creationStarted() {
void SingletonHolder<T>::createInstance() {
if (creating_thread_.load(std::memory_order_acquire) ==
std::this_thread::get_id()) {
- LOG(FATAL) << "circular singleton dependency: " << type_.name();
+ LOG(FATAL) << "circular singleton dependency: " << type().name();
}
std::lock_guard<std::mutex> entry_lock(mutex_);
SingletonHolderState::NotRegistered) {
auto ptr = SingletonVault::stackTraceGetter().load();
LOG(FATAL) << "Creating instance for unregistered singleton: "
- << type_.name() << "\n"
+ << type().name() << "\n"
<< "Stacktrace:"
- << "\n" << (ptr ? (*ptr)() : "(not available)");
+ << "\n"
+ << (ptr ? (*ptr)() : "(not available)");
}
if (state_.load(std::memory_order_acquire) == SingletonHolderState::Living) {
auto print_destructor_stack_trace =
std::make_shared<std::atomic<bool>>(false);
auto teardown = teardown_;
- auto type_name = type_.name();
+ auto type_name = type().name();
// Can't use make_shared -- no support for a custom deleter, sadly.
std::shared_ptr<T> instance(
{
RWSpinLock::WriteHolder wh(&vault_.mutex_);
- vault_.creation_order_.push_back(type_);
+ vault_.creation_order_.push_back(type());
}
}