From b998bc181c978165cf33f0be7b81d7ccf4574ce2 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 1 Apr 2024 14:23:41 +0200 Subject: [PATCH] Base: Fix linter warnings in ParameterPy --- src/Base/ParameterPy.cpp | 89 ++++++++++++++++++++++++---------------- 1 file changed, 54 insertions(+), 35 deletions(-) diff --git a/src/Base/ParameterPy.cpp b/src/Base/ParameterPy.cpp index f6ea8d797c..a409093895 100644 --- a/src/Base/ParameterPy.cpp +++ b/src/Base/ParameterPy.cpp @@ -50,9 +50,8 @@ class ParameterGrpObserver: public ParameterGrp::ObserverType // NOLINT { public: explicit ParameterGrpObserver(const Py::Object& obj) - { - inst = obj; - } + : inst {obj} + {} ParameterGrpObserver(const Py::Object& obj, const Py::Object& callable, ParameterGrp* target) : callable(callable) , _target(target) @@ -68,7 +67,7 @@ public: { Base::PyGILStateLocker lock; try { - ParameterGrp& rGrp = static_cast(rCaller); + auto& rGrp = dynamic_cast(rCaller); ParameterGrp::handle hGrp(&rGrp); Py::Callable method(this->inst.getAttr(std::string("onChange"))); Py::Tuple args(2); @@ -90,9 +89,11 @@ public: } public: + // NOLINTBEGIN Py::Object callable; boost::signals2::scoped_connection conn; ParameterGrp* _target = nullptr; // no reference counted, do not access + // NOLINTEND private: Py::Object inst; @@ -110,6 +111,7 @@ public: Py::Object repr() override; + // NOLINTBEGIN Py::Object getGroup(const Py::Tuple&); Py::Object getGroupName(const Py::Tuple&); Py::Object getGroups(const Py::Tuple&); @@ -158,6 +160,14 @@ public: Py::Object exportTo(const Py::Tuple&); Py::Object getContents(const Py::Tuple&); + // NOLINTEND + +private: + void tryCall(ParameterGrpObserver* obs, + ParameterGrp* Param, + ParameterGrp::ParamType Type, + const char* Name, + const char* Value); private: ParameterGrp::handle _cParamGrp; @@ -315,7 +325,7 @@ Py::Object ParameterGrpPy::getGroup(const Py::Tuple& args) Base::Reference handle = _cParamGrp->GetGroup(pstr); if (handle.isValid()) { // create a python wrapper class - ParameterGrpPy* pcParamGrp = new ParameterGrpPy(handle); + auto pcParamGrp = new ParameterGrpPy(handle); // increment the ref count return Py::asObject(pcParamGrp); } @@ -338,7 +348,7 @@ Py::Object ParameterGrpPy::getManager(const Py::Tuple& args) Base::Reference handle = _cParamGrp->Manager(); if (handle.isValid()) { // create a python wrapper class - ParameterGrpPy* pcParamGrp = new ParameterGrpPy(handle); + auto pcParamGrp = new ParameterGrpPy(handle); // increment the ref count return Py::asObject(pcParamGrp); } @@ -356,7 +366,7 @@ Py::Object ParameterGrpPy::getParent(const Py::Tuple& args) Base::Reference handle = _cParamGrp->Parent(); if (handle.isValid()) { // create a python wrapper class - ParameterGrpPy* pcParamGrp = new ParameterGrpPy(handle); + auto pcParamGrp = new ParameterGrpPy(handle); // increment the ref count return Py::asObject(pcParamGrp); } @@ -697,13 +707,33 @@ Py::Object ParameterGrpPy::attach(const Py::Tuple& args) } } - ParameterGrpObserver* obs = new ParameterGrpObserver(o); + auto obs = new ParameterGrpObserver(o); _cParamGrp->Attach(obs); _observers.push_back(obs); return Py::None(); } +void ParameterGrpPy::tryCall(ParameterGrpObserver* obs, + ParameterGrp* Param, + ParameterGrp::ParamType Type, + const char* Name, + const char* Value) +{ + Base::PyGILStateLocker lock; + Py::TupleN args(Py::asObject(new ParameterGrpPy(Param)), + Py::String(ParameterGrp::TypeName(Type)), + Py::String(Name ? Name : ""), + Py::String(Value ? Value : "")); + try { + Py::Callable(obs->callable).apply(args); + } + catch (Py::Exception&) { + Base::PyException e; + e.ReportException(); + } +} + Py::Object ParameterGrpPy::attachManager(const Py::Tuple& args) { PyObject* obj = nullptr; @@ -731,33 +761,22 @@ Py::Object ParameterGrpPy::attachManager(const Py::Tuple& args) } } - ParameterGrpObserver* obs = new ParameterGrpObserver(o, attr, _cParamGrp); - obs->conn = - _cParamGrp->Manager()->signalParamChanged.connect([obs](ParameterGrp* Param, - ParameterGrp::ParamType Type, - const char* Name, - const char* Value) { - if (!Param) { - return; + auto obs = new ParameterGrpObserver(o, attr, _cParamGrp); + ParameterManager* man = _cParamGrp->Manager(); + obs->conn = man->signalParamChanged.connect([obs, this](ParameterGrp* Param, + ParameterGrp::ParamType Type, + const char* Name, + const char* Value) { + if (!Param) { + return; + } + for (auto p = Param; p; p = p->Parent()) { + if (p == obs->_target) { + tryCall(obs, Param, Type, Name, Value); + break; } - for (auto p = Param; p; p = p->Parent()) { - if (p == obs->_target) { - Base::PyGILStateLocker lock; - Py::TupleN args(Py::asObject(new ParameterGrpPy(Param)), - Py::String(ParameterGrp::TypeName(Type)), - Py::String(Name ? Name : ""), - Py::String(Value ? Value : "")); - try { - Py::Callable(obs->callable).apply(args); - } - catch (Py::Exception&) { - Base::PyException e; - e.ReportException(); - } - break; - } - } - }); + } + }); _observers.push_back(obs); return Py::None(); @@ -775,7 +794,7 @@ Py::Object ParameterGrpPy::detach(const Py::Tuple& args) throw Py::TypeError("Object has no onChange attribute"); } - for (ParameterGrpObserverList::iterator it = _observers.begin(); it != _observers.end(); ++it) { + for (auto it = _observers.begin(); it != _observers.end(); ++it) { if ((*it)->isEqual(o)) { ParameterGrpObserver* obs = *it; _observers.erase(it);