SMACC
Loading...
Searching...
No Matches
smacc_client_impl.h
Go to the documentation of this file.
1/*****************************************************************************************************************
2 * ReelRobotix Inc. - Software License Agreement Copyright (c) 2018
3 * Authors: Pablo Inigo Blasco, Brett Aldrich
4 *
5 ******************************************************************************************************************/
6
7#pragma once
8
11
12namespace smacc
13{
14 template <typename EventType>
15 void ISmaccClient::postEvent(const EventType &ev)
16 {
18 }
19
20 template <typename EventType>
22 {
23 stateMachine_->postEvent<EventType>();
24 }
25
26 template <typename TComponent>
28 {
29 return this->getComponent<TComponent>(std::string());
30 }
31
32 template <typename TComponent>
33 TComponent *ISmaccClient::getComponent(std::string name)
34 {
35 for (auto &component : components_)
36 {
37 if (component.first.name != name)
38 continue;
39
40 auto *tcomponent = dynamic_cast<TComponent *>(component.second.get());
41 if (tcomponent != nullptr)
42 {
43 return tcomponent;
44 }
45 }
46
47 return nullptr;
48 }
49
50 //inline
52 {
53 return this->stateMachine_;
54 }
55
56 template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
57 SmaccComponentType *ISmaccClient::createNamedComponent(std::string name, TArgs... targs)
58 {
59 ComponentKey componentkey(&typeid(SmaccComponentType), name);
60
61 std::shared_ptr<SmaccComponentType> ret;
62
63 auto it = this->components_.find(componentkey);
64
65 if (it == this->components_.end())
66 {
67 auto tname = demangledTypeName<SmaccComponentType>();
68 ROS_DEBUG("%s smacc component is required. Creating a new instance.", tname.c_str());
69
70 ret = std::shared_ptr<SmaccComponentType>(new SmaccComponentType(targs...));
71 ret->setStateMachine(this->getStateMachine());
72 ret->owner_ = this;
73 ret->initialize(this);
74
75 this->components_[componentkey] = ret; //std::dynamic_pointer_cast<smacc::ISmaccComponent>(ret);
76 ROS_DEBUG("%s resource is required. Done.", tname.c_str());
77 }
78 else
79 {
80 ROS_DEBUG("%s resource is required. Found resource in cache.", demangledTypeName<SmaccComponentType>().c_str());
81 ret = dynamic_pointer_cast<SmaccComponentType>(it->second);
82 }
83
84 ret->template onOrthogonalAllocation<TOrthogonal, TClient>();
85
86 return ret.get();
87 }
88
89 template <typename SmaccComponentType, typename TOrthogonal, typename TClient, typename... TArgs>
90 SmaccComponentType *ISmaccClient::createComponent(TArgs... targs)
91 {
92 return this->createNamedComponent<SmaccComponentType, TOrthogonal, TClient>(std::string(), targs...);
93 }
94
95 template <typename TSmaccSignal, typename T>
96 void ISmaccClient::connectSignal(TSmaccSignal &signal, void (T::*callback)(), T *object)
97 {
98 return this->getStateMachine()->createSignalConnection(signal, callback, object);
99 }
100
101 template <typename SmaccClientType>
102 void ISmaccClient::requiresClient(SmaccClientType *&storage)
103 {
104 this->orthogonal_->requiresClient(storage);
105 }
106
107} // namespace smacc
ISmaccOrthogonal * orthogonal_
Definition: smacc_client.h:94
SmaccComponentType * createComponent(TArgs... targs)
void requiresClient(SmaccClientType *&storage)
ISmaccStateMachine * stateMachine_
Definition: smacc_client.h:93
std::map< ComponentKey, std::shared_ptr< smacc::ISmaccComponent > > components_
Definition: smacc_client.h:78
void connectSignal(TSmaccSignal &signal, void(T::*callback)(), T *object)
TComponent * getComponent()
ISmaccStateMachine * getStateMachine()
SmaccComponentType * createNamedComponent(std::string name, TArgs... targs)
bool requiresClient(SmaccClientType *&storage)
boost::signals2::connection createSignalConnection(TSmaccSignal &signal, TMemberFunctionPrototype callback, TSmaccObjectType *object)
void postEvent(EventType *ev, EventLifeTime evlifetime=EventLifeTime::ABSOLUTE)