35 #ifndef INCLUDE_NRT_CORE_MODEL_DETAILS_MANAGERIMPL_H
36 #define INCLUDE_NRT_CORE_MODEL_DETAILS_MANAGERIMPL_H
41 template <
class Comp>
inline
47 static_assert(std::is_base_of<nrt::Component, Comp>::value,
"Comp must derive from nrt::Component");
48 static_assert( ! std::is_base_of<nrt::ModuleBase, Comp>::value,
"Use nrt::Blackboard::addModule() for top-level "
49 "objects that derive from nrt::Module");
52 std::shared_ptr<Comp> subComp(
new Comp(computeInstanceName(instanceName)));
56 boost::upgrade_lock<boost::shared_mutex> uplck(itsSubMtx);
58 std::string
const inst = subComp->meta().instanceName;
60 for (std::shared_ptr<nrt::Component>
const & c : itsSubComponents)
61 if (inst == c->meta().instanceName)
62 NRT_FATAL(
"Cannot add two components with identical instanceNames [" << inst <<
']');
64 NRT_MODDEBUG(
"Adding Component [" << subComp->meta().str() <<
']');
67 boost::upgrade_to_unique_lock<boost::shared_mutex> ulck(uplck);
68 itsSubComponents.push_back(subComp);
69 subComp->itsParent =
this;
74 if (itsInitialized) subComp->init();
75 if (itsStarted) subComp->start();
76 if (itsRunning) subComp->launch();
82 template <
class Comp>
inline
88 static_assert(std::is_base_of<nrt::Component, Comp>::value,
"Comp must derive from nrt::Component");
89 static_assert( ! std::is_base_of<nrt::ModuleBase, Comp>::value,
"Use nrt::Blackboard::getModule() for top-level "
90 "objects that derive from nrt::Module");
92 boost::shared_lock<boost::shared_mutex> lck(itsSubMtx);
94 for (std::shared_ptr<nrt::Component> c : itsSubComponents)
95 if (instanceName == c->meta().instanceName)
97 std::shared_ptr<Comp> ret = std::dynamic_pointer_cast<Comp>(c);
99 NRT_FATAL(
"SubComponent [" << instanceName <<
"] is not of type [" << nrt::demangledName<Comp>() <<
']');
102 NRT_FATAL(
"SubComponent [" << instanceName <<
"] not found");
110 std::shared_ptr<Component> Manager::getComponent<Component>(std::string
const & instanceName)
const
112 boost::shared_lock<boost::shared_mutex> lck(itsSubMtx);
114 for (std::shared_ptr<Component> c : itsSubComponents)
if (instanceName == c->meta().instanceName)
return c;
116 NRT_FATAL(
"Component [" << instanceName <<
"] not found");
121 template <
class Comp>
126 static_assert(std::is_base_of<nrt::Component, Comp>::value,
"Comp must derive from nrt::Component");
130 boost::upgrade_lock<boost::shared_mutex> uplck(itsSubMtx);
132 for (
auto itr = itsSubComponents.begin(); itr != itsSubComponents.end(); ++itr)
133 if (itr->get() == component.get())
135 NRT_MODDEBUG(
"Removing Component [" << component->meta().str() <<
']');
137 boost::upgrade_to_unique_lock<boost::shared_mutex> ulck(uplck);
138 component->prepareForDeletion();
139 itsSubComponents.erase(itr);
141 if (component.use_count() > 1)
142 NRT_WARNING(component.use_count() - 1 <<
" additional external shared_ptr references exist to "
143 "Component [" << component->meta().str() <<
"]. Component was NOT deleted.");
148 NRT_WARNING(
"Component [" << component->meta().str() <<
"] not found. Ignored.");
151 #endif // INCLUDE_NRT_CORE_MODEL_DETAILS_MANAGERIMPL_H