SMACC2
Loading...
Searching...
No Matches
smacc_state.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#pragma once
21#include <smacc2/common.hpp>
22
23namespace smacc2
24{
26{
27public:
28 ISmaccState() = default;
29 virtual ~ISmaccState() = default;
30
31 // Not copyable or movable. This turns an accidental copy, such as a missing & in
32 // "auto & ss = context<SsMission>();", into a compile error instead of writes to a lost copy.
33 ISmaccState(const ISmaccState &) = delete;
34 ISmaccState & operator=(const ISmaccState &) = delete;
37
39
40 virtual std::string getName() = 0;
41
43
44 inline rclcpp::Node::SharedPtr & getNode() { return node_; }
45
46 inline rclcpp::Logger getLogger() { return *logger_; }
47
48 inline bool initializationFailed() const { return initializationFailed_; }
49
50 virtual std::string getClassName();
51
52 template <typename TOrthogonal, typename TBehavior, typename... Args>
53 std::shared_ptr<TBehavior> configure(Args &&... args);
54
55 // Component lookup across every orthogonal of the state machine, with the same shape and
56 // defaults as ISmaccClientBehavior::requiresComponent and ISmaccComponent::requiresComponent.
57 // Takes the state machine mutex; do not call it from an async behavior thread.
58 template <typename SmaccComponentType>
60 SmaccComponentType *& storage,
62
63 // Named variant: resolves the component instance created with this name, for clients
64 // holding several components of the same type.
65 template <typename SmaccComponentType>
67 const std::string & name, SmaccComponentType *& storage,
69
70 template <typename SmaccClientType>
71 void requiresClient(SmaccClientType *& storage);
72
73 template <typename T>
74 bool getGlobalSMData(const std::string & name, T & ret);
75
76 // Store globally in this state machine. (By value parameter )
77 template <typename T>
78 void setGlobalSMData(const std::string & name, T value);
79
80 template <
81 typename TStateReactor, typename TTriggerEvent, typename TEventList, typename... TEvArgs>
82 std::shared_ptr<TStateReactor> createStateReactor(TEvArgs... args);
83
84 template <typename TStateReactor, typename... TEvArgs>
85 std::shared_ptr<TStateReactor> createStateReactor(TEvArgs... args);
86
87 template <typename TEventGenerator, typename... TEvArgs>
88 std::shared_ptr<TEventGenerator> createEventGenerator(TEvArgs... args);
89
90 template <typename EventType>
91 void postEvent(const EventType & ev);
92
93 template <typename EventType>
94 void postEvent();
95
96 // used for transition logging
97 template <typename TransitionType>
98 void notifyTransition();
99
100 // used for transition logging
102 std::shared_ptr<smacc2::introspection::TypeInfo> & transitionTypeInfo);
103
104 inline std::vector<std::shared_ptr<StateReactor>> & getStateReactors() { return stateReactors_; }
105
106 inline std::vector<std::shared_ptr<SmaccEventGenerator>> & getEventGenerators()
107 {
108 return eventGenerators_;
109 }
110
111 template <typename TOrthogonal>
112 TOrthogonal * getOrthogonal();
113
114 // gets the client behavior given the client type and orthogonal type
115 // if the client behavior is not found, it returns nullptr
116 // the index parameter is used to specify the client behavior in case there are more than one
117 template <typename TOrthogonal, typename TClientBehavior>
118 TClientBehavior * getClientBehavior(int index = 0);
119
120 template <typename TEventGenerator>
121 TEventGenerator * getEventGenerator();
122
123 template <typename TStateReactor>
124 TStateReactor * getStateReactor();
125
126protected:
127 rclcpp::Node::SharedPtr node_;
128 std::shared_ptr<rclcpp::Logger> logger_;
129 std::vector<std::shared_ptr<StateReactor>> stateReactors_;
130 std::vector<std::shared_ptr<smacc2::SmaccEventGenerator>> eventGenerators_;
131
133
134 // Set when runtimeConfigure or onEntry threw: the state's behaviors are neither entered nor exited.
136
138};
139} // namespace smacc2
std::shared_ptr< rclcpp::Logger > logger_
ISmaccState(ISmaccState &&)=delete
rclcpp::Logger getLogger()
TClientBehavior * getClientBehavior(int index=0)
std::shared_ptr< TEventGenerator > createEventGenerator(TEvArgs... args)
ISmaccState * getParentState()
std::vector< std::shared_ptr< StateReactor > > stateReactors_
std::shared_ptr< TBehavior > configure(Args &&... args)
rclcpp::Node::SharedPtr node_
virtual std::string getClassName()
ISmaccState * parentState_
void requiresClient(SmaccClientType *&storage)
virtual std::string getName()=0
void requiresComponent(SmaccComponentType *&storage, ComponentRequirement requirementType=ComponentRequirement::SOFT)
std::shared_ptr< TStateReactor > createStateReactor(TEvArgs... args)
std::vector< std::shared_ptr< StateReactor > > & getStateReactors()
virtual ~ISmaccState()=default
bool getGlobalSMData(const std::string &name, T &ret)
TOrthogonal * getOrthogonal()
bool initializationFailed() const
TStateReactor * getStateReactor()
std::vector< std::shared_ptr< SmaccEventGenerator > > & getEventGenerators()
virtual ISmaccStateMachine & getStateMachine()=0
std::vector< std::shared_ptr< smacc2::SmaccEventGenerator > > eventGenerators_
ISmaccState & operator=(const ISmaccState &)=delete
const smacc2::introspection::SmaccStateInfo * stateInfo_
ISmaccState & operator=(ISmaccState &&)=delete
rclcpp::Node::SharedPtr & getNode()
TEventGenerator * getEventGenerator()
void setGlobalSMData(const std::string &name, T value)
void notifyTransitionFromTransitionTypeInfo(std::shared_ptr< smacc2::introspection::TypeInfo > &transitionTypeInfo)
ISmaccState(const ISmaccState &)=delete
ComponentRequirement
Definition common.hpp:82