Improve Event::Collector::getBus

Some of people make such thing:

listener.getBus().listen<T>()...

And they thought that listening with our listener.
So need to fix bad conceptual mistake.
This commit is contained in:
Dawid Drozd 2017-11-24 12:42:50 +01:00
parent 951d3f1e42
commit 3ddb5ac7c5
2 changed files with 34 additions and 3 deletions

View File

@ -11,6 +11,29 @@
namespace Dexode
{
class BusAttorney
{
public:
BusAttorney(std::shared_ptr<EventBus> bus)
: _bus(std::move(bus))
{
}
/**
* Notify all listeners for event
*
* @param event your event struct
*/
template<typename Event>
void notify(const Event& event)
{
_bus->notify(event);
}
private:
std::shared_ptr<EventBus> _bus;
};
class EventCollector
{
public:
@ -61,7 +84,10 @@ public:
}
}
const std::shared_ptr<EventBus>& getBus() const;
bool isUsing(const std::shared_ptr<EventBus>& bus) const;
///I wan't explicitly say getBus. Ok we could add method for notify but this is more explicit
BusAttorney getBus() const;
private:
int _token = 0;

View File

@ -86,9 +86,14 @@ void EventCollector::unlistenAll()
}
}
const std::shared_ptr<EventBus>& EventCollector::getBus() const
BusAttorney EventCollector::getBus() const
{
return _bus;
return BusAttorney{_bus};
}
bool EventCollector::isUsing(const std::shared_ptr<EventBus>& bus) const
{
return _bus == bus;
}
}