You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-[Internal data structure](#internal-data-structure)
9
-
6
+
*[Description](#a2_1)
7
+
*[API reference](#a2_2)
8
+
*[Header](#a3_1)
9
+
*[Template parameters](#a3_2)
10
+
*[Public types](#a3_3)
11
+
*[Member functions](#a3_4)
12
+
*[Nested callback safety](#a2_3)
13
+
*[Time complexities](#a2_4)
14
+
*[Internal data structure](#a2_5)
15
+
<!--endtoc-->
16
+
17
+
<aid="a2_1"></a>
10
18
## Description
11
19
12
20
CallbackList is the fundamental class in eventpp. The other classes EventDispatcher and EventQueue are built on CallbackList.
13
21
14
-
CallbackList holds a list of callbacks. On invocation, CallbackList simply invokes each callbacks one by one. Think CallbackList as the signal/slot system in Qt, or the callback function pointer in some Windows APIs (such as lpCompletionRoutine in `ReadFileEx`).
22
+
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`).
23
+
The *callback* can be any callback target -- functions, pointers to functions, , pointers to member functions, lambda expressions, and function objects.
15
24
16
-
<aname="apis"></a>
25
+
<aid="a2_2"></a>
17
26
## API reference
18
27
28
+
<aid="a3_1"></a>
19
29
### Header
20
30
21
31
eventpp/callbacklist.h
22
32
23
-
**Template parameters**
33
+
<aid="a3_2"></a>
34
+
### Template parameters
24
35
25
36
```c++
26
37
template <
@@ -32,13 +43,17 @@ class CallbackList;
32
43
`Prototype`: the callback prototype. It's C++ function type such as `void(int, std::string, const MyClass *)`.
33
44
`Policies`: the policies to configure and extend the callback list. The default value is `DefaultPolicies`. See [document of policies](policies.md) for details.
34
45
46
+
<aid="a3_3"></a>
35
47
### Public types
36
48
37
-
`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.
49
+
`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.
CallbackList can be copied, moved, assigned, and move assigned.
51
66
67
+
#### empty
68
+
52
69
```c++
53
70
bool empty() const;
54
71
```
55
72
Return true if the callback list is empty.
56
-
Note: in multi threading, this function returning true doesn't guarantee the list is empty. The list may become non-empty immediately after the function returns true.
73
+
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.
74
+
75
+
#### bool casting operator
57
76
58
77
```c++
59
78
operatorbool() const;
60
79
```
61
80
Return true if the callback list is not empty.
62
81
This operator allows a CallbackList instance be used in condition statement.
63
82
83
+
#### append
84
+
64
85
```c++
65
86
Handleappend(const Callback & callback);
66
87
```
67
88
Add the *callback* to the callback list.
68
89
The callback is added to the end of the callback list.
69
-
Return a handle which represents the callback. The handle can be used to remove this callback or insert other callback before this callback.
70
-
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.
90
+
Return a handle that represents the callback. The handle can be used to remove this callback or to insert additional callbacks before this callback.
91
+
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.
71
92
The time complexity is O(1).
72
93
94
+
#### prepend
95
+
73
96
```c++
74
97
Handle prepend(const Callback & callback);
75
98
```
76
99
Add the *callback* to the callback list.
77
100
The callback is added to the beginning of the callback list.
78
-
Return a handle which represents the callback. The handle can be used to remove this callback or insert other callback before this callback.
79
-
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.
101
+
Return a handle that represents the callback. The handle can be used to remove this callback or to insert additional callbacks before this callback.
102
+
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.
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.
86
-
Return a handle which represents the callback. The handle can be used to remove this callback or insert other callback before this callback.
87
-
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.
111
+
Return a handle that represents the callback. The handle can be used to remove this callback or to insert additional callbacks before this callback.
112
+
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.
88
113
The time complexity is O(1).
89
114
115
+
#### remove
90
116
```c++
91
117
bool remove(const Handle handle);
92
118
```
93
119
Remove the callback *handle* from the callback list.
94
120
Return true if the callback is removed successfully, false if the callback is not found.
**Note**: the `func` can remove any callbacks, or add other callbacks, safely.
109
136
137
+
#### forEachIf
138
+
110
139
```c++
111
140
template <typename Func>
112
-
boolforEachIf(Func && func);
141
+
boolforEachIf(Func && func) const;
113
142
```
114
143
Apply `func` to all callbacks. `func` must return a boolean value, and if the return value is false, forEachIf stops the looping immediately.
115
144
Return `true` if all callbacks are invoked, or `event` is not found, `false` if `func` returns `false`.
116
145
146
+
#### invoking operator
147
+
117
148
```c++
118
-
void operator() (Args ...args);
149
+
void operator() (Args ...args) const;
119
150
```
120
151
Invoke each callbacks in the callback list.
121
152
The callbacks are called with arguments `args`.
122
153
The callbacks are called in the thread same as the callee of `operator()`.
123
154
124
-
<aname="nested-callback-safety"></a>
155
+
<aid="a2_3"></a>
125
156
## Nested callback safety
126
157
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.
127
158
2. Any callbacks that are removed during a invoking are guaranteed not triggered.
128
159
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.
129
160
130
161
131
-
<aname="time-complexities"></a>
162
+
<aid="a2_4"></a>
132
163
## Time complexities
133
164
-`append`: O(1)
134
165
-`prepend`: O(1)
135
166
-`insert`: O(1)
136
167
-`remove`: O(1)
137
168
138
-
<aname="internal-data-structure"></a>
169
+
<aid="a2_5"></a>
139
170
## Internal data structure
140
171
141
172
CallbackList uses doubly linked list to manage the callbacks.
142
-
Each node is linked by shared pointer. Using shared pointer allows the node be removed while iterating.
173
+
Each node is linked by a shared pointer. Using shared pointer allows nodes to be removed during iterating.
Copy file name to clipboardexpand all lines: doc/conditionalremover.md
+20-2
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,42 @@
1
1
# Class ConditionalRemover reference
2
2
3
+
<!--begintoc-->
4
+
## Table Of Contents
5
+
6
+
*[Description](#a2_1)
7
+
*[API reference](#a2_2)
8
+
*[Header](#a3_1)
9
+
*[Template parameters](#a3_2)
10
+
*[Member functions](#a3_3)
11
+
*[Free functions](#a3_4)
12
+
*[Sample code](#a3_5)
13
+
<!--endtoc-->
14
+
15
+
<aid="a2_1"></a>
3
16
## Description
4
17
5
18
ConditionalRemover is a utility class that automatically removes listeners after the listeners are triggered and certain condition is satisfied.
6
19
ConditionalRemover is a pure functional class. After the member functions in ConditionalRemover are invoked, the ConditionalRemover object can be destroyed safely.
7
20
8
-
<aname="apis"></a>
21
+
<aid="a2_2"></a>
9
22
## API reference
10
23
24
+
<aid="a3_1"></a>
11
25
### Header
12
26
13
27
eventpp/utilities/conditionalremover.h
14
28
29
+
<aid="a3_2"></a>
15
30
### Template parameters
16
31
17
32
```c++
18
33
template <typename DispatcherType>
19
34
classConditionalRemover;
20
35
```
21
36
22
-
`DispatcherType` can be CallbackList, EventDispatcher, or EventQueue.
37
+
`DispatcherType` can be CallbackList, EventDispatcher, EventQueue, HeterCallbackList, HeterEventDispatcher, or HeterEventQueue.
The member functions have the same names with the corresponding underlying class (CallbackList, EventDispatcher, or EventQueue), and also have the same parameters except there is one more parameter, `condition`. `condition` is a predicate function of prototype `bool()`. It's invoked after each trigger, if it returns true, the listener will be removed.
Since ConditionalRemover takes one template parameter and it's verbose to instantiate its instance, the function `conditionalRemover` is used to construct an instance of ConditionalRemover via the deduced argument.
Copy file name to clipboardexpand all lines: doc/counterremover.md
+20-2
Original file line number
Diff line number
Diff line change
@@ -1,26 +1,42 @@
1
1
# Class CounterRemover reference
2
2
3
+
<!--begintoc-->
4
+
## Table Of Contents
5
+
6
+
*[Description](#a2_1)
7
+
*[API reference](#a2_2)
8
+
*[Header](#a3_1)
9
+
*[Template parameters](#a3_2)
10
+
*[Member functions](#a3_3)
11
+
*[Free functions](#a3_4)
12
+
*[Sample code](#a3_5)
13
+
<!--endtoc-->
14
+
15
+
<aid="a2_1"></a>
3
16
## Description
4
17
5
18
CounterRemover is a utility class that automatically removes listeners after the listeners are triggered for certain times.
6
19
CounterRemover is a pure functional class. After the member functions in CounterRemover are invoked, the CounterRemover object can be destroyed safely.
7
20
8
-
<aname="apis"></a>
21
+
<aid="a2_2"></a>
9
22
## API reference
10
23
24
+
<aid="a3_1"></a>
11
25
### Header
12
26
13
27
eventpp/utilities/counterremover.h
14
28
29
+
<aid="a3_2"></a>
15
30
### Template parameters
16
31
17
32
```c++
18
33
template <typename DispatcherType>
19
34
classCounterRemover;
20
35
```
21
36
22
-
`DispatcherType` can be CallbackList, EventDispatcher, or EventQueue.
37
+
`DispatcherType` can be CallbackList, EventDispatcher, EventQueue, HeterCallbackList, HeterEventDispatcher, or HeterEventQueue.
The member functions have the same names with the corresponding underlying class (CallbackList, EventDispatcher, or EventQueue), and also have the same parameters except there is one more parameter, `triggerCount`. `triggerCount` is decreased by one on each trigger, and when `triggerCount` is zero or negative, the listener will be removed.
74
90
The default value of `triggerCount` is 1, that means the listener is removed after the first trigger, which is one shot listener.
Since CounterRemover takes one template parameter and it's verbose to instantiate its instance, the function `counterRemover` is used to construct an instance of CounterRemover via the deduced argument.
0 commit comments