// the first parameter is the prototype of the listener.
eventpp::CallbackList<void()> callbackList;
// Add a callback.
// []() {} is the callback.
// Lambda is not required, any function or std::function
// or whatever function object with the required prototype is fine.
callbackList.append([]() {
std::cout << "Got callback 1." <<std::endl;
});
callbackList.append([]() {
std::cout << "Got callback 2." <<std::endl;
});
// Invoke the callback list
callbackList();
```
**Output**
> Got callback 1.
> Got callback 2.
**Remarks**
First let's define a callback list.
```c++
eventpp::CallbackList<void()> callbackList;
```
class CallbackList takes at least one template arguments. It is the *prototype* of the callback.
The *prototype* is C++ function type, such as `void (int)`, `void (const std::string &, const MyClass &, int, bool)`.
Now let's add a callback.
```c++
callbackList.append([]() {
std::cout << "Got callback 1." <<std::endl;
});
```
Function `append` takes one arguments, the *callback*.
The *callback* can be any callback target -- functions, pointers to functions, , pointers to member functions, lambda expressions, and function objects. It must be able to be called with the *prototype* declared in `callbackList`.
In the tutorial, we also add another callback.
Now let's invoke the callbackList.
```c++
callbackList();
```
During the invoking, all callbacks will be invoked one by one in the order of they were added.
### CallbackList tutorial 2, callback with parameters
Now the callback list prototype takes two parameters, `const std::string &` and `const bool`.
The callback's prototype is not required to be same as the callback list, it's fine as long as the prototype is compatible with the callback list. See the second callback, `[](std::string s, int b)`, its prototype is not same as the callback list.
### CallbackList tutorial 3, remove
**Code**
```c++
using CL = eventpp::CallbackList<void()>;
CL callbackList;
CL::Handle handle2;
// Add some callbacks.
callbackList.append([]() {
std::cout << "Got callback 1." <<std::endl;
});
handle2 = callbackList.append([]() {
std::cout << "Got callback 2." <<std::endl;
});
callbackList.append([]() {
std::cout << "Got callback 3." <<std::endl;
});
callbackList.remove(handle2);
// Invoke the callback list
// The "Got callback 2" callback should not be triggered.
callbackList();
```
**Output**
> Got callback 1.
> Got callback 3.
**Remarks**
### CallbackList tutorial 4, for each
**Code**
```c++
using CL = eventpp::CallbackList<void()>;
CL callbackList;
// Add some callbacks.
callbackList.append([]() {
std::cout << "Got callback 1." <<std::endl;
});
callbackList.append([]() {
std::cout << "Got callback 2." <<std::endl;
});
callbackList.append([]() {
std::cout << "Got callback 3." <<std::endl;
});
// Now call forEach to remove the second callback
// The forEach callback prototype is void(const CallbackList::Handle & handle, const CallbackList::Callback & callback)
// The "Got callback 2" callback should not be triggered.
callbackList();
```
**Output**
> Got callback 1.
> Got callback 3.
**Remarks**
## API reference
**Template parameters**
```c++
template <
typename Prototype,
typename Callback = void,
typename Threading = MultipleThreading
>
class CallbackList;
```
`Prototype`: the callback prototype. It's C++ function type such as `void(int, std::string, const MyClass *)`.
`Callback`: the underlying type to hold the callback. Default is `void`, which will be expanded to `std::function`.
`Threading`: threading model. Default is 'MultipleThreading'. Possible values:
*`MultipleThreading`: the core data is protected with mutex. It's the default value.
*`SingleThreading`: the core data is not protected and can't be accessed from multiple threads.
**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.
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.
The time complexity is O(1).
```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.