Update documentation and naming

This commit is contained in:
Dawid Drozd 2017-08-06 17:24:13 +02:00
parent 4c667c7c46
commit 2d14ca521d
5 changed files with 60 additions and 41 deletions

View File

@ -12,6 +12,11 @@ class Event
public: public:
using Callback = std::function<void(Args...)>; using Callback = std::function<void(Args...)>;
/**
* Creates unique Event for combination of event name and types of Event class
*
* @param name unique name to distinguish your event.
*/
constexpr explicit Event(const std::string& name) constexpr explicit Event(const std::string& name)
: _key{std::hash<std::string>{}(name + typeid(Callback).name())} : _key{std::hash<std::string>{}(name + typeid(Callback).name())}
, _name{name} , _name{name}

View File

@ -39,10 +39,10 @@ public:
EventBus& operator=(const EventBus&) = delete; EventBus& operator=(const EventBus&) = delete;
/** /**
* Register listener for notification. Returns token used for unlisten * Register listener for event. Returns token used for unlisten.
* *
* @param event * @param event - you want to listen for
* @param callback - your callback to handle notification * @param callback - your callback to handle event
* @return token used for unlisten * @return token used for unlisten
*/ */
template<typename ... Args> template<typename ... Args>
@ -55,10 +55,10 @@ public:
} }
/** /**
* Register listener for notification. Returns token used for unlisten * Register listener for event. Returns token used for unlisten.
* *
* @param eventName - name of your event * @param eventName - name of your event
* @param callback - your callback to handle notification * @param callback - your callback to handle event
* @return token used for unlisten * @return token used for unlisten
*/ */
template<typename ... Args> template<typename ... Args>
@ -70,8 +70,8 @@ public:
/** /**
* @param token - unique token for identification receiver. Simply pass token from @see EventBus::listen * @param token - unique token for identification receiver. Simply pass token from @see EventBus::listen
* @param event - pass notification like "getNotificationXYZ()" * @param event - you want to listen for
* @param callback - your callback to handle notification * @param callback - your callback to handle event
*/ */
template<typename ... Args> template<typename ... Args>
void listen(const int token void listen(const int token
@ -81,7 +81,7 @@ public:
using CallbackType = std::function<void(Args...)>; using CallbackType = std::function<void(Args...)>;
using Vector = VectorImpl<CallbackType>; using Vector = VectorImpl<CallbackType>;
assert(callback && "Please set it");//Check for valid object assert(callback && "callback should be valid");//Check for valid object
std::unique_ptr<VectorInterface>& vector = _callbacks[event.getKey()]; std::unique_ptr<VectorInterface>& vector = _callbacks[event.getKey()];
if (vector == nullptr) if (vector == nullptr)
@ -107,7 +107,7 @@ public:
/** /**
* @param token - token from EventBus::listen * @param token - token from EventBus::listen
* @param event - notification you wan't to unlisten. @see Notiier::listen * @param event - event you wan't to unlisten. @see Notiier::listen
*/ */
template<typename EventType, typename ... Args> template<typename EventType, typename ... Args>
void unlisten(const int token, const EventType& event) void unlisten(const int token, const EventType& event)
@ -119,6 +119,14 @@ public:
} }
} }
/**
* Notify all listeners for event with arguments
*
* @tparam EventType eg. Dexode::Event<int> event type
* @tparam Args all your types of your event
* @param event instance of Dexode::Event class
* @param params arguments that you want to pass
*/
template<typename EventType, typename ... Args> template<typename EventType, typename ... Args>
void notify(const EventType& event, Args&& ... params) void notify(const EventType& event, Args&& ... params)
{ {
@ -146,6 +154,13 @@ public:
// We can't reduce it to notify("yes",value) // We can't reduce it to notify("yes",value)
// It wouldn't be obvious which to call Event<int> or Event<int&> // It wouldn't be obvious which to call Event<int> or Event<int&>
// So it can take to a lot of mistakes // So it can take to a lot of mistakes
/**
* Notify all listeners for event with arguments
*
* @tparam Args all your types of your event
* @param eventName name of event
* @param params arguments that you want to pass
*/
template<typename ... Args> template<typename ... Args>
void notify(const std::string& eventName, Args&& ... params) void notify(const std::string& eventName, Args&& ... params)
{ {

View File

@ -14,8 +14,8 @@ namespace Dexode
class EventCollector class EventCollector
{ {
public: public:
EventCollector(const std::shared_ptr<EventBus>& notifier); EventCollector(const std::shared_ptr<EventBus>& bus);
EventCollector(EventBus* notifier); EventCollector(EventBus* bus);
EventCollector(EventCollector const& other); EventCollector(EventCollector const& other);
EventCollector(EventCollector&& other); EventCollector(EventCollector&& other);
@ -25,60 +25,60 @@ public:
EventCollector& operator=(EventCollector&& other); EventCollector& operator=(EventCollector&& other);
/** /**
* Register listener for notification. * Register listener for event.
* *
* @param notification - pass notification like "getNotificationXYZ()" * @param event - you want to listen for
* @param callback - your callback to handle notification * @param callback - your callback to handle event
*/ */
template<typename ... Args> template<typename ... Args>
void listen(const Event<Args...>& notification void listen(const Event<Args...>& event
, typename eventbus_traits<const std::function<void(Args...)>&>::type callback) , typename eventbus_traits<const std::function<void(Args...)>&>::type callback)
{ {
if (!callback || !_notifier) if (!callback || !_bus)
{ {
return;//Skip such things return;//Skip such things
} }
if (_token == 0) if (_token == 0)
{ {
_token = _notifier->listen(notification, callback); _token = _bus->listen(event, callback);
} }
else else
{ {
_notifier->listen(_token, notification, callback); _bus->listen(_token, event, callback);
} }
} }
/** /**
* Register listener for notification. Returns token used for unlisten * Register listener for notification. Returns token used for unlisten
* *
* @param notification - name of your notification * @param notification - name of your event
* @param callback - your callback to handle notification * @param callback - your callback to handle event
* @return token used for unlisten * @return token used for unlisten
*/ */
template<typename ... Args> template<typename ... Args>
void listen(const std::string& notificationName void listen(const std::string& eventName
, typename eventbus_traits<const std::function<void(Args...)>&>::type callback) , typename eventbus_traits<const std::function<void(Args...)>&>::type callback)
{ {
listen(Dexode::Event<Args...>{notificationName}, callback); listen(Dexode::Event<Args...>{eventName}, callback);
} }
void unlistenAll(); void unlistenAll();
/** /**
* @param notification - notification you wan't to unlisten. @see Notiier::listen * @param event - notification you wan't to unlisten. @see Notiier::listen
*/ */
template<typename NotificationType, typename ... Args> template<typename EventType, typename ... Args>
void unlisten(const NotificationType& notification) void unlisten(const EventType& event)
{ {
if (_notifier) if (_bus)
{ {
_notifier->unlisten(_token, notification); _bus->unlisten(_token, event);
} }
} }
private: private:
int _token = 0; int _token = 0;
std::shared_ptr<EventBus> _notifier; std::shared_ptr<EventBus> _bus;
}; };
} }

View File

@ -18,26 +18,26 @@ void null_deleter(Dexode::EventBus*)
namespace Dexode namespace Dexode
{ {
EventCollector::EventCollector(const std::shared_ptr<EventBus>& notifier) EventCollector::EventCollector(const std::shared_ptr<EventBus>& bus)
: _notifier(notifier) : _bus(bus)
{ {
assert(_notifier); assert(_bus);
} }
//Maybe ugly but hey ;) Less code and simply i can :D //Maybe ugly but hey ;) Less code and simply i can :D
EventCollector::EventCollector(EventBus* notifier) EventCollector::EventCollector(EventBus* bus)
: _notifier(notifier, &null_deleter) : _bus(bus, &null_deleter)
{ {
} }
EventCollector::EventCollector(EventCollector const& other) EventCollector::EventCollector(EventCollector const& other)
: _notifier(other._notifier) : _bus(other._bus)
{ {
} }
EventCollector::EventCollector(EventCollector&& other) EventCollector::EventCollector(EventCollector&& other)
: _token(other._token) : _token(other._token)
, _notifier(std::move(other._notifier)) , _bus(std::move(other._bus))
{ {
other._token = 0; other._token = 0;
} }
@ -53,10 +53,10 @@ EventCollector& EventCollector::operator=(EventCollector const& other)
{ {
return *this; return *this;
} }
if (other._notifier.get() != _notifier.get()) if (other._bus.get() != _bus.get())
{ {
unlistenAll(); unlistenAll();
_notifier = other._notifier; _bus = other._bus;
} }
return *this; return *this;
@ -73,16 +73,16 @@ EventCollector& EventCollector::operator=(EventCollector&& other)
_token = other._token; _token = other._token;
other._token = 0; other._token = 0;
_notifier = std::move(other._notifier); _bus = std::move(other._bus);
return *this; return *this;
} }
void EventCollector::unlistenAll() void EventCollector::unlistenAll()
{ {
if (_token != 0 && _notifier) if (_token != 0 && _bus)
{ {
_notifier->unlistenAll(_token); _bus->unlistenAll(_token);
} }
} }

View File

@ -7,7 +7,6 @@
void sampleUsages() void sampleUsages()
{ {
{ {
//Notify by Event object //Notify by Event object
Dexode::EventBus bus; Dexode::EventBus bus;