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

This commit is contained in:
wmayer
2023-08-14 13:40:11 +02:00
committed by wwmayer
parent 2dbeb68e6d
commit 761905dbc2
13 changed files with 104 additions and 100 deletions

View File

@@ -43,8 +43,8 @@ PyObject* BaseClassPy::isDerivedFrom(PyObject *args)
return nullptr;
Base::Type type = Base::Type::fromName(name);
bool v = (type != Base::Type::badType() && getBaseClassPtr()->getTypeId().isDerivedFrom(type));
return PyBool_FromLong(v ? 1 : 0);
bool valid = (type != Base::Type::badType() && getBaseClassPtr()->getTypeId().isDerivedFrom(type));
return PyBool_FromLong(valid ? 1 : 0);
}
PyObject* BaseClassPy::getAllDerivedFrom(PyObject *args)
@@ -55,8 +55,8 @@ PyObject* BaseClassPy::getAllDerivedFrom(PyObject *args)
std::vector<Base::Type> ary;
Base::Type::getAllDerivedFrom(getBaseClassPtr()->getTypeId(), ary);
Py::List res;
for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it)
res.append(Py::String(it->getName()));
for (const auto & it : ary)
res.append(Py::String(it.getName()));
return Py::new_reference_to(res);
}

View File

@@ -131,8 +131,8 @@ ConsoleSingleton::ConsoleSingleton()
ConsoleSingleton::~ConsoleSingleton()
{
ConsoleOutput::destruct();
for (std::set<ILogger * >::iterator Iter=_aclObservers.begin();Iter!=_aclObservers.end();++Iter)
delete (*Iter);
for (ILogger* Iter : _aclObservers)
delete Iter;
}
@@ -282,9 +282,9 @@ void ConsoleSingleton::DetachObserver(ILogger *pcObserver)
void Base::ConsoleSingleton::notifyPrivate(LogStyle category, IntendedRecipient recipient,
ContentType content, const std::string& notifiername, const std::string& msg)
{
for (std::set<ILogger * >::iterator Iter=_aclObservers.begin();Iter!=_aclObservers.end();++Iter) {
if ((*Iter)->isActive(category)) {
(*Iter)->SendLog(notifiername, msg, category, recipient, content); // send string to the listener
for (ILogger* Iter : _aclObservers) {
if (Iter->isActive(category)) {
Iter->SendLog(notifiername, msg, category, recipient, content); // send string to the listener
}
}
}
@@ -298,10 +298,10 @@ void ConsoleSingleton::postEvent(ConsoleSingleton::FreeCAD_ConsoleMsgType type,
ILogger *ConsoleSingleton::Get(const char *Name) const
{
const char* OName;
for (std::set<ILogger * >::const_iterator Iter=_aclObservers.begin();Iter!=_aclObservers.end();++Iter) {
OName = (*Iter)->Name(); // get the name
for (ILogger* Iter : _aclObservers) {
OName = Iter->Name(); // get the name
if (OName && strcmp(OName,Name) == 0)
return *Iter;
return Iter;
}
return nullptr;
}

View File

@@ -37,8 +37,8 @@ using namespace Base;
Factory::~Factory ()
{
for (std::map<const std::string, AbstractProducer*>::iterator pI = _mpcProducers.begin(); pI != _mpcProducers.end(); ++pI)
delete pI->second;
for (auto & it : _mpcProducers)
delete it.second;
}
void* Factory::Produce (const char *sClassName) const
@@ -66,9 +66,9 @@ std::list<std::string> Factory::CanProduce() const
{
std::list<std::string> lObjects;
for (std::map<const std::string, AbstractProducer*>::const_iterator pI = _mpcProducers.begin(); pI != _mpcProducers.end(); ++pI)
for (const auto & it : _mpcProducers)
{
lObjects.push_back(pI->first);
lObjects.push_back(it.first);
}
return lObjects;

View File

@@ -580,19 +580,19 @@ bool FileInfo::deleteDirectoryRecursive() const
return false;
std::vector<Base::FileInfo> List = getDirectoryContent();
for (std::vector<Base::FileInfo>::iterator It = List.begin();It!=List.end();++It) {
if (It->isDir()) {
for (Base::FileInfo& fi : List) {
if (fi.isDir()) {
// At least on Linux, directory needs execute permission to be
// deleted. We don't really need to set permission for directory
// anyway, since FC code does not touch directory permission.
//
// It->setPermissions(FileInfo::ReadWrite);
It->deleteDirectoryRecursive();
fi.deleteDirectoryRecursive();
}
else if (It->isFile()) {
It->setPermissions(FileInfo::ReadWrite);
It->deleteFile();
else if (fi.isFile()) {
fi.setPermissions(FileInfo::ReadWrite);
fi.deleteFile();
}
else {
throw Base::FileException("FileInfo::deleteDirectoryRecursive(): Unknown object Type in directory!");

View File

@@ -116,12 +116,14 @@ void Matrix4D::nullify()
bool Matrix4D::isNull() const
{
//NOLINTBEGIN
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
if (dMtrx4D[i][j] != 0.0)
return false;
}
}
//NOLINTEND
return true;
}
@@ -626,9 +628,11 @@ unsigned long Matrix4D::getMemSpace ()
void Matrix4D::Print () const
{
short i;
for (i = 0; i < 4; i++)
//NOLINTBEGIN
for (short i = 0; i < 4; i++) {
printf("%9.3f %9.3f %9.3f %9.3f\n", dMtrx4D[i][0], dMtrx4D[i][1], dMtrx4D[i][2], dMtrx4D[i][3]);
}
//NOLINTEND
}
void Matrix4D::transpose ()
@@ -650,11 +654,12 @@ void Matrix4D::transpose ()
std::string Matrix4D::toString() const
{
std::stringstream str;
for (int i = 0; i < 4; i++)
{
//NOLINTBEGIN
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
str << dMtrx4D[i][j] << " ";
}
//NOLINTEND
return str.str();
}
@@ -665,11 +670,12 @@ void Matrix4D::fromString(const std::string &str)
std::stringstream input;
input.str(str);
for (int i = 0; i < 4; i++)
{
//NOLINTBEGIN
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++)
input >> dMtrx4D[i][j];
}
//NOLINTEND
}
// Analyse the a transformation Matrix and describe the transformation

View File

@@ -118,7 +118,7 @@ class DOMPrintFilter : public DOMLSSerializerFilter
public:
/** @name Constructors */
DOMPrintFilter(ShowType whatToShow = DOMNodeFilter::SHOW_ALL);
explicit DOMPrintFilter(ShowType whatToShow = DOMNodeFilter::SHOW_ALL);
//@{
/** @name Destructors */
@@ -150,10 +150,9 @@ public:
bool handleError(const DOMError& domError) override;
void resetErrors() {}
private :
/* Unimplemented constructors and operators */
DOMPrintErrorHandler(const DOMErrorHandler&);
void operator=(const DOMErrorHandler&);
DOMPrintErrorHandler(const DOMErrorHandler&) = delete;
void operator=(const DOMErrorHandler&) = delete;
};
@@ -1374,28 +1373,28 @@ void ParameterGrp::NotifyAll()
{
// get all ints and notify
std::vector<std::pair<std::string,long> > IntMap = GetIntMap();
for (std::vector<std::pair<std::string,long> >::iterator It1= IntMap.begin(); It1 != IntMap.end(); ++It1)
Notify(It1->first.c_str());
for (const auto & it : IntMap)
Notify(it.first.c_str());
// get all booleans and notify
std::vector<std::pair<std::string,bool> > BoolMap = GetBoolMap();
for (std::vector<std::pair<std::string,bool> >::iterator It2= BoolMap.begin(); It2 != BoolMap.end(); ++It2)
Notify(It2->first.c_str());
for (const auto & it : BoolMap)
Notify(it.first.c_str());
// get all Floats and notify
std::vector<std::pair<std::string,double> > FloatMap = GetFloatMap();
for (std::vector<std::pair<std::string,double> >::iterator It3= FloatMap.begin(); It3 != FloatMap.end(); ++It3)
Notify(It3->first.c_str());
for (const auto & it : FloatMap)
Notify(it.first.c_str());
// get all strings and notify
std::vector<std::pair<std::string,std::string> > StringMap = GetASCIIMap();
for (std::vector<std::pair<std::string,std::string> >::iterator It4= StringMap.begin(); It4 != StringMap.end(); ++It4)
Notify(It4->first.c_str());
for (const auto & it : StringMap)
Notify(it.first.c_str());
// get all uints and notify
std::vector<std::pair<std::string,unsigned long> > UIntMap = GetUnsignedMap();
for (std::vector<std::pair<std::string,unsigned long> >::iterator It5= UIntMap.begin(); It5 != UIntMap.end(); ++It5)
Notify(It5->first.c_str());
for (const auto & it : UIntMap)
Notify(it.first.c_str());
}
void ParameterGrp::_Reset()

View File

@@ -48,7 +48,7 @@ namespace Base {
class ParameterGrpObserver : public ParameterGrp::ObserverType
{
public:
ParameterGrpObserver(const Py::Object& obj)
explicit ParameterGrpObserver(const Py::Object& obj)
{
inst = obj;
}
@@ -102,7 +102,7 @@ class ParameterGrpPy : public Py::PythonExtension<ParameterGrpPy>
public:
static void init_type(); // announce properties and methods
ParameterGrpPy(const Base::Reference<ParameterGrp> &rcParamGrp);
explicit ParameterGrpPy(const Base::Reference<ParameterGrp> &rcParamGrp);
~ParameterGrpPy() override;
Py::Object repr() override;
@@ -247,8 +247,7 @@ ParameterGrpPy::ParameterGrpPy(const Base::Reference<ParameterGrp> &rcParamGrp)
ParameterGrpPy::~ParameterGrpPy()
{
for (ParameterGrpObserverList::iterator it = _observers.begin(); it != _observers.end(); ++it) {
ParameterGrpObserver* obs = *it;
for (ParameterGrpObserver* obs : _observers) {
if (!obs->_target)
_cParamGrp->Detach(obs);
delete obs;
@@ -650,8 +649,8 @@ Py::Object ParameterGrpPy::attach(const Py::Tuple& args)
if (!o.hasAttr(std::string("onChange")))
throw Py::TypeError("Object has no onChange attribute");
for (ParameterGrpObserverList::iterator it = _observers.begin(); it != _observers.end(); ++it) {
if ((*it)->isEqual(o)) {
for (ParameterGrpObserver* it : _observers) {
if (it->isEqual(o)) {
throw Py::RuntimeError("Object is already attached.");
}
}
@@ -680,8 +679,8 @@ Py::Object ParameterGrpPy::attachManager(const Py::Tuple& args)
if (!attr.isCallable())
throw Py::TypeError("Object has no slotParamChanged callable attribute");
for (ParameterGrpObserverList::iterator it = _observers.begin(); it != _observers.end(); ++it) {
if ((*it)->isEqual(o)) {
for (ParameterGrpObserver* it : _observers) {
if (it->isEqual(o)) {
throw Py::RuntimeError("Object is already attached.");
}
}
@@ -766,51 +765,51 @@ Py::Object ParameterGrpPy::getContents(const Py::Tuple& args)
Py::List list;
// filling up Text nodes
std::vector<std::pair<std::string,std::string> > mcTextMap = _cParamGrp->GetASCIIMap();
for (std::vector<std::pair<std::string,std::string> >::iterator It2=mcTextMap.begin();It2!=mcTextMap.end();++It2) {
for (const auto & it : mcTextMap) {
Py::Tuple t2(3);
t2.setItem(0,Py::String("String"));
t2.setItem(1,Py::String(It2->first.c_str()));
t2.setItem(2,Py::String(It2->second.c_str()));
t2.setItem(1,Py::String(it.first.c_str()));
t2.setItem(2,Py::String(it.second.c_str()));
list.append(t2);
}
// filling up Int nodes
std::vector<std::pair<std::string,long> > mcIntMap = _cParamGrp->GetIntMap();
for (std::vector<std::pair<std::string,long> >::iterator It3=mcIntMap.begin();It3!=mcIntMap.end();++It3) {
for (const auto & it : mcIntMap) {
Py::Tuple t3(3);
t3.setItem(0,Py::String("Integer"));
t3.setItem(1,Py::String(It3->first.c_str()));
t3.setItem(2,Py::Long(It3->second));
t3.setItem(1,Py::String(it.first.c_str()));
t3.setItem(2,Py::Long(it.second));
list.append(t3);
}
// filling up Float nodes
std::vector<std::pair<std::string,double> > mcFloatMap = _cParamGrp->GetFloatMap();
for (std::vector<std::pair<std::string,double> >::iterator It4=mcFloatMap.begin();It4!=mcFloatMap.end();++It4) {
for (const auto & it : mcFloatMap) {
Py::Tuple t4(3);
t4.setItem(0,Py::String("Float"));
t4.setItem(1,Py::String(It4->first.c_str()));
t4.setItem(2,Py::Float(It4->second));
t4.setItem(1,Py::String(it.first.c_str()));
t4.setItem(2,Py::Float(it.second));
list.append(t4);
}
// filling up bool nodes
std::vector<std::pair<std::string,bool> > mcBoolMap = _cParamGrp->GetBoolMap();
for (std::vector<std::pair<std::string,bool> >::iterator It5=mcBoolMap.begin();It5!=mcBoolMap.end();++It5) {
for (const auto & it : mcBoolMap) {
Py::Tuple t5(3);
t5.setItem(0,Py::String("Boolean"));
t5.setItem(1,Py::String(It5->first.c_str()));
t5.setItem(2,Py::Boolean(It5->second));
t5.setItem(1,Py::String(it.first.c_str()));
t5.setItem(2,Py::Boolean(it.second));
list.append(t5);
}
// filling up UInt nodes
std::vector<std::pair<std::string,unsigned long> > mcUIntMap = _cParamGrp->GetUnsignedMap();
for (std::vector<std::pair<std::string,unsigned long> >::iterator It6=mcUIntMap.begin();It6!=mcUIntMap.end();++It6) {
for (const auto & it : mcUIntMap) {
Py::Tuple t6(3);
t6.setItem(0,Py::String("Unsigned Long"));
t6.setItem(1,Py::String(It6->first.c_str()));
t6.setItem(2,Py::Long(It6->second));
t6.setItem(1,Py::String(it.first.c_str()));
t6.setItem(2,Py::Long(it.second));
list.append(t6);
}

View File

@@ -78,25 +78,25 @@ void Persistence::RestoreDocFile(Reader &/*reader*/)
std::string Persistence::encodeAttribute(const std::string& str)
{
std::string tmp;
for (std::string::const_iterator it = str.begin(); it != str.end(); ++it) {
if (*it == '<')
for (char it : str) {
if (it == '<')
tmp += "&lt;";
else if (*it == '\"')
else if (it == '\"')
tmp += "&quot;";
else if (*it == '\'')
else if (it == '\'')
tmp += "&apos;";
else if (*it == '&')
else if (it == '&')
tmp += "&amp;";
else if (*it == '>')
else if (it == '>')
tmp += "&gt;";
else if (*it == '\r')
else if (it == '\r')
tmp += "&#13;";
else if (*it == '\n')
else if (it == '\n')
tmp += "&#10;";
else if (*it == '\t')
else if (it == '\t')
tmp += "&#9;";
else
tmp += *it;
tmp += it;
}
return tmp;

View File

@@ -461,8 +461,8 @@ const std::vector<std::string>& Base::XMLReader::getFilenames() const
bool Base::XMLReader::isRegistered(Base::Persistence *Object) const
{
if (Object) {
for (std::vector<FileEntry>::const_iterator it = FileList.begin(); it != FileList.end(); ++it) {
if (it->Object == Object)
for (const auto & it : FileList) {
if (it.Object == Object)
return true;
}
}

View File

@@ -97,9 +97,9 @@ private:
void findHighestSuffix(const std::vector<std::string>& names)
{
for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it) {
if (it->substr(0, base_name.length()) == base_name) { // same prefix
std::string suffix(it->substr(base_name.length()));
for (const auto & name : names) {
if (name.substr(0, base_name.length()) == base_name) { // same prefix
std::string suffix(name.substr(base_name.length()));
if (suffix.size() > 0) {
std::string::size_type pos = suffix.find_first_not_of("0123456789");
if (pos == std::string::npos) {
@@ -161,11 +161,11 @@ std::string Base::Tools::getIdentifier(const std::string& name)
if (!CleanName.empty() && CleanName[0] >= 48 && CleanName[0] <= 57)
CleanName[0] = '_';
// strip illegal chars
for (std::string::iterator it = CleanName.begin(); it != CleanName.end(); ++it) {
if (!((*it>=48 && *it<=57) || // number
(*it>=65 && *it<=90) || // uppercase letter
(*it>=97 && *it<=122))) // lowercase letter
*it = '_'; // it's neither number nor letter
for (char & it : CleanName) {
if (!((it>=48 && it<=57) || // number
(it>=65 && it<=90) || // uppercase letter
(it>=97 && it<=122))) // lowercase letter
it = '_'; // it's neither number nor letter
}
return CleanName;
@@ -175,8 +175,8 @@ std::wstring Base::Tools::widen(const std::string& str)
{
std::wostringstream wstm;
const std::ctype<wchar_t>& ctfacet = std::use_facet< std::ctype<wchar_t> >(wstm.getloc());
for (size_t i=0; i<str.size(); ++i)
wstm << ctfacet.widen(str[i]);
for (char i : str)
wstm << ctfacet.widen(i);
return wstm.str();
}
@@ -184,8 +184,8 @@ std::string Base::Tools::narrow(const std::wstring& str)
{
std::ostringstream stm;
const std::ctype<char>& ctfacet = std::use_facet< std::ctype<char> >(stm.getloc());
for (size_t i=0; i<str.size(); ++i)
stm << ctfacet.narrow(str[i], 0);
for (wchar_t i : str)
stm << ctfacet.narrow(i, 0);
return stm.str();
}

View File

@@ -394,10 +394,10 @@ void Polygon2d::Intersect (const Polygon2d &rclPolygon, std::list<Polygon2d> &rc
if (afIntersections.size() > 0) // intersections founded
{
for (std::set<double>::iterator pF = afIntersections.begin(); pF != afIntersections.end(); ++pF)
for (double it : afIntersections)
{
// intersection point
Vector2d clPtIS = clLine.FromPos(*pF);
Vector2d clPtIS = clLine.FromPos(it);
if (bInner)
{
clResultPolygon.Add(clPtIS);

View File

@@ -165,8 +165,8 @@ void Type::init()
void Type::destruct()
{
for(std::vector<TypeData*>::const_iterator it = typedata.begin();it!= typedata.end();++it)
delete *it;
for(auto it : typedata)
delete it;
typedata.clear();
typemap.clear();
loadModuleSet.clear();
@@ -218,11 +218,11 @@ int Type::getAllDerivedFrom(const Type type, std::vector<Type> & List)
{
int cnt = 0;
for(std::vector<TypeData*>::const_iterator it = typedata.begin();it!= typedata.end();++it)
for(auto it : typedata)
{
if ((*it)->type.isDerivedFrom(type))
if (it->type.isDerivedFrom(type))
{
List.push_back((*it)->type);
List.push_back(it->type);
cnt++;
}
}

View File

@@ -148,8 +148,8 @@ PyObject* TypePy::getAllDerivedFrom(PyObject *args)
std::vector<Base::Type> ary;
Base::Type::getAllDerivedFrom(type, ary);
Py::List res;
for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
res.append(Py::asObject(new TypePy(new Base::Type(*it))));
for (const auto & it : ary) {
res.append(Py::asObject(new TypePy(new Base::Type(it))));
}
return Py::new_reference_to(res);
}
@@ -163,8 +163,8 @@ PyObject* TypePy::getAllDerived(PyObject *args)
std::vector<Base::Type> ary;
Base::Type::getAllDerivedFrom(type, ary);
Py::List res;
for (std::vector<Base::Type>::iterator it = ary.begin(); it != ary.end(); ++it) {
res.append(Py::asObject(new TypePy(new Base::Type(*it))));
for (const auto & it : ary) {
res.append(Py::asObject(new TypePy(new Base::Type(it))));
}
return Py::new_reference_to(res);
}