From f5a63e0953b70e271ccdd0726b1ac647bb9c63d9 Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 13 Nov 2019 10:41:04 +0100 Subject: [PATCH] Enhabce TypePy: add convenience method getAllDerived() support TypePy asrgument in getAllDerivedFrom() and isDerivedFrom() return TypePy instead of str in getAllDerivedFrom() --- src/Base/TypePy.xml | 7 +++- src/Base/TypePyImp.cpp | 80 +++++++++++++++++++++++++++++++++--------- 2 files changed, 70 insertions(+), 17 deletions(-) diff --git a/src/Base/TypePy.xml b/src/Base/TypePy.xml index ce5c6efa3b..c05be1940f 100644 --- a/src/Base/TypePy.xml +++ b/src/Base/TypePy.xml @@ -39,6 +39,11 @@ namespace Base { Returns an invalid type id + + + Returns all descendants + + Returns the parent type id @@ -54,7 +59,7 @@ namespace Base { Returns true if given type is a father - + Returns all descendants diff --git a/src/Base/TypePyImp.cpp b/src/Base/TypePyImp.cpp index c6f6e065ed..d103fdaf3d 100644 --- a/src/Base/TypePyImp.cpp +++ b/src/Base/TypePyImp.cpp @@ -41,7 +41,7 @@ PyObject* TypePy::staticCallback_fromName (PyObject * /*self*/, PyObject *args) { const char *name; if (!PyArg_ParseTuple(args, "s", &name)) - return NULL; + return nullptr; Base::Type type = Base::Type::fromName(name); return new TypePy(new Base::Type(type)); @@ -51,7 +51,7 @@ PyObject* TypePy::staticCallback_fromKey (PyObject * /*self*/, PyObject *args) { unsigned int index; if (!PyArg_ParseTuple(args, "I", &index)) - return NULL; + return nullptr; Base::Type type = Base::Type::fromKey(index); return new TypePy(new Base::Type(type)); @@ -60,7 +60,7 @@ PyObject* TypePy::staticCallback_fromKey (PyObject * /*self*/, PyObject *args) PyObject* TypePy::staticCallback_getNumTypes (PyObject * /*self*/, PyObject *args) { if (!PyArg_ParseTuple(args, "")) - return NULL; + return nullptr; int num = Base::Type::getNumTypes(); return PyLong_FromLong(num); @@ -69,7 +69,7 @@ PyObject* TypePy::staticCallback_getNumTypes (PyObject * /*self*/, PyObject *arg PyObject* TypePy::staticCallback_getBadType (PyObject * /*self*/, PyObject *args) { if (!PyArg_ParseTuple(args, "")) - return NULL; + return nullptr; Base::Type type = Base::Type::badType(); return new TypePy(new Base::Type(type)); @@ -78,7 +78,7 @@ PyObject* TypePy::staticCallback_getBadType (PyObject * /*self*/, PyObject *args PyObject* TypePy::getParent(PyObject *args) { if (!PyArg_ParseTuple(args, "")) - return NULL; + return nullptr; Base::Type type = getBaseTypePtr()->getParent(); return new TypePy(new Base::Type(type)); @@ -87,7 +87,7 @@ PyObject* TypePy::getParent(PyObject *args) PyObject* TypePy::isBad(PyObject *args) { if (!PyArg_ParseTuple(args, "")) - return NULL; + return nullptr; bool v = getBaseTypePtr()->isBad(); return PyBool_FromLong(v ? 1 : 0); @@ -95,27 +95,75 @@ PyObject* TypePy::isBad(PyObject *args) PyObject* TypePy::isDerivedFrom(PyObject *args) { - const char *name; - if (!PyArg_ParseTuple(args, "s", &name)) - return NULL; + Base::Type type; + + do { + const char *name; + if (PyArg_ParseTuple(args, "s", &name)) { + type = Base::Type::fromName(name); + break; + } + + PyErr_Clear(); + PyObject* t; + if (PyArg_ParseTuple(args, "O!", &TypePy::Type, &t)) { + type = *static_cast(t)->getBaseTypePtr(); + break; + } + + PyErr_SetString(PyExc_TypeError, "TypeId or str expected"); + return nullptr; + } + while (false); - Base::Type type = Base::Type::fromName(name); bool v = (type != Base::Type::badType() && getBaseTypePtr()->isDerivedFrom(type)); return PyBool_FromLong(v ? 1 : 0); } PyObject* TypePy::staticCallback_getAllDerivedFrom(PyObject* /*self*/, PyObject *args) { - const char *name; - if (!PyArg_ParseTuple(args, "s", &name)) - return NULL; + Base::Type type; + + do { + const char *name; + if (PyArg_ParseTuple(args, "s", &name)) { + type = Base::Type::fromName(name); + break; + } + + PyErr_Clear(); + PyObject* t; + if (PyArg_ParseTuple(args, "O!", &TypePy::Type, &t)) { + type = *static_cast(t)->getBaseTypePtr(); + break; + } + + PyErr_SetString(PyExc_TypeError, "TypeId or str expected"); + return nullptr; + } + while (false); - Base::Type type = Base::Type::fromName(name); std::vector ary; Base::Type::getAllDerivedFrom(type, ary); Py::List res; - for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) - res.append(Py::String(it->getName())); + for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) { + res.append(Py::asObject(new TypePy(new Base::Type(*it)))); + } + return Py::new_reference_to(res); +} + +PyObject* TypePy::getAllDerived(PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + Base::Type type = Base::Type::fromName(getBaseTypePtr()->getName()); + std::vector ary; + Base::Type::getAllDerivedFrom(type, ary); + Py::List res; + for (std::vector::iterator it = ary.begin(); it != ary.end(); ++it) { + res.append(Py::asObject(new TypePy(new Base::Type(*it)))); + } return Py::new_reference_to(res); }