SMACC2
Loading...
Searching...
No Matches
smacc_client.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
22#include <cstdint>
23#include <smacc2/common.hpp>
24#include <smacc2/component.hpp>
25#include <typeinfo>
26
27namespace smacc2
28{
30{
31 ComponentKey(const std::type_info * typeinfo, const std::string & name)
32 : encodedKey(std::to_string(reinterpret_cast<std::uintptr_t>(typeinfo)) + "_" + name),
34 name(name)
35 {
36 }
37 std::string encodedKey;
38 const std::type_info * typeinfo;
39 std::string name;
40
41 bool operator<(const ComponentKey & other) const { return this->encodedKey < other.encodedKey; }
42 bool operator==(const ComponentKey & other) const { return this->encodedKey == other.encodedKey; }
43};
44
46{
47public:
49 virtual ~ISmaccClient();
50
51 // Not copyable or movable. This turns an accidental copy, such as a missing & in
52 // "auto & cl = *client;", into a compile error instead of calls on a detached copy.
53 ISmaccClient(const ISmaccClient &) = delete;
54 ISmaccClient & operator=(const ISmaccClient &) = delete;
57
58 virtual void onInitialize();
59
60 // Returns a custom identifier defined by the specific plugin implementation
61 virtual std::string getName() const;
62
63 template <typename TComponent>
64 TComponent * getComponent();
65
66 template <typename TComponent>
67 TComponent * getComponent(const std::string & name);
68
69 // Gets the i-th component of type TComponent
70 template <typename TComponent>
71 TComponent * getComponent(int index);
72
74
76
77 template <typename TSmaccSignal, typename T>
78 void connectSignal(TSmaccSignal & signal, void (T::*callback)(), T * object);
79
80 template <typename SmaccClientType>
81 void requiresClient(SmaccClientType *& storage);
82
83 void getComponents(std::vector<std::shared_ptr<ISmaccComponent>> & components);
84
85 const std::vector<std::shared_ptr<ISmaccComponent>> & iterateComponents() const;
86
87 // now this needs to be public because sub-components needs to use. This is something to improve.
88 template <typename EventType>
89 void postEvent(const EventType & ev);
90
91 // now this needs to be public because sub-components needs to use. This is something to improve.
92 template <typename EventType>
93 void postEvent();
94
95protected:
96 // Called when the orthogonal creates this client - used to initialize components
97 template <typename TOrthogonal, typename TClient>
99 {
100 }
101
102 template <typename TOrthogonal, typename TSourceObject>
104 {
105 }
106
107 // components
108 std::map<ComponentKey, std::shared_ptr<smacc2::ISmaccComponent>> components_;
109
110 template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
111 SmaccComponentType * createComponent(const TArgs &... targs);
112
113 template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
114 SmaccComponentType * createNamedComponent(const std::string & name, const TArgs &... targs);
115
116 rclcpp::Node::SharedPtr getNode();
117
118 inline rclcpp::Logger getLogger() { return getNode()->get_logger(); }
119
120private:
121 // A reference to the state machine object that owns this resource
124
125 // friend method called by orthogonal
126 void initialize();
127
128 // friend method called by orthogonal
129 // Assigns the owner of this resource to the given state machine parameter object
130 void setStateMachine(ISmaccStateMachine * stateMachine);
131
132 // friend method called by orthogonal
133 void setOrthogonal(ISmaccOrthogonal * orthogonal);
134
135 friend class ISmaccOrthogonal;
136 friend class ISmaccComponent;
137
138 template <typename TOrthogonal>
139 friend class Orthogonal;
140};
141} // namespace smacc2
ISmaccStateMachine * stateMachine_
virtual void onInitialize()
Definition client.cpp:32
rclcpp::Node::SharedPtr getNode()
Definition client.cpp:60
ISmaccStateMachine * getStateMachine()
const std::vector< std::shared_ptr< ISmaccComponent > > & iterateComponents() const
std::map< ComponentKey, std::shared_ptr< smacc2::ISmaccComponent > > components_
ISmaccClient & operator=(ISmaccClient &&)=delete
void setStateMachine(ISmaccStateMachine *stateMachine)
Definition client.cpp:48
virtual std::string getName() const
Definition client.cpp:42
void setOrthogonal(ISmaccOrthogonal *orthogonal)
Definition client.cpp:53
ISmaccClient(const ISmaccClient &)=delete
rclcpp::Logger getLogger()
virtual smacc2::introspection::TypeInfo::Ptr getType()
Definition client.cpp:55
ISmaccClient & operator=(const ISmaccClient &)=delete
SmaccComponentType * createComponent(const TArgs &... targs)
void connectSignal(TSmaccSignal &signal, void(T::*callback)(), T *object)
ISmaccOrthogonal * orthogonal_
SmaccComponentType * createNamedComponent(const std::string &name, const TArgs &... targs)
ISmaccClient(ISmaccClient &&)=delete
void requiresClient(SmaccClientType *&storage)
void getComponents(std::vector< std::shared_ptr< ISmaccComponent > > &components)
Definition client.cpp:34
virtual ~ISmaccClient()
Definition client.cpp:28
std::shared_ptr< TypeInfo > Ptr
const std::type_info * typeinfo
bool operator==(const ComponentKey &other) const
ComponentKey(const std::type_info *typeinfo, const std::string &name)
bool operator<(const ComponentKey &other) const