15 high_resolution_clock::duration period) {
17 process._period = period;
18 _processes.push_back(&process);
19 process._manager_ptr =
this;
21 if (Priority_min > process._priority || process._priority > Priority_max ){
22 throw Exception(
"Priority must be between -5(low priority) and 15(high priority)");
33 auto i = remove_if(_processes.begin(), _processes.end(), [&](
Process * p) {
36 _processes.erase(i, _processes.end());
51 if ( _channels.find(name) != _channels.end() ) {
52 return *(_channels[name]);
54 throw Exception(
"Tried to access an unregistered or non-existant channel.");
69 event_handlers[event_name].push_back(handler);
87 if ( event_handlers.find(event.
name()) != event_handlers.end() ) {
88 for (
auto handler : event_handlers[event.
name()] ) {
101 for(
auto process_ptr : _processes) {
118 return all([
this](
Process& p) { p._start(_elapsed) ;});
131 _update_mutex.lock();
138 _update_mutex.unlock();
146 std::sort(_processes.begin(), _processes.end(),[](
const Process * lhs,
const Process * rhs){
147 return lhs->_priority > rhs->_priority;
157 _simulated_time =
true;
166 _simulated_time =
false;
178 if (Priority_min <= priority && priority <= Priority_max ){
179 process._priority = priority;
182 throw Exception(
"Priority must be between -5(low priority) and 15(high priority)");
192 return run([&]() {
return _elapsed < runtime; });
200 return run([&]() {
return _running; });
209 _start_time = high_resolution_clock::now();
210 _elapsed = high_resolution_clock::duration::zero();
213 while ( condition() ) {
215 std::this_thread::sleep_for(_nice_sleep_amount);
216 update_elapsed_time();
229 void Manager::update_elapsed_time() {
232 if(_simulated_time && !_processes.empty()) {
235 auto min_iter = std::min_element(_processes.begin(), _processes.end() ,[](
Process * lhs,
Process * rhs) {
237 auto rhsTime = rhs->last_update() + rhs->period();
238 return lhsTime < rhsTime;
243 auto newTime = nextup->
last_update() + nextup->period();
249 _elapsed = high_resolution_clock::now() - _start_time;
The Process Manager class.
Manager & schedule(Process &process, high_resolution_clock::duration period)
Channel & channel(string)
Manager & watch(string event_name, std::function< void(Event &)> handler)
Manager & sort_processes()
Events that can be emitted, watched, and responded to with event handlers.
Manager & add_channel(Channel &)
Manager & use_simulated_time()
high_resolution_clock::duration last_update()
high_resolution_clock::duration period()
An exception class for Elma.
Manager & all(std::function< void(Process &)> f)
Manager & remove(Process &process)
A channel for sending double values to and from Process objects.
Manager & emit(const Event &event)
Manager & set_priority(Process &process, int priority)
Manager & use_real_time()
An abstract base class for processes.
Client & process_responses()