App: modernize C++: use range-based for loop

This commit is contained in:
wmayer
2023-08-14 16:37:45 +02:00
committed by wwmayer
parent 761905dbc2
commit 4991475341
22 changed files with 422 additions and 418 deletions

View File

@@ -560,7 +560,7 @@ int Document::getTransactionID(bool undo, unsigned pos) const {
if(pos>=mRedoTransactions.size())
return 0;
auto rit = mRedoTransactions.rbegin();
for(;pos;++rit,--pos);
for(;pos;++rit,--pos){}
return (*rit)->getID();
}
@@ -1598,8 +1598,8 @@ private:
Base::FileInfo di(fi.dirPath());
std::vector<Base::FileInfo> backup;
std::vector<Base::FileInfo> files = di.getDirectoryContent();
for (std::vector<Base::FileInfo>::iterator it = files.begin(); it != files.end(); ++it) {
std::string file = it->fileName();
for (const Base::FileInfo& it : files) {
std::string file = it.fileName();
if (file.substr(0,fn.length()) == fn) {
// starts with the same file name
std::string suf(file.substr(fn.length()));
@@ -1607,7 +1607,7 @@ private:
std::string::size_type nPos = suf.find_first_not_of("0123456789");
if (nPos==std::string::npos) {
// store all backup files
backup.push_back(*it);
backup.push_back(it);
nSuff = std::max<int>(nSuff, std::atol(suf.c_str()));
}
}
@@ -1617,9 +1617,9 @@ private:
if (!backup.empty() && (int)backup.size() >= numberOfFiles) {
// delete the oldest backup file we found
Base::FileInfo del = backup.front();
for (std::vector<Base::FileInfo>::iterator it = backup.begin(); it != backup.end(); ++it) {
if (it->lastModified() < del.lastModified())
del = *it;
for (const Base::FileInfo& it : backup) {
if (it.lastModified() < del.lastModified())
del = it;
}
del.deleteFile();
@@ -1673,10 +1673,10 @@ private:
Base::FileInfo di(fi.dirPath());
std::vector<Base::FileInfo> backup;
std::vector<Base::FileInfo> files = di.getDirectoryContent();
for (std::vector<Base::FileInfo>::iterator it = files.begin(); it != files.end(); ++it) {
if (it->isFile()) {
std::string file = it->fileName();
std::string fext = it->extension();
for (const Base::FileInfo& it : files) {
if (it.isFile()) {
std::string file = it.fileName();
std::string fext = it.extension();
std::string fextUp = fext;
std::transform(fextUp.begin(), fextUp.end(), fextUp.begin(),(int (*)(int))toupper);
// re-enforcing identification of the backup file
@@ -1690,7 +1690,7 @@ private:
// + complement with no "." + ".FCBak"
((fextUp == "FCBAK") && startsWith(file, pbn) &&
(checkValidComplement(file, pbn, fext)))) {
backup.push_back(*it);
backup.push_back(it);
}
}
}
@@ -1700,18 +1700,18 @@ private:
// delete the oldest backup file we found
// Base::FileInfo del = backup.front();
int nb = 0;
for (std::vector<Base::FileInfo>::iterator it = backup.begin(); it != backup.end(); ++it) {
for (Base::FileInfo& it : backup) {
nb++;
if (nb >= numberOfFiles) {
try {
if (!it->deleteFile()) {
if (!it.deleteFile()) {
backupManagementError = true;
Base::Console().Warning("Cannot remove backup file : %s\n", it->fileName().c_str());
Base::Console().Warning("Cannot remove backup file : %s\n", it.fileName().c_str());
}
}
catch (...) {
backupManagementError = true;
Base::Console().Warning("Cannot remove backup file : %s\n", it->fileName().c_str());
Base::Console().Warning("Cannot remove backup file : %s\n", it.fileName().c_str());
}
}
}
@@ -2192,15 +2192,17 @@ const char* Document::getFileName() const
/// Remove all modifications. After this call The document becomes valid again.
void Document::purgeTouched()
{
for (std::vector<DocumentObject*>::iterator It = d->objectArray.begin();It != d->objectArray.end();++It)
(*It)->purgeTouched();
for (auto It : d->objectArray)
It->purgeTouched();
}
bool Document::isTouched() const
{
for (std::vector<DocumentObject*>::const_iterator It = d->objectArray.begin();It != d->objectArray.end();++It)
if ((*It)->isTouched())
for (auto It : d->objectArray) {
if (It->isTouched()) {
return true;
}
}
return false;
}
@@ -2208,9 +2210,11 @@ vector<DocumentObject*> Document::getTouched() const
{
vector<DocumentObject*> result;
for (std::vector<DocumentObject*>::const_iterator It = d->objectArray.begin();It != d->objectArray.end();++It)
if ((*It)->isTouched())
result.push_back(*It);
for (auto It : d->objectArray) {
if (It->isTouched()) {
result.push_back(It);
}
}
return result;
}
@@ -2304,13 +2308,14 @@ std::vector<App::DocumentObject*> Document::getInList(const DocumentObject* me)
// result list
std::vector<App::DocumentObject*> result;
// go through all objects
for (auto It = d->objectMap.begin(); It != d->objectMap.end();++It) {
for (const auto & It : d->objectMap) {
// get the outList and search if me is in that list
std::vector<DocumentObject*> OutList = It->second->getOutList();
for (std::vector<DocumentObject*>::const_iterator It2=OutList.begin();It2!=OutList.end();++It2)
if (*It2 && *It2 == me)
std::vector<DocumentObject*> OutList = It.second->getOutList();
for (auto obj : OutList) {
if (obj && obj == me)
// add the parent object
result.push_back(It->second);
result.push_back(It.second);
}
}
return result;
}
@@ -2536,7 +2541,9 @@ void Document::_rebuildDependencyList(const std::vector<App::DocumentObject*> &o
* @param paths Map with current and new names
*/
void Document::renameObjectIdentifiers(const std::map<App::ObjectIdentifier, App::ObjectIdentifier> &paths, const std::function<bool(const App::DocumentObject*)> & selector)
void Document::renameObjectIdentifiers(const std::map<App::ObjectIdentifier,
App::ObjectIdentifier> &paths,
const std::function<bool(const App::DocumentObject*)> & selector)
{
std::map<App::ObjectIdentifier, App::ObjectIdentifier> extendedPaths;
@@ -2546,9 +2553,11 @@ void Document::renameObjectIdentifiers(const std::map<App::ObjectIdentifier, App
++it;
}
for (std::vector<DocumentObject*>::iterator it = d->objectArray.begin(); it != d->objectArray.end(); ++it)
if (selector(*it))
(*it)->renameObjectIdentifiers(extendedPaths);
for (auto it : d->objectArray) {
if (selector(it)) {
it->renameObjectIdentifiers(extendedPaths);
}
}
}
#ifdef USE_OLD_DAG
@@ -3213,8 +3222,8 @@ std::vector<DocumentObject *> Document::addObjects(const char* sType, const std:
// get all existing object names
std::vector<std::string> reservedNames;
reservedNames.reserve(d->objectMap.size());
for (auto pos = d->objectMap.begin();pos != d->objectMap.end();++pos) {
reservedNames.push_back(pos->first);
for (const auto & pos : d->objectMap) {
reservedNames.push_back(pos.first);
}
for (auto it = objects.begin(); it != objects.end(); ++it) {
@@ -3576,8 +3585,8 @@ std::vector<DocumentObject*> Document::copyObject(
md.setVerbose(recursive);
unsigned int memsize=1000; // ~ for the meta-information
for (std::vector<App::DocumentObject*>::iterator it = deps.begin(); it != deps.end(); ++it)
memsize += (*it)->getMemSize();
for (auto it : deps)
memsize += it->getMemSize();
// if less than ~10 MB
bool use_buffer=(memsize < 0xA00000);
@@ -3769,8 +3778,8 @@ DocumentObject * Document::getObjectByID(long id) const
// Note: This method is only used in Tree.cpp slotChangeObject(), see explanation there
bool Document::isIn(const DocumentObject *pFeat) const
{
for (auto o = d->objectMap.begin(); o != d->objectMap.end(); ++o) {
if (o->second == pFeat)
for (const auto & pos : d->objectMap) {
if (pos.second == pFeat)
return true;
}
@@ -3779,9 +3788,9 @@ bool Document::isIn(const DocumentObject *pFeat) const
const char * Document::getObjectName(DocumentObject *pFeat) const
{
for (auto pos = d->objectMap.begin();pos != d->objectMap.end();++pos) {
if (pos->second == pFeat)
return pos->first.c_str();
for (const auto & pos : d->objectMap) {
if (pos.second == pFeat)
return pos.first.c_str();
}
return nullptr;
@@ -3825,8 +3834,8 @@ std::string Document::getStandardObjectName(const char *Name, int d) const
std::vector<std::string> labels;
labels.reserve(mm.size());
for (std::vector<App::DocumentObject*>::const_iterator it = mm.begin(); it != mm.end(); ++it) {
std::string label = (*it)->Label.getValue();
for (auto it : mm) {
std::string label = it->Label.getValue();
labels.push_back(label);
}
return Base::Tools::getUniqueName(Name, labels, d);
@@ -3846,9 +3855,9 @@ const std::vector<DocumentObject*> &Document::getObjects() const
std::vector<DocumentObject*> Document::getObjectsOfType(const Base::Type& typeId) const
{
std::vector<DocumentObject*> Objects;
for (std::vector<DocumentObject*>::const_iterator it = d->objectArray.begin(); it != d->objectArray.end(); ++it) {
if ((*it)->getTypeId().isDerivedFrom(typeId))
Objects.push_back(*it);
for (auto it : d->objectArray) {
if (it->getTypeId().isDerivedFrom(typeId))
Objects.push_back(it);
}
return Objects;
}
@@ -3856,9 +3865,9 @@ std::vector<DocumentObject*> Document::getObjectsOfType(const Base::Type& typeId
std::vector< DocumentObject* > Document::getObjectsWithExtension(const Base::Type& typeId, bool derived) const {
std::vector<DocumentObject*> Objects;
for (std::vector<DocumentObject*>::const_iterator it = d->objectArray.begin(); it != d->objectArray.end(); ++it) {
if ((*it)->hasExtension(typeId, derived))
Objects.push_back(*it);
for (auto it : d->objectArray) {
if (it->hasExtension(typeId, derived))
Objects.push_back(it);
}
return Objects;
}
@@ -3877,14 +3886,14 @@ std::vector<DocumentObject*> Document::findObjects(const Base::Type& typeId, con
std::vector<DocumentObject*> Objects;
DocumentObject* found = nullptr;
for (std::vector<DocumentObject*>::const_iterator it = d->objectArray.begin(); it != d->objectArray.end(); ++it) {
if ((*it)->getTypeId().isDerivedFrom(typeId)) {
found = *it;
for (auto it : d->objectArray) {
if (it->getTypeId().isDerivedFrom(typeId)) {
found = it;
if (!rx_name.empty() && !boost::regex_search((*it)->getNameInDocument(), what, rx_name))
if (!rx_name.empty() && !boost::regex_search(it->getNameInDocument(), what, rx_name))
found = nullptr;
if (!rx_label.empty() && !boost::regex_search((*it)->Label.getValue(), what, rx_label))
if (!rx_label.empty() && !boost::regex_search(it->Label.getValue(), what, rx_label))
found = nullptr;
if (found)
@@ -3897,8 +3906,8 @@ std::vector<DocumentObject*> Document::findObjects(const Base::Type& typeId, con
int Document::countObjectsOfType(const Base::Type& typeId) const
{
int ct=0;
for (auto it = d->objectMap.begin(); it != d->objectMap.end(); ++it) {
if (it->second->getTypeId().isDerivedFrom(typeId))
for (const auto & it : d->objectMap) {
if (it.second->getTypeId().isDerivedFrom(typeId))
ct++;
}
@@ -3971,11 +3980,11 @@ Document::getPathsByOutList(const App::DocumentObject* from, const App::Document
std::vector<Path> all_paths;
DocumentP::findAllPathsAt(all_nodes, index_from, all_paths, tmp);
for (std::vector<Path>::iterator it = all_paths.begin(); it != all_paths.end(); ++it) {
Path::iterator jt = std::find(it->begin(), it->end(), index_to);
if (jt != it->end()) {
for (const Path& it : all_paths) {
Path::const_iterator jt = std::find(it.begin(), it.end(), index_to);
if (jt != it.end()) {
std::list<App::DocumentObject*> path;
for (Path::iterator kt = it->begin(); kt != jt; ++kt) {
for (Path::const_iterator kt = it.begin(); kt != jt; ++kt) {
path.push_back(d->objectArray[*kt]);
}
@@ -3999,8 +4008,9 @@ bool Document::mustExecute() const
return touched;
}
for (std::vector<DocumentObject*>::const_iterator It = d->objectArray.begin();It != d->objectArray.end();++It)
if ((*It)->isTouched() || (*It)->mustExecute()==1)
for (auto It : d->objectArray) {
if (It->isTouched() || It->mustExecute()==1)
return true;
}
return false;
}