Go to the documentation of this file.
12 #ifndef RTCTK_COMPONENTFRAMEWORK_STATEMACHINEENGINE_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_STATEMACHINEENGINE_HPP
18 #include <rad/signal.hpp>
19 #include <rad/smAdapter.hpp>
24 using GuardMethod = std::function<bool(scxml4cpp::Context* c)>;
78 template <
class Event>
81 RegisterAction(
id, [action = std::move(action)](scxml4cpp::Context* ctx) {
83 assert(ctx->getLastEvent());
84 rad::SMEvent& ev =
static_cast<rad::SMEvent&
>(*ctx->getLastEvent());
85 action(
static_cast<Event const&
>(ev.GetEv()));
111 template <
class Event>
114 RegisterGuard(
id, [guard = std::move(guard)](scxml4cpp::Context* ctx) ->
bool {
116 assert(ctx->getLastEvent());
117 assert(ctx->getLastEvent()->getId() == Event::ID);
118 rad::SMEvent& ev =
static_cast<rad::SMEvent&
>(*ctx->getLastEvent());
119 return guard(
static_cast<Event const&
>(ev.GetEv()));
162 template <
typename T,
typename Ex>
165 if(
auto req=rad::GetPayloadNothrow<T>(event); req) {
166 req->SetException(Ex{T::ID,
state});
208 boost::asio::io_service m_io_service;
209 scxml4cpp::Context m_fsm_ctx;
210 scxml4cpp::EventQueue m_event_queue;
211 rad::SMAdapter m_fsm_engine;
212 rad::Signal m_signal;
220 std::list<Model> m_models_to_append;
221 std::list<scxml4cpp::Action*> m_action_list;
222 std::list<scxml4cpp::Activity*> m_activity_list;
225 template <
typename EVENT>
227 rad::SMEvent* ev =
dynamic_cast<rad::SMEvent*
>(c->getLastEvent());
232 auto req = rad::GetPayloadNothrow<EVENT>(ev->GetEv());
233 if (req ==
nullptr) {
237 return std::make_shared<typename EVENT::payload_t>(*req);
242 #endif // RTCTK_COMPONENTFRAMEWORK_STATEMACHINEENGINE_HPP
Provides macros and utilities for exception handling.
void AppendModel(const std::string &name, const std::string &model)
Append state machine model fragment.
Definition: stateMachineEngine.cpp:57
std::function< void()> SuccessMethod
Definition: stateMachineEngine.hpp:26
void Work()
Runs the event loop of the state machine.
Definition: stateMachineEngine.cpp:133
name
Definition: wscript:15
void RegisterModel(const std::string &name, const std::string &model)
Register a new state machine model.
Definition: stateMachineEngine.cpp:52
std::function< void(StopToken f)> ActivityMethod
Definition: stateMachineEngine.hpp:25
Definition: commandReplier.cpp:20
std::function< void(const rad::AnyEvent &, const std::string &state)> RejectMethod
Definition: stateMachineEngine.hpp:28
void RegisterGuardStatic(std::string const &id, std::function< bool(Event const &)> guard)
Register guard for statically known event type.
Definition: stateMachineEngine.hpp:112
std::function< void(std::exception_ptr)> FailureMethod
Definition: stateMachineEngine.hpp:27
void RegisterGuard(std::string const &id, GuardMethod guard)
Register guard.
Definition: stateMachineEngine.cpp:77
rad::StopToken StopToken
Definition: stopToken.hpp:19
void RegisterActionStatic(std::string const &id, std::function< void(Event const &)> action)
Register action for statically known event type.
Definition: stateMachineEngine.hpp:79
Definition: stateMachineEngine.hpp:31
void RegisterAction(std::string const &id, ActionMethod action)
Register action.
Definition: stateMachineEngine.cpp:65
~StateMachineEngine()
Definition: stateMachineEngine.cpp:42
std::function< void(const std::string &)> StateMethod
Definition: stateMachineEngine.hpp:29
void RegisterRejectHandler()
Register default reject handler.
Definition: stateMachineEngine.hpp:163
std::function< bool(scxml4cpp::Context *c)> GuardMethod
Definition: stateMachineEngine.hpp:24
void RegisterStateChangeHandler(StateMethod on_statechange)
Register state changed handler.
Definition: stateMachineEngine.cpp:111
std::string GetState()
Queries the current state.
Definition: stateMachineEngine.cpp:129
std::shared_ptr< typename EVENT::payload_t > GetPayloadNothrow(scxml4cpp::Context *c)
Definition: stateMachineEngine.hpp:226
void PostEvent(rad::SharedEvent s)
Injects a new event into the state machine engine.
Definition: stateMachineEngine.cpp:124
StateMachineEngine(rad::UniqueEvent exit_event)
Definition: stateMachineEngine.cpp:33
void Stop()
Stops execution of the state machine event loop.
Definition: stateMachineEngine.cpp:155
void RegisterActivity(std::string const &id, ActivityMethod activity, SuccessMethod on_success, FailureMethod on_failure)
Register activity.
Definition: stateMachineEngine.cpp:90
std::function< void(scxml4cpp::Context *c)> ActionMethod
Definition: stateMachineEngine.hpp:23