Base: Add 'getTypeIfDerivedFrom' member function to Type class

This commit is contained in:
marioalexis
2021-09-01 10:10:39 -03:00
committed by wwmayer
parent 82b97374fd
commit 33b2c7d353
3 changed files with 29 additions and 11 deletions

View File

@@ -216,26 +216,29 @@ PyObject* TypePy::createInstance (PyObject *args)
if (!PyArg_ParseTuple(args, ""))
return nullptr;
Base::BaseClass* base = static_cast<Base::BaseClass*>(getBaseTypePtr()->createInstance());
if (!base) {
Py_Return;
}
Py::String name(getBaseTypePtr()->getName());
Py::TupleN tuple(name);
return createPyObject(base);
return createInstanceByName(tuple.ptr());
}
PyObject* TypePy::createInstanceByName (PyObject *args)
{
const char* type;
const char* name;
PyObject* load = Py_False;
if (!PyArg_ParseTuple(args, "s|O!", &type, &PyBool_Type, &load))
if (!PyArg_ParseTuple(args, "s|O!", &name, &PyBool_Type, &load))
return nullptr;
Base::BaseClass* base = static_cast<Base::BaseClass*>
(Base::Type::createInstanceByName(type, PyObject_IsTrue(load) ? true : false));
if (!base) {
bool bLoad = PyObject_IsTrue(load) ? true : false;
Base::Type type = Base::Type::getTypeIfDerivedFrom(name, Base::BaseClass::getClassTypeId(), bLoad);
if (type.isBad())
Py_Return;
}
void* typeInstance = type.createInstance();
if (!typeInstance)
Py_Return;
Base::BaseClass* base = static_cast<Base::BaseClass*>(typeInstance);
return createPyObject(base);
}