Base: Add 'getTypeIfDerivedFrom' member function to Type class
This commit is contained in:
@@ -234,3 +234,16 @@ int Type::getNumTypes(void)
|
||||
{
|
||||
return typedata.size();
|
||||
}
|
||||
|
||||
Type Type::getTypeIfDerivedFrom(const char* name , const Type parent, bool bLoadModule)
|
||||
{
|
||||
if (bLoadModule)
|
||||
importModule(name);
|
||||
|
||||
Type type = fromName(name);
|
||||
|
||||
if (type.isDerivedFrom(parent))
|
||||
return type;
|
||||
else
|
||||
return Type::badType();
|
||||
}
|
||||
|
||||
@@ -101,6 +101,8 @@ public:
|
||||
bool isDerivedFrom(const Type type) const;
|
||||
|
||||
static int getAllDerivedFrom(const Type type, std::vector<Type>& 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);
|
||||
|
||||
static int getNumTypes(void);
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user