SMACC2
Loading...
Searching...
No Matches
smacc_component_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
23#include <string>
24
25#include <smacc2/component.hpp>
27
28namespace smacc2
29{
30template <typename EventType>
31void ISmaccComponent::postEvent(const EventType & ev)
32{
34}
35
36template <typename EventType>
38{
39 auto ev = new EventType();
41}
42
43template <typename TComponent>
45 TComponent *& requiredComponentStorage, bool throwExceptionIfNotExist)
46{
48 requiredComponentStorage,
49 throwExceptionIfNotExist ? ComponentRequirement::HARD : ComponentRequirement::SOFT);
50}
51
52template <typename TComponent>
54 std::string name, TComponent *& requiredComponentStorage, bool throwExceptionIfNotExist)
55{
57 name, requiredComponentStorage,
58 throwExceptionIfNotExist ? ComponentRequirement::HARD : ComponentRequirement::SOFT);
59}
60
61template <typename TComponent>
63 TComponent *& requiredComponentStorage, ComponentRequirement requirementType)
64{
65 requiredComponentStorage = this->owner_->getComponent<TComponent>();
66
67 if (requiredComponentStorage == nullptr && requirementType == ComponentRequirement::HARD)
68 {
69 RCLCPP_DEBUG_STREAM(
70 this->getLogger(), std::string("Required component ") +
71 demangleSymbol(typeid(TComponent).name()) +
72 " not found. Available components:");
73
74 std::vector<std::shared_ptr<ISmaccComponent>> components;
75 this->owner_->getComponents(components);
76
77 for (auto c : components)
78 {
79 RCLCPP_DEBUG(this->getLogger(), "- Component %s", c->getName().c_str());
80 }
81
82 throw std::runtime_error(
83 std::string("Component ") + demangleSymbol(typeid(TComponent).name()) + " not found");
84 }
85}
86
87template <typename TComponent>
89 std::string name, TComponent *& requiredComponentStorage, ComponentRequirement requirementType)
90{
91 requiredComponentStorage = this->owner_->getComponent<TComponent>(name);
92
93 if (requiredComponentStorage == nullptr && requirementType == ComponentRequirement::HARD)
94 {
95 RCLCPP_DEBUG_STREAM(
96 this->getLogger(), std::string("Required component with name: '") + name + "'" +
97 demangleSymbol(typeid(TComponent).name()) +
98 " not found. Available components:");
99
100 std::vector<std::shared_ptr<ISmaccComponent>> components;
101 this->owner_->getComponents(components);
102
103 for (auto c : components)
104 {
105 RCLCPP_DEBUG(this->getLogger(), " - Component %s", c->getName().c_str());
106 }
107
108 throw std::runtime_error(
109 std::string("Component ") + demangleSymbol(typeid(TComponent).name()) +
110 std::string(" not found"));
111 }
112}
113
114template <typename TClient>
115void ISmaccComponent::requiresClient(TClient *& requiredClientStorage)
116{
117 this->owner_->requiresClient(requiredClientStorage);
118}
119
120template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
121SmaccComponentType * ISmaccComponent::createSiblingComponent(TArgs... targs)
122{
123 return this->owner_->createComponent<SmaccComponentType, TOrthogonal, TClient>(targs...);
124}
125
126template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
127SmaccComponentType * ISmaccComponent::createSiblingNamedComponent(std::string name, TArgs... targs)
128{
129 return this->owner_->createNamedComponent<SmaccComponentType, TOrthogonal, TClient>(
130 name, targs...);
131}
132
133template <typename TOrthogonal, typename TClient>
137
138} // namespace smacc2
SmaccComponentType * createNamedComponent(std::string name, TArgs... targs)
SmaccComponentType * createComponent(TArgs... targs)
void requiresClient(SmaccClientType *&storage)
void getComponents(std::vector< std::shared_ptr< ISmaccComponent > > &components)
Definition client.cpp:34
ISmaccClient * owner_
void requiresClient(TClient *&requiredClientStorage)
SmaccComponentType * createSiblingComponent(TArgs... targs)
SmaccComponentType * createSiblingNamedComponent(std::string name, TArgs... targs)
rclcpp::Logger getLogger() const
ISmaccStateMachine * stateMachine_
void requiresComponent(TComponent *&requiredComponentStorage, bool throwExceptionIfNotExist)
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)
std::string demangleSymbol()
ComponentRequirement
Definition common.hpp:75