1
0
mirror of https://github.com/wqking/eventpp.git synced 2024-12-27 16:41:11 +08:00
eventpp/doc/hetercallbacklist.md

158 lines
5.7 KiB
Markdown
Raw Normal View History

2019-03-31 19:38:49 +08:00
# Class HeterCallbackList reference
2019-04-02 19:33:01 +08:00
<!--begintoc-->
2019-03-31 19:38:49 +08:00
## Table Of Contents
2019-03-31 22:32:04 +08:00
* [Description](#a2_1)
* [API reference](#a2_2)
* [Header](#a3_1)
* [Template parameters](#a3_2)
* [Public types](#a3_3)
* [Member functions](#a3_4)
<!--endtoc-->
<a id="a2_1"></a>
2019-03-31 19:38:49 +08:00
## Description
HeterCallbackList is the heterogeneous counterpart of CallbackList in eventpp. The other classes HeterEventDispatcher and HeterEventQueue are built on HeterCallbackList.
HeterCallbackList holds a list of callbacks. The callbacks can have different prototypes. At the time of the call, HeterCallbackList invokes each callback which matches the invoking parameters.
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>
2019-03-31 19:38:49 +08:00
## API reference
2019-03-31 22:32:04 +08:00
<a id="a3_1"></a>
2019-03-31 19:38:49 +08:00
### Header
eventpp/hetercallbacklist.h
2019-03-31 22:32:04 +08:00
<a id="a3_2"></a>
2019-03-31 19:38:49 +08:00
### Template parameters
```c++
template <
typename PrototypeList,
typename Policies = DefaultPolicies
2019-03-31 19:38:49 +08:00
>
class HeterCallbackList;
```
`Prototype`: a list of function types in `eventpp::HeterTuple`, such as `eventpp::HeterTuple<void (), void (std::string), void (int, int)>`.
`Policies`: the policies to configure and extend the callback list. The default value is `DefaultPolicies`. See [document of policies](policies.md) for details.
2019-03-31 22:32:04 +08:00
<a id="a3_3"></a>
2019-03-31 19:38:49 +08:00
### Public types
`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.
`Callback`: the callback storage type.
2019-03-31 22:32:04 +08:00
<a id="a3_4"></a>
2019-03-31 19:38:49 +08:00
### Member functions
2019-04-02 19:33:01 +08:00
#### constructors
2019-03-31 19:38:49 +08:00
```c++
HeterCallbackList() noexcept;
HeterCallbackList(const HeterCallbackList & other);
HeterCallbackList(HeterCallbackList && other) noexcept;
HeterCallbackList & operator = (const HeterCallbackList & other);
HeterCallbackList & operator = (HeterCallbackList && other) noexcept;
```
HeterCallbackList can be copied, moved, assigned, and move assigned.
2019-04-02 19:33:01 +08:00
#### empty
2019-03-31 19:38:49 +08:00
```c++
bool empty() const;
```
Return true if the callback list is empty.
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.
2019-04-02 19:33:01 +08:00
#### bool casting operator
2019-03-31 19:38:49 +08:00
```c++
operator bool() const;
```
Return true if the callback list is not empty.
This operator allows a HeterCallbackList instance be used in condition statement.
2019-04-02 19:33:01 +08:00
#### append
2019-03-31 19:38:49 +08:00
```c++
template <typename C>
Handle append(const C & callback);
```
Add the `callback` to the callback list.
The callback is added to the end of the callback list.
The callback type `C` must be specified in `PrototypeList`.
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.
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### prepend
2019-03-31 19:38:49 +08:00
```c++
template <typename C>
Handle prepend(const C & callback);
```
Add the *callback* to the callback list.
The callback is added to the beginning of the callback list.
The callback type `C` must be specified in `PrototypeList`.
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.
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### insert
2019-03-31 19:38:49 +08:00
```c++
template <typename C>
Handle insert(const C & callback, const Handle & before);
```
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.
The callback type `C` must be specified in `PrototypeList`.
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.
The time complexity is O(1).
2019-04-02 19:33:01 +08:00
#### remove
2019-03-31 19:38:49 +08:00
```c++
bool remove(const Handle & handle);
```
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
2019-03-31 19:38:49 +08:00
```c++
template <typename Prototype, typename Func>
void forEach(Func && func) const;
```
Apply `func` to all callbacks which has the `Prototype`.
The `func` can be one of the two prototypes:
```c++
2019-04-02 19:33:01 +08:00
AnyReturnType func(const HeterCallbackList::Handle & handle, const std::function<Prototype> & callback);
AnyReturnType func(const std::function<Prototype> & callback);
2019-03-31 19:38:49 +08:00
```
**Note**: the `func` can remove any callbacks, or add other callbacks, safely.
2019-04-02 19:33:01 +08:00
#### forEachIf
2019-03-31 19:38:49 +08:00
```c++
template <typename Prototype, typename Func>
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
2019-03-31 19:38:49 +08:00
```c++
template <typename ...Args>
void operator() (Args && ...args) const;
```
Invoke each callbacks that can be called with `Args` in the callback list.
The callbacks are called with arguments `args`.
The callbacks are called in the thread same as the callee of `operator()`.