From 7807eecc08839ee3f5b64560a03f5081838c9809 Mon Sep 17 00:00:00 2001 From: tqcq <99722391+tqcq@users.noreply.github.com> Date: Thu, 4 Apr 2024 03:46:23 +0000 Subject: [PATCH] fix multi singleton --- src/sled/event_bus/event_bus.cc | 1 + src/sled/event_bus/event_bus.h | 21 +++++++++------------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/sled/event_bus/event_bus.cc b/src/sled/event_bus/event_bus.cc index 0135fb7..281fbe7 100644 --- a/src/sled/event_bus/event_bus.cc +++ b/src/sled/event_bus/event_bus.cc @@ -15,5 +15,6 @@ GetEventRegistryCount() { return g_event_registry_count.load(std::memory_order_acquire); } + }// namespace internal }// namespace sled diff --git a/src/sled/event_bus/event_bus.h b/src/sled/event_bus/event_bus.h index 38bfc80..7bdb663 100644 --- a/src/sled/event_bus/event_bus.h +++ b/src/sled/event_bus/event_bus.h @@ -19,9 +19,6 @@ using EnableNotVolatile = typename std::enable_if::value>; // using RawType = typename std::remove_const::type>::type; void IncrementEvenetRegistryCount(); int GetEventRegistryCount(); -}// namespace internal - -namespace { template class EventRegistry { @@ -36,8 +33,8 @@ public: static EventRegistry &Instance() { - static EventRegistry instance_; - return instance_; + static EventRegistry registry; + return registry; } static std::function &GetCleanupHandler() @@ -112,7 +109,7 @@ private: SubscriberTable signals_; }; -}// namespace +}// namespace internal class EventBus { public: @@ -138,14 +135,14 @@ public: void Post(Event &&event) { using U = typename internal::RawType; - EventRegistry::Instance().Post(this, std::forward(event)); + internal::EventRegistry::Instance().Post(this, std::forward(event)); } template void PostTo(From &&value) { using U = typename internal::RawType; - EventRegistry::Instance().Post(this, std::forward(value)); + internal::EventRegistry::Instance().Post(this, std::forward(value)); } // On ([](const Event1 &){}) @@ -158,21 +155,21 @@ public: sled::MutexLock lock(&mutex_); auto iter = cleanup_handlers_.find(std::type_index(typeid(U))); if (iter == cleanup_handlers_.end()) { - cleanup_handlers_[std::type_index(typeid(U))] = EventRegistry::GetCleanupHandler(); + cleanup_handlers_[std::type_index(typeid(U))] = internal::EventRegistry::GetCleanupHandler(); } } - EventRegistry::Instance().Subscribe(this, instance, method); + internal::EventRegistry::Instance().Subscribe(this, instance, method); } template typename std::enable_if::value>::type Unsubscribe(C *instance) { using U = typename internal::RawType; - EventRegistry::Instance().Unsubscribe(this, instance); + internal::EventRegistry::Instance().Unsubscribe(this, instance); { sled::MutexLock lock(&mutex_); - if (EventRegistry::Instance().IsEmpty(this)) { + if (internal::EventRegistry::Instance().IsEmpty(this)) { auto iter = cleanup_handlers_.find(std::type_index(typeid(U))); if (iter != cleanup_handlers_.end()) { iter->second(this);