1
0
mirror of https://github.com/wqking/eventpp.git synced 2024-12-29 18:38:33 +08:00
eventpp/doc/eventdispatcher.md

166 lines
6.9 KiB
Markdown
Raw Normal View History

2018-05-13 12:26:27 +08:00
# Class EventDispatcher reference
2019-04-02 19:33:01 +08:00
<!--begintoc-->
2018-05-14 20:00:29 +08:00
## Table Of Contents
2019-03-31 22:32:04 +08:00
* [Description](#a2_1)
* [API reference](#a2_2)
2019-03-31 11:22:36 +08:00
* [Header](#a3_1)
* [Template parameters](#a3_2)
* [Public types](#a3_3)
* [Member functions](#a3_4)
2019-03-31 22:32:04 +08:00
* [Nested listener safety](#a2_3)
* [Time complexities](#a2_4)
* [Internal data structure](#a2_5)
2019-03-31 11:22:36 +08:00
<!--endtoc-->
2019-03-31 22:32:04 +08:00
<a id="a2_1"></a>
## Description
EventDispatcher is something like std::map<EventType, CallbackList>.
EventDispatcher holds a map of `<EventType, CallbackList>` pairs. On dispatching, EventDispatcher finds the CallbackList of the event type, then invoke the callback list. The invocation is always synchronous. The listeners are triggered when `EventDispatcher::dispatch` is called.
2019-03-31 22:32:04 +08:00
<a id="a2_2"></a>
2018-05-13 12:26:27 +08:00
## API reference
2019-03-31 11:22:36 +08:00
<a id="a3_1"></a>
### Header
2018-05-19 17:42:41 +08:00
eventpp/eventdispatcher.h
2019-03-31 11:22:36 +08:00
<a id="a3_2"></a>
### Template parameters
2018-05-13 12:26:27 +08:00
```c++
template <
2018-05-26 09:01:18 +08:00
typename Event,
2018-05-13 12:26:27 +08:00
typename Prototype,
2018-05-26 09:01:18 +08:00
typename Policies = DefaultPolicies
2018-05-13 12:26:27 +08:00
>
class EventDispatcher;
```
2018-05-26 09:01:18 +08:00
`Event`: the *event type*. The type used to identify the event. Events with same type are the same event. The event type must be able to be used as the key in `std::map` or `std::unordered_map`, so it must be either comparable with `operator <` or has specialization of `std::hash`.
`Prototype`: the listener prototype. It's C++ function type such as `void(int, std::string, const MyClass *)`.
`Policies`: the policies to configure and extend the dispatcher. The default value is `DefaultPolicies`. See [document of policies](policies.md) for details.
2018-05-13 12:26:27 +08:00
2019-03-31 11:22:36 +08:00
<a id="a3_3"></a>
### Public types
2018-05-13 12:26:27 +08:00
`Handle`: the handle type returned by appendListener, prependListener and insertListener. A handle can be used to insert a listener or remove a listener. To check if a `Handle` is empty, convert it to boolean, *false* is empty. `Handle` is copyable.
2018-05-13 12:26:27 +08:00
`Callback`: the callback storage type.
2018-05-26 09:01:18 +08:00
`Event`: the event type.
2018-05-13 12:26:27 +08:00
2019-03-31 11:22:36 +08:00
<a id="a3_4"></a>
### Member functions
2018-05-13 12:26:27 +08:00
2019-04-02 19:33:01 +08:00
#### constructors
2018-05-13 12:26:27 +08:00
```c++
EventDispatcher();
EventDispatcher(const EventDispatcher & other);
EventDispatcher(EventDispatcher && other) noexcept;
EventDispatcher & operator = (const EventDispatcher & other);
EventDispatcher & operator = (EventDispatcher && other) noexcept;
2018-05-13 12:26:27 +08:00
```
EventDispatcher can be copied, moved, assigned, and move assigned.
2018-05-13 12:26:27 +08:00
2019-04-02 19:33:01 +08:00
#### appendListener
2018-05-13 12:26:27 +08:00
```c++
2018-05-19 17:42:41 +08:00
Handle appendListener(const Event & event, const Callback & callback);
2018-05-13 12:26:27 +08:00
```
Add the *callback* to the dispatcher to listen to *event*.
The listener is added to the end of the listener list.
Return a handle which represents the listener. The handle can be used to remove this listener or insert other listener before this listener.
If `appendListener` is called in another listener during a dispatching, the new listener is guaranteed not triggered during the same dispatching.
2018-05-26 09:01:18 +08:00
If the same callback is added twice, it results duplicated listeners.
2018-05-13 12:26:27 +08:00
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### prependListener
2018-05-13 12:26:27 +08:00
```c++
2018-05-19 17:42:41 +08:00
Handle prependListener(const Event & event, const Callback & callback);
2018-05-13 12:26:27 +08:00
```
Add the *callback* to the dispatcher to listen to *event*.
The listener is added to the beginning of the listener list.
Return a handle which represents the listener. The handle can be used to remove this listener or insert other listener before this listener.
If `prependListener` is called in another listener during a dispatching, the new listener is guaranteed not triggered during the same dispatching.
2018-05-13 12:26:27 +08:00
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### insertListener
2018-05-13 12:26:27 +08:00
```c++
2018-05-19 17:42:41 +08:00
Handle insertListener(const Event & event, const Callback & callback, const Handle before);
2018-05-13 12:26:27 +08:00
```
Insert the *callback* to the dispatcher to listen to *event* before the listener handle *before*. If *before* is not found, *callback* is added at the end of the listener list.
Return a handle which represents the listener. The handle can be used to remove this listener or insert other listener before this listener.
If `insertListener` is called in another listener during a dispatching, the new listener is guaranteed not triggered during the same dispatching.
2018-05-13 12:26:27 +08:00
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### removeListener
2018-05-13 12:26:27 +08:00
```c++
2018-05-19 17:42:41 +08:00
bool removeListener(const Event & event, const Handle handle);
2018-05-13 12:26:27 +08:00
```
Remove the listener *handle* which listens to *event* from the dispatcher.
Return true if the listener is removed successfully, false if the listener is not found.
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### forEach
2018-05-13 12:26:27 +08:00
```c++
template <typename Func>
2018-05-19 17:42:41 +08:00
void forEach(const Event & event, Func && func);
2018-05-13 12:26:27 +08:00
```
Apply `func` to all listeners of `event`.
2019-03-31 19:38:49 +08:00
The `func` can be one of the two prototypes:
2018-05-13 12:26:27 +08:00
```c++
AnyReturnType func(const EventDispatcher::Handle &, const EventDispatcher::Callback &);
AnyReturnType func(const EventDispatcher::Callback &);
```
**Note**: the `func` can remove any listeners, or add other listeners, safely.
2019-04-02 19:33:01 +08:00
#### forEachIf
```c++
template <typename Func>
2018-05-19 17:42:41 +08:00
bool forEachIf(const Event & event, Func && func);
```
Apply `func` to all listeners of `event`. `func` must return a boolean value, and if the return value is false, forEachIf stops the looping immediately.
Return `true` if all listeners are invoked, or `event` is not found, `false` if `func` returns `false`.
2019-04-02 19:33:01 +08:00
#### dispatch
2019-03-31 19:38:49 +08:00
```c++
void dispatch(Args ...args);
template <typename T>
void dispatch(T && first, Args ...args);
```
Dispatch an event. The event type is deducted from the arguments of `dispatch`.
In both overloads, the listeners are called with arguments `args`.
The function is synchronous. The listeners are called in the thread same as the caller of `dispatch`.
2020-11-25 21:32:38 +08:00
The two overloaded functions have similar but slightly difference. How to use them depends on the `ArgumentPassingMode` policy. Please reference the [document of policies](policies.md) for more information.
2019-03-31 22:32:04 +08:00
<a id="a2_3"></a>
## Nested listener safety
1. If a listener adds another listener of the same event to the dispatcher during a dispatching, the new listener is guaranteed not to be triggered within the same dispatching. This is guaranteed by an unsigned 64 bits integer counter. This rule will be broken is the counter is overflowed to zero in a dispatching, but this rule will continue working on the subsequence dispatching.
2. Any listeners that are removed during a dispatching are guaranteed not triggered.
3. All above points are not true in multiple threading. That's to say, if one thread is invoking a callback list, the other thread add or remove a callback, the added or removed callback may be triggered during the invoking.
2018-05-13 12:26:27 +08:00
2019-03-31 22:32:04 +08:00
<a id="a2_4"></a>
2018-05-13 12:26:27 +08:00
## Time complexities
The time complexities being discussed here is about when operating on the listener in the underlying list, and `n` is the number of listeners. It doesn't include the event searching in the underlying `std::map` which is always O(log n).
2018-05-14 20:00:29 +08:00
- `appendListener`: O(1)
- `prependListener`: O(1)
- `insertListener`: O(1)
- `removeListener`: O(1)
2018-05-13 12:26:27 +08:00
2019-03-31 22:32:04 +08:00
<a id="a2_5"></a>
2018-05-13 12:26:27 +08:00
## Internal data structure
2018-12-26 20:10:46 +08:00
EventDispatcher uses [CallbackList](callbacklist.md) to manage the listener callbacks.