Skip to content

Commit bb83599

Browse files
committed
Merge branch 'heterogeneous'
2 parents 6ad5c1f + c072e39 commit bb83599

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+16845
-10525
lines changed

doc/callbacklist.md

+58-27
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
# Class CallbackList reference
22

3+
<!--begintoc-->
34
## Table Of Contents
45

5-
- [API reference](#apis)
6-
- [Nested callback safety](#nested-callback-safety)
7-
- [Time complexities](#time-complexities)
8-
- [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+
<a id="a2_1"></a>
1018
## Description
1119

1220
CallbackList is the fundamental class in eventpp. The other classes EventDispatcher and EventQueue are built on CallbackList.
1321

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.
1524

16-
<a name="apis"></a>
25+
<a id="a2_2"></a>
1726
## API reference
1827

28+
<a id="a3_1"></a>
1929
### Header
2030

2131
eventpp/callbacklist.h
2232

23-
**Template parameters**
33+
<a id="a3_2"></a>
34+
### Template parameters
2435

2536
```c++
2637
template <
@@ -32,13 +43,17 @@ class CallbackList;
3243
`Prototype`: the callback prototype. It's C++ function type such as `void(int, std::string, const MyClass *)`.
3344
`Policies`: the policies to configure and extend the callback list. The default value is `DefaultPolicies`. See [document of policies](policies.md) for details.
3445

46+
<a id="a3_3"></a>
3547
### Public types
3648

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.
3850
`Callback`: the callback storage type.
3951

52+
<a id="a3_4"></a>
4053
### Member functions
4154

55+
#### constructors
56+
4257
```c++
4358
CallbackList() noexcept;
4459
CallbackList(const CallbackList & other);
@@ -49,94 +64,110 @@ CallbackList & operator = (CallbackList && other) noexcept;
4964
5065
CallbackList can be copied, moved, assigned, and move assigned.
5166
67+
#### empty
68+
5269
```c++
5370
bool empty() const;
5471
```
5572
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
5776

5877
```c++
5978
operator bool() const;
6079
```
6180
Return true if the callback list is not empty.
6281
This operator allows a CallbackList instance be used in condition statement.
6382

83+
#### append
84+
6485
```c++
6586
Handle append(const Callback & callback);
6687
```
6788
Add the *callback* to the callback list.
6889
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.
7192
The time complexity is O(1).
7293
94+
#### prepend
95+
7396
```c++
7497
Handle prepend(const Callback & callback);
7598
```
7699
Add the *callback* to the callback list.
77100
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.
80103
The time complexity is O(1).
81104

105+
#### insert
106+
82107
```c++
83108
Handle insert(const Callback & callback, const Handle before);
84109
```
85110
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.
88113
The time complexity is O(1).
89114
115+
#### remove
90116
```c++
91117
bool remove(const Handle handle);
92118
```
93119
Remove the callback *handle* from the callback list.
94120
Return true if the callback is removed successfully, false if the callback is not found.
95121
The time complexity is O(1).
96122

123+
#### forEach
124+
97125
```c++
98126
template <typename Func>
99-
void forEach(Func && func);
127+
void forEach(Func && func) const;
100128
```
101129
Apply `func` to all callbacks.
102-
The `func` can be one of the three prototypes:
130+
The `func` can be one of the two prototypes:
103131
```c++
104-
AnyReturnType func(const EventDispatcher::Handle &, const EventDispatcher::Callback &);
105-
AnyReturnType func(const EventDispatcher::Handle &);
106-
AnyReturnType func(const EventDispatcher::Callback &);
132+
AnyReturnType func(const CallbackList::Handle &, const CallbackList::Callback &);
133+
AnyReturnType func(const CallbackList::Callback &);
107134
```
108135
**Note**: the `func` can remove any callbacks, or add other callbacks, safely.
109136

137+
#### forEachIf
138+
110139
```c++
111140
template <typename Func>
112-
bool forEachIf(Func && func);
141+
bool forEachIf(Func && func) const;
113142
```
114143
Apply `func` to all callbacks. `func` must return a boolean value, and if the return value is false, forEachIf stops the looping immediately.
115144
Return `true` if all callbacks are invoked, or `event` is not found, `false` if `func` returns `false`.
116145
146+
#### invoking operator
147+
117148
```c++
118-
void operator() (Args ...args);
149+
void operator() (Args ...args) const;
119150
```
120151
Invoke each callbacks in the callback list.
121152
The callbacks are called with arguments `args`.
122153
The callbacks are called in the thread same as the callee of `operator()`.
123154

124-
<a name="nested-callback-safety"></a>
155+
<a id="a2_3"></a>
125156
## Nested callback safety
126157
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.
127158
2. Any callbacks that are removed during a invoking are guaranteed not triggered.
128159
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.
129160

130161

131-
<a name="time-complexities"></a>
162+
<a id="a2_4"></a>
132163
## Time complexities
133164
- `append`: O(1)
134165
- `prepend`: O(1)
135166
- `insert`: O(1)
136167
- `remove`: O(1)
137168

138-
<a name="internal-data-structure"></a>
169+
<a id="a2_5"></a>
139170
## Internal data structure
140171

141172
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.

doc/conditionalremover.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
# Class ConditionalRemover reference
22

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+
<a id="a2_1"></a>
316
## Description
417

518
ConditionalRemover is a utility class that automatically removes listeners after the listeners are triggered and certain condition is satisfied.
619
ConditionalRemover is a pure functional class. After the member functions in ConditionalRemover are invoked, the ConditionalRemover object can be destroyed safely.
720

8-
<a name="apis"></a>
21+
<a id="a2_2"></a>
922
## API reference
1023

24+
<a id="a3_1"></a>
1125
### Header
1226

1327
eventpp/utilities/conditionalremover.h
1428

29+
<a id="a3_2"></a>
1530
### Template parameters
1631

1732
```c++
1833
template <typename DispatcherType>
1934
class ConditionalRemover;
2035
```
2136

22-
`DispatcherType` can be CallbackList, EventDispatcher, or EventQueue.
37+
`DispatcherType` can be CallbackList, EventDispatcher, EventQueue, HeterCallbackList, HeterEventDispatcher, or HeterEventQueue.
2338

39+
<a id="a3_3"></a>
2440
### Member functions
2541

2642
```c++
@@ -78,6 +94,7 @@ typename CallbackListType::Handle insert(
7894
7995
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.
8096
97+
<a id="a3_4"></a>
8198
### Free functions
8299
83100
```c++
@@ -87,6 +104,7 @@ ConditionalRemover<DispatcherType> conditionalRemover(DispatcherType & dispatche
87104

88105
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.
89106

107+
<a id="a3_5"></a>
90108
### Sample code
91109

92110
```c++

doc/counterremover.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,42 @@
11
# Class CounterRemover reference
22

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+
<a id="a2_1"></a>
316
## Description
417

518
CounterRemover is a utility class that automatically removes listeners after the listeners are triggered for certain times.
619
CounterRemover is a pure functional class. After the member functions in CounterRemover are invoked, the CounterRemover object can be destroyed safely.
720

8-
<a name="apis"></a>
21+
<a id="a2_2"></a>
922
## API reference
1023

24+
<a id="a3_1"></a>
1125
### Header
1226

1327
eventpp/utilities/counterremover.h
1428

29+
<a id="a3_2"></a>
1530
### Template parameters
1631

1732
```c++
1833
template <typename DispatcherType>
1934
class CounterRemover;
2035
```
2136

22-
`DispatcherType` can be CallbackList, EventDispatcher, or EventQueue.
37+
`DispatcherType` can be CallbackList, EventDispatcher, EventQueue, HeterCallbackList, HeterEventDispatcher, or HeterEventQueue.
2338

39+
<a id="a3_3"></a>
2440
### Member functions
2541

2642
```c++
@@ -73,6 +89,7 @@ typename CallbackListType::Handle insert(
7389
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.
7490
The default value of `triggerCount` is 1, that means the listener is removed after the first trigger, which is one shot listener.
7591
92+
<a id="a3_4"></a>
7693
### Free functions
7794
7895
```c++
@@ -82,6 +99,7 @@ CounterRemover<DispatcherType> counterRemover(DispatcherType & dispatcher);
8299

83100
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.
84101

102+
<a id="a3_5"></a>
85103
### Sample code
86104

87105
```c++

0 commit comments

Comments
 (0)