From 9823322ebbc354abdc12c998de062542da86b04b Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Wed, 15 Jan 2025 14:38:40 +0100 Subject: [PATCH] [all] use std::views::keys/values --- .../contact/RuleBasedContactManager.cpp | 6 +- .../lagrangian/solver/LCPConstraintSolver.cpp | 10 ++-- .../linearsolver/direct/EigenSolverFactory.h | 11 ++-- .../statecontainer/MechanicalObject.inl | 6 +- Sofa/GL/src/sofa/gl/GLSLShader.cpp | 8 ++- .../Core/src/sofa/core/ObjectFactory.cpp | 56 +++++++++---------- .../core/collision/NarrowPhaseDetection.cpp | 6 +- .../src/sofa/core/objectmodel/BaseObject.cpp | 19 +++---- .../src/sofa/helper/system/PluginManager.cpp | 14 +++-- .../Helper/test/system/PluginManager_test.cpp | 5 +- .../sofa/simulation/TaskSchedulerFactory.cpp | 4 +- .../sofa/simulation/TaskSchedulerRegistry.cpp | 6 +- Sofa/framework/Type/src/sofa/type/vector_T.h | 13 ++++- .../SceneCheckMissingRequiredPlugin.cpp | 10 ++-- applications/projects/runSofa/Main.cpp | 3 +- 15 files changed, 98 insertions(+), 79 deletions(-) diff --git a/Sofa/Component/Collision/Response/Contact/src/sofa/component/collision/response/contact/RuleBasedContactManager.cpp b/Sofa/Component/Collision/Response/Contact/src/sofa/component/collision/response/contact/RuleBasedContactManager.cpp index 444c275cee4..7a2c6cc412c 100644 --- a/Sofa/Component/Collision/Response/Contact/src/sofa/component/collision/response/contact/RuleBasedContactManager.cpp +++ b/Sofa/Component/Collision/Response/Contact/src/sofa/component/collision/response/contact/RuleBasedContactManager.cpp @@ -24,6 +24,8 @@ #include #include +#include + namespace sofa::component::collision::response::contact { @@ -45,9 +47,9 @@ RuleBasedContactManager::RuleBasedContactManager() RuleBasedContactManager::~RuleBasedContactManager() { - for(const auto& d : variablesData) + for(const auto& d : variablesData | std::views::values) { - delete d.second; + delete d; } } diff --git a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp index 18dc8b33258..efc37a5b3dc 100644 --- a/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp +++ b/Sofa/Component/Constraint/Lagrangian/Solver/src/sofa/component/constraint/lagrangian/solver/LCPConstraintSolver.cpp @@ -37,6 +37,8 @@ using sofa::simulation::mechanicalvisitor::MechanicalGetConstraintInfoVisitor; #include using sofa::simulation::mechanicalvisitor::MechanicalVOpVisitor; +#include + using sofa::core::VecId; namespace sofa::component::constraint::lagrangian::solver @@ -709,12 +711,12 @@ void LCPConstraintSolver::keepContactForcesValue() for (unsigned int c=0; c<_numConstraints; ++c) _previousForces[c] = (*_result)[c]; // clear previous history - for (auto& previousConstraint : _previousConstraints) + for (auto& buf : _previousConstraints | std::views::values) { - ConstraintBlockBuf& buf = previousConstraint.second; - for (auto& it2 : buf.persistentToConstraintIdMap) - it2.second = -1; + for (auto& constraintId : buf.persistentToConstraintIdMap | std::views::values) + constraintId = -1; } + // fill info from current ids for (const ConstraintBlockInfo& info : constraintBlockInfo) { diff --git a/Sofa/Component/LinearSolver/Direct/src/sofa/component/linearsolver/direct/EigenSolverFactory.h b/Sofa/Component/LinearSolver/Direct/src/sofa/component/linearsolver/direct/EigenSolverFactory.h index a0076cf817b..7eb2579437e 100644 --- a/Sofa/Component/LinearSolver/Direct/src/sofa/component/linearsolver/direct/EigenSolverFactory.h +++ b/Sofa/Component/LinearSolver/Direct/src/sofa/component/linearsolver/direct/EigenSolverFactory.h @@ -32,6 +32,8 @@ #include #include +#include + namespace sofa::component::linearsolver::direct { @@ -195,13 +197,8 @@ class SOFA_COMPONENT_LINEARSOLVER_DIRECT_API EigenSolverFactory [[nodiscard]] sofa::type::vector registeredObjects() const { - sofa::type::vector list; - list.reserve(m_registeredTypes.size()); - for(const auto& [key, _] : m_registeredTypes) - { - list.push_back(key); - } - return list; + auto list = m_registeredTypes | std::views::keys; + return {list.begin(), list.end()}; } private: diff --git a/Sofa/Component/StateContainer/src/sofa/component/statecontainer/MechanicalObject.inl b/Sofa/Component/StateContainer/src/sofa/component/statecontainer/MechanicalObject.inl index 919a37297e2..c39ebc8a908 100644 --- a/Sofa/Component/StateContainer/src/sofa/component/statecontainer/MechanicalObject.inl +++ b/Sofa/Component/StateContainer/src/sofa/component/statecontainer/MechanicalObject.inl @@ -42,6 +42,8 @@ #include #include +#include + namespace { @@ -1003,8 +1005,8 @@ void MechanicalObject::init() // Print a warning if one or more vector don't match the maximum size bool allSizeAreEqual = true; - for (const std::pair& vector_size : vector_sizes) { - const Size& size = vector_size.second; + for (const auto& size : vector_sizes | std::views::values) + { if (size > 1 && size != maxSize) { allSizeAreEqual = false; break; diff --git a/Sofa/GL/src/sofa/gl/GLSLShader.cpp b/Sofa/GL/src/sofa/gl/GLSLShader.cpp index aa1b1e5272d..bc842f3c0ab 100644 --- a/Sofa/GL/src/sofa/gl/GLSLShader.cpp +++ b/Sofa/GL/src/sofa/gl/GLSLShader.cpp @@ -29,6 +29,8 @@ #include +#include + namespace sofa::gl { @@ -140,10 +142,10 @@ void GLSLShader::SetShaderFileName(GLint target, const std::string& filename) void GLSLShader::forceReloadShaderFromFile(const std::string& filename) { - for(auto& fn : m_hShaderContents){ - if(fn.second.filename == filename) + for(auto& shaderContent : m_hShaderContents | std::views::values){ + if(shaderContent.filename == filename) { - fn.second.text = "" ; + shaderContent.text = "" ; } } } diff --git a/Sofa/framework/Core/src/sofa/core/ObjectFactory.cpp b/Sofa/framework/Core/src/sofa/core/ObjectFactory.cpp index 20402e7c25f..77e33ed62b0 100644 --- a/Sofa/framework/Core/src/sofa/core/ObjectFactory.cpp +++ b/Sofa/framework/Core/src/sofa/core/ObjectFactory.cpp @@ -28,6 +28,8 @@ #include #include +#include + namespace sofa::core { @@ -275,9 +277,9 @@ objectmodel::BaseObject::SPtr ObjectFactory::createObject(objectmodel::BaseConte { std::vector possibleNames; possibleNames.reserve(registry.size()); - for(auto& k : registry) + for(const auto& objectName : registry | std::views::keys) { - possibleNames.emplace_back(k.first); + possibleNames.emplace_back(objectName); } arg->logError("But the following object(s) exist:"); @@ -346,18 +348,17 @@ objectmodel::BaseObject::SPtr ObjectFactory::createObject(objectmodel::BaseConte /// The object has been created, but not with the template given by the user if (!usertemplatename.empty() && object->getTemplateName() != userresolved) { - std::vector templateList; - if (entry) - for (const auto& cr : entry->creatorMap) - templateList.push_back(cr.first); - std::stringstream ss; + std::string ss; bool isUserTemplateNameInTemplateList = false; - for(unsigned int i = 0; i < templateList.size(); ++i) + if (entry) { - ss << templateList[i]; - isUserTemplateNameInTemplateList |= (templateList[i] == usertemplatename || templateList[i] == userresolved); - if (i != templateList.size() - 1) - ss << ", "; + auto templateList = entry->creatorMap | std::views::keys; + isUserTemplateNameInTemplateList = std::ranges::find_if(templateList, [&](const auto& templateName) + { + return templateName == usertemplatename || templateName == userresolved; + }) != templateList.end(); + + ss = sofa::helper::join(templateList.begin(), templateList.end(), ","); } if (isUserTemplateNameInTemplateList) { @@ -369,7 +370,7 @@ objectmodel::BaseObject::SPtr ObjectFactory::createObject(objectmodel::BaseConte else { msg_error(object.get()) << "Requested template '" << usertemplatename << "' " - << "cannot be found in the list of available templates [" << ss.str() << "]. " + << "cannot be found in the list of available templates [" << ss << "]. " << "Falling back to the first compatible template: '" << object->getTemplateName() << "'."; } @@ -387,28 +388,25 @@ objectmodel::BaseObject::SPtr ObjectFactory::createObject(objectmodel::BaseConte m_callbackOnCreate(object.get(), arg); ///////////////////////// All this code is just there to implement the MakeDataAlias component. - std::vector todelete; - for(auto& kv : entry->m_dataAlias) - { - if(object->findData(kv.first)==nullptr) + auto aliasesToDeleteView = entry->m_dataAlias + | std::views::keys + | std::views::filter([&object](const auto& dataName) { - msg_warning(object.get()) << "The object '"<< (object->getClassName()) <<"' does not have an alias named '"<< kv.first <<"'. " - << "To remove this error message you need to use a valid data name for the 'dataname field'. "; - - todelete.push_back(kv.first); - } - } - - for(auto& todeletename : todelete) + return !object->findData(dataName); + }); + sofa::type::vector aliasesToDelete(std::ranges::begin(aliasesToDeleteView), std::ranges::end(aliasesToDeleteView)); + for (const auto& alias : aliasesToDelete) { - entry->m_dataAlias.erase( entry->m_dataAlias.find(todeletename) ) ; + msg_warning(object.get()) << "The object '"<< (object->getClassName()) <<"' does not have an alias named '"<< alias <<"'. " + << "To remove this error message you need to use a valid data name for the 'dataname field'. "; + entry->m_dataAlias.erase( entry->m_dataAlias.find(alias) ) ; } - for(auto& kv : entry->m_dataAlias) + for(auto& [dataName, dataAliases] : entry->m_dataAlias) { objectmodel::BaseObjectDescription newdesc; - for(std::string& alias : kv.second){ - object->addAlias(object->findData(kv.first), alias.c_str()) ; + for(std::string& alias : dataAliases){ + object->addAlias(object->findData(dataName), alias.c_str()) ; /// The Alias is used in the argument const std::string val(arg->getAttribute(alias)); diff --git a/Sofa/framework/Core/src/sofa/core/collision/NarrowPhaseDetection.cpp b/Sofa/framework/Core/src/sofa/core/collision/NarrowPhaseDetection.cpp index af9c8208d9d..82d87a5131e 100644 --- a/Sofa/framework/Core/src/sofa/core/collision/NarrowPhaseDetection.cpp +++ b/Sofa/framework/Core/src/sofa/core/collision/NarrowPhaseDetection.cpp @@ -22,15 +22,15 @@ #include #include +#include + namespace sofa::core::collision { NarrowPhaseDetection::~NarrowPhaseDetection() { - for (const auto& it : m_outputsMap) + for (DetectionOutputVector* do_vec : m_outputsMap | std::views::values) { - DetectionOutputVector* do_vec = it.second; - if (do_vec != nullptr) { do_vec->clear(); diff --git a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseObject.cpp b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseObject.cpp index 6cdd40f0692..477ecb3dc59 100644 --- a/Sofa/framework/Core/src/sofa/core/objectmodel/BaseObject.cpp +++ b/Sofa/framework/Core/src/sofa/core/objectmodel/BaseObject.cpp @@ -27,6 +27,7 @@ #include #include #include +#include namespace sofa::core::objectmodel @@ -276,9 +277,9 @@ void BaseObject::reinit() void BaseObject::updateInternal() { const auto& mapTrackedData = m_internalDataTracker.getMapTrackedData(); - for( auto const& it : mapTrackedData ) + for(const auto* data : mapTrackedData | std::views::keys) { - it.first->updateIfDirty(); + data->updateIfDirty(); } if(m_internalDataTracker.hasChanged()) @@ -300,19 +301,15 @@ void BaseObject::cleanTracker() bool BaseObject::hasDataChanged(const objectmodel::BaseData& data) { - bool dataFoundinTracker = false; const auto& mapTrackedData = m_internalDataTracker.getMapTrackedData(); const std::string & dataName = data.getName(); - for( auto const& it : mapTrackedData ) - { - if(it.first->getName()==dataName) + auto trackedData = mapTrackedData | std::views::keys; + if (std::ranges::find_if(trackedData, + [&dataName](const BaseData* d) { - dataFoundinTracker=true; - break; - } - } - if(!dataFoundinTracker) + return d->getName() == dataName; + }) == trackedData.end()) { msg_error()<< "Data " << dataName << " is not tracked"; return false; diff --git a/Sofa/framework/Helper/src/sofa/helper/system/PluginManager.cpp b/Sofa/framework/Helper/src/sofa/helper/system/PluginManager.cpp index 2c60200c2bc..27b9fab1b6b 100644 --- a/Sofa/framework/Helper/src/sofa/helper/system/PluginManager.cpp +++ b/Sofa/framework/Helper/src/sofa/helper/system/PluginManager.cpp @@ -27,6 +27,8 @@ using sofa::helper::system::FileSystem; #include #include +#include + #if __has_include() #include namespace fs = std::filesystem; @@ -83,7 +85,7 @@ PluginManager::~PluginManager() void PluginManager::cleanup() { - for (const auto& [key, callback] : m_onPluginCleanupCallbacks) + for (const auto& callback : m_onPluginCleanupCallbacks | std::views::values) { if(callback) { @@ -129,7 +131,7 @@ void PluginManager::readFromIniFile(const std::string& path, type::vector using sofa::testing::BaseTest; +#include #include using sofa::helper::system::PluginManager; @@ -93,9 +94,9 @@ struct PluginManager_test: public BaseTest PluginManager&pm = PluginManager::getInstance(); //empty loaded plugin(s) std::vector toDelete; - for (const auto& it : pm.getPluginMap()) + for (const auto& pluginName : pm.getPluginMap() | std::views::keys) { - toDelete.push_back(it.first); + toDelete.push_back(pluginName); } for(const std::string& p : toDelete) diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/TaskSchedulerFactory.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/TaskSchedulerFactory.cpp index 3777fa34ea9..305e150f8e2 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/TaskSchedulerFactory.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/TaskSchedulerFactory.cpp @@ -23,6 +23,8 @@ #include #include +#include + namespace sofa::simulation { @@ -54,7 +56,7 @@ TaskScheduler* TaskSchedulerFactory::instantiate(const std::string& name) std::set TaskSchedulerFactory::getAvailableSchedulers() { std::set schedulers; - for (const auto& [name, _] : m_schedulerCreationFunctions) + for (const auto& name : m_schedulerCreationFunctions | std::views::keys) { schedulers.insert(name); } diff --git a/Sofa/framework/Simulation/Core/src/sofa/simulation/TaskSchedulerRegistry.cpp b/Sofa/framework/Simulation/Core/src/sofa/simulation/TaskSchedulerRegistry.cpp index bb3afeb8f03..82cbed6650c 100644 --- a/Sofa/framework/Simulation/Core/src/sofa/simulation/TaskSchedulerRegistry.cpp +++ b/Sofa/framework/Simulation/Core/src/sofa/simulation/TaskSchedulerRegistry.cpp @@ -23,6 +23,8 @@ #include #include +#include + namespace sofa::simulation { @@ -66,9 +68,9 @@ const std::optional>& TaskSchedulerRegist void TaskSchedulerRegistry::clear() { - for (const auto& p : m_schedulers) + for (const auto& scheduler : m_schedulers | std::views::values) { - delete p.second; + delete scheduler; } m_schedulers.clear(); } diff --git a/Sofa/framework/Type/src/sofa/type/vector_T.h b/Sofa/framework/Type/src/sofa/type/vector_T.h index e0ad65b7615..05751725ef1 100644 --- a/Sofa/framework/Type/src/sofa/type/vector_T.h +++ b/Sofa/framework/Type/src/sofa/type/vector_T.h @@ -80,6 +80,11 @@ class vector : public std::vector > /// Move constructor vector(std::vector&& v): std::vector(std::move(v)) {} + /// Constructor from a range + template< std::input_iterator InputIt > + constexpr vector(InputIt first, InputIt last) + : std::vector(first, last) {} + /// Copy operator vector& operator=(const std::vector& x) { @@ -154,8 +159,8 @@ class vector : public std::vector > std::fill(this->begin(), this->end(), value); } - /// this function is useful for vector_device because it resize the vector without device operation (if device is not valid). - /// Therefore the function is used in asynchronous code to safely resize a vector which is either cuda of type::vector + /// this function is useful for vector_device because it resizes the vector without device operation (if device is not valid). + /// Therefore, the function is used in asynchronous code to safely resize a vector which is either cuda of type::vector void fastResize(Size n) { this->resize(n); @@ -163,4 +168,8 @@ class vector : public std::vector > }; +//template deduction guide +template< std::input_iterator InputIt > +vector(InputIt first, InputIt last) -> vector::value_type>; + } /// namespace sofa::type diff --git a/applications/projects/SceneChecking/src/SceneChecking/SceneCheckMissingRequiredPlugin.cpp b/applications/projects/SceneChecking/src/SceneChecking/SceneCheckMissingRequiredPlugin.cpp index 1d3bf16e0f9..4a6bfcf4264 100644 --- a/applications/projects/SceneChecking/src/SceneChecking/SceneCheckMissingRequiredPlugin.cpp +++ b/applications/projects/SceneChecking/src/SceneChecking/SceneCheckMissingRequiredPlugin.cpp @@ -29,6 +29,8 @@ #include #include +#include + namespace sofa::_scenechecking_ { @@ -71,11 +73,11 @@ void SceneCheckMissingRequiredPlugin::doCheckOn(sofa::simulation::Node* node) } //sort and remove duplicates - for (auto& plugins : m_requiredPlugins) + for (auto& objects : m_requiredPlugins | std::views::values) { - auto& v = plugins.second; - std::sort(v.begin(), v.end()); - v.erase(std::unique(v.begin(), v.end()), v.end()); + std::ranges::sort(objects); + const auto ret = std::ranges::unique(objects); + objects.erase(ret.begin(), ret.end()); } } diff --git a/applications/projects/runSofa/Main.cpp b/applications/projects/runSofa/Main.cpp index 6688d961f1e..5df85abb7dd 100644 --- a/applications/projects/runSofa/Main.cpp +++ b/applications/projects/runSofa/Main.cpp @@ -19,6 +19,7 @@ * * * Contact information: contact@sofa-framework.org * ******************************************************************************/ +#include #include using std::ostringstream ; #include @@ -414,7 +415,7 @@ int main(int argc, char** argv) sofa::core::ObjectFactory* objectFactory = sofa::core::ObjectFactory::getInstance(); // calling explicitly registerObjects from loadedPlugins - for (const auto& [pluginPath, plugin] : pluginManager.getPluginMap()) + for (const auto& plugin : pluginManager.getPluginMap() | std::views::values) { const auto& pluginName = plugin.getModuleName(); objectFactory->registerObjectsFromPlugin(pluginName);