From 15b14783d14890d8d13108a54ad7b8f06f71cd0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Tr=C3=B6ger?= Date: Wed, 21 Sep 2016 17:19:57 +0200 Subject: [PATCH] Extensions: Handle static/dynamic cast After the change of the virtual inheritance position in the inheritance chain some formally added dynamic_cast's can be reverted to the old behaviour --- src/App/DocumentObject.h | 22 -------------------- src/App/PropertyContainer.h | 24 +++++++++++++++++++++- src/App/PropertyLinks.cpp | 12 ++++------- src/Gui/propertyeditor/PropertyItem.cpp | 16 ++++++++------- src/Mod/Fem/App/PropertyPostDataObject.cpp | 18 ++++++++-------- src/Mod/Part/App/PropertyTopoShape.cpp | 18 ++++++++-------- 6 files changed, 52 insertions(+), 58 deletions(-) diff --git a/src/App/DocumentObject.h b/src/App/DocumentObject.h index c1026b81a..6e1d997f3 100644 --- a/src/App/DocumentObject.h +++ b/src/App/DocumentObject.h @@ -259,26 +259,4 @@ protected: // attributes } //namespace App -/** - * Overload of freecads base class cast for all cases without any virtual inheritance - */ -template T * freecad_dynamic_cast(App::DocumentObject * t) -{ - if (t && t->isDerivedFrom(T::getClassTypeId())) - return static_cast(t); - else - return nullptr; -} - -/** - * See explaination above - */ -template const T * freecad_dynamic_cast(const App::DocumentObject * t) -{ - if (t && t->isDerivedFrom(T::getClassTypeId())) - return static_cast(t); - else - return nullptr; -} - #endif // APP_DOCUMENTOBJECT_H diff --git a/src/App/PropertyContainer.h b/src/App/PropertyContainer.h index 8e9a0a985..cec58250f 100644 --- a/src/App/PropertyContainer.h +++ b/src/App/PropertyContainer.h @@ -270,7 +270,29 @@ template<> void _class_::init(void){\ _class_::propertyData.addParentPropertyData(_parentclass_::getPropertyDataPtr());\ } - } // namespace App + +/** + * Overload of freecads base class cast for all cases without any virtual inheritance + */ +template T * freecad_dynamic_cast(App::PropertyContainer* t) +{ + if (t && t->isDerivedFrom(T::getClassTypeId())) + return static_cast(t); + else + return nullptr; +} + +/** + * See explaination above + */ +template const T * freecad_dynamic_cast(const App::PropertyContainer* t) +{ + if (t && t->isDerivedFrom(T::getClassTypeId())) + return static_cast(t); + else + return nullptr; +} + #endif // APP_PROPERTYCONTAINER_H diff --git a/src/App/PropertyLinks.cpp b/src/App/PropertyLinks.cpp index 9e5466981..d088eeb17 100644 --- a/src/App/PropertyLinks.cpp +++ b/src/App/PropertyLinks.cpp @@ -130,8 +130,7 @@ void PropertyLink::Restore(Base::XMLReader &reader) assert(getContainer()->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId()) ); if (name != "") { - DocumentObject* parent = dynamic_cast(getContainer()); - if(!parent) throw Base::TypeError("Property container is not document object"); + DocumentObject* parent = static_cast(getContainer()); App::Document* document = parent->getDocument(); DocumentObject* object = document ? document->getObject(name.c_str()) : 0; if (!object) { @@ -289,8 +288,7 @@ void PropertyLinkList::Restore(Base::XMLReader &reader) // referenced objects in XML which do not exist anymore in the new // document. Thus, we should silently ingore this. // Property not in an object! - DocumentObject* father = dynamic_cast(getContainer()); - if(!father) throw Base::TypeError("Property container is not document object"); + DocumentObject* father = static_cast(getContainer()); App::Document* document = father->getDocument(); DocumentObject* child = document ? document->getObject(name.c_str()) : 0; if (child) @@ -482,8 +480,7 @@ void PropertyLinkSub::Restore(Base::XMLReader &reader) DocumentObject *pcObject; if (!name.empty()) { - auto* parent = dynamic_cast(getContainer()); - if(!parent) throw Base::TypeError("Property container is not document object"); + auto* parent = static_cast(getContainer()); App::Document* document = parent->getDocument(); pcObject = document ? document->getObject(name.c_str()) : 0; if (!pcObject) { @@ -824,8 +821,7 @@ void PropertyLinkSubList::Restore(Base::XMLReader &reader) // referenced objects in XML which do not exist anymore in the new // document. Thus, we should silently ignore this. // Property not in an object! - DocumentObject* father = dynamic_cast(getContainer()); - if(!father) throw Base::TypeError("Property container is not document object"); + DocumentObject* father = static_cast(getContainer()); App::Document* document = father->getDocument(); DocumentObject* child = document ? document->getObject(name.c_str()) : 0; if (child) diff --git a/src/Gui/propertyeditor/PropertyItem.cpp b/src/Gui/propertyeditor/PropertyItem.cpp index 23533541b..4e6feba1d 100644 --- a/src/Gui/propertyeditor/PropertyItem.cpp +++ b/src/Gui/propertyeditor/PropertyItem.cpp @@ -288,8 +288,8 @@ QString PropertyItem::pythonIdentifier(const App::Property* prop) const QString propName = QString::fromLatin1(parent->getPropertyName(prop)); return QString::fromLatin1("FreeCAD.getDocument(\"%1\").%2").arg(docName).arg(propName); } - App::DocumentObject* obj = dynamic_cast(parent); - if (obj) { + if (parent->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId())) { + App::DocumentObject* obj = static_cast(parent); App::Document* doc = obj->getDocument(); QString docName = QString::fromLatin1(App::GetApplication().getDocumentName(doc)); QString objName = QString::fromLatin1(obj->getNameInDocument()); @@ -3375,9 +3375,10 @@ QVariant PropertyLinkItem::value(const App::Property* prop) const else { // no object assigned // the document name - App::DocumentObject* obj = dynamic_cast(c); - if (obj) + if (c->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId())) { + App::DocumentObject* obj = static_cast(c); list << QString::fromLatin1(obj->getDocument()->getName()); + } else list << QString::fromLatin1(""); @@ -3388,9 +3389,10 @@ QVariant PropertyLinkItem::value(const App::Property* prop) const } // the name of this object - App::DocumentObject* cobj = dynamic_cast(c); - if (cobj) - list << QString::fromLatin1(cobj->getNameInDocument()); + if (c->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId())) { + App::DocumentObject* obj = static_cast(c); + list << QString::fromLatin1(obj->getNameInDocument()); + } else list << QString::fromLatin1("Null"); diff --git a/src/Mod/Fem/App/PropertyPostDataObject.cpp b/src/Mod/Fem/App/PropertyPostDataObject.cpp index 3d8ce7cc5..73f21bcd5 100644 --- a/src/Mod/Fem/App/PropertyPostDataObject.cpp +++ b/src/Mod/Fem/App/PropertyPostDataObject.cpp @@ -263,11 +263,10 @@ void PropertyPostDataObject::SaveDocFile (Base::Writer &writer) const // We only print an error message but continue writing the next files to the // stream... App::PropertyContainer* father = this->getContainer(); - if (father) { - App::DocumentObject* obj = dynamic_cast(father); - if(obj) - Base::Console().Error("Dataset of '%s' cannot be written to vtk file '%s'\n", - obj->Label.getValue(),fi.filePath().c_str()); + if (father && father->isDerivedFrom(App::DocumentObject::getClassTypeId())) { + App::DocumentObject* obj = static_cast(father); + Base::Console().Error("Dataset of '%s' cannot be written to vtk file '%s'\n", + obj->Label.getValue(),fi.filePath().c_str()); } else { Base::Console().Error("Cannot save vtk file '%s'\n", fi.filePath().c_str()); @@ -332,11 +331,10 @@ void PropertyPostDataObject::RestoreDocFile(Base::Reader &reader) // We only print an error message but continue reading the next files from the // stream... App::PropertyContainer* father = this->getContainer(); - if (father) { - App::DocumentObject* obj = dynamic_cast(father); - if(obj) - Base::Console().Error("Dataset file '%s' with data of '%s' seems to be empty\n", - fi.filePath().c_str(),obj->Label.getValue()); + if (father && father->isDerivedFrom(App::DocumentObject::getClassTypeId())) { + App::DocumentObject* obj = static_cast(father); + Base::Console().Error("Dataset file '%s' with data of '%s' seems to be empty\n", + fi.filePath().c_str(),obj->Label.getValue()); } else { Base::Console().Warning("Loaded Dataset file '%s' seems to be empty\n", fi.filePath().c_str()); diff --git a/src/Mod/Part/App/PropertyTopoShape.cpp b/src/Mod/Part/App/PropertyTopoShape.cpp index a24bbb23f..433d822e6 100644 --- a/src/Mod/Part/App/PropertyTopoShape.cpp +++ b/src/Mod/Part/App/PropertyTopoShape.cpp @@ -296,11 +296,10 @@ void PropertyPartShape::SaveDocFile (Base::Writer &writer) const // We only print an error message but continue writing the next files to the // stream... App::PropertyContainer* father = this->getContainer(); - if (father) { - App::DocumentObject* obj = dynamic_cast(father); - if(obj) - Base::Console().Error("Shape of '%s' cannot be written to BRep file '%s'\n", - obj->Label.getValue(),fi.filePath().c_str()); + if (father && father->isDerivedFrom(App::DocumentObject::getClassTypeId())) { + App::DocumentObject* obj = static_cast(father); + Base::Console().Error("Shape of '%s' cannot be written to BRep file '%s'\n", + obj->Label.getValue(),fi.filePath().c_str()); } else { Base::Console().Error("Cannot save BRep file '%s'\n", fi.filePath().c_str()); @@ -376,11 +375,10 @@ void PropertyPartShape::RestoreDocFile(Base::Reader &reader) // We only print an error message but continue reading the next files from the // stream... App::PropertyContainer* father = this->getContainer(); - if (father) { - App::DocumentObject* obj = dynamic_cast(father); - if(obj) - Base::Console().Error("BRep file '%s' with shape of '%s' seems to be empty\n", - fi.filePath().c_str(),obj->Label.getValue()); + if (father && father->isDerivedFrom(App::DocumentObject::getClassTypeId())) { + App::DocumentObject* obj = static_cast(father); + Base::Console().Error("BRep file '%s' with shape of '%s' seems to be empty\n", + fi.filePath().c_str(),obj->Label.getValue()); } else { Base::Console().Warning("Loaded BRep file '%s' seems to be empty\n", fi.filePath().c_str());