RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
customLifeCycle.hpp
Go to the documentation of this file.
1
11
12#ifndef RTCTK_EXAMPLECUSTOM_CUSTOMLIFECYCLE_HPP
13#define RTCTK_EXAMPLECUSTOM_CUSTOMLIFECYCLE_HPP
14
15#include "customCmdsImpl.hpp"
20
21namespace rtctk::exampleCustom {
22
23namespace cfw = componentFramework;
24
25template <typename Super>
26struct CustomLifeCycle : Super {
27 static_assert(std::is_base_of_v<cfw::RtcComponent, Super>,
28 "'CustomLifeCycle' requires 'RtcComponent'");
29
30 class BizLogicIf : public Super::BizLogicIf {
31 public:
32 virtual void ActivityFoo(cfw::StopToken st) {
33 }
34 virtual void ActivityBar(cfw::StopToken st, const cfw::JsonPayload& args) {
35 }
36 };
37
38 class InputStage : public Super::InputStage {
39 public:
40 using Super::InputStage::InputStage;
41
42 void Start() override {
43 Super::InputStage::Start();
44
45 CustomCmdsImpl::Register(this->m_replier, this->m_engine);
46 }
47 };
48
49 class OutputStage : public Super::OutputStage {
50 public:
51 OutputStage(cfw::StateMachineEngine& engine, BizLogicIf& bl)
52 : Super::OutputStage(engine, bl) {
53 using namespace cfw;
54
55 // Handlers ###################################################################
56
57 engine.RegisterRejectHandler<events::Foo, RequestRejected>();
58 engine.RegisterRejectHandler<events::Bar, RequestRejected>();
59
61 this->m_engine.PostEvent(std::make_unique<events::CustomDone>());
62 };
63
64 m_custom_error_handler = [this](std::exception_ptr eptr) {
65 this->m_engine.PostEvent(std::make_unique<events::CustomError>(std::move(eptr)));
66 };
67
68 // Actions #####################################################################
69
70 engine.RegisterActionStatic<events::Foo>("ActionFooEntry", [this](const auto& ev) {
71 assert(not m_tmp_foo_request);
72 m_tmp_foo_request = ev.GetPayload();
73 });
74
75 engine.RegisterActionStatic<events::CustomDone>("ActionFooDone", [this](const auto&) {
76 assert(m_tmp_foo_request);
77 m_tmp_foo_request->SetReplyValue(STD_OK_REPLY);
78 m_tmp_foo_request.reset();
79 });
80
81 engine.RegisterActionStatic<events::CustomError>(
82 "ActionFooFailed", [this](const auto& ev) {
83 const auto& eptr = ev.GetPayload();
84 assert(m_tmp_foo_request);
85 m_tmp_foo_request->SetException(
87 m_tmp_foo_request.reset();
88 });
89
90 engine.RegisterActionStatic<events::Bar>("ActionBarEntry", [this](const auto& ev) {
91 assert(not m_tmp_bar_request);
92 m_tmp_bar_request = ev.GetPayload();
93 });
94
95 engine.RegisterActionStatic<events::CustomDone>("ActionBarDone", [this](const auto&) {
96 assert(m_tmp_bar_request);
97 m_tmp_bar_request->SetReplyValue(STD_OK_REPLY);
98 m_tmp_bar_request.reset();
99 });
100
101 engine.RegisterActionStatic<events::CustomError>(
102 "ActionBarFailed", [this](const auto& ev) {
103 const auto& eptr = ev.GetPayload();
104 assert(m_tmp_bar_request);
105 m_tmp_bar_request->SetException(
107 m_tmp_bar_request.reset();
108 });
109
110 // Activities #####################################################################
111
112 engine.RegisterActivity(
113 "ActivityFoo",
114 [this](StopToken stop_token) {
115 static_cast<BizLogicIf&>(this->m_logic).ActivityFoo(stop_token);
116 },
119
120 engine.RegisterActivity(
121 "ActivityBar",
122 [this](StopToken stop_token) {
123 std::string args = m_tmp_bar_request->GetRequestPayload();
124 JsonPayload arg = JsonPayload::parse(args);
125 static_cast<BizLogicIf&>(this->m_logic).ActivityBar(stop_token, arg);
126 },
129 }
130
131 protected:
132 std::optional<rad::cii::Request<std::string>> m_tmp_foo_request;
133 std::optional<rad::cii::Request<std::string, std::string>> m_tmp_bar_request;
134 std::function<void()> m_custom_success_handler;
135 std::function<void(std::exception_ptr)> m_custom_error_handler;
136 };
137
138 class ModelBuilder : public Super::ModelBuilder {
139 public:
140 explicit ModelBuilder(cfw::StateMachineEngine& engine) : Super::ModelBuilder(engine) {
141 using namespace cfw;
142
143 // clang-format off
144 this->mm.ModStateType("On::Operational", Parallel);
145
146 this->mm.AddState(Composite, "On::Operational:Custom", "On::Operational");
147 this->mm.AddState(Initial, "On::Operational:Custom:Initial", "On::Operational:Custom");
148 this->mm.AddState(Simple, "On::Operational:Custom:Idle", "On::Operational:Custom");
149 this->mm.AddState(Simple, "On::Operational:Custom:Foo", "On::Operational:Custom", "ActivityFoo" ,"ActionFooEntry");
150 this->mm.AddState(Simple, "On::Operational:Custom:Bar", "On::Operational:Custom", "ActivityBar" ,"ActionBarEntry");
151
152 this->mm.AddTrans("On::Operational:Custom:Initial" , "On::Operational:Custom:Idle");
153 this->mm.AddTrans("On::Operational:Custom:Idle" , "On::Operational:Custom:Foo" , "events.Foo" ,"");
154 this->mm.AddTrans("On::Operational:Custom:Foo" , "On::Operational:Custom:Idle" , "events.CustomDone" ,"" ,"ActionFooDone" );
155 this->mm.AddTrans("On::Operational:Custom:Foo" , "On::Operational:Custom:Idle" , "events.CustomError" ,"" ,"ActionFooFailed" );
156 this->mm.AddTrans("On::Operational:Custom:Idle" , "On::Operational:Custom:Bar" , "events.Bar" ,"");
157 this->mm.AddTrans("On::Operational:Custom:Bar" , "On::Operational:Custom:Idle" , "events.CustomDone" ,"" ,"ActionBarDone" );
158 this->mm.AddTrans("On::Operational:Custom:Bar" , "On::Operational:Custom:Idle" , "events.CustomError" ,"" ,"ActionBarFailed" );
159 // clang-format on
160 }
161 };
162};
163
164} // namespace rtctk::exampleCustom
165
166#endif
Adapter object intended to be used in contexts without direct access to the output-stream object.
Definition exceptions.hpp:168
std::string Str() const
Convenience function for constructing a std::string from the exception.
Definition exceptions.hpp:186
Thrown if the command was accepted but the task to run failed.
Definition rtcComponent.hpp:52
Thrown if a command is not allowed in current state or guard.
Definition rtcComponent.hpp:39
static void Register(cfw::CommandReplier &replier, cfw::StateMachineEngine &engine)
Definition customCmdsImpl.hpp:31
Definition customLifeCycle.hpp:30
virtual void ActivityFoo(cfw::StopToken st)
Definition customLifeCycle.hpp:32
virtual void ActivityBar(cfw::StopToken st, const cfw::JsonPayload &args)
Definition customLifeCycle.hpp:34
Definition customLifeCycle.hpp:38
void Start() override
Definition customLifeCycle.hpp:42
ModelBuilder(cfw::StateMachineEngine &engine)
Definition customLifeCycle.hpp:140
std::optional< rad::cii::Request< std::string, std::string > > m_tmp_bar_request
Definition customLifeCycle.hpp:133
OutputStage(cfw::StateMachineEngine &engine, BizLogicIf &bl)
Definition customLifeCycle.hpp:51
std::function< void()> m_custom_success_handler
Definition customLifeCycle.hpp:134
std::optional< rad::cii::Request< std::string > > m_tmp_foo_request
Definition customLifeCycle.hpp:132
std::function< void(std::exception_ptr)> m_custom_error_handler
Definition customLifeCycle.hpp:135
Implementation of custom MAL commands.
Provides macros and utilities for exception handling.
Defines the JSON payload type JsonPayload.
Definition commandReplier.cpp:21
@ Simple
Definition model.hpp:22
@ Composite
Definition model.hpp:22
@ Parallel
Definition model.hpp:22
@ Initial
Definition model.hpp:22
rad::StopToken StopToken
Definition stopToken.hpp:19
const std::string STD_OK_REPLY
Definition stdComponent.hpp:85
nlohmann::json JsonPayload
Type requirements:
Definition jsonPayload.hpp:24
Definition businessLogic.cpp:22
Lifecycle of a basic 'RtcComponent'.
A simple Stop Token.
Definition customLifeCycle.hpp:26