controller.cpp 1.16 KB
Newer Older
1
2
3
4
5
#include "controller.h"
#include <mutex>

namespace Modules {

Leander Schulten's avatar
Leander Schulten committed
6
Controller::Controller():run_(false)
7
8
9
10
11
12
13
{

}

void Controller::run() noexcept{
    while (run_) {
        std::this_thread::sleep_for(std::chrono::milliseconds(1));
Leander Schulten's avatar
Leander Schulten committed
14
15
16
        std::unique_lock<std::mutex> l(vectorLock);
        for(auto pb = runningProgramms.begin() ; pb != runningProgramms.end();){
            if((*pb)->doStep(1)){
17
                (**pb).stop();
Leander Schulten's avatar
Leander Schulten committed
18
19
20
21
22
                pb = runningProgramms.erase(pb);
            }else{
                ++pb;
            }
        }
23
24
25
    }
}

Leander Schulten's avatar
Leander Schulten committed
26
27
28
void Controller::runProgramm(std::shared_ptr<ProgramBlock> pb){
    std::unique_lock<std::mutex> l(vectorLock);
    runningProgramms.push_back(pb);
29
    pb->start();
Leander Schulten's avatar
Leander Schulten committed
30
31
32
33
34
}

void Controller::stopProgramm(std::shared_ptr<ProgramBlock> pb){
    std::unique_lock<std::mutex> l(vectorLock);
    runningProgramms.erase(std::remove(runningProgramms.begin(),runningProgramms.end(),pb),runningProgramms.end());
35
36
37
38
39
40
    pb->stop();
}

bool Controller::isProgramRunning(ProgramBlock * pb){
    std::unique_lock<std::mutex> l(vectorLock);
    return std::any_of(runningProgramms.cbegin(),runningProgramms.cend(),[=](const auto & p){return p.get()==pb;});
Leander Schulten's avatar
Leander Schulten committed
41
42
}

43
}