1
0
mirror of https://github.com/wqking/eventpp.git synced 2024-12-27 00:17:02 +08:00
eventpp/doc/callbacklist.md

136 lines
5.4 KiB
Markdown
Raw Normal View History

2018-05-13 12:26:27 +08:00
# Class CallbackList reference
2018-05-14 20:00:29 +08:00
## Table Of Contents
- [API reference](#apis)
- [Nested callback safety](#nested-callback-safety)
- [Time complexities](#time-complexities)
- [Internal data structure](#internal-data-structure)
2018-05-16 20:16:41 +08:00
<a name="apis"></a>
2018-05-13 12:26:27 +08:00
## API reference
2018-05-19 17:42:41 +08:00
**Header**
eventpp/callbacklist.h
2018-05-13 12:26:27 +08:00
**Template parameters**
```c++
template <
typename Prototype,
2018-05-26 09:01:18 +08:00
typename Policies = DefaultPolicies
2018-05-13 12:26:27 +08:00
>
class CallbackList;
```
`Prototype`: the callback prototype. It's C++ function type such as `void(int, std::string, const MyClass *)`.
2018-05-26 09:01:18 +08:00
`Policies`: the policies to configure and extend the callback list. The default value is `DefaultPolicies`. See [document of policies](policies.md) for details.
2018-05-13 12:26:27 +08:00
**Public types**
`Handle`: the handle type returned by appendListener, prependListener and insertListener. A handle can be used to insert a callback or remove a callback. 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.
**Functions**
```c++
CallbackList() = default;
CallbackList(CallbackList &&) = delete;
CallbackList(const CallbackList &) = delete;
CallbackList & operator = (const CallbackList &) = delete;
```
CallbackList can not be copied, moved, or assigned.
```c++
bool empty() const;
```
Return true if the callback list is empty.
Note: in multi threading, this function returning true doesn't guarantee the list is empty. The list may be come non-empty when the function returns true.
```c++
operator bool() const;
```
Return true if the callback list is not empty.
This operator allows a CallbackList instance be used in condition statement.
2018-05-13 12:26:27 +08:00
```c++
2018-05-19 17:42:41 +08:00
Handle append(const Callback & callback);
2018-05-13 12:26:27 +08:00
```
Add the *callback* to the callback list.
The callback is added to the end of the callback list.
Return a handle which represents the callback. The handle can be used to remove this callback or insert other callback before this callback.
If `append` is called in another callback during the invoking of the callback list, the new callback is guaranteed not triggered during the same callback list invoking.
2018-05-13 12:26:27 +08:00
The time complexity is O(1).
```c++
2018-05-19 17:42:41 +08:00
Handle prepend(const Callback & callback);
2018-05-13 12:26:27 +08:00
```
Add the *callback* to the callback list.
The callback is added to the beginning of the callback list.
Return a handle which represents the callback. The handle can be used to remove this callback or insert other callback before this callback.
If `prepend` is called in another callback during the invoking of the callback list, the new callback is guaranteed not triggered during the same callback list invoking.
2018-05-13 12:26:27 +08:00
The time complexity is O(1).
```c++
2018-05-19 17:42:41 +08:00
Handle insert(const Callback & callback, const Handle before);
2018-05-13 12:26:27 +08:00
```
Insert the *callback* to the callback list before the callback handle *before*. If *before* is not found, *callback* is added at the end of the callback list.
Return a handle which represents the callback. The handle can be used to remove this callback or insert other callback before this callback.
If `insert` is called in another callback during the invoking of the callback list, the new callback is guaranteed not triggered during the same callback list invoking.
2018-05-13 12:26:27 +08:00
The time complexity is O(1).
```c++
2018-05-19 17:42:41 +08:00
bool remove(const Handle handle);
2018-05-13 12:26:27 +08:00
```
Remove the callback *handle* from the callback list.
Return true if the callback is removed successfully, false if the callback is not found.
The time complexity is O(1).
```c++
template <typename Func>
2018-05-19 17:42:41 +08:00
void forEach(Func && func);
2018-05-13 12:26:27 +08:00
```
Apply `func` to all callbacks.
The `func` can be one of the three prototypes:
```c++
AnyReturnType func(const EventDispatcher::Handle &, const EventDispatcher::Callback &);
AnyReturnType func(const EventDispatcher::Handle &);
AnyReturnType func(const EventDispatcher::Callback &);
```
**Note**: the `func` can remove any callbacks, or add other callbacks, safely.
```c++
template <typename Func>
2018-05-19 17:42:41 +08:00
bool forEachIf(Func && func);
```
Apply `func` to all callbacks. `func` must return a boolean value, and if the return value is false, forEachIf stops the looping immediately.
Return `true` if all callbacks are invoked, or `event` is not found, `false` if `func` returns `false`.
2018-05-13 12:26:27 +08:00
```c++
2018-05-19 17:42:41 +08:00
void operator() (Args ...args);
2018-05-13 12:26:27 +08:00
```
Invoke each callbacks in the callback list.
The callbacks are called with arguments `args`.
The callbacks are called in the thread same as the callee of `operator()`.
2018-05-16 20:16:41 +08:00
<a name="nested-callback-safety"></a>
## Nested callback safety
1. If a callback adds another callback to the callback list during a invoking, the new callback is guaranteed not to be triggered within the same invoking. This is guaranteed by an unsigned 64 bits integer counter. This rule will be broken is the counter is overflowed to zero in a invoking, but this rule will continue working on the subsequence invoking.
2. Any callbacks that are removed during a invoking are guaranteed not triggered.
3. All above points are not true in multiple threading. That's to say, if one thread is dispatching an event, the other thread add or remove a listener, the added or removed listener may be triggered during the dispatch.
2018-05-16 20:16:41 +08:00
<a name="time-complexities"></a>
## Time complexities
2018-05-14 20:00:29 +08:00
- `append`: O(1)
- `prepend`: O(1)
- `insert`: O(1)
- `remove`: O(1)
2018-05-16 20:16:41 +08:00
<a name="internal-data-structure"></a>
2018-05-13 12:26:27 +08:00
## Internal data structure
2018-05-13 19:21:19 +08:00
CallbackList uses doubly linked list to manage the callbacks.
2018-05-13 12:26:27 +08:00
Each node is linked by shared pointer. Using shared pointer allows the node be removed while iterating.