SMACC2
Loading...
Searching...
No Matches
smacc_state_impl.hpp
Go to the documentation of this file.
1// Copyright 2025 Robosoft Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15/*****************************************************************************************************************
16 *
17 * Authors: Pablo Inigo Blasco, Brett Aldrich
18 *
19 ******************************************************************************************************************/
20
21#pragma once
29
30namespace smacc2
31{
32using namespace smacc2::introspection;
33#define THIS_STATE_NAME ((demangleSymbol(typeid(*this).name()).c_str()))
34template <typename TOrthogonal, typename TBehavior, typename... Args>
35std::shared_ptr<TBehavior> ISmaccState::configure(Args &&... args)
36{
37 std::string orthogonalkey = demangledTypeName<TOrthogonal>();
38 RCLCPP_INFO(
39 getLogger(), "[%s] Configuring orthogonal: %s", THIS_STATE_NAME, orthogonalkey.c_str());
40
41 TOrthogonal * orthogonal = this->getOrthogonal<TOrthogonal>();
42 if (orthogonal != nullptr)
43 {
44 // A compile error here usually means the behavior's constructor parameters do not match.
45 auto clientBehavior = std::make_shared<TBehavior>(args...);
46 orthogonal->addClientBehavior(clientBehavior);
47 clientBehavior->template onStateOrthogonalAllocation<TOrthogonal, TBehavior>();
48 // Set here and not in onStateOrthogonalAllocation, which a derived behavior may hide.
49 ISmaccClientBehavior * behavior = clientBehavior.get();
50 behavior->postInitializationFailureEventFn_ = [behavior](const std::string & what)
51 {
53 ev->behaviorName = behavior->getName();
54 ev->what = what;
55 behavior->postEvent(ev);
56 };
57 return clientBehavior;
58 }
59 else
60 {
61 RCLCPP_ERROR(
62 getLogger(), "[%s] Skipping client behavior creation in orthogonal [%s]. It does not exist.",
63 THIS_STATE_NAME, orthogonalkey.c_str());
64 return nullptr;
65 }
66}
67//-------------------------------------------------------------------------------------------------------
68
69template <typename SmaccComponentType>
71 SmaccComponentType *& storage, ComponentRequirement requirementType)
72{
73 this->getStateMachine().requiresComponent(storage, requirementType);
74}
75
76template <typename SmaccComponentType>
78 const std::string & name, SmaccComponentType *& storage, ComponentRequirement requirementType)
79{
80 this->getStateMachine().requiresComponent(name, storage, requirementType);
81}
82//-------------------------------------------------------------------------------------------------------
83
84template <typename SmaccClientType>
85void ISmaccState::requiresClient(SmaccClientType *& storage)
86{
87 storage = nullptr;
88 auto & orthogonals = this->getStateMachine().getOrthogonals();
89 for (auto & ortho : orthogonals)
90 {
91 ortho.second->requiresClient(storage);
92 if (storage != nullptr) return;
93 }
94
95 // Keep the std::string alive across the log call. This used to take c_str() of the temporary
96 // returned by demangleSymbol into a const char *, which read freed memory here. The name is
97 // only needed on this path, so it is demangled here rather than on every lookup.
98 const std::string sname = demangleSymbol(typeid(*this).name());
99 RCLCPP_ERROR(
100 getLogger(),
101 "[%s] Client of type '%s' not found in any orthogonal of the current state machine. This may "
102 "produce a segmentation fault if the returned reference is used.",
103 sname.c_str(), demangleSymbol<SmaccClientType>().c_str());
104}
105//-------------------------------------------------------------------------------------------------------
106
107template <typename T>
108bool ISmaccState::getGlobalSMData(const std::string & name, T & ret)
109{
110 return this->getStateMachine().getGlobalSMData(name, ret);
111}
112//-------------------------------------------------------------------------------------------------------
113
114// Store globally in this state machine. (By value parameter )
115template <typename T>
116void ISmaccState::setGlobalSMData(const std::string & name, T value)
117{
118 this->getStateMachine().setGlobalSMData(name, value);
119}
120//-------------------------------------------------------------------------------------------------------
121
122template <typename TStateReactor, typename... TEvArgs>
123std::shared_ptr<TStateReactor> ISmaccState::createStateReactor(TEvArgs... args)
124{
125 auto sr = std::make_shared<TStateReactor>(args...);
126 stateReactors_.push_back(sr);
127 return sr;
128}
129
130template <typename TEventGenerator, typename... TEvArgs>
131std::shared_ptr<TEventGenerator> ISmaccState::createEventGenerator(TEvArgs... args)
132{
133 auto eg = std::make_shared<TEventGenerator>(args...);
134 eventGenerators_.push_back(eg);
135 return eg;
136}
137
138// used to iterate on the source events list and fill the information of the stateReactorInfo structure
139template <typename TEventList>
141{
144
145 template <typename T>
147 {
148 auto evinfo = std::make_shared<SmaccEventInfo>(TypeInfo::getTypeInfoFromType<T>());
149 srInfo_->sourceEventTypes.push_back(evinfo);
150 EventLabel<T>(evinfo->label);
151 }
152};
153
154// used to iterate on the source events list and fill the information of the stateReactorInfo structure
155// (is it required alreadyy having the AddTEventTypeStateReactorInfo?)
156template <typename TEventList>
158{
161
162 template <typename T>
164 {
165 sr_->addInputEvent<T>();
166 }
167};
168
169template <typename TStateReactor, typename TTriggerEvent, typename TEventList, typename... TEvArgs>
170std::shared_ptr<TStateReactor> ISmaccState::createStateReactor(TEvArgs... args)
171{
172 auto sr = std::make_shared<TStateReactor>(args...);
173 sr->initialize(this);
174 sr->template setOutputEvent<TTriggerEvent>();
175
176 using boost::mpl::_1;
177 using wrappedList = typename boost::mpl::transform<TEventList, _1>::type;
179 boost::mpl::for_each<wrappedList>(op);
180
181 stateReactors_.push_back(sr);
182 return sr;
183}
184
185template <typename TOrthogonal>
187{
188 return this->getStateMachine().getOrthogonal<TOrthogonal>();
189}
190
191template <typename TOrthogonal, typename TClientBehavior>
192TClientBehavior * ISmaccState::getClientBehavior(int index)
193{
194 return this->getStateMachine().getClientBehavior<TOrthogonal, TClientBehavior>(index);
195}
196
197template <typename TEventGenerator>
199{
200 TEventGenerator * ret = nullptr;
201 for (auto & evg : this->eventGenerators_)
202 {
203 ret = dynamic_cast<TEventGenerator *>(evg.get());
204 if (ret != nullptr) break;
205 }
206 return ret;
207}
208
209template <typename TStateReactor>
211{
212 TStateReactor * ret = nullptr;
213 for (auto & sr : this->eventGenerators_)
214 {
215 ret = dynamic_cast<TStateReactor *>(sr.get());
216 if (ret != nullptr) break;
217 }
218 return ret;
219}
220
221//-------------------------------------------------------------------------------------------------------
222
223template <typename EventType>
224void ISmaccState::postEvent(const EventType & ev)
225{
227}
228
229template <typename EventType>
231{
232 getStateMachine().postEvent<EventType>();
233}
234//-------------------------------------------------------------------------------------------------------
235
236template <typename TransitionType>
242
243//-------------------------------------------------------------------------------------------------------------------
244
245} // namespace smacc2
246
247// implementation depends on state definition
std::function< void(const std::string &)> postInitializationFailureEventFn_
void setGlobalSMData(const std::string &name, T value)
TClientBehavior * getClientBehavior(int index=0)
const std::map< std::string, std::shared_ptr< smacc2::ISmaccOrthogonal > > & getOrthogonals() const
bool getGlobalSMData(const std::string &name, T &ret)
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType, ISmaccOrthogonal *first=nullptr)
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)
rclcpp::Logger getLogger()
TClientBehavior * getClientBehavior(int index=0)
std::shared_ptr< TEventGenerator > createEventGenerator(TEvArgs... args)
std::vector< std::shared_ptr< StateReactor > > stateReactors_
std::shared_ptr< TBehavior > configure(Args &&... args)
void requiresClient(SmaccClientType *&storage)
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
std::shared_ptr< TStateReactor > createStateReactor(TEvArgs... args)
bool getGlobalSMData(const std::string &name, T &ret)
TOrthogonal * getOrthogonal()
TStateReactor * getStateReactor()
virtual ISmaccStateMachine & getStateMachine()=0
std::vector< std::shared_ptr< smacc2::SmaccEventGenerator > > eventGenerators_
TEventGenerator * getEventGenerator()
void setGlobalSMData(const std::string &name, T value)
void notifyTransitionFromTransitionTypeInfo(std::shared_ptr< smacc2::introspection::TypeInfo > &transitionTypeInfo)
static TypeInfo::Ptr getTypeInfoFromType()
std::enable_if< HasEventLabel< T >::value, void >::type EventLabel(std::string &label)
std::string demangleSymbol()
std::string demangledTypeName()
boost::intrusive_ptr< EventType > makeEvent(Args &&... args)
ComponentRequirement
Definition common.hpp:82
#define THIS_STATE_NAME
smacc2::SmaccStateReactorInfo * srInfo_
AddTEventTypeStateReactorInfo(smacc2::SmaccStateReactorInfo *srInfo)
AddTEventTypeStateReactor(smacc2::StateReactor *sr)
std::vector< std::shared_ptr< SmaccEventInfo > > sourceEventTypes