Rename EventContainer to EventCollector

This commit is contained in:
Dawid Drozd 2016-11-14 09:24:40 +01:00
parent d4b0e37a74
commit d14e07bd3e
3 changed files with 96 additions and 44 deletions

83
src/EventCollector.cpp Normal file
View File

@ -0,0 +1,83 @@
//
// Created by Dawid Drozd aka Gelldur on 18/10/16.
//
#include "EventCollector.h"
namespace Dexode
{
EventCollector::EventCollector(const std::shared_ptr<Notifier>& notifier)
: _notifier(notifier)
{
assert(_notifier);
}
void null_deleter(Notifier*)
{
}
//Maybe ugly but hey ;) Less code and simply i can :D
EventCollector::EventCollector(Notifier& notifier)
: _notifier(&notifier, &null_deleter)
{
assert(_notifier);
}
EventCollector::EventCollector(EventCollector const& other)
: _notifier(other._notifier)
{
}
EventCollector::EventCollector(EventCollector&& other)
: _token(other._token)
, _notifier(std::move(other._notifier))
{
other._token = 0;
}
EventCollector::~EventCollector()
{
unlistenAll();
}
EventCollector& EventCollector::operator=(EventCollector const& other)
{
if (this == &other)
{
return *this;
}
if (other._notifier.get() != _notifier.get())
{
unlistenAll();
_notifier = other._notifier;
}
return *this;
}
EventCollector& EventCollector::operator=(EventCollector&& other)
{
if (this == &other)
{
return *this;
}
unlistenAll();
_token = other._token;
other._token = 0;
_notifier = std::move(other._notifier);
return *this;
}
void EventCollector::unlistenAll()
{
if (_token != 0)
{
_notifier->unlistenAll(_token);
}
}
}

View File

@ -11,13 +11,18 @@
namespace Dexode
{
class EventContainer
class EventCollector
{
public:
EventContainer(const std::shared_ptr<Notifier>& notifier);
EventContainer(Notifier& notifier = Notifier::getGlobal());
EventCollector(const std::shared_ptr<Notifier>& notifier);
EventCollector(Notifier& notifier = Notifier::getGlobal());
EventCollector(EventCollector const& other);
EventCollector(EventCollector&& other);
~EventContainer();
~EventCollector();
EventCollector& operator=(EventCollector const& other);
EventCollector& operator=(EventCollector&& other);
/**
* Register listener for notification. Returns token used to unregister
@ -29,6 +34,10 @@ public:
void listen(const Notification<Args...>& notification
, typename notifier_traits<const std::function<void(Args...)>&>::type callback)
{
if (!callback)
{
return;//Skip such things
}
if (_token == 0)
{
_token = _notifier->listen(notification, callback);

View File

@ -1,40 +0,0 @@
//
// Created by Dawid Drozd aka Gelldur on 18/10/16.
//
#include "EventContainer.h"
namespace Dexode
{
EventContainer::EventContainer(const std::shared_ptr<Notifier>& notifier)
: _notifier(notifier)
{
assert(_notifier);
}
void null_deleter(Notifier*)
{
}
//Maybe ugly but hey ;) Less code and simply i can :D
EventContainer::EventContainer(Notifier& notifier)
: _notifier(&notifier, &null_deleter)
{
assert(_notifier);
}
EventContainer::~EventContainer()
{
unlistenAll();
}
void EventContainer::unlistenAll()
{
if (_token != 0)
{
_notifier->unlistenAll(_token);
}
}
}