From ee28daa6699eccb6dd2220bc2131c1ba52525e5f Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 27 Sep 2015 22:08:08 +0200 Subject: [PATCH] + replace old C casts with static_cast in generated C++ code, remove unused parent structure of Python classes, support of -1 as return value of setCustomAttributes --- src/Base/ParameterPy.cpp | 5 -- src/Base/PyObjectBase.cpp | 5 -- src/Base/PyObjectBase.h | 7 +-- src/Gui/WidgetFactory.cpp | 5 -- src/Mod/Test/Gui/UnitTestPy.cpp | 5 -- .../templateClassPyExport.py | 49 +++++++++---------- 6 files changed, 25 insertions(+), 51 deletions(-) diff --git a/src/Base/ParameterPy.cpp b/src/Base/ParameterPy.cpp index f0d3154a6..31c5f9e96 100644 --- a/src/Base/ParameterPy.cpp +++ b/src/Base/ParameterPy.cpp @@ -223,11 +223,6 @@ PyMethodDef ParameterGrpPy::Methods[] = { {NULL, NULL, 0, NULL} /* Sentinel */ }; -//-------------------------------------------------------------------------- -// Parents structure -//-------------------------------------------------------------------------- -PyParentObject ParameterGrpPy::Parents[] = {&PyObjectBase::Type,&ParameterGrpPy::Type, NULL}; - //-------------------------------------------------------------------------- // constructor //-------------------------------------------------------------------------- diff --git a/src/Base/PyObjectBase.cpp b/src/Base/PyObjectBase.cpp index 3c0e4c2a0..47bdd87ef 100644 --- a/src/Base/PyObjectBase.cpp +++ b/src/Base/PyObjectBase.cpp @@ -132,11 +132,6 @@ PyMethodDef PyObjectBase::Methods[] = { {NULL, NULL, 0, NULL} /* Sentinel */ }; -/*------------------------------ - * PyObjectBase Parents -- Every class, even the abstract one should have parents -------------------------------*/ -PyParentObject PyObjectBase::Parents[] = {&PyObjectBase::Type, NULL}; - PyObject* PyObjectBase::__getattr(PyObject * obj, char *attr) { // This should be the entry in Type diff --git a/src/Base/PyObjectBase.h b/src/Base/PyObjectBase.h index 36d668f51..9ad306332 100644 --- a/src/Base/PyObjectBase.h +++ b/src/Base/PyObjectBase.h @@ -143,9 +143,7 @@ typedef PyTypeObject * PyParentObject; public: \ static PyTypeObject Type; \ static PyMethodDef Methods[]; \ - static PyParentObject Parents[]; \ - virtual PyTypeObject *GetType(void) {return &Type;} \ - virtual PyParentObject *GetParents(void) {return Parents;} + virtual PyTypeObject *GetType(void) {return &Type;} /** This defines the _getattr_up macro * which allows attribute and method calls @@ -485,9 +483,6 @@ BaseExport extern PyObject* BaseExceptionFreeCADError; #endif // DONT_CATCH_CXX_EXCEPTIONS -/// Root definition of the inheritance tree of the FreeCAD python objects -#define PARENTSBasePyObjectBase &Base::PyObjectBase::Type,NULL - /** Python helper class * This class encapsulate the Decoding of UTF8 to a python object. * Including exception handling. diff --git a/src/Gui/WidgetFactory.cpp b/src/Gui/WidgetFactory.cpp index 189d4344b..b58ddae52 100644 --- a/src/Gui/WidgetFactory.cpp +++ b/src/Gui/WidgetFactory.cpp @@ -915,11 +915,6 @@ PyMethodDef PyResource::Methods[] = { {NULL, NULL} /* Sentinel */ }; -//-------------------------------------------------------------------------- -// Parents structure -//-------------------------------------------------------------------------- -PyParentObject PyResource::Parents[] = {&PyObjectBase::Type,&PyResource::Type, NULL}; - //-------------------------------------------------------------------------- // constructor //-------------------------------------------------------------------------- diff --git a/src/Mod/Test/Gui/UnitTestPy.cpp b/src/Mod/Test/Gui/UnitTestPy.cpp index d57e658f4..2311d0930 100644 --- a/src/Mod/Test/Gui/UnitTestPy.cpp +++ b/src/Mod/Test/Gui/UnitTestPy.cpp @@ -271,11 +271,6 @@ PyMethodDef TestGui::UnitTestPy::Methods[] = { {NULL, NULL} /* Sentinel */ }; -//-------------------------------------------------------------------------- -// Parents structure -//-------------------------------------------------------------------------- -PyParentObject TestGui::UnitTestPy::Parents[] = {&PyObjectBase::Type, NULL}; - //-------------------------------------------------------------------------- // Constructor //-------------------------------------------------------------------------- diff --git a/src/Tools/generateTemplates/templateClassPyExport.py b/src/Tools/generateTemplates/templateClassPyExport.py index 3bc5c7b44..b1b6d39ed 100644 --- a/src/Tools/generateTemplates/templateClassPyExport.py +++ b/src/Tools/generateTemplates/templateClassPyExport.py @@ -60,9 +60,7 @@ public: static PyObject * richCompare(PyObject *v, PyObject *w, int op); - static PyGetSetDef GetterSetter[]; - static PyParentObject Parents[]; virtual PyTypeObject *GetType(void) {return &Type;}; - virtual PyParentObject *GetParents(void) {return Parents;} public: @self.export.Name@(@self.export.TwinPointer@ *pcObject, PyTypeObject *T = &Type); @@ -201,6 +199,7 @@ public: /// getter method for special attributes (e.g. dynamic ones) PyObject *getCustomAttributes(const char* attr) const; /// setter for special attributes (e.g. dynamic ones) + /// Output: Success=1, Failure=-1, Ignore=0 int setCustomAttributes(const char* attr, PyObject *obj); PyObject *_getattr(char *attr); // __getattr__ function int _setattr(char *attr, PyObject *value); // __setattr__ function @@ -217,8 +216,6 @@ public: - }; -#define PARENTS@self.export.Namespace@@self.export.Name@ &@self.export.Name@::Type,PARENTS@self.export.FatherNamespace@@self.export.Father@ - } //namespace @self.export.Namespace@ #endif // @self.export.Namespace.upper()@_@self.export.Name.upper()@_H @@ -439,14 +436,14 @@ PyObject * @self.export.Name@::staticCallback_@i.Name@ (PyObject *self, PyObject - { // test if twin object not allready deleted - if (!((PyObjectBase*) self)->isValid()){ + if (!static_cast(self)->isValid()) { PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!"); return NULL; } + if (not i.Const): // test if object is set Const - if (((PyObjectBase*) self)->isConst()){ + if (static_cast(self)->isConst()) { PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a non const method"); return NULL; } @@ -454,13 +451,13 @@ PyObject * @self.export.Name@::staticCallback_@i.Name@ (PyObject *self, PyObject try { // catches all exceptions coming up from c++ and generate a python exception + if i.Keyword: - PyObject* ret = ((@self.export.Name@*)self)->@i.Name@(args, kwd); + PyObject* ret = static_cast<@self.export.Name@*>(self)->@i.Name@(args, kwd); = else: - PyObject* ret = ((@self.export.Name@*)self)->@i.Name@(args); + PyObject* ret = static_cast<@self.export.Name@*>(self)->@i.Name@(args); - + if (not i.Const): if (ret != 0) - ((@self.export.Name@*)self)->startNotify(); + static_cast<@self.export.Name@*>(self)->startNotify(); - return ret; } @@ -524,13 +521,13 @@ PyObject * @self.export.Name@::staticCallback_@i.Name@ (PyObject *self, PyObject // has to be implemented in @self.export.Name@Imp.cpp PyObject * @self.export.Name@::staticCallback_get@i.Name@ (PyObject *self, void * /*closure*/) { - if (!((PyObjectBase*) self)->isValid()){ + if (!static_cast(self)->isValid()){ PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!"); return NULL; } try { - return Py::new_reference_to(((@self.export.Name@*)self)->get@i.Name@()); + return Py::new_reference_to(static_cast<@self.export.Name@*>(self)->get@i.Name@()); } catch (const Py::Exception&) { // The exception text is already set return NULL; @@ -543,7 +540,7 @@ PyObject * @self.export.Name@::staticCallback_get@i.Name@ (PyObject *self, void + if (i.ReadOnly): int @self.export.Name@::staticCallback_set@i.Name@ (PyObject *self, PyObject * /*value*/, void * /*closure*/) { - if (!((PyObjectBase*) self)->isValid()){ + if (!static_cast(self)->isValid()){ PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!"); return -1; } @@ -553,21 +550,21 @@ int @self.export.Name@::staticCallback_set@i.Name@ (PyObject *self, PyObject * / } = else: int @self.export.Name@::staticCallback_set@i.Name@ (PyObject *self, PyObject *value, void * /*closure*/) -{ - if (!((PyObjectBase*) self)->isValid()){ +{ + if (!static_cast(self)->isValid()){ PyErr_SetString(PyExc_ReferenceError, "This object is already deleted most likely through closing a document. This reference is no longer valid!"); return -1; } - if (((PyObjectBase*) self)->isConst()){ + if (static_cast(self)->isConst()){ PyErr_SetString(PyExc_ReferenceError, "This object is immutable, you can not set any attribute or call a method"); return -1; } try { + if (i.Parameter.Type == "Float"): - ((@self.export.Name@*)self)->set@i.Name@(Py::@i.Parameter.Type@(PyNumber_Float(value),true)); + static_cast<@self.export.Name@*>(self)->set@i.Name@(Py::@i.Parameter.Type@(PyNumber_Float(value),true)); = else: - ((@self.export.Name@*)self)->set@i.Name@(Py::@i.Parameter.Type@(value,false)); + static_cast<@self.export.Name@*>(self)->set@i.Name@(Py::@i.Parameter.Type@(value,false)); - return 0; } catch (const Py::Exception&) { @@ -583,16 +580,12 @@ int @self.export.Name@::staticCallback_set@i.Name@ (PyObject *self, PyObject *va - -//-------------------------------------------------------------------------- -// Parents structure -//-------------------------------------------------------------------------- -PyParentObject @self.export.Name@::Parents[] = { PARENTS@self.export.Namespace@@self.export.Name@ }; //-------------------------------------------------------------------------- // Constructor //-------------------------------------------------------------------------- @self.export.Name@::@self.export.Name@(@self.export.TwinPointer@ *pcObject, PyTypeObject *T) - : @self.export.Father@(reinterpret_cast<@self.export.Father@::PointerType>(pcObject), T) + : @self.export.Father@(static_cast<@self.export.Father@::PointerType>(pcObject), T) { + if (self.export.Reference): pcObject->ref(); @@ -624,7 +617,7 @@ int @self.export.Name@::PyInit(PyObject* /*args*/, PyObject* /*kwd*/) - + if (self.export.Delete): // delete the handled object when the PyObject dies - @self.export.Name@::PointerType ptr = reinterpret_cast<@self.export.Name@::PointerType>(_pcTwinPointer); + @self.export.Name@::PointerType ptr = static_cast<@self.export.Name@::PointerType>(_pcTwinPointer); delete ptr; - } @@ -709,12 +702,18 @@ PyObject *@self.export.Name@::_getattr(char *attr) // __getattr__ function: n } } -int @self.export.Name@::_setattr(char *attr, PyObject *value) // __setattr__ function: note only need to handle new state +int @self.export.Name@::_setattr(char *attr, PyObject *value) // __setattr__ function: note only need to handle new state { try { // setter for special Attributes (e.g. dynamic ones) int r = setCustomAttributes(attr, value); - if(r==1) return 0; + // r = 1: handled + // r = -1: error + // r = 0: ignore + if (r == 1) + return 0; + else if (r == -1) + return -1; } #ifndef DONT_CATCH_CXX_EXCEPTIONS catch(const Base::Exception& e) // catch the FreeCAD exceptions