SMACC2
Loading...
Searching...
No Matches
smacc_state_reactor.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
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
43// Reactor callbacks (onEventNotified, triggers, update, event callbacks) run on the state machine
44// thread between two events with no lock held: posting events is fine, blocking on the state
45// machine (a transition, a state exit) deadlocks it.
47{
48public:
50 std::function<void()> postEventFn;
51 std::vector<const std::type_info *> eventTypes;
52 std::map<const std::type_info *, std::function<void(void *)>> eventCallbacks_;
53
55 virtual ~StateReactor();
56
57 // Not copyable or movable. This turns an accidental copy, such as a missing & in
58 // "auto & sr = *reactor;", into a compile error instead of calls on a detached copy.
59 StateReactor(const StateReactor &) = delete;
60 StateReactor & operator=(const StateReactor &) = delete;
63
64 virtual void onInitialized();
65
66 virtual void onEntry();
67 virtual void onExit();
68
69 virtual void onEventNotified(const std::type_info * eventType);
70
71 template <typename EventType>
72 void postEvent(const EventType & ev);
73
74 template <typename EventType>
75 void postEvent();
76
77 // type based event callback
78 template <typename T, typename TClass>
79 void createEventCallback(void (TClass::*callback)(T *), TClass * object);
80
81 // type based event callback
82 template <typename T>
83 void createEventCallback(std::function<void(T *)> callback);
84
85 void update();
86
87 //must returns true when the output event is triggered
88 virtual bool triggers() = 0;
89
90 template <typename TEv>
91 void addInputEvent();
92
93 template <typename TEv>
94 void setOutputEvent();
95
96 //TDerived
98
99 rclcpp::Node::SharedPtr & getNode();
100
101 rclcpp::Logger getLogger();
102
103private:
105
106 template <typename TEvent>
107 void notifyEvent(TEvent * ev)
108 {
109 //the state machine uses this method to notify this state reactor some event happened.
110 auto tid = &(typeid(TEvent));
111 if (std::find(eventTypes.begin(), eventTypes.end(), tid) != eventTypes.end())
112 {
113 this->onEventNotified(tid);
114 this->update();
115
116 if (eventCallbacks_.count(tid))
117 {
118 eventCallbacks_[tid](static_cast<void *>(ev));
119 }
120 }
121 }
122};
123
124} // namespace smacc2
void initialize(ISmaccState *ownerState)
virtual void onEventNotified(const std::type_info *eventType)
virtual void onEntry()
StateReactor & operator=(const StateReactor &)=delete
virtual void onInitialized()
virtual void onExit()
StateReactor(StateReactor &&)=delete
StateReactor & operator=(StateReactor &&)=delete
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
StateReactor(const StateReactor &)=delete
std::map< const std::type_info *, std::function< void(void *)> > eventCallbacks_
virtual bool triggers()=0