From 0371a67dae6eca8b644c2232d5f6760e98de2646 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 29 Oct 2020 17:18:10 +0100 Subject: [PATCH] Base: [skip ci] expose Type::createInstance and Type::createInstanceByName to Python --- src/Base/TypePy.xml | 10 ++++++++++ src/Base/TypePyImp.cpp | 31 +++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Base/TypePy.xml b/src/Base/TypePy.xml index c05be1940f..ccccd1253b 100644 --- a/src/Base/TypePy.xml +++ b/src/Base/TypePy.xml @@ -64,6 +64,16 @@ namespace Base { Returns all descendants + + + Creates an instance of this type + + + + + Creates an instance of the named type + + The name of the type id diff --git a/src/Base/TypePyImp.cpp b/src/Base/TypePyImp.cpp index 69806d32ae..9cec3ce344 100644 --- a/src/Base/TypePyImp.cpp +++ b/src/Base/TypePyImp.cpp @@ -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(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::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()));