From 890eaff1782c57c33a207e77a86e776229651075 Mon Sep 17 00:00:00 2001 From: Dawid Drozd Date: Wed, 30 Oct 2019 17:28:59 +0100 Subject: [PATCH] Update encapsulation for accessing private stuff from EventBus --- lib/CMakeLists.txt | 1 + lib/src/dexode/EventBus.hpp | 3 +- lib/src/dexode/eventbus/Listener.hpp | 13 +++-- .../eventbus/internal/ListenerAttorney.hpp | 50 +++++++++++++++++++ 4 files changed, 61 insertions(+), 6 deletions(-) create mode 100644 lib/src/dexode/eventbus/internal/ListenerAttorney.hpp diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 18fbe4a..fc4137a 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -39,6 +39,7 @@ add_library(EventBus src/dexode/eventbus/internal/AsyncCallbackVector.h src/dexode/eventbus/internal/CallbackVector.h src/dexode/eventbus/internal/common.h + src/dexode/eventbus/internal/ListenerAttorney.hpp src/dexode/eventbus/internal/TransactionCallbackVector.h src/dexode/eventbus/Listener.hpp src/dexode/eventbus/strategy/Protected.cpp src/dexode/eventbus/strategy/Protected.hpp diff --git a/lib/src/dexode/EventBus.hpp b/lib/src/dexode/EventBus.hpp index 97d252f..4c0af00 100644 --- a/lib/src/dexode/EventBus.hpp +++ b/lib/src/dexode/EventBus.hpp @@ -14,7 +14,8 @@ namespace dexode template class EventBus { - friend class eventbus::Listener>; + template + friend class dexode::eventbus::internal::ListenerAttorney; public: using Listener = eventbus::Listener>; diff --git a/lib/src/dexode/eventbus/Listener.hpp b/lib/src/dexode/eventbus/Listener.hpp index 38eb495..a6d4097 100644 --- a/lib/src/dexode/eventbus/Listener.hpp +++ b/lib/src/dexode/eventbus/Listener.hpp @@ -4,6 +4,8 @@ #include #include +#include "dexode/eventbus/internal/ListenerAttorney.hpp" + namespace dexode::eventbus { @@ -14,7 +16,7 @@ public: explicit Listener() = default; // Dummy listener explicit Listener(std::shared_ptr bus) - : _id{bus->newListenerID()} + : _id{internal::ListenerAttorney::newListenerID(*bus)} , _bus{std::move(bus)} {} @@ -65,8 +67,9 @@ public: { throw std::runtime_error{"bus is null"}; } - _bus->template listen(_id, - std::forward>(callback)); + + internal::ListenerAttorney::template listen( + *_bus, _id, std::forward>(callback)); } void unlistenAll() @@ -75,7 +78,7 @@ public: { throw std::runtime_error{"bus is null"}; } - _bus->unlistenAll(_id); + internal::ListenerAttorney::unlistenAll(*_bus, _id); } template @@ -85,7 +88,7 @@ public: { throw std::runtime_error{"bus is null"}; } - _bus->template unlisten(_id); + internal::ListenerAttorney::template unlisten(*_bus, _id); } private: diff --git a/lib/src/dexode/eventbus/internal/ListenerAttorney.hpp b/lib/src/dexode/eventbus/internal/ListenerAttorney.hpp new file mode 100644 index 0000000..71ab0c8 --- /dev/null +++ b/lib/src/dexode/eventbus/internal/ListenerAttorney.hpp @@ -0,0 +1,50 @@ +// +// Created by gelldur on 30.10.2019. +// +#pragma once + +//#include "dexode/EventBus.hpp" + +namespace dexode::eventbus +{ +template +class Listener; +} // namespace dexode::eventbus + +namespace dexode::eventbus::internal +{ + +template +class ListenerAttorney +{ + template + friend class dexode::eventbus::Listener; + +private: + static constexpr std::uint32_t newListenerID(EventBus_t& bus) + { + return bus.newListenerID(); + } + + template + static constexpr void listen(EventBus_t& bus, + const std::uint32_t listenerID, + std::function&& callback) + { + bus.template listen(listenerID, + std::forward>(callback)); + } + + static constexpr void unlistenAll(EventBus_t& bus, const std::uint32_t listenerID) + { + bus.unlistenAll(listenerID); + } + + template + static constexpr void unlisten(EventBus_t& bus, const std::uint32_t listenerID) + { + bus.template unlisten(listenerID); + } +}; + +} // namespace dexode::eventbus::internal