Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[all] use std::views::keys/values #5221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <sofa/core/visual/VisualParams.h>
#include <sofa/core/ObjectFactory.h>

#include <ranges>

namespace sofa::component::collision::response::contact
{

Expand All @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ using sofa::simulation::mechanicalvisitor::MechanicalGetConstraintInfoVisitor;
#include <sofa/simulation/mechanicalvisitor/MechanicalVOpVisitor.h>
using sofa::simulation::mechanicalvisitor::MechanicalVOpVisitor;

#include <ranges>

using sofa::core::VecId;

namespace sofa::component::constraint::lagrangian::solver
Expand Down Expand Up @@ -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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <sofa/helper/logging/Messaging.h>
#include <sofa/type/vector.h>

#include <ranges>


namespace sofa::component::linearsolver::direct
{
Expand Down Expand Up @@ -195,13 +197,8 @@ class SOFA_COMPONENT_LINEARSOLVER_DIRECT_API EigenSolverFactory
[[nodiscard]]
sofa::type::vector<OrderingMethodName> registeredObjects() const
{
sofa::type::vector<OrderingMethodName> 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
#include <cassert>
#include <sofa/linearalgebra/CompressedRowSparseMatrixMechanical.h>

#include <ranges>


namespace
{
Expand Down Expand Up @@ -1003,8 +1005,8 @@ void MechanicalObject<DataTypes>::init()

// Print a warning if one or more vector don't match the maximum size
bool allSizeAreEqual = true;
for (const std::pair<const std::string, const Size>& 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;
Expand Down
8 changes: 5 additions & 3 deletions Sofa/GL/src/sofa/gl/GLSLShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

#include <sofa/helper/logging/Messaging.h>

#include <ranges>

namespace sofa::gl
{

Expand Down Expand Up @@ -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 = "" ;
}
}
}
Expand Down
56 changes: 27 additions & 29 deletions Sofa/framework/Core/src/sofa/core/ObjectFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <sofa/helper/DiffLib.h>
#include <sofa/helper/system/PluginManager.h>

#include <ranges>

namespace sofa::core
{

Expand Down Expand Up @@ -275,9 +277,9 @@ objectmodel::BaseObject::SPtr ObjectFactory::createObject(objectmodel::BaseConte
{
std::vector<std::string> 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:");
Expand Down Expand Up @@ -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<std::string> 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)
{
Expand All @@ -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() << "'.";
}
Expand All @@ -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<std::string> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
#include <sofa/core/collision/NarrowPhaseDetection.h>
#include <sofa/core/visual/VisualParams.h>

#include <ranges>

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();
Expand Down
19 changes: 8 additions & 11 deletions Sofa/framework/Core/src/sofa/core/objectmodel/BaseObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <sofa/core/topology/Topology.h>
#include <sofa/helper/TagFactory.h>
#include <iostream>
#include <ranges>


namespace sofa::core::objectmodel
Expand Down Expand Up @@ -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())
Expand All @@ -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;
Expand Down
14 changes: 8 additions & 6 deletions Sofa/framework/Helper/src/sofa/helper/system/PluginManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ using sofa::helper::system::FileSystem;
#include <sofa/helper/StringUtils.h>
#include <sofa/helper/logging/Messaging.h>

#include <ranges>

#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -129,7 +131,7 @@ void PluginManager::readFromIniFile(const std::string& path, type::vector<std::s
void PluginManager::writeToIniFile(const std::string& path)
{
std::ofstream outstream(path.c_str());
for( const auto& [pluginPath, _] : m_pluginMap)
for(const auto& pluginPath : m_pluginMap | std::views::keys)
{
if (const auto* plugin = getPlugin(pluginPath))
{
Expand Down Expand Up @@ -236,7 +238,7 @@ PluginManager::PluginLoadStatus PluginManager::loadPluginByPath(const std::strin

msg_info("PluginManager") << "Loaded plugin: " << pluginPath;

for (const auto& [key, callback] : m_onPluginLoadedCallbacks)
for (const auto& callback : m_onPluginLoadedCallbacks | std::views::values)
{
if(callback)
{
Expand Down Expand Up @@ -385,12 +387,12 @@ Plugin* PluginManager::getPlugin(const std::string& plugin, const std::string& /
// check if a plugin with a same name but a different path is loaded
// problematic case per se but at least we can warn the user
const auto& pluginName = GetPluginNameFromPath(pluginPath);
for (auto& k : m_pluginMap)
for (auto& loadedPlugin : m_pluginMap | std::views::values)
{
if (pluginName == k.second.getModuleName())
if (pluginName == loadedPlugin.getModuleName())
{
msg_warning("PluginManager") << "Plugin " << pluginName << " is already loaded from a different path, check you configuration.";
return &k.second;
return &loadedPlugin;
}
}

Expand Down
5 changes: 3 additions & 2 deletions Sofa/framework/Helper/test/system/PluginManager_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <sofa/testing/BaseTest.h>
using sofa::testing::BaseTest;

#include <ranges>
#include <fstream>

using sofa::helper::system::PluginManager;
Expand Down Expand Up @@ -93,9 +94,9 @@ struct PluginManager_test: public BaseTest
PluginManager&pm = PluginManager::getInstance();
//empty loaded plugin(s)
std::vector<std::string> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <sofa/simulation/TaskScheduler.h>
#include <sofa/helper/logging/Messaging.h>

#include <ranges>

namespace sofa::simulation
{

Expand Down Expand Up @@ -54,7 +56,7 @@ TaskScheduler* TaskSchedulerFactory::instantiate(const std::string& name)
std::set<std::string> TaskSchedulerFactory::getAvailableSchedulers()
{
std::set<std::string> schedulers;
for (const auto& [name, _] : m_schedulerCreationFunctions)
for (const auto& name : m_schedulerCreationFunctions | std::views::keys)
{
schedulers.insert(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include <sofa/helper/logging/Messaging.h>
#include <sofa/simulation/TaskScheduler.h>

#include <ranges>

namespace sofa::simulation
{

Expand Down Expand Up @@ -66,9 +68,9 @@ const std::optional<std::pair<std::string, TaskScheduler*>>& 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();
}
Expand Down
Loading
Loading