feat update
This commit is contained in:
19
src/sled/event_bus/event_bus.cc
Normal file
19
src/sled/event_bus/event_bus.cc
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "sled/event_bus/event_bus.h"
|
||||
|
||||
namespace sled {
|
||||
namespace internal {
|
||||
static std::atomic<int> g_event_registry_count{0};
|
||||
|
||||
void
|
||||
IncrementEvenetRegistryCount()
|
||||
{
|
||||
g_event_registry_count.fetch_add(1, std::memory_order_relaxed);
|
||||
}
|
||||
|
||||
int
|
||||
GetEventRegistryCount()
|
||||
{
|
||||
return g_event_registry_count.load(std::memory_order_acquire);
|
||||
}
|
||||
}// namespace internal
|
||||
}// namespace sled
|
@@ -17,6 +17,8 @@ using RawType = typename std::remove_cv<typename std::remove_reference<Event>::t
|
||||
template<typename T>
|
||||
using EnableNotVolatile = typename std::enable_if<!std::is_volatile<T>::value>;
|
||||
// using RawType = typename std::remove_const<typename std::remove_reference<Event>::type>::type;
|
||||
void IncrementEvenetRegistryCount();
|
||||
int GetEventRegistryCount();
|
||||
}// namespace internal
|
||||
|
||||
namespace {
|
||||
@@ -24,9 +26,14 @@ namespace {
|
||||
template<typename Event>
|
||||
class EventRegistry {
|
||||
public:
|
||||
static_assert(!std::is_const<Event>::value, "Event type must be non-const");
|
||||
static_assert(!std::is_volatile<Event>::value, "Event type must be non-volatile");
|
||||
static_assert(!std::is_reference<Event>::value, "Event type must be non-reference");
|
||||
using Dispatcher = sigslot::signal1<Event>;
|
||||
using SubscriberTable = std::unordered_map<EventBus *, Dispatcher>;
|
||||
|
||||
EventRegistry() { internal::IncrementEvenetRegistryCount(); }
|
||||
|
||||
static EventRegistry &Instance()
|
||||
{
|
||||
static EventRegistry instance_;
|
||||
@@ -52,13 +59,16 @@ public:
|
||||
void Subscribe(EventBus *bus, C *instance, F &&method)
|
||||
{
|
||||
sled::SharedMutexWriteLock lock(&shared_mutex_);
|
||||
auto iter = signals_.find(bus);
|
||||
if (iter == signals_.end()) {
|
||||
signals_.emplace(bus, Dispatcher());
|
||||
iter = signals_.find(bus);
|
||||
bool is_empty = signals_.empty();
|
||||
{
|
||||
auto iter = signals_.find(bus);
|
||||
if (iter == signals_.end()) {
|
||||
signals_.emplace(bus, Dispatcher());
|
||||
iter = signals_.find(bus);
|
||||
}
|
||||
auto &dispatcher = iter->second;
|
||||
dispatcher.connect(instance, method);
|
||||
}
|
||||
auto &dispatcher = iter->second;
|
||||
dispatcher.connect(instance, method);
|
||||
}
|
||||
|
||||
template<typename C>
|
||||
@@ -112,6 +122,8 @@ public:
|
||||
template<typename mt_policy = MultiThreadedLocal>
|
||||
using Subscriber = sigslot::has_slots<mt_policy>;
|
||||
|
||||
static int EventRegistryCount() { return internal::GetEventRegistryCount(); }
|
||||
|
||||
EventBus() = default;
|
||||
|
||||
~EventBus()
|
||||
|
@@ -252,4 +252,22 @@ TEST_SUITE("EventBus")
|
||||
});
|
||||
CHECK_EQ(subscriber.counter.load(), 2);
|
||||
}
|
||||
|
||||
TEST_CASE("EventRegistryCount")
|
||||
{
|
||||
struct EventType1 {};
|
||||
|
||||
struct EventType2 {};
|
||||
|
||||
sled::EventBus bus;
|
||||
int current_count = sled::EventBus::EventRegistryCount();
|
||||
bus.Post(EventType1{});
|
||||
CHECK_EQ(sled::EventBus::EventRegistryCount(), current_count + 1);
|
||||
|
||||
bus.Post(EventType1{});
|
||||
CHECK_EQ(sled::EventBus::EventRegistryCount(), current_count + 1);
|
||||
|
||||
bus.Post(EventType2{});
|
||||
CHECK_EQ(sled::EventBus::EventRegistryCount(), current_count + 2);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user