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>
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>
74
75 void getComponents(std::vector<std::shared_ptr<ISmaccComponent>> & components);
76
77 const std::vector<std::shared_ptr<ISmaccComponent>> & iterateComponents() const;
78
79 // now this needs to be public because sub-components needs to use. This is something to improve.
80 template <typename EventType>
81 void postEvent(const EventType & ev);
82
83 // now this needs to be public because sub-components needs to use. This is something to improve.
84 template <typename EventType>
85 void postEvent();
86
87protected:
88 // it is called after the client initialization, provides information about the orthogonal it is located in
89 template <typename TOrthogonal, typename TSourceObject>
91 {
92 }
93
94 // components
95 std::map<ComponentKey, std::shared_ptr<smacc2::ISmaccComponent>> components_;
96
97 template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
99
100 template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
101 SmaccComponentType * createNamedComponent(std::string name, TArgs... targs);
102
103 rclcpp::Node::SharedPtr getNode();
104
105 inline rclcpp::Logger getLogger() { return getNode()->get_logger(); }
106
107private:
108 // A reference to the state machine object that owns this resource
111
112 // friend method called by orthogonal
113 void initialize();
114
115 // friend method called by orthogonal
116 // Assigns the owner of this resource to the given state machine parameter object
118
119 // friend method called by orthogonal
121
122 friend class ISmaccOrthogonal;
123 friend class ISmaccComponent;
124};
125} // 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_
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
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
bool operator<(const ComponentKey &other) const