SMACC2
Loading...
Searching...
No Matches
smacc_state_reactor_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
24
25namespace smacc2
26{
27template <typename EventType>
28void StateReactor::postEvent(const EventType & ev)
29{
31}
32
33template <typename EventType>
35{
36 ownerState->postEvent<EventType>();
37}
38
39template <typename TEv>
41{
42 this->postEventFn = [this]()
43 {
44 RCLCPP_INFO_STREAM(
45 this->getLogger(), "[State Reactor Base] postingfn posting event: " << demangleSymbol<TEv>());
47 };
48}
49
50template <typename TEv>
52{
53 this->eventTypes.push_back(&typeid(TEv));
54}
55
56template <typename T, typename TClass>
57void StateReactor::createEventCallback(void (TClass::*callback)(T *), TClass * object)
58{
59 const auto * eventtype = &typeid(T);
60 this->eventCallbacks_[eventtype] = [=](void * msg)
61 {
62 T * evptr = static_cast<T *>(msg);
63 (object->*callback)(evptr);
64 };
65}
66
67template <typename T>
68void StateReactor::createEventCallback(std::function<void(T *)> callback)
69{
70 const auto * eventtype = &typeid(T);
71 this->eventCallbacks_[eventtype] = [=](void * msg)
72 {
73 T * evptr = static_cast<T *>(msg);
74 callback(evptr);
75 };
76}
77
78namespace introspection
79{
80template <typename TEv>
82{
84 functor.fn = [this](std::shared_ptr<smacc2::StateReactor> sr)
85 {
86 auto srInfo = srInfo_.lock();
87 RCLCPP_INFO(
88 logger_, "[%s] State Reactor adding input event: %s",
89 srInfo ? srInfo->stateReactorType->getFullName().c_str() : "?",
90 demangledTypeName<TEv>().c_str());
91 sr->addInputEvent<TEv>();
92 };
93
94 this->callbacks_.push_back(functor);
95
96 auto evtype = TypeInfo::getFromStdTypeInfo(typeid(TEv));
97 auto evinfo = std::make_shared<SmaccEventInfo>(evtype);
98 EventLabel<TEv>(evinfo->label);
99
100 // The info owns this handler and is alive for as long as the static reactor map holds it.
101 auto srInfo = srInfo_.lock();
102 if (srInfo != nullptr)
103 {
104 srInfo->sourceEventTypes.push_back(evinfo);
105 }
106}
107
108template <typename TEv>
110{
112 functor.fn = [this](std::shared_ptr<smacc2::StateReactor> sr)
113 {
114 auto srInfo = srInfo_.lock();
115 RCLCPP_INFO(
116 logger_, "[%s] State Reactor setting output event: %s",
117 srInfo ? srInfo->stateReactorType->getFullName().c_str() : "?",
118 demangledTypeName<TEv>().c_str());
119 sr->setOutputEvent<TEv>();
120 };
121
122 this->callbacks_.push_back(functor);
123}
124} // namespace introspection
125
126} // namespace smacc2
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)
virtual ISmaccStateMachine & getStateMachine()=0
void postEvent(const EventType &ev)
std::function< void()> postEventFn
void createEventCallback(void(TClass::*callback)(T *), TClass *object)
rclcpp::Logger getLogger()
std::vector< const std::type_info * > eventTypes
std::map< const std::type_info *, std::function< void(void *)> > eventCallbacks_
std::vector< StateReactorCallbackFunctor > callbacks_
std::weak_ptr< smacc2::introspection::SmaccStateReactorInfo > srInfo_
static TypeInfo::Ptr getFromStdTypeInfo(const std::type_info &tid)
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)
std::function< void(std::shared_ptr< smacc2::StateReactor >)> fn