SMACC2
smacc_state_reactor.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
22
23#include <algorithm>
24#include <boost/statechart/event.hpp>
25#include <functional>
26#include <map>
27#include <memory>
29#include <vector>
30
31namespace smacc2
32{
33class ISmaccState;
34class ISMaccStateMachine;
35
36namespace state_reactors
37{
39{
40};
41} // namespace state_reactors
42
44{
45public:
47 std::function<void()> postEventFn;
48 std::vector<const std::type_info *> eventTypes;
49 std::map<const std::type_info *, std::function<void(void *)>> eventCallbacks_;
50
52 virtual ~StateReactor();
53
54 virtual void onInitialized();
55
56 virtual void onEntry();
57 virtual void onExit();
58
59 virtual void onEventNotified(const std::type_info * eventType);
60
61 template <typename EventType>
62 void postEvent(const EventType & ev);
63
64 template <typename EventType>
65 void postEvent();
66
67 // type based event callback
68 template <typename T, typename TClass>
69 void createEventCallback(void (TClass::*callback)(T *), TClass * object);
70
71 // type based event callback
72 template <typename T>
73 void createEventCallback(std::function<void(T *)> callback);
74
75 void update();
76
77 //must returns true when the output event is triggered
78 virtual bool triggers() = 0;
79
80 template <typename TEv>
81 void addInputEvent();
82
83 template <typename TEv>
84 void setOutputEvent();
85
86 //TDerived
88
89 rclcpp::Node::SharedPtr & getNode();
90
91 rclcpp::Logger getLogger();
92
93private:
95
96 template <typename TEvent>
97 void notifyEvent(TEvent * ev)
98 {
99 //the state machine uses this method to notify this state reactor some event happened.
100 auto tid = &(typeid(TEvent));
101 if (std::find(eventTypes.begin(), eventTypes.end(), tid) != eventTypes.end())
102 {
103 this->onEventNotified(tid);
104 this->update();
105
106 if (eventCallbacks_.count(tid))
107 {
108 eventCallbacks_[tid]((void *)ev);
109 }
110 }
111 }
112};
113
114} // namespace smacc2
void initialize(ISmaccState *ownerState)
virtual void onEventNotified(const std::type_info *eventType)
virtual void onEntry()
virtual void onInitialized()
virtual void onExit()
void notifyEvent(TEvent *ev)
std::function< void()> postEventFn
rclcpp::Node::SharedPtr & getNode()
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_
virtual bool triggers()=0
void callback(const image_tools::ROSCvMatContainer &img)