mirror of https://gitlab.com/cppit/jucipp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
832 B
43 lines
832 B
|
6 years ago
|
#include "dispatcher.hpp"
|
||
|
8 years ago
|
#include <vector>
|
||
|
10 years ago
|
|
||
|
|
Dispatcher::Dispatcher() {
|
||
|
7 years ago
|
connect();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Dispatcher::connect() {
|
||
|
8 years ago
|
connection = dispatcher.connect([this] {
|
||
|
8 years ago
|
std::vector<std::list<std::function<void()>>::iterator> its;
|
||
|
|
{
|
||
|
7 years ago
|
LockGuard lock(functions_mutex);
|
||
|
8 years ago
|
if(functions.empty())
|
||
|
|
return;
|
||
|
8 years ago
|
its.reserve(functions.size());
|
||
|
8 years ago
|
for(auto it = functions.begin(); it != functions.end(); ++it)
|
||
|
8 years ago
|
its.emplace_back(it);
|
||
|
|
}
|
||
|
8 years ago
|
for(auto &it : its)
|
||
|
8 years ago
|
(*it)();
|
||
|
|
{
|
||
|
7 years ago
|
LockGuard lock(functions_mutex);
|
||
|
8 years ago
|
for(auto &it : its)
|
||
|
8 years ago
|
functions.erase(it);
|
||
|
|
}
|
||
|
10 years ago
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
Dispatcher::~Dispatcher() {
|
||
|
|
disconnect();
|
||
|
|
}
|
||
|
|
|
||
|
|
void Dispatcher::disconnect() {
|
||
|
|
connection.disconnect();
|
||
|
|
}
|
||
|
7 years ago
|
|
||
|
|
void Dispatcher::reset() {
|
||
|
7 years ago
|
LockGuard lock(functions_mutex);
|
||
|
7 years ago
|
disconnect();
|
||
|
|
functions.clear();
|
||
|
|
connect();
|
||
|
|
}
|