From 8c3b9c0d847656f7a254490047d40cf54e2847a1 Mon Sep 17 00:00:00 2001 From: Friedemann Kleint Date: Wed, 18 Jul 2018 12:39:27 +0200 Subject: libshiboken: Refactor loops Avoid repeated invocation of container.end() in the loop condition by either assigning to a variable or use range-based-for where possible. Use auto for iterators to allow for changing the container type. Task-number: PYSIDE-727 Change-Id: I5217207a3a7fb60823ccbcbf0915bf6cf21a3e4d Reviewed-by: Alexandru Croitor --- sources/shiboken2/libshiboken/basewrapper.cpp | 129 +++++++++-------------- sources/shiboken2/libshiboken/bindingmanager.cpp | 25 ++--- sources/shiboken2/libshiboken/sbkconverter.cpp | 8 +- sources/shiboken2/libshiboken/sbkenum.cpp | 6 +- 4 files changed, 68 insertions(+), 100 deletions(-) (limited to 'sources') diff --git a/sources/shiboken2/libshiboken/basewrapper.cpp b/sources/shiboken2/libshiboken/basewrapper.cpp index ec705a421..5c5559a14 100644 --- a/sources/shiboken2/libshiboken/basewrapper.cpp +++ b/sources/shiboken2/libshiboken/basewrapper.cpp @@ -118,19 +118,16 @@ static int SbkObject_traverse(PyObject* self, visitproc visit, void* arg) //Visit children Shiboken::ParentInfo* pInfo = sbkSelf->d->parentInfo; if (pInfo) { - std::set::const_iterator it = pInfo->children.begin(); - for(; it != pInfo->children.end(); ++it) - Py_VISIT(*it); + for (SbkObject *c : pInfo->children) + Py_VISIT(c); } //Visit refs Shiboken::RefCountMap* rInfo = sbkSelf->d->referredObjects; if (rInfo) { - Shiboken::RefCountMap::const_iterator it = rInfo->begin(); - for (; it != rInfo->end(); ++it) { - std::list::const_iterator ref = it->second.begin(); - for(; ref != it->second.end(); ++ref) - Py_VISIT(*ref); + for (auto it = rInfo->begin(), end = rInfo->end(); it != end; ++it) { + for (PyObject *ref : it->second) + Py_VISIT(ref); } } @@ -352,10 +349,9 @@ PyObject* SbkObjectTypeTpNew(PyTypeObject* metatype, PyObject* args, PyObject* k sotp->d_func = nullptr; sotp->is_user_type = 1; - std::list::const_iterator it = bases.begin(); - for (; it != bases.end(); ++it) { - if (PepType_SOTP(*it)->subtype_init) - PepType_SOTP(*it)->subtype_init(newType, args, kwds); + for (SbkObjectType *base : bases) { + if (PepType_SOTP(base)->subtype_init) + PepType_SOTP(base)->subtype_init(newType, args, kwds); } return reinterpret_cast(newType); @@ -522,8 +518,7 @@ void DtorCallerVisitor::visit(SbkObjectType* node) void DtorCallerVisitor::done() { - std::list >::const_iterator it = m_ptrs.begin(); - for (; it != m_ptrs.end(); ++it) { + for (auto it = m_ptrs.begin(), end = m_ptrs.end(); it != end; ++it) { Shiboken::ThreadStateSaver threadSaver; threadSaver.save(); PepType_SOTP(it->second)->cpp_dtor(it->first); @@ -643,11 +638,9 @@ std::list splitPyObject(PyObject* pyObj) static void decRefPyObjectList(const std::list& lst, PyObject *skip) { - std::list::const_iterator iter = lst.begin(); - while(iter != lst.end()) { - if (*iter != skip) - Py_DECREF(*iter); - ++iter; + for (PyObject *o : lst) { + if (o != skip) + Py_DECREF(o); } } @@ -842,12 +835,13 @@ static void setSequenceOwnership(PyObject* pyObj, bool owner) if (PySequence_Check(pyObj) && has_length) { Py_ssize_t size = PySequence_Size(pyObj); if (size > 0) { - std::list objs = splitPyObject(pyObj); - for (auto it = objs.begin(), end = objs.end(); it != end; ++it) { - if (owner) - getOwnership(*it); - else - releaseOwnership(*it); + const auto objs = splitPyObject(pyObj); + if (owner) { + for (SbkObject *o : objs) + getOwnership(o); + } else { + for (SbkObject *o : objs) + releaseOwnership(o); } } } else if (Object::checkType(pyObj)) { @@ -975,10 +969,9 @@ void invalidate(SbkObject* self) static void recursive_invalidate(PyObject* pyobj, std::set& seen) { - std::list objs = splitPyObject(pyobj); - std::list::const_iterator it = objs.begin(); - for (; it != objs.end(); it++) - recursive_invalidate(*it, seen); + const auto objs = splitPyObject(pyobj); + for (SbkObject *o : objs) + recursive_invalidate(o, seen); } static void recursive_invalidate(SbkObject* self, std::set& seen) @@ -997,29 +990,23 @@ static void recursive_invalidate(SbkObject* self, std::set& seen) if (self->d->parentInfo) { // Create a copy because this list can be changed during the process ChildrenList copy = self->d->parentInfo->children; - ChildrenList::iterator it = copy.begin(); - for (; it != copy.end(); ++it) { + for (SbkObject *child : copy) { // invalidate the child - recursive_invalidate(*it, seen); + recursive_invalidate(child, seen); // if the parent not is a wrapper class, then remove children from him, because We do not know when this object will be destroyed if (!self->d->validCppObject) - removeParent(*it, true, true); + removeParent(child, true, true); } } // If has ref to other objects invalidate all if (self->d->referredObjects) { RefCountMap& refCountMap = *(self->d->referredObjects); - RefCountMap::iterator iter; - for (iter = refCountMap.begin(); iter != refCountMap.end(); ++iter) { - const std::list lst = iter->second; - std::list::const_iterator it = lst.begin(); - while(it != lst.end()) { - recursive_invalidate(*it, seen); - ++it; - } + for (auto it = refCountMap.begin(), end = refCountMap.end(); it != end; ++it) { + for (PyObject *o : it->second) + recursive_invalidate(o, seen); } } } @@ -1035,22 +1022,18 @@ void makeValid(SbkObject* self) // If it is a parent make all children valid if (self->d->parentInfo) { - ChildrenList::iterator it = self->d->parentInfo->children.begin(); - for (; it != self->d->parentInfo->children.end(); ++it) - makeValid(*it); + for (SbkObject *child : self->d->parentInfo->children) + makeValid(child); } // If has ref to other objects make all valid again if (self->d->referredObjects) { RefCountMap& refCountMap = *(self->d->referredObjects); RefCountMap::iterator iter; - for (iter = refCountMap.begin(); iter != refCountMap.end(); ++iter) { - const std::list lst = iter->second; - std::list::const_iterator it = lst.begin(); - while(it != lst.end()) { - if (Shiboken::Object::checkType(*it)) - makeValid(reinterpret_cast(*it)); - ++it; + for (auto it = refCountMap.begin(), end = refCountMap.end(); it != end; ++it) { + for (PyObject *o : it->second) { + if (Shiboken::Object::checkType(o)) + makeValid(reinterpret_cast(o)); } } } @@ -1166,15 +1149,14 @@ SbkObject *findColocatedChild(SbkObject *wrapper, ChildrenList& children = pInfo->children; - ChildrenList::iterator childrenEnd = children.end(); - for (ChildrenList::iterator iChild = children.begin(); iChild != childrenEnd; ++iChild) { - if (!((*iChild)->d && (*iChild)->d->cptr)) + for (SbkObject *child : children) { + if (!(child->d && child->d->cptr)) continue; - if ((*iChild)->d->cptr[0] == wrapper->d->cptr[0]) { - if (reinterpret_cast(Py_TYPE(*iChild)) == reinterpret_cast(instanceType)) - return const_cast((*iChild)); + if (child->d->cptr[0] == wrapper->d->cptr[0]) { + if (reinterpret_cast(Py_TYPE(child)) == reinterpret_cast(instanceType)) + return child; else - return findColocatedChild(const_cast(*iChild), instanceType); + return findColocatedChild(child, instanceType); } } return 0; @@ -1442,13 +1424,10 @@ void keepReference(SbkObject* self, const char* key, PyObject* referredObject, b RefCountMap& refCountMap = *(self->d->referredObjects); RefCountMap::iterator iter = refCountMap.find(key); - std::list objects; if (iter != refCountMap.end()) { - objects = (*iter).second; - std::list::const_iterator found = std::find(objects.begin(), objects.end(), referredObject); - + const auto found = std::find(iter->second.begin(), iter->second.end(), referredObject); // skip if objects already exists - if (found != objects.end()) + if (found != iter->second.end()) return; } @@ -1456,13 +1435,13 @@ void keepReference(SbkObject* self, const char* key, PyObject* referredObject, b refCountMap[key].push_back(referredObject); Py_INCREF(referredObject); } else if (!append) { - if (objects.size() > 0) - decRefPyObjectList(objects, isNone ? 0 : referredObject); + if (iter != refCountMap.end() && !iter->second.empty()) + decRefPyObjectList(iter->second, isNone ? 0 : referredObject); if (isNone) { if (iter != refCountMap.end()) refCountMap.erase(iter); } else { - objects.clear(); + RefCountMap::mapped_type objects; objects.push_back(referredObject); refCountMap[key] = objects; Py_INCREF(referredObject); @@ -1492,9 +1471,8 @@ void clearReferences(SbkObject* self) return; RefCountMap& refCountMap = *(self->d->referredObjects); - RefCountMap::iterator iter; - for (iter = refCountMap.begin(); iter != refCountMap.end(); ++iter) - decRefPyObjectList(iter->second); + for (auto it = refCountMap.begin(), end = refCountMap.end(); it != end; ++it) + decRefPyObjectList(it->second); self->d->referredObjects->clear(); } @@ -1533,9 +1511,8 @@ std::string info(SbkObject* self) if (self->d->parentInfo && self->d->parentInfo->children.size()) { s << "children.......... "; - ChildrenList& children = self->d->parentInfo->children; - for (ChildrenList::const_iterator it = children.begin(); it != children.end(); ++it) { - Shiboken::AutoDecRef child(PyObject_Str(reinterpret_cast(*it))); + for (SbkObject *sbkChild : self->d->parentInfo->children) { + Shiboken::AutoDecRef child(PyObject_Str(reinterpret_cast(sbkChild))); s << String::toCString(child) << ' '; } s << '\n'; @@ -1544,14 +1521,12 @@ std::string info(SbkObject* self) if (self->d->referredObjects && self->d->referredObjects->size()) { Shiboken::RefCountMap& map = *self->d->referredObjects; s << "referred objects.. "; - Shiboken::RefCountMap::const_iterator it = map.begin(); - for (; it != map.end(); ++it) { + for (auto it = map.begin(), end = map.end(); it != end; ++it) { if (it != map.begin()) s << " "; s << '"' << it->first << "\" => "; - std::list::const_iterator j = it->second.begin(); - for (; j != it->second.end(); ++j) { - Shiboken::AutoDecRef obj(PyObject_Str(*j)); + for (PyObject *o : it->second) { + Shiboken::AutoDecRef obj(PyObject_Str(o)); s << String::toCString(obj) << ' '; } s << ' '; diff --git a/sources/shiboken2/libshiboken/bindingmanager.cpp b/sources/shiboken2/libshiboken/bindingmanager.cpp index 2cc7847a8..9d0e3d62e 100644 --- a/sources/shiboken2/libshiboken/bindingmanager.cpp +++ b/sources/shiboken2/libshiboken/bindingmanager.cpp @@ -78,14 +78,13 @@ public: file << "digraph D {\n"; - Edges::const_iterator i = m_edges.begin(); - for (; i != m_edges.end(); ++i) { - SbkObjectType* node1 = i->first; + for (auto i = m_edges.begin(), end = m_edges.end(); i != end; ++i) { + auto node1 = reinterpret_cast(i->first); const NodeList& nodeList = i->second; - NodeList::const_iterator j = nodeList.begin(); - for (; j != nodeList.end(); ++j) { - file << '"' << reinterpret_cast(*j)->tp_name << "\" -> \"" - << reinterpret_cast(node1)->tp_name << "\"\n"; + for (const SbkObjectType *o : nodeList) { + auto node2 = reinterpret_cast(o); + file << '"' << node2->tp_name << "\" -> \"" + << node1->tp_name << "\"\n"; } } file << "}\n"; @@ -97,9 +96,8 @@ public: Edges::const_iterator edgesIt = m_edges.find(type); if (edgesIt != m_edges.end()) { const NodeList& adjNodes = m_edges.find(type)->second; - NodeList::const_iterator i = adjNodes.begin(); - for (; i != adjNodes.end(); ++i) { - SbkObjectType* newType = identifyType(cptr, *i, baseType); + for (SbkObjectType *node : adjNodes) { + SbkObjectType* newType = identifyType(cptr, node, baseType); if (newType) return newType; } @@ -128,10 +126,9 @@ static void showWrapperMap(const WrapperMap& wrapperMap) if (Py_VerboseFlag > 0) { fprintf(stderr, "-------------------------------\n"); fprintf(stderr, "WrapperMap: %p (size: %d)\n", &wrapperMap, (int) wrapperMap.size()); - WrapperMap::const_iterator iter; - for (iter = wrapperMap.begin(); iter != wrapperMap.end(); ++iter) { - const SbkObject *sbkObj = iter->second; - fprintf(stderr, "key: %p, value: %p (%s, refcnt: %d)\n", iter->first, + for (auto it = wrapperMap.begin(), end = wrapperMap.end(); it != end; ++it) { + const SbkObject *sbkObj = it->second; + fprintf(stderr, "key: %p, value: %p (%s, refcnt: %d)\n", it->first, static_cast(sbkObj), (Py_TYPE(sbkObj))->tp_name, int(reinterpret_cast(sbkObj)->ob_refcnt)); diff --git a/sources/shiboken2/libshiboken/sbkconverter.cpp b/sources/shiboken2/libshiboken/sbkconverter.cpp index a13222de6..65844151f 100644 --- a/sources/shiboken2/libshiboken/sbkconverter.cpp +++ b/sources/shiboken2/libshiboken/sbkconverter.cpp @@ -246,10 +246,8 @@ PythonToCppFunc isPythonToCppPointerConvertible(SbkObjectType *type, PyObject *p static inline PythonToCppFunc IsPythonToCppConvertible(const SbkConverter *converter, PyObject *pyIn) { assert(pyIn); - const ToCppConversionList& convs = converter->toCppConversions; - for (ToCppConversionList::const_iterator conv = convs.begin(), end = convs.end(); conv != end; ++conv) { - PythonToCppFunc toCppFunc = 0; - if ((toCppFunc = (*conv).first(pyIn))) + for (const ToCppConversion &c : converter->toCppConversions) { + if (PythonToCppFunc toCppFunc = c.first(pyIn)) return toCppFunc; } return 0; @@ -361,7 +359,7 @@ bool isImplicitConversion(SbkObjectType *type, PythonToCppFunc toCppFunc) // Note that we don't check if the Python to C++ conversion is in // the list of the type's conversions, for it is expected that the // caller knows what he's doing. - ToCppConversionList::iterator conv = PepType_SOTP(type)->converter->toCppConversions.begin(); + const auto conv = PepType_SOTP(type)->converter->toCppConversions.cbegin(); return toCppFunc != (*conv).second; } diff --git a/sources/shiboken2/libshiboken/sbkenum.cpp b/sources/shiboken2/libshiboken/sbkenum.cpp index bd007f079..e413dc96e 100644 --- a/sources/shiboken2/libshiboken/sbkenum.cpp +++ b/sources/shiboken2/libshiboken/sbkenum.cpp @@ -650,8 +650,6 @@ DeclaredEnumTypes::DeclaredEnumTypes() DeclaredEnumTypes::~DeclaredEnumTypes() { - std::list::const_iterator it = m_enumTypes.begin(); - for (; it != m_enumTypes.end(); ++it) { /* * PYSIDE-595: This was "delete *it;" before introducing 'PyType_FromSpec'. * XXX what should I do now? @@ -660,8 +658,8 @@ DeclaredEnumTypes::~DeclaredEnumTypes() * So right now I am doing nothing. Surely wrong but no crash. * See also the comment in function 'createGlobalEnumItem'. */ - //fprintf(stderr, "ttt %d %s\n", Py_REFCNT(*it), *it->tp_name); - } + // for (PyTypeObject *o : m_enumTypes) + // fprintf(stderr, "ttt %d %s\n", Py_REFCNT(o), o->tp_name); m_enumTypes.clear(); } -- cgit v1.2.3