Skip to content
Snippets Groups Projects
Commit 2993e5f5 authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Add a few comments

parent 562e1269
No related branches found
No related tags found
No related merge requests found
Pipeline #27275 passed
...@@ -26,8 +26,7 @@ struct Handle { ...@@ -26,8 +26,7 @@ struct Handle {
friend struct BaseHandler; friend struct BaseHandler;
/** /**
* Cancel the timer job. If currently executing it will block and wait for * Cancel the callback and invalidate the handle.
* the job to complete.
*/ */
inline void cancel() { if (handler_) handler_->remove(*this); handler_ = nullptr; } inline void cancel() { if (handler_) handler_->remove(*this); handler_ = nullptr; }
...@@ -62,8 +61,17 @@ struct Handle { ...@@ -62,8 +61,17 @@ struct Handle {
Handle(BaseHandler *h, int id) : handler_(h), id_(id) {} Handle(BaseHandler *h, int id) : handler_(h), id_(id) {}
}; };
/**
* This class is used to manage callbacks. The template parameters are the
* arguments to be passed to the callback when triggered. This class is already
* thread-safe.
*/
template <typename ...ARGS> template <typename ...ARGS>
struct Handler : BaseHandler { struct Handler : BaseHandler {
/**
* Add a new callback function. It returns a `Handle` object that must
* remain in scope, the destructor of the `Handle` will remove the callback.
*/
Handle on(const std::function<bool(ARGS...)> &f) { Handle on(const std::function<bool(ARGS...)> &f) {
std::unique_lock<std::mutex> lk(mutex_); std::unique_lock<std::mutex> lk(mutex_);
int id = id_++; int id = id_++;
...@@ -71,6 +79,12 @@ struct Handler : BaseHandler { ...@@ -71,6 +79,12 @@ struct Handler : BaseHandler {
return make_handle(this, id); return make_handle(this, id);
} }
/**
* Safely trigger all callbacks. Note that `Handler` is locked when
* triggering so callbacks cannot make modifications to it or they will
* lock up. To remove a callback, return false from the callback, else
* return true.
*/
void trigger(ARGS ...args) { void trigger(ARGS ...args) {
std::unique_lock<std::mutex> lk(mutex_); std::unique_lock<std::mutex> lk(mutex_);
try { try {
...@@ -82,6 +96,10 @@ struct Handler : BaseHandler { ...@@ -82,6 +96,10 @@ struct Handler : BaseHandler {
} }
} }
/**
* Remove a callback using its `Handle`. This is equivalent to allowing the
* `Handle` to be destroyed or cancelled.
*/
void remove(const Handle &h) override { void remove(const Handle &h) override {
std::unique_lock<std::mutex> lk(mutex_); std::unique_lock<std::mutex> lk(mutex_);
callbacks_.erase(h.id()); callbacks_.erase(h.id());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment