SMACC2
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 RCLCPP_INFO_STREAM(
44 this->getLogger(), "[State Reactor Base] postingfn posting event: " << demangleSymbol<TEv>());
45 auto * ev = new 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 T * evptr = (T *)msg;
62 (object->*callback)(evptr);
63 };
64}
65
66template <typename T>
67void StateReactor::createEventCallback(std::function<void(T *)> callback)
68{
69 const auto * eventtype = &typeid(T);
70 this->eventCallbacks_[eventtype] = [=](void * msg) {
71 T * evptr = (T *)msg;
72 callback(evptr);
73 };
74}
75
76namespace introspection
77{
78template <typename TEv>
80{
82 functor.fn = [this](std::shared_ptr<smacc2::StateReactor> sr) {
83 RCLCPP_INFO(
84 nh_->get_logger(), "[%s] State Reactor adding input event: %s",
85 srInfo_->stateReactorType->getFullName().c_str(), demangledTypeName<TEv>().c_str());
86 sr->addInputEvent<TEv>();
87 };
88
89 this->callbacks_.push_back(functor);
90
91 auto evtype = TypeInfo::getFromStdTypeInfo(typeid(TEv));
92 auto evinfo = std::make_shared<SmaccEventInfo>(evtype);
93 EventLabel<TEv>(evinfo->label);
94
95 srInfo_->sourceEventTypes.push_back(evinfo);
96}
97
98template <typename TEv>
100{
102 functor.fn = [this](std::shared_ptr<smacc2::StateReactor> sr) {
103 RCLCPP_INFO(
104 nh_->get_logger(), "[%s] State Reactor setting output event: %s",
105 srInfo_->stateReactorType->getFullName().c_str(), demangledTypeName<TEv>().c_str());
106 sr->setOutputEvent<TEv>();
107 };
108
109 this->callbacks_.push_back(functor);
110}
111} // namespace introspection
112
113} // 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)
void callback(const image_tools::ROSCvMatContainer &img)
std::function< void(std::shared_ptr< smacc2::StateReactor >)> fn