From 6f687e14b44d55078fdd83e9847449cba6443a77 Mon Sep 17 00:00:00 2001 From: Damien Marchal Date: Tue, 25 Aug 2020 16:50:14 +0200 Subject: [PATCH] =?UTF-8?q?[SofaKernel]=C2=A0Implement=20an=20update=20mec?= =?UTF-8?q?hanism=20on=20component:=20RequiredPlugin?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../modules/SofaBaseUtils/RequiredPlugin.cpp | 54 +++++++++++++------ .../modules/SofaBaseUtils/RequiredPlugin.h | 4 +- 2 files changed, 42 insertions(+), 16 deletions(-) diff --git a/SofaKernel/modules/SofaBaseUtils/RequiredPlugin.cpp b/SofaKernel/modules/SofaBaseUtils/RequiredPlugin.cpp index 0930a5ef09b..4df79cff6e0 100644 --- a/SofaKernel/modules/SofaBaseUtils/RequiredPlugin.cpp +++ b/SofaKernel/modules/SofaBaseUtils/RequiredPlugin.cpp @@ -50,18 +50,39 @@ RequiredPlugin::RequiredPlugin() , d_stopAfterFirstSuffixFound( initData(&d_stopAfterFirstSuffixFound , true, "stopAfterFirstSuffixFound", "For each plugin name, stop after the first suffix that is loaded successfully")) , d_requireOne ( initData(&d_requireOne , false, "requireOne", "Display an error message if no plugin names were successfully loaded")) , d_requireAll ( initData(&d_requireAll , true, "requireAll", "Display an error message if any plugin names failed to be loaded")) + , d_loadedPlugins(initData(&d_loadedPlugins, "loadedPlugins", "List of the plugins that are have been loaded.")) { this->f_printLog.setValue(true); // print log by default, to identify which pluging is responsible in case of a crash during loading + + /// Add a callback to update the required plugin when its data are changed + addUpdateCallback("reloadPlugins", {&name, + &d_pluginName, &d_suffixMap, &d_stopAfterFirstNameFound, &d_stopAfterFirstSuffixFound, + &d_requireAll, &d_requireOne}, + [this](const sofa::core::DataTracker&) + { + clearLoggedMessages(); + /// Reload the plugins and check at least one is loaded. + if(loadPlugin()) + return sofa::core::objectmodel::ComponentState::Valid; + return sofa::core::objectmodel::ComponentState::Invalid; + }, {&d_loadedPlugins}); } void RequiredPlugin::parse(sofa::core::objectmodel::BaseObjectDescription* arg) { + d_componentState = sofa::core::objectmodel::ComponentState::Invalid; + Inherit1::parse(arg); - loadPlugin(); + if(loadPlugin()) + d_componentState = sofa::core::objectmodel::ComponentState::Valid; } -void RequiredPlugin::loadPlugin() +bool RequiredPlugin::loadPlugin() { + /// Get a write accessor to the loadedPlugin + auto loadedPlugins = sofa::helper::getWriteOnlyAccessor(d_loadedPlugins); + loadedPlugins.clear(); + sofa::helper::system::PluginManager* pluginManager = &sofa::helper::system::PluginManager::getInstance(); std::string defaultSuffix = pluginManager->getDefaultSuffix(); const helper::vector >& sMap = d_suffixMap.getValue(); @@ -80,34 +101,33 @@ void RequiredPlugin::loadPlugin() if (suffixVec.empty()) suffixVec.push_back(defaultSuffix); + /// Copy the lost of names provided as arguments + const helper::vector& nameVec = d_pluginName.getValue(); + helper::vector pluginsToLoad = nameVec; + /// In case the pluginName is not set we copy the provided name into the set to load. if(!d_pluginName.isSet() && name.isSet()) { - helper::WriteOnlyAccessor>> pluginsName = d_pluginName ; - pluginsName.push_back(this->getName()); + pluginsToLoad.push_back(this->getName()); } - const helper::vector& nameVec = d_pluginName.getValue(); - helper::vector nameVecCopy = nameVec; - - helper::vector< std::string > loaded; helper::vector< std::string > failed; std::ostringstream errmsg; - for (std::size_t nameIndex = 0; nameIndex < nameVecCopy.size(); ++nameIndex) + for (auto& pluginName : pluginsToLoad) { - const std::string& name = FileSystem::cleanPath( nameVecCopy[nameIndex] ); // name is not necessarily a path + const std::string& name = FileSystem::cleanPath( pluginName ); // name is not necessarily a path bool nameLoaded = false; - for (std::size_t suffixIndex = 0; suffixIndex < suffixVec.size(); ++suffixIndex) + for (auto& suffix : suffixVec) { - const std::string& suffix = suffixVec[suffixIndex]; if ( pluginManager->pluginIsLoaded(name) ) { + loadedPlugins.push_back(name); nameLoaded = true; if (d_stopAfterFirstSuffixFound.getValue()) break; } else if ( pluginManager->loadPlugin(name, suffix, true, true, &errmsg) ) { - loaded.push_back(name); + loadedPlugins.push_back(name); nameLoaded = true; if (d_stopAfterFirstSuffixFound.getValue()) break; } @@ -121,20 +141,24 @@ void RequiredPlugin::loadPlugin() break; } } + + bool hasFailed=false; if (!failed.empty()) { - if ((d_requireAll.getValue() || (d_requireOne.getValue() && loaded.empty()))) + if ((d_requireAll.getValue() || (d_requireOne.getValue() && loadedPlugins.empty()))) { + hasFailed = true; msg_error() << errmsg.str() << msgendl << "Failed to load: " << failed ; } else { msg_warning() << errmsg.str() << msgendl - << "Failed to load optional: " << failed; + << "Unable to load optional: " << failed; } } pluginManager->init(); + return !hasFailed; } } // namespace misc diff --git a/SofaKernel/modules/SofaBaseUtils/RequiredPlugin.h b/SofaKernel/modules/SofaBaseUtils/RequiredPlugin.h index b4d2b352d4b..25852a83ccc 100644 --- a/SofaKernel/modules/SofaBaseUtils/RequiredPlugin.h +++ b/SofaKernel/modules/SofaBaseUtils/RequiredPlugin.h @@ -56,6 +56,8 @@ class SOFA_BASE_UTILS_API RequiredPlugin : public core::objectmodel::BaseObject sofa::core::objectmodel::Data d_requireOne; ///< Display an error message if no plugin names were successfully loaded sofa::core::objectmodel::Data d_requireAll; ///< Display an error message if any plugin names failed to be loaded + sofa::core::objectmodel::Data > d_loadedPlugins; ///< name of the loaded plugins + protected: RequiredPlugin(); ~RequiredPlugin() override {} @@ -64,7 +66,7 @@ class SOFA_BASE_UTILS_API RequiredPlugin : public core::objectmodel::BaseObject void parse(sofa::core::objectmodel::BaseObjectDescription* arg) override; - void loadPlugin(); + bool loadPlugin(); };