SMACC2
smacc_orthogonal_impl.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
21#pragma once
22#include <cassert>
25
26namespace smacc2
27{
28template <typename SmaccClientType>
29bool ISmaccOrthogonal::requiresClient(SmaccClientType *& storage)
30{
31 for (auto & client : clients_)
32 {
33 storage = dynamic_cast<SmaccClientType *>(client.get());
34 if (storage != nullptr) return true;
35 }
36
37 auto requiredClientName = demangledTypeName<SmaccClientType>();
38 RCLCPP_WARN_STREAM(
39 getLogger(), "Required client ["
40 << requiredClientName
41 << "] not found in current orthogonal. Searching in other orthogonals.");
42
43 for (auto & orthoentry : this->getStateMachine()->getOrthogonals())
44 {
45 for (auto & client : orthoentry.second->getClients())
46 {
47 storage = dynamic_cast<SmaccClientType *>(client.get());
48 if (storage != nullptr)
49 {
50 RCLCPP_WARN_STREAM(
51 getLogger(),
52 "Required client [" << requiredClientName << "] found in other orthogonal.");
53 return true;
54 }
55 }
56 }
57
58 RCLCPP_ERROR_STREAM(
59 getLogger(), "Required client ["
60 << requiredClientName
61 << "] not found even in other orthogonals. Returning null pointer. If the "
62 "requested client is used may result in a segmentation fault.");
63 return false;
64}
65
66template <typename SmaccComponentType>
67void ISmaccOrthogonal::requiresComponent(SmaccComponentType *& storage)
68{
69 if (stateMachine_ == nullptr)
70 {
71 RCLCPP_ERROR(
72 getLogger(),
73 "Cannot use the requiresComponent funcionality from an orthogonal before onInitialize");
74 }
75 else
76 {
78 }
79}
80
81template <typename TOrthogonal, typename TClient>
83{
84 client->setStateMachine(getStateMachine());
85 client->setOrthogonal(this);
86
87 client->template onOrthogonalAllocation<TOrthogonal, TClient>();
88}
89
90template <typename TClientBehavior>
92{
93 for (auto & cb : this->clientBehaviors_)
94 {
95 auto * ret = dynamic_cast<TClientBehavior *>(cb.get());
96 if (ret != nullptr)
97 {
98 return ret;
99 }
100 }
101
102 return nullptr;
103}
104
105inline const std::vector<std::shared_ptr<smacc2::ISmaccClient>> & ISmaccOrthogonal::getClients()
106{
107 return clients_;
108}
109
110inline const std::vector<std::shared_ptr<smacc2::ISmaccClientBehavior>> &
112{
113 return this->clientBehaviors_;
114}
115
116template <typename T>
117void ISmaccOrthogonal::setGlobalSMData(std::string name, T value)
118{
119 this->getStateMachine()->setGlobalSMData(name, value);
120}
121
122template <typename T>
123bool ISmaccOrthogonal::getGlobalSMData(std::string name, T & ret)
124{
125 return this->getStateMachine()->getGlobalSMData(name, ret);
126}
127
128//inline
130
131template <typename TOrthogonal, typename TClient>
132class ClientHandler : public TClient
133{
134public:
135 template <typename... TArgs>
137 {
138 }
139
141
142 template <typename SmaccComponentType, typename... TArgs>
143 SmaccComponentType * createComponent(TArgs... targs)
144 {
145 return ISmaccClient::createComponent<SmaccComponentType, TOrthogonal, TClient, TArgs...>(
146 targs...);
147 }
148
149 template <typename SmaccComponentType, typename... TArgs>
150 SmaccComponentType * createNamedComponent(std::string name, TArgs... targs)
151 {
152 return ISmaccClient::createNamedComponent<SmaccComponentType, TOrthogonal, TClient, TArgs...>(
153 name, targs...);
154 }
155
157 {
158 return smacc2::introspection::TypeInfo::getTypeInfoFromType<TClient>();
159 }
160};
161
162template <typename TOrthogonal>
164{
165public:
166 template <typename TClient, typename... TArgs>
167 std::shared_ptr<ClientHandler<TOrthogonal, TClient>> createClient(TArgs... args)
168 {
169 //static_assert(std::is_base_of<ISmaccOrthogonal, TOrthogonal>::value, "The object Tag must be the orthogonal type where the client was created");
170 // if (typeid(*this) != typeid(TOrthogonal))
171 // {
172 // RCLCPP_ERROR_STREAM(getLogger(),"Error creating client. The object Tag must be the type of the orthogonal where the client was created:" << demangleType(typeid(*this)) << ". The object creation was skipped and a nullptr was returned");
173 // return nullptr;
174 // }
175
176 RCLCPP_INFO(
177 getLogger(), "[%s] creating client object, type:'%s' object tag: '%s'",
178 demangleType(typeid(*this)).c_str(), demangledTypeName<TClient>().c_str(),
179 demangledTypeName<TOrthogonal>().c_str());
180
181 auto client = std::make_shared<ClientHandler<TOrthogonal, TClient>>(args...);
182 this->template assignClientToOrthogonal<TOrthogonal, TClient>(client.get());
183
184 // it is stored the client (not the client handler)
185 clients_.push_back(client);
186
187 return client;
188 }
189};
190} // namespace smacc2
smacc2::introspection::TypeInfo::Ptr getType() override
SmaccComponentType * createComponent(TArgs... targs)
SmaccComponentType * createNamedComponent(std::string name, TArgs... targs)
SmaccComponentType * createNamedComponent(std::string name, TArgs... targs)
SmaccComponentType * createComponent(TArgs... targs)
std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > clientBehaviors_
ISmaccStateMachine * getStateMachine()
const std::vector< std::shared_ptr< smacc2::ISmaccClientBehavior > > & getClientBehaviors() const
TClientBehavior * getClientBehavior()
void setGlobalSMData(std::string name, T value)
bool getGlobalSMData(std::string name, T &ret)
void requiresComponent(SmaccComponentType *&storage)
std::vector< std::shared_ptr< smacc2::ISmaccClient > > clients_
ISmaccStateMachine * stateMachine_
bool requiresClient(SmaccClientType *&storage)
void assignClientToOrthogonal(TClient *client)
const std::vector< std::shared_ptr< smacc2::ISmaccClient > > & getClients()
bool getGlobalSMData(std::string name, T &ret)
void setGlobalSMData(std::string name, T value)
void requiresComponent(SmaccComponentType *&storage)
std::shared_ptr< ClientHandler< TOrthogonal, TClient > > createClient(TArgs... args)
std::shared_ptr< TypeInfo > Ptr
std::string demangleType(const std::type_info *tinfo)