diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 2ed92e3..ab977b9 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -46,9 +46,8 @@ add_library(EventBus # New version of EventBus 3.0 include/dexode/EventBus.hpp include/dexode/eventbus/Listener.hpp - include/dexode/eventbus/Subscriber.hpp src/dexode/eventbus/strategy/Protected.cpp include/dexode/eventbus/strategy/Protected.hpp - src/dexode/eventbus/strategy/Transaction.cpp include/dexode/eventbus/strategy/Transaction.hpp + include/dexode/eventbus/strategy/Transaction.hpp ) add_library(Dexode::EventBus ALIAS EventBus) @@ -63,7 +62,6 @@ target_include_directories(EventBus PUBLIC set(EventBus_PUBLIC_HEADERS include/dexode/EventBus.hpp include/dexode/eventbus/Listener.hpp - include/dexode/eventbus/Subscriber.hpp include/dexode/eventbus/strategy/Protected.hpp include/dexode/eventbus/strategy/Transaction.hpp ) diff --git a/lib/include/dexode/EventBus.hpp b/lib/include/dexode/EventBus.hpp index b8d06c0..3e273d8 100644 --- a/lib/include/dexode/EventBus.hpp +++ b/lib/include/dexode/EventBus.hpp @@ -9,7 +9,6 @@ #include #include "eventbus/Listener.hpp" -#include "eventbus/Subscriber.hpp" #include "eventbus/internal/common.h" namespace dexode @@ -22,7 +21,6 @@ class EventBus public: using Listener = eventbus::Listener>; - using Subscriber = eventbus::Subscriber>; constexpr EventBus() = default; ~EventBus() = default; @@ -33,11 +31,6 @@ public: EventBus& operator=(EventBus&&) = delete; EventBus& operator=(const EventBus&) = delete; - Listener createListener() - { - return Listener{*this}; - } - template constexpr void post(const Event& event) { diff --git a/lib/include/dexode/eventbus/Listener.hpp b/lib/include/dexode/eventbus/Listener.hpp index ad49177..8bc4b57 100644 --- a/lib/include/dexode/eventbus/Listener.hpp +++ b/lib/include/dexode/eventbus/Listener.hpp @@ -13,20 +13,17 @@ class Listener public: explicit Listener() = default; // Dummy listener - explicit Listener(Bus& bus) - : _id{bus.newListenerID()} - , _bus{&bus} + explicit Listener(std::shared_ptr bus) + : _id{bus->newListenerID()} + , _bus{std::move(bus)} {} Listener(const Listener& other) = delete; Listener(Listener&& other) : _id(other._id) - , _bus(other._bus) - { - other._id = 0; - other._bus = nullptr; - } + , _bus(std::move(other._bus)) + {} ~Listener() { @@ -38,7 +35,7 @@ public: Listener& operator=(const Listener& other) = delete; - Listener& operator=(Listener&& other) + Listener& operator=(Listener&& other) noexcept { if(this == &other) { @@ -47,12 +44,10 @@ public: if(_bus != nullptr) { - unlistenAll(); + unlistenAll(); // remove previous } _id = other._id; - other._id = 0; - _bus = other._bus; - other._bus = nullptr; + _bus = std::move(other._bus); return *this; } @@ -68,16 +63,6 @@ public: std::forward>(callback)); } - template - void listen(const std::function& callback) - { - if(_bus == nullptr) - { - throw std::runtime_error{"bus is null"}; - } - _bus->template listen(_id, callback); - } - void unlistenAll() { if(_bus == nullptr) @@ -99,7 +84,7 @@ public: private: std::uint32_t _id = 0; - Bus* _bus = nullptr; + std::shared_ptr _bus = nullptr; }; } // namespace dexode::eventbus diff --git a/lib/include/dexode/eventbus/Subscriber.hpp b/lib/include/dexode/eventbus/Subscriber.hpp deleted file mode 100644 index 6271a48..0000000 --- a/lib/include/dexode/eventbus/Subscriber.hpp +++ /dev/null @@ -1,71 +0,0 @@ -#pragma once - -#include "dexode/EventBus.hpp" - -namespace dexode::eventbus -{ -template -class Subscriber -{ -public: - Subscriber(std::shared_ptr retainBus) - : _listener{*retainBus} - , _bus{std::move(retainBus)} - {} - - Subscriber(Subscriber&& other) - : _listener(std::move(other._listener)) - , _bus(std::move(other._bus)) - {} - - ~Subscriber() - { - unlistenAll(); // extra safety if order of members is inverse - } - - Subscriber& operator=(Subscriber&& other) - { - if(this == &other) - { - return *this; - } - - _listener = std::move(other._listener); - _bus = std::move(other._bus); - - return *this; - } - - template - void listen(std::function&& callback) - { - _listener.template listen(std::forward>(callback)); - } - - template - void listen(const std::function& callback) - { - _listener.template listen(callback); - } - - void unlistenAll() - { - _listener.unlistenAll(); - } - - template - void unlisten() - { - _listener.template unlisten(); - } - - const std::shared_ptr& getEventBus() const - { - return _bus; - } - -private: - Listener _listener; - std::shared_ptr _bus; // own -}; -} // namespace dexode::eventbus diff --git a/lib/src/dexode/eventbus/strategy/Transaction.cpp b/lib/src/dexode/eventbus/strategy/Transaction.cpp deleted file mode 100644 index 935c4c5..0000000 --- a/lib/src/dexode/eventbus/strategy/Transaction.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include "dexode/eventbus/strategy/Transaction.hpp" - -namespace dexode::eventbus::strategy -{ - -}