From caa2b3e9ff887d6d6fbae54fb8c4341fc7060cde Mon Sep 17 00:00:00 2001 From: Dawid Drozd Date: Sat, 14 Sep 2019 14:59:40 +0200 Subject: [PATCH] Add dummy Listener without assigned bus Thanks to that we can store Listener without ASAP initialization with bus --- lib/include/dexode/eventbus/Listener.hpp | 29 ++++++++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/include/dexode/eventbus/Listener.hpp b/lib/include/dexode/eventbus/Listener.hpp index 8be6265..ad49177 100644 --- a/lib/include/dexode/eventbus/Listener.hpp +++ b/lib/include/dexode/eventbus/Listener.hpp @@ -11,7 +11,9 @@ template class Listener { public: - Listener(Bus& bus) + explicit Listener() = default; // Dummy listener + + explicit Listener(Bus& bus) : _id{bus.newListenerID()} , _bus{&bus} {} @@ -43,8 +45,10 @@ public: return *this; } - unlistenAll(); - + if(_bus != nullptr) + { + unlistenAll(); + } _id = other._id; other._id = 0; _bus = other._bus; @@ -56,6 +60,10 @@ public: template void listen(std::function&& callback) { + if(_bus == nullptr) + { + throw std::runtime_error{"bus is null"}; + } _bus->template listen(_id, std::forward>(callback)); } @@ -63,24 +71,35 @@ public: 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) + { + throw std::runtime_error{"bus is null"}; + } _bus->unlistenAll(_id); } template void unlisten() { + if(_bus == nullptr) + { + throw std::runtime_error{"bus is null"}; + } _bus->template unlisten(_id); } private: std::uint32_t _id = 0; - Bus* _bus; + Bus* _bus = nullptr; }; } // namespace dexode::eventbus