3 #include "parameters/Scope.h"
5 #include "helpers/ScopeException.h"
10 template<u
int32_t N_ACTIVES,
bool STATIC_PIMPL>
15 std::array<std::shared_future<ControllerReturnStatus>, N_ACTIVES>
futures;
18 std::array<StopCondition, N_ACTIVES>
stops;
35 DBOUT(L
"BaseControllerImpl::Run - this should not happen, the derived class' Run method should have been called\n");
36 while ( !ac->
IsSet() )
37 std::this_thread::sleep_for(std::chrono::milliseconds(100));
39 return ControllerReturnStatus(ControllerReturnStatus::error);
45 : parameters(_parameters) {
51 DBOUT(L
"BaseController::Impl::~Impl\n");
57 DBOUT(L
"BaseController::Impl::Start\n");
59 for( uint32_t a = 0; a < N_ACTIVES ; a++ ) {
62 futures[a] = std::async(std::bind(&
Impl::Run,
this, &stops[a], a));
68 virtual void StopOne(
const uint32_t& _a) {
69 ATLASSERT(_a < N_ACTIVES);
75 DBOUT(L
"BaseController::Impl::Stop\n");
76 for ( uint32_t a = 0 ; a < N_ACTIVES ; a++ )
84 virtual ControllerReturnStatus
WaitForOne(
const uint32_t& _a,
const int32_t& _wait_time) {
85 if ( futures[_a].valid() ) {
86 std::future_status::future_status state(std::future_status::ready);
89 if ( _wait_time == -1 )
92 state = futures[_a].wait_for(std::chrono::milliseconds(_wait_time));
95 if ( state == std::future_status::ready )
96 return futures[_a].get();
99 throw ScopeException(
"WaitForOne did not succeed, async thread did not return");
102 DBOUT(L
"WaitForOne future is not valid (async worker function is not running)");
104 return ControllerReturnStatus::error;
110 virtual ControllerReturnStatus
WaitForAll(
const int32_t& _wait_time) {
111 ControllerReturnStatus totalresult(ControllerReturnStatus::none);
112 for ( uint32_t a = 0 ; a < N_ACTIVES ; a++ ) {
114 totalresult = (ControllerReturnStatus)( totalresult | WaitForOne(a, _wait_time) );
118 DBOUT(L
"BaseController::Impl::WaitForAll returns\n");
virtual ControllerReturnStatus Run(StopCondition *const ac, const uint32_t &_n)
The worker function that will run asynchronously.
parameters::Scope parameters
the Controller's own set of ScopeParameters
Thread-safe lock-free bool to signal a requested stop to the worker function currently executed in th...
The master parameters class.
Simple exception class for Scope.
virtual ~Impl()
It is important that this is virtual, only by this the correct destructor for the pimpl is called in ...
std::array< StopCondition, N_ACTIVES > stops
handed to the asynchronous Run worker functions
std::array< std::shared_future< ControllerReturnStatus >, N_ACTIVES > futures
futures from the asynchronous Run worker functions
virtual void Start(const parameters::Scope &_params)
Execute the Run worker functions asynchronously, give them a reference to a StopCondition and get the...
In here all declarations for all kinds of datatypes Scope needs.
virtual ControllerReturnStatus WaitForAll(const int32_t &_wait_time)
Wait for the futures of all async worker functions.
virtual void StopAll()
Request all async worker function to stop.
virtual void StopOne(const uint32_t &_a)
Request one async worker function to stop by settings its StopCondition to true.
#define DBOUT(s)
A debug output to the debug console.
void ScopeExceptionHandler(const std::string &_origin, const bool &_log, const bool &_showmessagebox, const bool &_trace, const bool &_rethrow)
Handles all exceptions and does nice logging.
Impl(const parameters::Scope &_parameters)
virtual ControllerReturnStatus WaitForOne(const uint32_t &_a, const int32_t &_wait_time)
Waits for the future of one async worker function.
void Set(const bool &_a=true)
BaseController operator=(const BaseController &i)
disable assignment
Base class for all controllers.
Implementation class of the BaseController, uses std::async to execute worker functions in a separate...