SMACC2
smacc_client_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
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(std::string name)
72{
73 for (auto & component : components_)
74 {
75 if (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>
91SmaccComponentType * ISmaccClient::createNamedComponent(std::string name, TArgs... targs)
92{
93 ComponentKey componentkey(&typeid(SmaccComponentType), name);
94
95 std::shared_ptr<SmaccComponentType> ret;
96
97 auto it = this->components_.find(componentkey);
98
99 if (it == this->components_.end())
100 {
101 auto tname = demangledTypeName<SmaccComponentType>();
102 RCLCPP_INFO(
103 getLogger(),
104 "Creating a new component of type %s smacc component is required by client '%s'. Creating a "
105 "new instance %s",
106 this->getName().c_str(), demangledTypeName<SmaccComponentType>().c_str(), tname.c_str());
107
108 ret = std::shared_ptr<SmaccComponentType>(new SmaccComponentType(targs...));
109 ret->setStateMachine(this->getStateMachine());
110 ret->owner_ = this;
111 ret->initialize(this);
112
113 this->components_[componentkey] =
114 ret; //std::dynamic_pointer_cast<smacc2::ISmaccComponent>(ret);
115 RCLCPP_DEBUG(getLogger(), "%s resource is required. Done.", tname.c_str());
116 }
117 else
118 {
119 RCLCPP_INFO(
120 getLogger(), "%s resource is required. Found resource in cache.",
121 demangledTypeName<SmaccComponentType>().c_str());
122 ret = dynamic_pointer_cast<SmaccComponentType>(it->second);
123 }
124
125 ret->template onOrthogonalAllocation<TOrthogonal, TClient>();
126
127 return ret.get();
128}
129
130template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
131SmaccComponentType * ISmaccClient::createComponent(TArgs... targs)
132{
133 return this->createNamedComponent<SmaccComponentType, TOrthogonal, TClient>(
134 std::string(), targs...);
135}
136
137template <typename TSmaccSignal, typename T>
138void ISmaccClient::connectSignal(TSmaccSignal & signal, void (T::*callback)(), T * object)
139{
140 return this->getStateMachine()->createSignalConnection(signal, callback, object);
141}
142
143template <typename SmaccClientType>
144void ISmaccClient::requiresClient(SmaccClientType *& storage)
145{
146 this->orthogonal_->requiresClient(storage);
147}
148
149} // namespace smacc2
ISmaccStateMachine * stateMachine_
ISmaccStateMachine * getStateMachine()
std::map< ComponentKey, std::shared_ptr< smacc2::ISmaccComponent > > components_
virtual std::string getName() const
Definition: client.cpp:42
SmaccComponentType * createNamedComponent(std::string name, TArgs... targs)
rclcpp::Logger getLogger()
SmaccComponentType * createComponent(TArgs... targs)
void connectSignal(TSmaccSignal &signal, void(T::*callback)(), T *object)
ISmaccOrthogonal * orthogonal_
void requiresClient(SmaccClientType *&storage)
TComponent * getComponent()
bool requiresClient(SmaccClientType *&storage)
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)
void callback(const image_tools::ROSCvMatContainer &img)