mirror of
https://github.com/gelldur/EventBus.git
synced 2025-01-14 01:07:59 +08:00
Update documentation and naming
This commit is contained in:
parent
4c667c7c46
commit
2d14ca521d
@ -12,6 +12,11 @@ class Event
|
||||
public:
|
||||
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)
|
||||
: _key{std::hash<std::string>{}(name + typeid(Callback).name())}
|
||||
, _name{name}
|
||||
|
@ -39,10 +39,10 @@ public:
|
||||
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 callback - your callback to handle notification
|
||||
* @param event - you want to listen for
|
||||
* @param callback - your callback to handle event
|
||||
* @return token used for unlisten
|
||||
*/
|
||||
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 callback - your callback to handle notification
|
||||
* @param callback - your callback to handle event
|
||||
* @return token used for unlisten
|
||||
*/
|
||||
template<typename ... Args>
|
||||
@ -70,8 +70,8 @@ public:
|
||||
|
||||
/**
|
||||
* @param token - unique token for identification receiver. Simply pass token from @see EventBus::listen
|
||||
* @param event - pass notification like "getNotificationXYZ()"
|
||||
* @param callback - your callback to handle notification
|
||||
* @param event - you want to listen for
|
||||
* @param callback - your callback to handle event
|
||||
*/
|
||||
template<typename ... Args>
|
||||
void listen(const int token
|
||||
@ -81,7 +81,7 @@ public:
|
||||
using CallbackType = std::function<void(Args...)>;
|
||||
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()];
|
||||
if (vector == nullptr)
|
||||
@ -107,7 +107,7 @@ public:
|
||||
|
||||
/**
|
||||
* @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>
|
||||
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>
|
||||
void notify(const EventType& event, Args&& ... params)
|
||||
{
|
||||
@ -146,6 +154,13 @@ public:
|
||||
// We can't reduce it to notify("yes",value)
|
||||
// It wouldn't be obvious which to call Event<int> or Event<int&>
|
||||
// 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>
|
||||
void notify(const std::string& eventName, Args&& ... params)
|
||||
{
|
||||
|
@ -14,8 +14,8 @@ namespace Dexode
|
||||
class EventCollector
|
||||
{
|
||||
public:
|
||||
EventCollector(const std::shared_ptr<EventBus>& notifier);
|
||||
EventCollector(EventBus* notifier);
|
||||
EventCollector(const std::shared_ptr<EventBus>& bus);
|
||||
EventCollector(EventBus* bus);
|
||||
EventCollector(EventCollector const& other);
|
||||
EventCollector(EventCollector&& other);
|
||||
|
||||
@ -25,60 +25,60 @@ public:
|
||||
EventCollector& operator=(EventCollector&& other);
|
||||
|
||||
/**
|
||||
* Register listener for notification.
|
||||
* Register listener for event.
|
||||
*
|
||||
* @param notification - pass notification like "getNotificationXYZ()"
|
||||
* @param callback - your callback to handle notification
|
||||
* @param event - you want to listen for
|
||||
* @param callback - your callback to handle event
|
||||
*/
|
||||
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)
|
||||
{
|
||||
if (!callback || !_notifier)
|
||||
if (!callback || !_bus)
|
||||
{
|
||||
return;//Skip such things
|
||||
}
|
||||
if (_token == 0)
|
||||
{
|
||||
_token = _notifier->listen(notification, callback);
|
||||
_token = _bus->listen(event, callback);
|
||||
}
|
||||
else
|
||||
{
|
||||
_notifier->listen(_token, notification, callback);
|
||||
_bus->listen(_token, event, callback);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register listener for notification. Returns token used for unlisten
|
||||
*
|
||||
* @param notification - name of your notification
|
||||
* @param callback - your callback to handle notification
|
||||
* @param notification - name of your event
|
||||
* @param callback - your callback to handle event
|
||||
* @return token used for unlisten
|
||||
*/
|
||||
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)
|
||||
{
|
||||
listen(Dexode::Event<Args...>{notificationName}, callback);
|
||||
listen(Dexode::Event<Args...>{eventName}, callback);
|
||||
}
|
||||
|
||||
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>
|
||||
void unlisten(const NotificationType& notification)
|
||||
template<typename EventType, typename ... Args>
|
||||
void unlisten(const EventType& event)
|
||||
{
|
||||
if (_notifier)
|
||||
if (_bus)
|
||||
{
|
||||
_notifier->unlisten(_token, notification);
|
||||
_bus->unlisten(_token, event);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
int _token = 0;
|
||||
std::shared_ptr<EventBus> _notifier;
|
||||
std::shared_ptr<EventBus> _bus;
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -18,26 +18,26 @@ void null_deleter(Dexode::EventBus*)
|
||||
namespace Dexode
|
||||
{
|
||||
|
||||
EventCollector::EventCollector(const std::shared_ptr<EventBus>& notifier)
|
||||
: _notifier(notifier)
|
||||
EventCollector::EventCollector(const std::shared_ptr<EventBus>& bus)
|
||||
: _bus(bus)
|
||||
{
|
||||
assert(_notifier);
|
||||
assert(_bus);
|
||||
}
|
||||
|
||||
//Maybe ugly but hey ;) Less code and simply i can :D
|
||||
EventCollector::EventCollector(EventBus* notifier)
|
||||
: _notifier(notifier, &null_deleter)
|
||||
EventCollector::EventCollector(EventBus* bus)
|
||||
: _bus(bus, &null_deleter)
|
||||
{
|
||||
}
|
||||
|
||||
EventCollector::EventCollector(EventCollector const& other)
|
||||
: _notifier(other._notifier)
|
||||
: _bus(other._bus)
|
||||
{
|
||||
}
|
||||
|
||||
EventCollector::EventCollector(EventCollector&& other)
|
||||
: _token(other._token)
|
||||
, _notifier(std::move(other._notifier))
|
||||
, _bus(std::move(other._bus))
|
||||
{
|
||||
other._token = 0;
|
||||
}
|
||||
@ -53,10 +53,10 @@ EventCollector& EventCollector::operator=(EventCollector const& other)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
if (other._notifier.get() != _notifier.get())
|
||||
if (other._bus.get() != _bus.get())
|
||||
{
|
||||
unlistenAll();
|
||||
_notifier = other._notifier;
|
||||
_bus = other._bus;
|
||||
}
|
||||
|
||||
return *this;
|
||||
@ -73,16 +73,16 @@ EventCollector& EventCollector::operator=(EventCollector&& other)
|
||||
|
||||
_token = other._token;
|
||||
other._token = 0;
|
||||
_notifier = std::move(other._notifier);
|
||||
_bus = std::move(other._bus);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void EventCollector::unlistenAll()
|
||||
{
|
||||
if (_token != 0 && _notifier)
|
||||
if (_token != 0 && _bus)
|
||||
{
|
||||
_notifier->unlistenAll(_token);
|
||||
_bus->unlistenAll(_token);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,6 @@
|
||||
|
||||
void sampleUsages()
|
||||
{
|
||||
|
||||
{
|
||||
//Notify by Event object
|
||||
Dexode::EventBus bus;
|
||||
|
Loading…
x
Reference in New Issue
Block a user