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
This commit is contained in:
Stefan Tröger 2016-09-21 17:19:57 +02:00 committed by wmayer
parent b101b11c67
commit 15b14783d1
6 changed files with 52 additions and 58 deletions

View File

@ -259,26 +259,4 @@ protected: // attributes
} //namespace App
/**
* Overload of freecads base class cast for all cases without any virtual inheritance
*/
template<typename T> T * freecad_dynamic_cast(App::DocumentObject * t)
{
if (t && t->isDerivedFrom(T::getClassTypeId()))
return static_cast<T*>(t);
else
return nullptr;
}
/**
* See explaination above
*/
template<typename T> const T * freecad_dynamic_cast(const App::DocumentObject * t)
{
if (t && t->isDerivedFrom(T::getClassTypeId()))
return static_cast<const T*>(t);
else
return nullptr;
}
#endif // APP_DOCUMENTOBJECT_H

View File

@ -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<typename T> T * freecad_dynamic_cast(App::PropertyContainer* t)
{
if (t && t->isDerivedFrom(T::getClassTypeId()))
return static_cast<T*>(t);
else
return nullptr;
}
/**
* See explaination above
*/
template<typename T> const T * freecad_dynamic_cast(const App::PropertyContainer* t)
{
if (t && t->isDerivedFrom(T::getClassTypeId()))
return static_cast<const T*>(t);
else
return nullptr;
}
#endif // APP_PROPERTYCONTAINER_H

View File

@ -130,8 +130,7 @@ void PropertyLink::Restore(Base::XMLReader &reader)
assert(getContainer()->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId()) );
if (name != "") {
DocumentObject* parent = dynamic_cast<DocumentObject*>(getContainer());
if(!parent) throw Base::TypeError("Property container is not document object");
DocumentObject* parent = static_cast<DocumentObject*>(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<DocumentObject*>(getContainer());
if(!father) throw Base::TypeError("Property container is not document object");
DocumentObject* father = static_cast<DocumentObject*>(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<DocumentObject*>(getContainer());
if(!parent) throw Base::TypeError("Property container is not document object");
auto* parent = static_cast<DocumentObject*>(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<DocumentObject*>(getContainer());
if(!father) throw Base::TypeError("Property container is not document object");
DocumentObject* father = static_cast<DocumentObject*>(getContainer());
App::Document* document = father->getDocument();
DocumentObject* child = document ? document->getObject(name.c_str()) : 0;
if (child)

View File

@ -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<App::DocumentObject*>(parent);
if (obj) {
if (parent->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId())) {
App::DocumentObject* obj = static_cast<App::DocumentObject*>(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<App::DocumentObject*>(c);
if (obj)
if (c->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId())) {
App::DocumentObject* obj = static_cast<App::DocumentObject*>(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<App::DocumentObject*>(c);
if (cobj)
list << QString::fromLatin1(cobj->getNameInDocument());
if (c->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId())) {
App::DocumentObject* obj = static_cast<App::DocumentObject*>(c);
list << QString::fromLatin1(obj->getNameInDocument());
}
else
list << QString::fromLatin1("Null");

View File

@ -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<App::DocumentObject*>(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<App::DocumentObject*>(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<App::DocumentObject*>(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<App::DocumentObject*>(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());

View File

@ -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<App::DocumentObject*>(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<App::DocumentObject*>(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<App::DocumentObject*>(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<App::DocumentObject*>(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());