SMACC2
Loading...
Searching...
No Matches
smacc_state_reactor_impl.hpp
Go to the documentation of this file.
1// Copyright 2021 RobosoftAI 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>());
46 auto * ev = new TEv();
48 };
49}
50
51template <typename TEv>
53{
54 this->eventTypes.push_back(&typeid(TEv));
55}
56
57template <typename T, typename TClass>
58void StateReactor::createEventCallback(void (TClass::*callback)(T *), TClass * object)
59{
60 const auto * eventtype = &typeid(T);
61 this->eventCallbacks_[eventtype] = [=](void * msg)
62 {
63 T * evptr = (T *)msg;
64 (object->*callback)(evptr);
65 };
66}
67
68template <typename T>
69void StateReactor::createEventCallback(std::function<void(T *)> callback)
70{
71 const auto * eventtype = &typeid(T);
72 this->eventCallbacks_[eventtype] = [=](void * msg)
73 {
74 T * evptr = (T *)msg;
75 callback(evptr);
76 };
77}
78
79namespace introspection
80{
81template <typename TEv>
83{
85 functor.fn = [this](std::shared_ptr<smacc2::StateReactor> sr)
86 {
87 RCLCPP_INFO(
88 nh_->get_logger(), "[%s] State Reactor adding input event: %s",
89 srInfo_->stateReactorType->getFullName().c_str(), demangledTypeName<TEv>().c_str());
90 sr->addInputEvent<TEv>();
91 };
92
93 this->callbacks_.push_back(functor);
94
95 auto evtype = TypeInfo::getFromStdTypeInfo(typeid(TEv));
96 auto evinfo = std::make_shared<SmaccEventInfo>(evtype);
97 EventLabel<TEv>(evinfo->label);
98
99 srInfo_->sourceEventTypes.push_back(evinfo);
100}
101
102template <typename TEv>
104{
106 functor.fn = [this](std::shared_ptr<smacc2::StateReactor> sr)
107 {
108 RCLCPP_INFO(
109 nh_->get_logger(), "[%s] State Reactor setting output event: %s",
110 srInfo_->stateReactorType->getFullName().c_str(), demangledTypeName<TEv>().c_str());
111 sr->setOutputEvent<TEv>();
112 };
113
114 this->callbacks_.push_back(functor);
115}
116} // namespace introspection
117
118} // 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::shared_ptr< smacc2::introspection::SmaccStateReactorInfo > srInfo_
static TypeInfo::Ptr getFromStdTypeInfo(const std::type_info &tid)
std::function< void(std::shared_ptr< smacc2::StateReactor >)> fn