SMACC2
Loading...
Searching...
No Matches
smacc_client_impl.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
25
26namespace smacc2
27{
28template <typename EventType>
29void ISmaccClient::postEvent(const EventType & ev)
30{
32}
33
34template <typename EventType>
36{
37 stateMachine_->postEvent<EventType>();
38}
39
40template <typename TComponent>
42{
43 return this->getComponent<TComponent>(std::string());
44}
45
46template <typename TComponent>
47TComponent * ISmaccClient::getComponent(int index)
48{
49 int count = 0;
50 for (auto & component : components_)
51 {
52 auto * tcomponent = dynamic_cast<TComponent *>(component.second.get());
53 if (tcomponent != nullptr)
54 {
55 if (count == index)
56 {
57 return tcomponent;
58 }
59 else
60 {
61 count++;
62 continue;
63 }
64 }
65 }
66
67 return nullptr;
68}
69
70template <typename TComponent>
71TComponent * ISmaccClient::getComponent(const std::string & name)
72{
73 for (auto & component : components_)
74 {
75 if (!name.empty() && component.first.name != name) continue;
76
77 auto * tcomponent = dynamic_cast<TComponent *>(component.second.get());
78 if (tcomponent != nullptr)
79 {
80 return tcomponent;
81 }
82 }
83
84 return nullptr;
85}
86
87//inline
89
90template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
92 const std::string & name, const TArgs &... targs)
93{
94 ComponentKey componentkey(&typeid(SmaccComponentType), name);
95
96 std::shared_ptr<SmaccComponentType> ret;
97
98 auto it = this->components_.find(componentkey);
99
100 if (it == this->components_.end())
101 {
103 RCLCPP_INFO(
104 getLogger(), "Creating a new component of type: %s, required by client '%s'.", tname.c_str(),
105 this->getName().c_str());
106
107 ret = std::make_shared<SmaccComponentType>(targs...);
108 ret->setStateMachine(this->getStateMachine());
109 ret->initialize(this);
110
112
113 this->components_[componentkey] = ret;
114 RCLCPP_DEBUG(getLogger(), "%s resource is required. Done.", tname.c_str());
115 }
116 else
117 {
118 RCLCPP_INFO(
119 getLogger(), "%s resource is required. Found resource in cache.",
121 ret = dynamic_pointer_cast<SmaccComponentType>(it->second);
122 }
123
125
126 return ret.get();
127}
128
129template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
130SmaccComponentType * ISmaccClient::createComponent(const TArgs &... targs)
131{
133 std::string(), targs...);
134}
135
136template <typename TSmaccSignal, typename T>
137void ISmaccClient::connectSignal(TSmaccSignal & signal, void (T::*callback)(), T * object)
138{
139 return this->getStateMachine()->createSignalConnection(signal, callback, object);
140}
141
142template <typename SmaccClientType>
143void ISmaccClient::requiresClient(SmaccClientType *& storage)
144{
145 this->orthogonal_->requiresClient(storage);
146}
147
148} // namespace smacc2
ISmaccStateMachine * stateMachine_
ISmaccStateMachine * getStateMachine()
std::map< ComponentKey, std::shared_ptr< smacc2::ISmaccComponent > > components_
rclcpp::Logger getLogger()
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)
void requiresClient(SmaccClientType *&storage)
bool requiresClient(SmaccClientType *&storage)
smacc2::SmaccSignalConnection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)
std::string demangledTypeName()