Base: [skip ci] expose Type::createInstance and Type::createInstanceByName to Python

This commit is contained in:
wmayer
2020-10-29 17:18:10 +01:00
parent 20a52f3f68
commit 0371a67dae
2 changed files with 41 additions and 0 deletions

View File

@@ -64,6 +64,16 @@ namespace Base {
<UserDocu>Returns all descendants</UserDocu>
</Documentation>
</Methode>
<Methode Name="createInstance">
<Documentation>
<UserDocu>Creates an instance of this type</UserDocu>
</Documentation>
</Methode>
<Methode Name="createInstanceByName" Static="true">
<Documentation>
<UserDocu>Creates an instance of the named type</UserDocu>
</Documentation>
</Methode>
<Attribute Name="Name" ReadOnly="true">
<Documentation>
<UserDocu>The name of the type id</UserDocu>

View File

@@ -167,6 +167,37 @@ PyObject* TypePy::getAllDerived(PyObject *args)
return Py::new_reference_to(res);
}
PyObject* TypePy::createInstance (PyObject *args)
{
if (!PyArg_ParseTuple(args, ""))
return nullptr;
Base::BaseClass* base = static_cast<Base::BaseClass*>(getBaseTypePtr()->createInstance());
if (!base) {
Py_Return;
}
//TODO: At the moment "base" will never be destroyed and causes a memory leak
return base->getPyObject();
}
PyObject* TypePy::createInstanceByName (PyObject *args)
{
const char* type;
PyObject* load = Py_False;
if (!PyArg_ParseTuple(args, "s|O!", &type, &PyBool_Type, &load))
return nullptr;
Base::BaseClass* base = static_cast<Base::BaseClass*>
(Base::Type::createInstanceByName(type, PyObject_IsTrue(load) ? true : false));
if (!base) {
Py_Return;
}
//TODO: At the moment "base" will never be destroyed and causes a memory leak
return base->getPyObject();
}
Py::String TypePy::getName(void) const
{
return Py::String(std::string(getBaseTypePtr()->getName()));