SMACC2
Loading...
Searching...
No Matches
smacc_orthogonal.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#include <smacc2/common.hpp>
23#include <utility>
24
25namespace smacc2
26{
28{
29public:
30 ISmaccOrthogonal() = default;
31 virtual ~ISmaccOrthogonal() = default;
32
33 // Not copyable or movable. This turns an accidental copy, such as a missing & in
34 // "auto & orthogonal = *ptr;", into a compile error instead of calls on a detached copy.
39
41
43
44 void initState(ISmaccState * state);
45
46 void addClientBehavior(std::shared_ptr<smacc2::ISmaccClientBehavior> clBehavior);
47
48 void runtimeConfigure();
49
50 void onEntry();
51
52 void onExit();
53
54 void onDispose();
55
56 virtual std::string getName() const;
57
58 template <typename SmaccComponentType>
59 void requiresComponent(SmaccComponentType *& storage);
60
61 template <typename SmaccClientType>
62 bool requiresClient(SmaccClientType *& storage);
63
64 inline const std::vector<std::shared_ptr<smacc2::ISmaccClient>> & getClients();
65
66 inline const std::vector<std::vector<std::shared_ptr<smacc2::ISmaccClientBehavior>>> &
67 getClientBehaviors() const;
68
69 template <typename T>
70 void setGlobalSMData(const std::string & name, T value);
71
72 template <typename T>
73 bool getGlobalSMData(const std::string & name, T & ret);
74
75 // returns a client behavior of a given type. If the client behavior is not found, it returns nullptr
76 // the index parameter is used to specify the client behavior in case there are more than one
77 template <typename TClientBehavior>
78 TClientBehavior * getClientBehavior(int index = 0);
79
80 rclcpp::Node::SharedPtr getNode();
81 inline rclcpp::Logger getLogger() { return getNode()->get_logger(); }
82
83protected:
84 virtual void onInitialize();
85
86 void initializeClients();
87
88 template <typename TOrthogonal, typename TClient>
89 void assignClientToOrthogonal(TClient * client);
90
91 std::vector<std::shared_ptr<smacc2::ISmaccClient>> clients_;
92
93private:
94 // Null until setStateMachine() is called from createOrthogonal().
96
97 std::vector<std::vector<std::shared_ptr<smacc2::ISmaccClientBehavior>>> clientBehaviors_;
98 friend class ISmaccStateMachine;
99
100 // Guards clientBehaviors_. It is a leaf lock: it is never held while behavior code runs or
101 // while any other lock is taken. runtimeConfigure(), onEntry() and onExit() copy the current
102 // state's behavior list under it and then call the behaviors without it, so a behavior that
103 // takes the state machine's m_mutex_ (requiresComponent, createSignalConnection) does not
104 // create an orthogonal-then-state-machine lock order; the exit path already holds m_mutex_
105 // when it locks this mutex. CbSequence adds behaviors through addClientBehavior from its own
106 // thread, which is what the lock is for.
107 std::mutex mutex_;
108
109 std::vector<std::shared_ptr<smacc2::ISmaccClientBehavior>> currentClientBehaviors();
110};
111
112} // namespace smacc2
std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > currentClientBehaviors()
virtual ~ISmaccOrthogonal()=default
void addClientBehavior(std::shared_ptr< smacc2::ISmaccClientBehavior > clBehavior)
ISmaccOrthogonal(ISmaccOrthogonal &&)=delete
ISmaccStateMachine * getStateMachine()
ISmaccOrthogonal & operator=(const ISmaccOrthogonal &)=delete
ISmaccOrthogonal & operator=(ISmaccOrthogonal &&)=delete
void setStateMachine(ISmaccStateMachine *value)
bool getGlobalSMData(const std::string &name, T &ret)
virtual void onInitialize()
void setGlobalSMData(const std::string &name, T value)
void requiresComponent(SmaccComponentType *&storage)
std::vector< std::shared_ptr< smacc2::ISmaccClient > > clients_
void initState(ISmaccState *state)
ISmaccOrthogonal(const ISmaccOrthogonal &)=delete
ISmaccStateMachine * stateMachine_
bool requiresClient(SmaccClientType *&storage)
void assignClientToOrthogonal(TClient *client)
std::vector< std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > > clientBehaviors_
virtual std::string getName() const
rclcpp::Node::SharedPtr getNode()
const std::vector< std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > > & getClientBehaviors() const
TClientBehavior * getClientBehavior(int index=0)
const std::vector< std::shared_ptr< smacc2::ISmaccClient > > & getClients()