From a9828d6feaf392529cedafe51390bf1b876c90cc Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 20:24:25 +0100 Subject: [PATCH 01/17] Base: Use isBad() instead of comparing types with == --- src/Base/BaseClass.cpp | 8 ++++---- src/Base/BaseClassPyImp.cpp | 2 +- src/Base/TypePyImp.cpp | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Base/BaseClass.cpp b/src/Base/BaseClass.cpp index 301cb5c33b..9e7978fd9a 100644 --- a/src/Base/BaseClass.cpp +++ b/src/Base/BaseClass.cpp @@ -56,11 +56,11 @@ BaseClass::~BaseClass() = default; void BaseClass::init() { - assert(BaseClass::classTypeId == Type::badType() && "don't init() twice!"); + assert(BaseClass::classTypeId.isBad() && "don't init() twice!"); /* Make sure superclass gets initialized before subclass. */ /*assert(strcmp(#_parentclass_), "inherited"));*/ /*Type parentType(Type::fromName(#_parentclass_));*/ - /*assert(parentType != Type::badType() && "you forgot init() on parentclass!");*/ + /*assert(!parentType.isBad() && "you forgot init() on parentclass!");*/ /* Set up entry in the type system. */ BaseClass::classTypeId = @@ -84,11 +84,11 @@ void BaseClass::initSubclass(Base::Type& toInit, Type::instantiationMethod method) { // don't init twice! - assert(toInit == Base::Type::badType()); + assert(toInit.isBad()); // get the parent class Base::Type parentType(Base::Type::fromName(ParentName)); // forgot init parent! - assert(parentType != Base::Type::badType()); + assert(!parentType.isBad()); // create the new type toInit = Base::Type::createType(parentType, ClassName, method); diff --git a/src/Base/BaseClassPyImp.cpp b/src/Base/BaseClassPyImp.cpp index d432672264..ac17bb9bc3 100644 --- a/src/Base/BaseClassPyImp.cpp +++ b/src/Base/BaseClassPyImp.cpp @@ -44,7 +44,7 @@ PyObject* BaseClassPy::isDerivedFrom(PyObject* args) } Base::Type type = Base::Type::fromName(name); - bool valid = (type != Base::Type::badType() && getBaseClassPtr()->isDerivedFrom(type)); + bool valid = (!type.isBad() && getBaseClassPtr()->isDerivedFrom(type)); return PyBool_FromLong(valid ? 1 : 0); } diff --git a/src/Base/TypePyImp.cpp b/src/Base/TypePyImp.cpp index c997bb69c4..bb7dd94f89 100644 --- a/src/Base/TypePyImp.cpp +++ b/src/Base/TypePyImp.cpp @@ -122,7 +122,7 @@ PyObject* TypePy::isDerivedFrom(PyObject* args) return nullptr; } while (false); - bool val = (type != Base::Type::badType() && getBaseTypePtr()->isDerivedFrom(type)); + bool val = (!type.isBad() && getBaseTypePtr()->isDerivedFrom(type)); return PyBool_FromLong(val ? 1 : 0); } From 69ffc443d6986560e5d30d102647b7e599ff5a5e Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 20:34:22 +0100 Subject: [PATCH 02/17] App: Use isBad() instead of comparing types with == --- src/App/Extension.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/App/Extension.cpp b/src/App/Extension.cpp index c84e613de4..25a6032a2e 100644 --- a/src/App/Extension.cpp +++ b/src/App/Extension.cpp @@ -54,7 +54,7 @@ App::PropertyData App::Extension::propertyData; void App::Extension::init() { - assert(Extension::classTypeId == Base::Type::badType() && "don't init() twice!"); + assert(Extension::classTypeId.isBad() && "don't init() twice!"); /* Set up entry in the type system. */ Extension::classTypeId = @@ -187,11 +187,11 @@ void Extension::initExtensionSubclass(Base::Type& toInit, Base::Type::instantiationMethod method) { // don't init twice! - assert(toInit == Base::Type::badType()); + assert(toInit.isBad()); // get the parent class Base::Type parentType(Base::Type::fromName(ParentName)); // forgot init parent! - assert(parentType != Base::Type::badType()); + assert(!parentType.isBad()); // create the new type toInit = Base::Type::createType(parentType, ClassName, method); From 8af7f1b043e08edddce1e69fed6df0388f87dd87 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 20:34:38 +0100 Subject: [PATCH 03/17] Gui: Use isBad() instead of comparing types with == --- src/Gui/DAGView/DAGFilter.cpp | 4 ++-- src/Gui/DAGView/DAGModel.cpp | 6 +++--- src/Gui/Dialogs/DlgAddPropertyVarSet.cpp | 2 +- src/Gui/Dialogs/DlgExpressionInput.cpp | 2 +- src/Gui/Selection/Selection.cpp | 6 +++--- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Gui/DAGView/DAGFilter.cpp b/src/Gui/DAGView/DAGFilter.cpp index e331d52d2e..1545143a27 100644 --- a/src/Gui/DAGView/DAGFilter.cpp +++ b/src/Gui/DAGView/DAGFilter.cpp @@ -44,7 +44,7 @@ FilterOrigin::FilterOrigin() : FilterBase() bool FilterOrigin::goFilter(const Vertex &vertexIn, const Graph &graphIn, const GraphLinkContainer &linkIn) const { Base::Type originType = Base::Type::fromName("App::Origin"); - assert (originType != Base::Type::badType()); + assert (!originType.isBad()); //if child of origin hide. InEdgeIterator it, itEnd; for (boost::tie(it, itEnd) = boost::in_edges(vertexIn, graphIn); it != itEnd; ++it) @@ -72,7 +72,7 @@ bool FilterTyped::goFilter(const Gui::DAG::Vertex& vertexIn, const Graph& graphI if (type.empty()) return false; Base::Type theType = Base::Type::fromName(type.c_str()); - if (theType == Base::Type::badType()) + if (theType.isBad()) return false; const GraphLinkRecord &sourceRecord = findRecord(vertexIn, linkIn); diff --git a/src/Gui/DAGView/DAGModel.cpp b/src/Gui/DAGView/DAGModel.cpp index 9dc8a902a9..a699d94470 100644 --- a/src/Gui/DAGView/DAGModel.cpp +++ b/src/Gui/DAGView/DAGModel.cpp @@ -1186,11 +1186,11 @@ void Model::visiblyIsolate(Gui::DAG::Vertex sourceIn) std::vector out; Base::Type type; type = Base::Type::fromName("App::DocumentObjectGroup"); - if (type != Base::Type::badType()) out.push_back(type); + if (!type.isBad()) out.push_back(type); type = Base::Type::fromName("App::Part"); - if (type != Base::Type::badType()) out.push_back(type); + if (!type.isBad()) out.push_back(type); type = Base::Type::fromName("PartDesign::Body"); - if (type != Base::Type::badType()) out.push_back(type); + if (!type.isBad()) out.push_back(type); return out; }; diff --git a/src/Gui/Dialogs/DlgAddPropertyVarSet.cpp b/src/Gui/Dialogs/DlgAddPropertyVarSet.cpp index 312666ded2..1b505e7365 100644 --- a/src/Gui/Dialogs/DlgAddPropertyVarSet.cpp +++ b/src/Gui/Dialogs/DlgAddPropertyVarSet.cpp @@ -441,7 +441,7 @@ void DlgAddPropertyVarSet::checkGroup() { void DlgAddPropertyVarSet::checkType() { std::string type = ui->comboBoxType->currentText().toStdString(); - if (Base::Type::fromName(type.c_str()) == Base::Type::badType()) { + if (Base::Type::fromName(type.c_str()).isBad()) { throw CreatePropertyException("Invalid name"); } } diff --git a/src/Gui/Dialogs/DlgExpressionInput.cpp b/src/Gui/Dialogs/DlgExpressionInput.cpp index 98e279af6a..708bf25c2e 100644 --- a/src/Gui/Dialogs/DlgExpressionInput.cpp +++ b/src/Gui/Dialogs/DlgExpressionInput.cpp @@ -190,7 +190,7 @@ Base::Type DlgExpressionInput::determineTypeVarSet() bool DlgExpressionInput::typeOkForVarSet() { std::string unitType = impliedUnit.getTypeString(); - return determineTypeVarSet() != Base::Type::badType(); + return !determineTypeVarSet().isBad(); } void DlgExpressionInput::initializeVarSets() diff --git a/src/Gui/Selection/Selection.cpp b/src/Gui/Selection/Selection.cpp index 0c9b8c65a8..9a9a2ea94b 100644 --- a/src/Gui/Selection/Selection.cpp +++ b/src/Gui/Selection/Selection.cpp @@ -330,7 +330,7 @@ std::vector SelectionSingleton::getObjectList(const char* pDocN std::map SortMap; // check the type - if (typeId == Base::Type::badType()) + if (typeId.isBad()) return temp; App::Document *pcDoc = nullptr; @@ -519,7 +519,7 @@ vector SelectionSingleton::getObjectsOfType(const Base::Ty std::vector SelectionSingleton::getObjectsOfType(const char* typeName, const char* pDocName, ResolveMode resolve) const { Base::Type typeId = Base::Type::fromName(typeName); - if (typeId == Base::Type::badType()) + if (typeId.isBad()) return {}; return getObjectsOfType(typeId, pDocName, resolve); } @@ -541,7 +541,7 @@ unsigned int SelectionSingleton::countObjectsOfType(const Base::Type& typeId, co unsigned int SelectionSingleton::countObjectsOfType(const char* typeName, const char* pDocName, ResolveMode resolve) const { Base::Type typeId = Base::Type::fromName(typeName); - if (typeId == Base::Type::badType()) + if (typeId.isBad()) return 0; return countObjectsOfType(typeId, pDocName, resolve); } From 2c966f502085a83d1d8c0411076f8be0df166f72 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 20:34:45 +0100 Subject: [PATCH 04/17] Part: Use isBad() instead of comparing types with == --- src/Mod/Part/App/GeometryPyImp.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Mod/Part/App/GeometryPyImp.cpp b/src/Mod/Part/App/GeometryPyImp.cpp index 8cc3b90743..9c7488270a 100644 --- a/src/Mod/Part/App/GeometryPyImp.cpp +++ b/src/Mod/Part/App/GeometryPyImp.cpp @@ -242,7 +242,7 @@ PyObject* GeometryPy::getExtensionOfType(PyObject *args) Base::Type type = Base::Type::fromName(o); - if(type != Base::Type::badType()) { + if(!type.isBad()) { try { std::shared_ptr ext(this->getGeometryPtr()->getExtension(type)); @@ -313,7 +313,7 @@ PyObject* GeometryPy::hasExtensionOfType(PyObject *args) Base::Type type = Base::Type::fromName(o); - if(type != Base::Type::badType()) { + if(!type.isBad()) { try { return Py::new_reference_to(Py::Boolean(this->getGeometryPtr()->hasExtension(type))); } @@ -360,7 +360,7 @@ PyObject* GeometryPy::deleteExtensionOfType(PyObject *args) Base::Type type = Base::Type::fromName(o); - if(type != Base::Type::badType()) { + if(!type.isBad()) { try { this->getGeometryPtr()->deleteExtension(type); Py_Return; From 142c061a90f72f890ca1bee429c7b6e8258b2f87 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 20:34:55 +0100 Subject: [PATCH 05/17] Sketcher: Use isBad() instead of comparing types with == --- src/Mod/Sketcher/App/ExternalGeometryFacadePyImp.cpp | 6 +++--- src/Mod/Sketcher/App/GeometryFacadePyImp.cpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Mod/Sketcher/App/ExternalGeometryFacadePyImp.cpp b/src/Mod/Sketcher/App/ExternalGeometryFacadePyImp.cpp index cebe5f27b1..ef0166f24f 100644 --- a/src/Mod/Sketcher/App/ExternalGeometryFacadePyImp.cpp +++ b/src/Mod/Sketcher/App/ExternalGeometryFacadePyImp.cpp @@ -302,7 +302,7 @@ PyObject* ExternalGeometryFacadePy::getExtensionOfType(PyObject* args) Base::Type type = Base::Type::fromName(o); - if (type != Base::Type::badType()) { + if (!type.isBad()) { try { std::shared_ptr ext( this->getExternalGeometryFacadePtr()->getExtension(type)); @@ -379,7 +379,7 @@ PyObject* ExternalGeometryFacadePy::hasExtensionOfType(PyObject* args) Base::Type type = Base::Type::fromName(o); - if (type != Base::Type::badType()) { + if (!type.isBad()) { try { return Py::new_reference_to( Py::Boolean(this->getExternalGeometryFacadePtr()->hasExtension(type))); @@ -427,7 +427,7 @@ PyObject* ExternalGeometryFacadePy::deleteExtensionOfType(PyObject* args) Base::Type type = Base::Type::fromName(o); - if (type != Base::Type::badType()) { + if (!type.isBad()) { try { this->getExternalGeometryFacadePtr()->deleteExtension(type); Py_Return; diff --git a/src/Mod/Sketcher/App/GeometryFacadePyImp.cpp b/src/Mod/Sketcher/App/GeometryFacadePyImp.cpp index 1aa73bad22..8a0ee1d17a 100644 --- a/src/Mod/Sketcher/App/GeometryFacadePyImp.cpp +++ b/src/Mod/Sketcher/App/GeometryFacadePyImp.cpp @@ -278,7 +278,7 @@ PyObject* GeometryFacadePy::getExtensionOfType(PyObject* args) Base::Type type = Base::Type::fromName(o); - if (type != Base::Type::badType()) { + if (!type.isBad()) { try { std::shared_ptr ext( this->getGeometryFacadePtr()->getExtension(type)); @@ -355,7 +355,7 @@ PyObject* GeometryFacadePy::hasExtensionOfType(PyObject* args) Base::Type type = Base::Type::fromName(o); - if (type != Base::Type::badType()) { + if (!type.isBad()) { try { return Py::new_reference_to( Py::Boolean(this->getGeometryFacadePtr()->hasExtension(type))); @@ -403,7 +403,7 @@ PyObject* GeometryFacadePy::deleteExtensionOfType(PyObject* args) Base::Type type = Base::Type::fromName(o); - if (type != Base::Type::badType()) { + if (!type.isBad()) { try { this->getGeometryFacadePtr()->deleteExtension(type); Py_Return; From 27222321c6700db21d7c1385f339e693993568df Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 20:24:40 +0100 Subject: [PATCH 06/17] Base: Use constant for Type::BadType instead Type::badType() --- src/Base/BaseClass.cpp | 5 ++--- src/Base/BaseClass.h | 4 ++-- src/Base/Type.cpp | 23 +++++++++-------------- src/Base/Type.h | 2 +- src/Base/TypePyImp.cpp | 3 +-- 5 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/Base/BaseClass.cpp b/src/Base/BaseClass.cpp index 9e7978fd9a..16c0ee8341 100644 --- a/src/Base/BaseClass.cpp +++ b/src/Base/BaseClass.cpp @@ -32,7 +32,7 @@ using namespace Base; -Type BaseClass::classTypeId = Base::Type::badType(); // NOLINT +Type BaseClass::classTypeId = Base::Type::BadType; //************************************************************************** @@ -63,8 +63,7 @@ void BaseClass::init() /*assert(!parentType.isBad() && "you forgot init() on parentclass!");*/ /* Set up entry in the type system. */ - BaseClass::classTypeId = - Type::createType(Type::badType(), "Base::BaseClass", BaseClass::create); + BaseClass::classTypeId = Type::createType(Type::BadType, "Base::BaseClass", BaseClass::create); } Type BaseClass::getClassTypeId() diff --git a/src/Base/BaseClass.h b/src/Base/BaseClass.h index 33d7ced2f2..3c3dced6d8 100644 --- a/src/Base/BaseClass.h +++ b/src/Base/BaseClass.h @@ -65,7 +65,7 @@ private: { \ return _class_::classTypeId; \ } \ - Base::Type _class_::classTypeId = Base::Type::badType(); \ + Base::Type _class_::classTypeId = Base::Type::BadType; \ void* _class_::create(void) \ { \ return new _class_(); \ @@ -99,7 +99,7 @@ private: { \ return _class_::classTypeId; \ } \ - Base::Type _class_::classTypeId = Base::Type::badType(); \ + Base::Type _class_::classTypeId = Base::Type::BadType; \ void* _class_::create(void) \ { \ return 0; \ diff --git a/src/Base/Type.cpp b/src/Base/Type.cpp index 421833305f..6a55ab669b 100644 --- a/src/Base/Type.cpp +++ b/src/Base/Type.cpp @@ -37,8 +37,8 @@ using namespace Base; struct Base::TypeData { TypeData(const char* theName, - const Type type = Type::badType(), - const Type theParent = Type::badType(), + const Type type = Type::BadType, + const Type theParent = Type::BadType, Type::instantiationMethod method = nullptr) : name(theName) , parent(theParent) @@ -56,6 +56,8 @@ std::map Type::typemap; std::vector Type::typedata; std::set Type::loadModuleSet; +const Type Type::BadType; + void* Type::createInstance() const { instantiationMethod method = typedata[index]->instMethod; @@ -77,7 +79,7 @@ void* Type::createInstanceByName(const char* TypeName, bool bLoadModule) // now the type should be in the type map Type type = fromName(TypeName); - if (type == badType()) { + if (type == BadType) { return nullptr; } @@ -117,13 +119,6 @@ std::string Type::getModuleName(const char* ClassName) : std::string(); } -Type Type::badType() -{ - Type bad; - bad.index = 0; - return bad; -} - Type Type::createType(const Type& parent, const char* name, instantiationMethod method) { @@ -167,7 +162,7 @@ Type Type::fromName(const char* name) return typedata[pos->second]->type; } - return Type::badType(); + return Type::BadType; } Type Type::fromKey(unsigned int key) @@ -176,7 +171,7 @@ Type Type::fromKey(unsigned int key) return typedata[key]->type; } - return Type::badType(); + return BadType; } const char* Type::getName() const @@ -198,7 +193,7 @@ bool Type::isDerivedFrom(const Type& type) const return true; } temp = temp.getParent(); - } while (temp != badType()); + } while (!temp.isBad()); return false; } @@ -233,5 +228,5 @@ Type Type::getTypeIfDerivedFrom(const char* name, const Type& parent, bool bLoad return type; } - return Type::badType(); + return BadType; } diff --git a/src/Base/Type.h b/src/Base/Type.h index fad31905db..b54455e566 100644 --- a/src/Base/Type.h +++ b/src/Base/Type.h @@ -126,7 +126,7 @@ public: bool operator>=(const Type& type) const; bool operator>(const Type& type) const; - static Type badType(); + static const Type BadType; static void init(); static void destruct(); diff --git a/src/Base/TypePyImp.cpp b/src/Base/TypePyImp.cpp index bb7dd94f89..7dc9336584 100644 --- a/src/Base/TypePyImp.cpp +++ b/src/Base/TypePyImp.cpp @@ -76,8 +76,7 @@ PyObject* TypePy::getBadType(PyObject* args) return nullptr; } - Base::Type type = Base::Type::badType(); - return new TypePy(new Base::Type(type)); + return new TypePy(new Base::Type(Base::Type::BadType)); } PyObject* TypePy::getParent(PyObject* args) From e840c840c0e9c16482e7b2565cf5636e178fd7e5 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 22:10:53 +0100 Subject: [PATCH 07/17] App: Use constant for Type::BadType instead Type::badType() --- src/App/Extension.cpp | 2 +- src/App/Extension.h | 6 +++--- src/App/ProjectFile.cpp | 4 ++-- src/App/PropertyContainer.h | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/App/Extension.cpp b/src/App/Extension.cpp index 25a6032a2e..44170f5988 100644 --- a/src/App/Extension.cpp +++ b/src/App/Extension.cpp @@ -58,7 +58,7 @@ void App::Extension::init() /* Set up entry in the type system. */ Extension::classTypeId = - Base::Type::createType(Base::Type::badType(), "App::Extension", Extension::create); + Base::Type::createType(Base::Type::BadType, "App::Extension", Extension::create); } using namespace App; diff --git a/src/App/Extension.h b/src/App/Extension.h index 7c1545435e..09672b4816 100644 --- a/src/App/Extension.h +++ b/src/App/Extension.h @@ -56,7 +56,7 @@ private: \ #define EXTENSION_TYPESYSTEM_SOURCE_P(_class_) \ Base::Type _class_::getExtensionClassTypeId(void) { return _class_::classTypeId; } \ Base::Type _class_::getExtensionTypeId(void) const { return _class_::classTypeId; } \ -Base::Type _class_::classTypeId = Base::Type::badType(); \ +Base::Type _class_::classTypeId = Base::Type::BadType; \ void * _class_::create(void){\ return new _class_ ();\ } @@ -65,7 +65,7 @@ void * _class_::create(void){\ #define EXTENSION_TYPESYSTEM_SOURCE_ABSTRACT_P(_class_) \ Base::Type _class_::getExtensionClassTypeId(void) { return _class_::classTypeId; } \ Base::Type _class_::getExtensionTypeId(void) const { return _class_::classTypeId; } \ -Base::Type _class_::classTypeId = Base::Type::badType(); \ +Base::Type _class_::classTypeId = Base::Type::BadType; \ void * _class_::create(void){return 0;} /// define to implement a subclass of Base::BaseClass @@ -76,7 +76,7 @@ void _class_::init(void){\ } #define EXTENSION_TYPESYSTEM_SOURCE_TEMPLATE(_class_) \ -template<> Base::Type _class_::classTypeId = Base::Type::badType(); \ +template<> Base::Type _class_::classTypeId = Base::Type::BadType; \ template<> Base::Type _class_::getExtensionClassTypeId(void) { return _class_::classTypeId; } \ template<> Base::Type _class_::getExtensionTypeId(void) const { return _class_::classTypeId; } \ template<> void * _class_::create(void){\ diff --git a/src/App/ProjectFile.cpp b/src/App/ProjectFile.cpp index 7cc7490100..2363111746 100644 --- a/src/App/ProjectFile.cpp +++ b/src/App/ProjectFile.cpp @@ -363,7 +363,7 @@ Base::Type ProjectFile::getTypeId(const std::string& name) const // // if (!xmlDocument) { - return Base::Type::badType(); + return Base::Type::BadType; } DOMNodeList* nodes = xmlDocument->getElementsByTagName(XStrLiteral("Objects").unicodeForm()); @@ -388,7 +388,7 @@ Base::Type ProjectFile::getTypeId(const std::string& name) const } } - return Base::Type::badType(); + return Base::Type::BadType; } std::list ProjectFile::getPropertyFiles(const std::string& name) const diff --git a/src/App/PropertyContainer.h b/src/App/PropertyContainer.h index 5a6b7647ae..078b745771 100644 --- a/src/App/PropertyContainer.h +++ b/src/App/PropertyContainer.h @@ -375,7 +375,7 @@ void _class_::init(void){\ } #define TYPESYSTEM_SOURCE_TEMPLATE(_class_) \ -template<> Base::Type _class_::classTypeId = Base::Type::badType(); \ +template<> Base::Type _class_::classTypeId = Base::Type::BadType; \ template<> Base::Type _class_::getClassTypeId(void) { return _class_::classTypeId; } \ template<> Base::Type _class_::getTypeId(void) const { return _class_::classTypeId; } \ template<> void * _class_::create(void){\ From 44a7704ad9a09e591e8655a11997aead3202d565 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 22:10:59 +0100 Subject: [PATCH 08/17] Gui: Use constant for Type::BadType instead Type::badType() --- src/Gui/Dialogs/DlgExpressionInput.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Gui/Dialogs/DlgExpressionInput.cpp b/src/Gui/Dialogs/DlgExpressionInput.cpp index 708bf25c2e..0b1116d0c5 100644 --- a/src/Gui/Dialogs/DlgExpressionInput.cpp +++ b/src/Gui/Dialogs/DlgExpressionInput.cpp @@ -179,7 +179,7 @@ Base::Type DlgExpressionInput::determineTypeVarSet() std::string unitTypeString = impliedUnit.getTypeString(); if (unitTypeString.empty()) { // no type was provided - return Base::Type::badType(); + return Base::Type::BadType; } std::string typeString = "App::Property" + unitTypeString; From 17ed01bbf2ad27dcd51b554c6ee4950e20e0a8f3 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 22:11:18 +0100 Subject: [PATCH 09/17] Measure: Use constant for Type::BadType instead Type::badType() --- src/Mod/Measure/App/MeasureDistance.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Mod/Measure/App/MeasureDistance.cpp b/src/Mod/Measure/App/MeasureDistance.cpp index aa748856df..c2a5695d55 100644 --- a/src/Mod/Measure/App/MeasureDistance.cpp +++ b/src/Mod/Measure/App/MeasureDistance.cpp @@ -415,12 +415,12 @@ std::vector MeasureDistanceDetached::getSubject() const Base::Type MeasureDistanceType::getClassTypeId() { - return Base::Type::badType(); + return Base::Type::BadType; } Base::Type MeasureDistanceType::getTypeId() const { - return Base::Type::badType(); + return Base::Type::BadType; } void MeasureDistanceType::init() @@ -436,7 +436,7 @@ void* MeasureDistanceType::create() return new MeasureDistanceDetached(); } -Base::Type MeasureDistanceType::classTypeId = Base::Type::badType(); +Base::Type MeasureDistanceType::classTypeId = Base::Type::BadType; // Migrate old MeasureDistance Type From 211b3209ac4014614e0c1332ff7925bf97fe812c Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 22:11:31 +0100 Subject: [PATCH 10/17] Part: Use constant for Type::BadType instead Type::badType() --- src/Mod/Part/App/GeometryDefaultExtension.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Mod/Part/App/GeometryDefaultExtension.h b/src/Mod/Part/App/GeometryDefaultExtension.h index 1e3bc41fff..439c1dc229 100644 --- a/src/Mod/Part/App/GeometryDefaultExtension.h +++ b/src/Mod/Part/App/GeometryDefaultExtension.h @@ -87,7 +87,7 @@ namespace Part { // 6. Register your type and corresponding python type in AppPart.cpp template - Base::Type GeometryDefaultExtension::classTypeId{Base::Type::badType()}; + Base::Type GeometryDefaultExtension::classTypeId{Base::Type::BadType}; // Must be explicitly declared here template<> void * GeometryDefaultExtension::create(); From 65c6614081b0d638d7d574947d594358cc0d1111 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 22:11:46 +0100 Subject: [PATCH 11/17] Sketcher: Use constant for Type::BadType instead Type::badType() --- src/Mod/Sketcher/Gui/CommandConstraints.cpp | 4 ++-- src/Mod/Sketcher/Gui/TaskSketcherElements.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Mod/Sketcher/Gui/CommandConstraints.cpp b/src/Mod/Sketcher/Gui/CommandConstraints.cpp index 0706d22c2e..d46109ce7f 100644 --- a/src/Mod/Sketcher/Gui/CommandConstraints.cpp +++ b/src/Mod/Sketcher/Gui/CommandConstraints.cpp @@ -1526,7 +1526,7 @@ public: selIdPair.GeoId = GeoEnum::GeoUndef; selIdPair.PosId = Sketcher::PointPos::none; std::stringstream ss; - Base::Type newselGeoType = Base::Type::badType(); + Base::Type newselGeoType = Base::Type::BadType; int VtId = getPreselectPoint(); int CrvId = getPreselectCurve(); @@ -1662,7 +1662,7 @@ protected: SelIdPair selIdPair; getIdsFromName(selElement, Obj, selIdPair.GeoId, selIdPair.PosId); - Base::Type newselGeoType = Base::Type::badType(); + Base::Type newselGeoType = Base::Type::BadType; if (isEdge(selIdPair.GeoId, selIdPair.PosId)) { const Part::Geometry* geo = Obj->getGeometry(selIdPair.GeoId); newselGeoType = geo->getTypeId(); diff --git a/src/Mod/Sketcher/Gui/TaskSketcherElements.cpp b/src/Mod/Sketcher/Gui/TaskSketcherElements.cpp index 3d5695a50f..e5df986f5d 100644 --- a/src/Mod/Sketcher/Gui/TaskSketcherElements.cpp +++ b/src/Mod/Sketcher/Gui/TaskSketcherElements.cpp @@ -463,7 +463,7 @@ private: icons.emplace( std::piecewise_construct, - std::forward_as_tuple(Base::Type::badType()), + std::forward_as_tuple(Base::Type::BadType), std::forward_as_tuple( std::initializer_list< std::pair>> { @@ -479,14 +479,14 @@ private: auto typekey = icons.find(type); if (typekey == icons.end()) {// Not supported Geometry Type - Defaults to invalid icon - typekey = icons.find(Base::Type::badType()); + typekey = icons.find(Base::Type::BadType); pos = Sketcher::PointPos::none; } auto poskey = typekey->second.find(pos); if (poskey == typekey->second.end()) {// invalid PointPos for type - Provide Invalid icon - typekey = icons.find(Base::Type::badType()); + typekey = icons.find(Base::Type::BadType); pos = Sketcher::PointPos::none; poskey = typekey->second.find(pos); } From e956bc210169c6c1046256e00c0dea3ef2f31ca0 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 20:25:43 +0100 Subject: [PATCH 12/17] Base: Constrain size of Base::Type --- src/Base/Type.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/Base/Type.cpp b/src/Base/Type.cpp index 6a55ab669b..40ae71e4e0 100644 --- a/src/Base/Type.cpp +++ b/src/Base/Type.cpp @@ -33,6 +33,15 @@ using namespace Base; +static_assert(sizeof(Base::Type) == sizeof(unsigned int), + "Base::Type has been designed to be small to be passed around by value efficiently. " + "The size of Base::Type has changed. Be careful when adding more data members."); + +static_assert( + sizeof(Base::Type) <= 2 * sizeof(void*), + "Base::Type has been designed to be small to be passed around by value efficiently. " + "When the size grows larger than ~2 words, consider passing by const reference instead. " + "Exact limit depends on the architecture and ABI."); struct Base::TypeData { From 140527d64aacc37f7fe33610c3804199c1b06046 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 20:26:50 +0100 Subject: [PATCH 13/17] Base: Use pass by value instead of reference as size is small --- src/Base/Type.cpp | 15 ++++++--------- src/Base/Type.h | 8 ++++---- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Base/Type.cpp b/src/Base/Type.cpp index 40ae71e4e0..91605898ba 100644 --- a/src/Base/Type.cpp +++ b/src/Base/Type.cpp @@ -129,7 +129,7 @@ std::string Type::getModuleName(const char* ClassName) } -Type Type::createType(const Type& parent, const char* name, instantiationMethod method) +Type Type::createType(const Type parent, const char* name, instantiationMethod method) { Type newType; newType.index = static_cast(Type::typedata.size()); @@ -193,9 +193,8 @@ Type Type::getParent() const return typedata[index]->parent; } -bool Type::isDerivedFrom(const Type& type) const +bool Type::isDerivedFrom(const Type type) const { - Type temp(*this); do { if (temp == type) { @@ -207,7 +206,7 @@ bool Type::isDerivedFrom(const Type& type) const return false; } -int Type::getAllDerivedFrom(const Type& type, std::vector& List) +int Type::getAllDerivedFrom(const Type type, std::vector& List) { int cnt = 0; @@ -225,15 +224,13 @@ int Type::getNumTypes() return static_cast(typedata.size()); } -Type Type::getTypeIfDerivedFrom(const char* name, const Type& parent, bool bLoadModule) +Type Type::getTypeIfDerivedFrom(const char* name, const Type parent, bool loadModule) { - if (bLoadModule) { + if (loadModule) { importModule(name); } - Type type = fromName(name); - - if (type.isDerivedFrom(parent)) { + if (Type type(fromName(name)); type.isDerivedFrom(parent)) { return type; } diff --git a/src/Base/Type.h b/src/Base/Type.h index b54455e566..1bcc25edc4 100644 --- a/src/Base/Type.h +++ b/src/Base/Type.h @@ -101,17 +101,17 @@ public: static Type fromKey(unsigned int key); const char* getName() const; Type getParent() const; - bool isDerivedFrom(const Type& type) const; + bool isDerivedFrom(const Type type) const; - static int getAllDerivedFrom(const Type& type, std::vector& List); + static int getAllDerivedFrom(const Type type, std::vector& List); /// Returns the given named type if is derived from parent type, otherwise return bad type static Type - getTypeIfDerivedFrom(const char* name, const Type& parent, bool bLoadModule = false); + getTypeIfDerivedFrom(const char* name, const Type parent, bool loadModule = false); static int getNumTypes(); static Type - createType(const Type& parent, const char* name, instantiationMethod method = nullptr); + createType(const Type parent, const char* name, instantiationMethod method = nullptr); unsigned int getKey() const; bool isBad() const; From 280c1c610da41747a16148a3c5556c6230b5b0b4 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 20:28:43 +0100 Subject: [PATCH 14/17] Base: Minor cleanup * Add const * Add final * [[nodiscard]] * Fix parameter names * Add TypeId instead of unsigned int * Add "BadType" string constant Some some other tweaks --- src/Base/Type.cpp | 96 +++++++++++++++++++++++------------------------ src/Base/Type.h | 46 ++++++++++++----------- 2 files changed, 71 insertions(+), 71 deletions(-) diff --git a/src/Base/Type.cpp b/src/Base/Type.cpp index 91605898ba..cf5d4d5bb1 100644 --- a/src/Base/Type.cpp +++ b/src/Base/Type.cpp @@ -33,7 +33,7 @@ using namespace Base; -static_assert(sizeof(Base::Type) == sizeof(unsigned int), +static_assert(sizeof(Base::Type) == sizeof(Type::TypeId), "Base::Type has been designed to be small to be passed around by value efficiently. " "The size of Base::Type has changed. Be careful when adding more data members."); @@ -45,22 +45,27 @@ static_assert( struct Base::TypeData { - TypeData(const char* theName, - const Type type = Type::BadType, - const Type theParent = Type::BadType, - Type::instantiationMethod method = nullptr) - : name(theName) - , parent(theParent) + TypeData(const char* name, + const Type type, + const Type parent, + const Type::instantiationMethod instMethod) + : name(name) + , parent(parent) , type(type) - , instMethod(method) + , instMethod(instMethod) {} - std::string name; - Type parent; - Type type; - Type::instantiationMethod instMethod; + const std::string name; + const Type parent; + const Type type; + const Type::instantiationMethod instMethod; }; +namespace +{ +constexpr const char* BadTypeName = "BadType"; +} + std::map Type::typemap; std::vector Type::typedata; std::set Type::loadModuleSet; @@ -79,26 +84,23 @@ bool Type::canInstantiate() const return method != nullptr; } -void* Type::createInstanceByName(const char* TypeName, bool bLoadModule) +void* Type::createInstanceByName(const char* typeName, bool loadModule) { // if not already, load the module - if (bLoadModule) { - importModule(TypeName); + if (loadModule) { + importModule(typeName); } // now the type should be in the type map - Type type = fromName(TypeName); - if (type == BadType) { - return nullptr; - } - + const Type type = fromName(typeName); + // let createInstance handle isBad check return type.createInstance(); } -void Type::importModule(const char* TypeName) +void Type::importModule(const char* typeName) { // cut out the module name - const std::string mod = getModuleName(TypeName); + const std::string mod = getModuleName(typeName); // ignore base modules if (mod == "App" || mod == "Gui" || mod == "Base") { @@ -106,22 +108,21 @@ void Type::importModule(const char* TypeName) } // remember already loaded modules - const auto pos = loadModuleSet.find(mod); - if (pos != loadModuleSet.end()) { + if (loadModuleSet.contains(mod)) { return; } // lets load the module Interpreter().loadModule(mod.c_str()); #ifdef FC_LOGLOADMODULE - Console().Log("Act: Module %s loaded through class %s \n", Mod.c_str(), TypeName); + Console().Log("Act: Module %s loaded through class %s \n", Mod.c_str(), typeName); #endif loadModuleSet.insert(mod); } -std::string Type::getModuleName(const char* ClassName) +const std::string Type::getModuleName(const char* className) { - std::string_view classNameView(ClassName); + std::string_view classNameView(className); auto pos = classNameView.find("::"); return pos != std::string_view::npos ? std::string(classNameView.substr(0, pos)) @@ -129,15 +130,16 @@ std::string Type::getModuleName(const char* ClassName) } -Type Type::createType(const Type parent, const char* name, instantiationMethod method) +const Type Type::createType(const Type parent, const char* name, instantiationMethod method) { + assert(name && name[0] != '\0' && "Type name must not be empty"); + Type newType; newType.index = static_cast(Type::typedata.size()); - TypeData* typeData = new TypeData(name, newType, parent, method); - Type::typedata.push_back(typeData); + Type::typedata.emplace_back(new TypeData(name, newType, parent, method)); // add to dictionary for fast lookup - Type::typemap[name] = newType.getKey(); + Type::typemap.emplace(name, newType.getKey()); return newType; } @@ -145,11 +147,9 @@ Type Type::createType(const Type parent, const char* name, instantiationMethod m void Type::init() { - assert(Type::typedata.empty()); - - - Type::typedata.push_back(new TypeData("BadType")); - Type::typemap["BadType"] = 0; + assert(Type::typedata.size() == 0 && "Type::init() should only be called once"); + typedata.emplace_back(new TypeData(BadTypeName, BadType, BadType, nullptr)); + typemap[BadTypeName] = 0; } void Type::destruct() @@ -162,19 +162,17 @@ void Type::destruct() loadModuleSet.clear(); } -Type Type::fromName(const char* name) +const Type Type::fromName(const char* name) { - std::map::const_iterator pos; - - pos = typemap.find(name); - if (pos != typemap.end()) { - return typedata[pos->second]->type; + const auto pos = typemap.find(name); + if (pos == typemap.end()) { + return Type::BadType; } - return Type::BadType; + return typedata[pos->second]->type; } -Type Type::fromKey(unsigned int key) +const Type Type::fromKey(TypeId key) { if (key < typedata.size()) { return typedata[key]->type; @@ -188,7 +186,7 @@ const char* Type::getName() const return typedata[index]->name.c_str(); } -Type Type::getParent() const +const Type Type::getParent() const { return typedata[index]->parent; } @@ -206,13 +204,13 @@ bool Type::isDerivedFrom(const Type type) const return false; } -int Type::getAllDerivedFrom(const Type type, std::vector& List) +int Type::getAllDerivedFrom(const Type type, std::vector& list) { int cnt = 0; for (auto it : typedata) { if (it->type.isDerivedFrom(type)) { - List.push_back(it->type); + list.push_back(it->type); cnt++; } } @@ -224,13 +222,13 @@ int Type::getNumTypes() return static_cast(typedata.size()); } -Type Type::getTypeIfDerivedFrom(const char* name, const Type parent, bool loadModule) +const Type Type::getTypeIfDerivedFrom(const char* name, const Type parent, bool loadModule) { if (loadModule) { importModule(name); } - if (Type type(fromName(name)); type.isDerivedFrom(parent)) { + if (const Type type(fromName(name)); type.isDerivedFrom(parent)) { return type; } diff --git a/src/Base/Type.h b/src/Base/Type.h index 1bcc25edc4..178912b3f8 100644 --- a/src/Base/Type.h +++ b/src/Base/Type.h @@ -77,9 +77,10 @@ struct TypeData; information: super classes must be registered before any of their derived classes are. */ -class BaseExport Type +class BaseExport Type final { public: + using TypeId = unsigned int; /// Construction Type(const Type& type) = default; Type(Type&& type) = default; @@ -88,33 +89,33 @@ public: ~Type() = default; /// creates a instance of this type - void* createInstance() const; + [[nodiscard]] void* createInstance() const; /// Checks whether this type can instantiate - bool canInstantiate() const; + [[nodiscard]] bool canInstantiate() const; /// creates a instance of the named type - static void* createInstanceByName(const char* TypeName, bool bLoadModule = false); + [[nodiscard]] static void* createInstanceByName(const char* typeName, bool loadModule = false); static void importModule(const char* TypeName); using instantiationMethod = void* (*)(); - static Type fromName(const char* name); - static Type fromKey(unsigned int key); - const char* getName() const; - Type getParent() const; - bool isDerivedFrom(const Type type) const; + [[nodiscard]] static const Type fromName(const char* name); + [[nodiscard]] static const Type fromKey(TypeId key); + [[nodiscard]] const char* getName() const; + [[nodiscard]] const Type getParent() const; + [[nodiscard]] bool isDerivedFrom(const Type type) const; - static int getAllDerivedFrom(const Type type, std::vector& List); + static int getAllDerivedFrom(const Type type, std::vector& list); /// Returns the given named type if is derived from parent type, otherwise return bad type - static Type + [[nodiscard]] static const Type getTypeIfDerivedFrom(const char* name, const Type parent, bool loadModule = false); - static int getNumTypes(); + [[nodiscard]] static int getNumTypes(); - static Type + [[nodiscard]] static const Type createType(const Type parent, const char* name, instantiationMethod method = nullptr); - unsigned int getKey() const; - bool isBad() const; + [[nodiscard]] TypeId getKey() const; + [[nodiscard]] bool isBad() const; Type& operator=(const Type& type) = default; Type& operator=(Type&& type) = default; @@ -130,19 +131,20 @@ public: static void init(); static void destruct(); - static std::string getModuleName(const char* ClassName); - + static const std::string getModuleName(const char* className); private: - unsigned int index {0}; + TypeId index {BadTypeIndex}; - static std::map typemap; - static std::vector typedata; + static std::map typemap; + static std::vector typedata; // use pointer to hide implementation details static std::set loadModuleSet; + + static constexpr TypeId BadTypeIndex = 0; }; -inline unsigned int Type::getKey() const +inline Type::TypeId Type::getKey() const { return this->index; } @@ -179,7 +181,7 @@ inline bool Type::operator>(const Type& type) const inline bool Type::isBad() const { - return (this->index == 0); + return this->index == BadTypeIndex; } } // namespace Base From ece25df768d886ab9dd6c011c7f6233df3093572 Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Tue, 18 Feb 2025 08:16:13 +0100 Subject: [PATCH 15/17] Base: Add asserts to check that Type has been initialized --- src/Base/Type.cpp | 18 ++++++++++++++++-- src/Base/Type.h | 2 ++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/Base/Type.cpp b/src/Base/Type.cpp index cf5d4d5bb1..63eb34245a 100644 --- a/src/Base/Type.cpp +++ b/src/Base/Type.cpp @@ -72,15 +72,25 @@ std::set Type::loadModuleSet; const Type Type::BadType; +Type::instantiationMethod Type::getInstantiationMethod() const +{ + assert(typedata.size() >= 1 && "Type::init() must be called before creating instances"); + assert(typedata.size() > index && "Type index out of bounds"); + if (isBad() || typedata.size() <= index) { + return nullptr; + } + return typedata[index]->instMethod; +} + void* Type::createInstance() const { - instantiationMethod method = typedata[index]->instMethod; + const auto method = getInstantiationMethod(); return method ? (*method)() : nullptr; } bool Type::canInstantiate() const { - instantiationMethod method = typedata[index]->instMethod; + const auto method = getInstantiationMethod(); return method != nullptr; } @@ -183,11 +193,15 @@ const Type Type::fromKey(TypeId key) const char* Type::getName() const { + assert(typedata.size() >= 1 + && "Type::init() must be called before fetching names, even for bad types"); return typedata[index]->name.c_str(); } const Type Type::getParent() const { + assert(typedata.size() >= 1 + && "Type::init() must be called before fetching parents, even for bad types"); return typedata[index]->parent; } diff --git a/src/Base/Type.h b/src/Base/Type.h index 178912b3f8..d96f46c22a 100644 --- a/src/Base/Type.h +++ b/src/Base/Type.h @@ -134,6 +134,8 @@ public: static const std::string getModuleName(const char* className); private: + [[nodiscard]] instantiationMethod getInstantiationMethod() const; + TypeId index {BadTypeIndex}; static std::map typemap; From e7119ee2432601be9ab81816bf99306682ce4b9c Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Thu, 13 Feb 2025 21:11:49 +0100 Subject: [PATCH 16/17] Base: Add documentation of Type --- src/Base/Type.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/Base/Type.h b/src/Base/Type.h index d96f46c22a..49bedcaa01 100644 --- a/src/Base/Type.h +++ b/src/Base/Type.h @@ -88,33 +88,39 @@ public: /// Destruction ~Type() = default; - /// creates a instance of this type + /// Creates an instance of this type [[nodiscard]] void* createInstance() const; /// Checks whether this type can instantiate [[nodiscard]] bool canInstantiate() const; - /// creates a instance of the named type + /// Creates an instance of the named type [[nodiscard]] static void* createInstanceByName(const char* typeName, bool loadModule = false); static void importModule(const char* TypeName); using instantiationMethod = void* (*)(); + /// Returns a type object by name [[nodiscard]] static const Type fromName(const char* name); + /// Returns a type object by key [[nodiscard]] static const Type fromKey(TypeId key); + /// Returns the name of the type [[nodiscard]] const char* getName() const; + /// Returns the parent type [[nodiscard]] const Type getParent() const; + /// Checks whether this type is derived from "type" [[nodiscard]] bool isDerivedFrom(const Type type) const; - + /// Returns all descendants from the given type static int getAllDerivedFrom(const Type type, std::vector& list); /// Returns the given named type if is derived from parent type, otherwise return bad type [[nodiscard]] static const Type getTypeIfDerivedFrom(const char* name, const Type parent, bool loadModule = false); - + /// Returns the number of types created so far [[nodiscard]] static int getNumTypes(); - + /// Creates a new type with the given name, parent and instantiation method [[nodiscard]] static const Type createType(const Type parent, const char* name, instantiationMethod method = nullptr); - + /// Returns the inner index of the type [[nodiscard]] TypeId getKey() const; + /// Checks if the type is invalid [[nodiscard]] bool isBad() const; Type& operator=(const Type& type) = default; @@ -131,6 +137,7 @@ public: static void init(); static void destruct(); + /// Returns the name of the module the class is defined in static const std::string getModuleName(const char* className); private: From a0a64c1fb8969996b85cfad2329eb7dde131a68e Mon Sep 17 00:00:00 2001 From: Benjamin Nauck Date: Mon, 24 Feb 2025 09:31:22 +0100 Subject: [PATCH 17/17] Base: Make Type::importModule private --- src/App/PropertyStandard.cpp | 13 +++---------- src/Base/Type.h | 2 +- 2 files changed, 4 insertions(+), 11 deletions(-) diff --git a/src/App/PropertyStandard.cpp b/src/App/PropertyStandard.cpp index 238df5d8b5..e8471d6d53 100644 --- a/src/App/PropertyStandard.cpp +++ b/src/App/PropertyStandard.cpp @@ -3527,17 +3527,10 @@ unsigned int PropertyPersistentObject::getMemSize() const void PropertyPersistentObject::setValue(const char* type) { - if (!type) { - type = ""; - } - if (type[0]) { - Base::Type::importModule(type); - Base::Type t = Base::Type::fromName(type); + if (!Base::Tools::isNullOrEmpty(type)) { + Base::Type t = Base::Type::getTypeIfDerivedFrom(type, Persistence::getClassTypeId()); if (t.isBad()) { - throw Base::TypeError("Invalid type"); - } - if (!t.isDerivedFrom(Persistence::getClassTypeId())) { - throw Base::TypeError("Type must be derived from Base::Persistence"); + throw Base::TypeError("Invalid type or type must be derived from Base::Persistence"); } if (_pObject && _pObject->getTypeId() == t) { return; diff --git a/src/Base/Type.h b/src/Base/Type.h index 49bedcaa01..adba866cef 100644 --- a/src/Base/Type.h +++ b/src/Base/Type.h @@ -94,7 +94,6 @@ public: [[nodiscard]] bool canInstantiate() const; /// Creates an instance of the named type [[nodiscard]] static void* createInstanceByName(const char* typeName, bool loadModule = false); - static void importModule(const char* TypeName); using instantiationMethod = void* (*)(); @@ -142,6 +141,7 @@ public: private: [[nodiscard]] instantiationMethod getInstantiationMethod() const; + static void importModule(const char* TypeName); TypeId index {BadTypeIndex};