Update encapsulation for accessing private stuff from EventBus

This commit is contained in:
Dawid Drozd 2019-10-30 17:28:59 +01:00
parent 1adbc61a03
commit 890eaff178
4 changed files with 61 additions and 6 deletions

View File

@ -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

View File

@ -14,7 +14,8 @@ namespace dexode
template <class Strategy>
class EventBus
{
friend class eventbus::Listener<EventBus<Strategy>>;
template <typename>
friend class dexode::eventbus::internal::ListenerAttorney;
public:
using Listener = eventbus::Listener<EventBus<Strategy>>;

View File

@ -4,6 +4,8 @@
#include <functional>
#include <memory>
#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> bus)
: _id{bus->newListenerID()}
: _id{internal::ListenerAttorney<Bus>::newListenerID(*bus)}
, _bus{std::move(bus)}
{}
@ -65,8 +67,9 @@ public:
{
throw std::runtime_error{"bus is null"};
}
_bus->template listen<Event>(_id,
std::forward<std::function<void(const Event&)>>(callback));
internal::ListenerAttorney<Bus>::template listen<Event>(
*_bus, _id, std::forward<std::function<void(const Event&)>>(callback));
}
void unlistenAll()
@ -75,7 +78,7 @@ public:
{
throw std::runtime_error{"bus is null"};
}
_bus->unlistenAll(_id);
internal::ListenerAttorney<Bus>::unlistenAll(*_bus, _id);
}
template <typename Event>
@ -85,7 +88,7 @@ public:
{
throw std::runtime_error{"bus is null"};
}
_bus->template unlisten<Event>(_id);
internal::ListenerAttorney<Bus>::template unlisten<Event>(*_bus, _id);
}
private:

View File

@ -0,0 +1,50 @@
//
// Created by gelldur on 30.10.2019.
//
#pragma once
//#include "dexode/EventBus.hpp"
namespace dexode::eventbus
{
template <typename>
class Listener;
} // namespace dexode::eventbus
namespace dexode::eventbus::internal
{
template <typename EventBus_t>
class ListenerAttorney
{
template <typename>
friend class dexode::eventbus::Listener;
private:
static constexpr std::uint32_t newListenerID(EventBus_t& bus)
{
return bus.newListenerID();
}
template <class Event>
static constexpr void listen(EventBus_t& bus,
const std::uint32_t listenerID,
std::function<void(const Event&)>&& callback)
{
bus.template listen<Event>(listenerID,
std::forward<std::function<void(const Event&)>>(callback));
}
static constexpr void unlistenAll(EventBus_t& bus, const std::uint32_t listenerID)
{
bus.unlistenAll(listenerID);
}
template <typename Event>
static constexpr void unlisten(EventBus_t& bus, const std::uint32_t listenerID)
{
bus.template unlisten<Event>(listenerID);
}
};
} // namespace dexode::eventbus::internal