Commit 3ead7ff8 authored by tqcq's avatar tqcq
Browse files

feat update

parent e7fbe48b
Loading
Loading
Loading
Loading
+12 −9
Original line number Diff line number Diff line
@@ -50,9 +50,11 @@ target_include_directories(sled PUBLIC src/ 3party/eigen 3party/inja
                                       3party/rxcpp)
target_sources(
  sled
  PRIVATE src/sled/async/async.cc
  PRIVATE 
  src/sled/async/async.cc
          src/sled/debugging/demangle.cc
          src/sled/debugging/symbolize.cc
          src/sled/event_bus/event_bus.cc
          src/sled/filesystem/path.cc
          src/sled/log/log.cc
          src/sled/network/async_resolver.cc
@@ -61,7 +63,11 @@ target_sources(
          src/sled/network/physical_socket_server.cc
          src/sled/network/socket_address.cc
          src/sled/network/socket_server.cc
          src/sled/operations_chain.cc
          src/sled/profiling/profiling.cc
          src/sled/random.cc
          src/sled/sigslot.cc
          src/sled/status.cc
          src/sled/strings/base64.cc
          src/sled/strings/utils.cc
          src/sled/synchronization/event.cc
@@ -72,21 +78,18 @@ target_sources(
          src/sled/system/pid.cc
          src/sled/system/thread.cc
          src/sled/system/thread_pool.cc
          src/sled/system_time.cc
          src/sled/task_queue/pending_task_safety_flag.cc
          src/sled/task_queue/task_queue_base.cc
          src/sled/testing/test.cc
          src/sled/testing/benchmark.cc
          src/sled/testing/test.cc
          src/sled/timer/task_queue_timeout.cc
          src/sled/timer/timer.cc
          src/sled/time_utils.cc
          src/sled/units/time_delta.cc
          src/sled/units/timestamp.cc
          src/sled/operations_chain.cc
          src/sled/random.cc
          src/sled/sigslot.cc
          src/sled/status.cc
          src/sled/system_time.cc
          src/sled/time_utils.cc
          src/sled/uri.cc)
          src/sled/uri.cc
)
# set(BUILD_RTTR_DYNAMIC OFF) set(BUILD_UNIT_TESTS OFF)
# set(BUILD_WITH_STATIC_RUNTIME_LIBS ON) set(BUILD_WITH_DOCUMENTATION OFF)
# add_subdirectory(3party/rttr EXCLUDE_FROM_ALL) include(CheckCCompilerFlag)
+19 −0
Original line number Diff line number Diff line
#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
+18 −6
Original line number Diff line number Diff line
@@ -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,6 +59,8 @@ public:
    void Subscribe(EventBus *bus, C *instance, F &&method)
    {
        sled::SharedMutexWriteLock lock(&shared_mutex_);
        bool is_empty = signals_.empty();
        {
            auto iter = signals_.find(bus);
            if (iter == signals_.end()) {
                signals_.emplace(bus, Dispatcher());
@@ -60,6 +69,7 @@ public:
            auto &dispatcher = iter->second;
            dispatcher.connect(instance, method);
        }
    }

    template<typename C>
    void Unsubscribe(EventBus *bus, C *instance)
@@ -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()
+18 −0
Original line number Diff line number Diff line
@@ -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);
    }
}