SMACC2
Loading...
Searching...
No Matches
smacc_client.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#pragma once
21
22#include <smacc2/common.hpp>
23#include <smacc2/component.hpp>
24#include <typeinfo>
25
26namespace smacc2
27{
29{
30 ComponentKey(const std::type_info * typeinfo, std::string name)
31 {
32 this->name = name;
33 this->typeinfo = typeinfo;
34 encodedKey = std::to_string((long)(void *)typeinfo) + "_" + name;
35 }
36 std::string encodedKey;
37 const std::type_info * typeinfo;
38 std::string name;
39
40 bool operator<(const ComponentKey & other) const { return this->encodedKey < other.encodedKey; }
41 bool operator==(const ComponentKey & other) const { return this->encodedKey == other.encodedKey; }
42};
43
45{
46public:
48 virtual ~ISmaccClient();
49
50 virtual void onInitialize();
51
52 // Returns a custom identifier defined by the specific plugin implementation
53 virtual std::string getName() const;
54
55 template <typename TComponent>
56 TComponent * getComponent();
57
58 template <typename TComponent>
59 TComponent * getComponent(std::string name);
60
61 // Gets the i-th component of type TComponent
62 template <typename TComponent>
63 TComponent * getComponent(int index);
64
66
68
69 template <typename TSmaccSignal, typename T>
70 void connectSignal(TSmaccSignal & signal, void (T::*callback)(), T * object);
71
72 template <typename SmaccClientType>
73 void requiresClient(SmaccClientType *& storage);
74
75 void getComponents(std::vector<std::shared_ptr<ISmaccComponent>> & components);
76
77 // now this needs to be public because sub-components needs to use. This is something to improve.
78 template <typename EventType>
79 void postEvent(const EventType & ev);
80
81 // now this needs to be public because sub-components needs to use. This is something to improve.
82 template <typename EventType>
83 void postEvent();
84
85protected:
86 // it is called after the client initialization, provides information about the orthogonal it is located in
87 template <typename TOrthogonal, typename TSourceObject>
89 {
90 }
91
92 // components
93 std::map<ComponentKey, std::shared_ptr<smacc2::ISmaccComponent>> components_;
94
95 template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
96 SmaccComponentType * createComponent(TArgs... targs);
97
98 template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
99 SmaccComponentType * createNamedComponent(std::string name, TArgs... targs);
100
101 rclcpp::Node::SharedPtr getNode();
102
103 inline rclcpp::Logger getLogger() { return getNode()->get_logger(); }
104
105private:
106 // A reference to the state machine object that owns this resource
109
110 // friend method called by orthogonal
111 void initialize();
112
113 // friend method called by orthogonal
114 // Assigns the owner of this resource to the given state machine parameter object
115 void setStateMachine(ISmaccStateMachine * stateMachine);
116
117 // friend method called by orthogonal
118 void setOrthogonal(ISmaccOrthogonal * orthogonal);
119
120 friend class ISmaccOrthogonal;
121 friend class ISmaccComponent;
122};
123} // namespace smacc2
ISmaccStateMachine * stateMachine_
virtual void onInitialize()
Definition: client.cpp:32
rclcpp::Node::SharedPtr getNode()
Definition: client.cpp:60
ISmaccStateMachine * getStateMachine()
std::map< ComponentKey, std::shared_ptr< smacc2::ISmaccComponent > > components_
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
SmaccComponentType * createNamedComponent(std::string name, TArgs... targs)
rclcpp::Logger getLogger()
virtual smacc2::introspection::TypeInfo::Ptr getType()
Definition: client.cpp:55
SmaccComponentType * createComponent(TArgs... targs)
void connectSignal(TSmaccSignal &signal, void(T::*callback)(), T *object)
ISmaccOrthogonal * orthogonal_
void requiresClient(SmaccClientType *&storage)
void getComponents(std::vector< std::shared_ptr< ISmaccComponent > > &components)
Definition: client.cpp:34
virtual ~ISmaccClient()
Definition: client.cpp:28
TComponent * getComponent()
std::shared_ptr< TypeInfo > Ptr
const std::type_info * typeinfo
ComponentKey(const std::type_info *typeinfo, std::string name)
bool operator==(const ComponentKey &other) const
std::string encodedKey
bool operator<(const ComponentKey &other) const