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

175 lines
6.4 KiB
Markdown
Raw Normal View History

2018-05-13 12:26:27 +08:00
# Class CallbackList 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 callback 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
CallbackList is the fundamental class in eventpp. The other classes EventDispatcher and EventQueue are built on CallbackList.
2018-12-15 22:40:46 +08:00
CallbackList holds a list of callbacks. At the time of the call, CallbackList simply invokes each callback one by one. Consider CallbackList as the signal/slot system in Qt, or the callback function pointer in some Windows APIs (such as lpCompletionRoutine in `ReadFileEx`).
The *callback* can be any callback target -- functions, pointers to functions, , pointers to member functions, lambda expressions, and function objects.
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/callbacklist.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 <
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
2019-03-31 11:22:36 +08:00
<a id="a3_3"></a>
### Public types
2018-05-13 12:26:27 +08:00
2021-01-03 21:25:35 +08:00
`Handle`: the handle type returned by `append`, `prepend` and `insert`. 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.
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++
CallbackList() noexcept;
CallbackList(const CallbackList & other);
CallbackList(CallbackList && other) noexcept;
CallbackList & operator = (const CallbackList & other);
CallbackList & operator = (CallbackList && other) noexcept;
void swap(CallbackList & other) noexcept;
2018-05-13 12:26:27 +08:00
```
CallbackList can be copied, moved, assigned, move assigned, and swapped.
2018-05-13 12:26:27 +08:00
2019-04-02 19:33:01 +08:00
#### empty
```c++
bool empty() const;
```
Return true if the callback list is empty.
2021-01-03 21:25:35 +08:00
Note: in multi threading, this function returning true doesn't guarantee that the list is empty. The list may immediately become non-empty after the function returns true, and vice versa.
2019-04-02 19:33:01 +08:00
#### bool casting operator
```c++
operator bool() const;
```
Return true if the callback list is not empty.
This operator allows a CallbackList instance be used in condition statement.
2019-04-02 19:33:01 +08:00
#### append
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.
2018-12-15 22:40:46 +08:00
Return a handle that represents the callback. The handle can be used to remove this callback or to insert additional callbacks before this callback.
If `append` is called in another callback during the invoking of the callback list, the new callback is guaranteed not to be triggered during the same callback list invoking.
2018-05-13 12:26:27 +08:00
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### prepend
2018-05-13 12:26:27 +08:00
```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.
2018-12-15 22:40:46 +08:00
Return a handle that represents the callback. The handle can be used to remove this callback or to insert additional callbacks before this callback.
If `prepend` is called in another callback during the invoking of the callback list, the new callback is guaranteed not to be triggered during the same callback list invoking.
2018-05-13 12:26:27 +08:00
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### insert
2018-05-13 12:26:27 +08:00
```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.
2018-12-15 22:40:46 +08:00
Return a handle that represents the callback. The handle can be used to remove this callback or to insert additional callbacks before this callback.
If `insert` is called in another callback during the invoking of the callback list, the new callback is guaranteed not to be triggered during the same callback list invoking.
2018-05-13 12:26:27 +08:00
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### remove
2018-05-13 12:26:27 +08:00
```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).
2019-04-02 19:33:01 +08:00
#### forEach
2018-05-13 12:26:27 +08:00
```c++
template <typename Func>
2019-03-31 19:38:49 +08:00
void forEach(Func && func) const;
2018-05-13 12:26:27 +08:00
```
Apply `func` to all callbacks.
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++
2019-03-28 15:31:50 +08:00
AnyReturnType func(const CallbackList::Handle &, const CallbackList::Callback &);
AnyReturnType func(const CallbackList::Callback &);
2018-05-13 12:26:27 +08:00
```
2021-01-03 21:25:35 +08:00
Note: the `func` can remove any callbacks, or add other callbacks, safely.
2018-05-13 12:26:27 +08:00
2019-04-02 19:33:01 +08:00
#### forEachIf
```c++
template <typename Func>
2019-03-31 19:38:49 +08:00
bool forEachIf(Func && func) const;
```
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`.
2019-04-02 19:33:01 +08:00
#### invoking operator
2018-05-13 12:26:27 +08:00
```c++
2019-03-31 19:38:49 +08:00
void operator() (Args ...args) const;
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()`.
2019-03-31 22:32:04 +08:00
<a id="a2_3"></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 invoking a callback list, the other thread add or remove a callback, the added or removed callback may be called during the invoking.
2019-03-31 22:32:04 +08:00
<a id="a2_4"></a>
## Time complexities
2018-05-14 20:00:29 +08:00
- `append`: O(1)
- `prepend`: O(1)
- `insert`: O(1)
- `remove`: O(1)
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-05-13 19:21:19 +08:00
CallbackList uses doubly linked list to manage the callbacks.
2018-12-15 22:40:46 +08:00
Each node is linked by a shared pointer. Using shared pointer allows nodes to be removed during iterating.