From c4b657b5b23c72fcaeaf4d2d0205cdbcf1482c25 Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 19 Aug 2018 16:06:16 +0200 Subject: [PATCH] interrogate: support implicit typecast operators in some cases For example, this will let us pass a ConfigVariableFilename to anything that accepts a Filename, just like in C++. Does not work if the return value if the typecast operator requires management. --- .../interfaceMakerPythonNative.cxx | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dtool/src/interrogate/interfaceMakerPythonNative.cxx b/dtool/src/interrogate/interfaceMakerPythonNative.cxx index 3134fcb2eb..e20aff91ef 100644 --- a/dtool/src/interrogate/interfaceMakerPythonNative.cxx +++ b/dtool/src/interrogate/interfaceMakerPythonNative.cxx @@ -1089,6 +1089,27 @@ write_class_details(ostream &out, Object *obj) { } } + // Are there any implicit cast operators that can cast this object to our + // desired pointer? + for (Function *func : obj->_methods) { + for (FunctionRemap *remap : func->_remaps) { + if (remap->_type == FunctionRemap::T_typecast_method && + is_remap_legal(remap) && + !remap->_return_type->return_value_needs_management() && + (remap->_cppfunc->_storage_class & CPPInstance::SC_explicit) == 0 && + TypeManager::is_pointer(remap->_return_type->get_new_type())) { + + CPPType *cast_type = remap->_return_type->get_orig_type(); + CPPType *obj_type = TypeManager::unwrap(TypeManager::resolve_type(remap->_return_type->get_new_type())); + string return_expr = "(" + cast_type->get_local_name(&parser) + ")*local_this"; + out << " // " << *remap->_cppfunc << "\n"; + out << " if (requested_type == Dtool_Ptr_" << make_safe_name(obj_type->get_local_name(&parser)) << ") {\n"; + out << " return (void *)(" << remap->_return_type->get_return_expr(return_expr) << ");\n"; + out << " }\n"; + } + } + } + out << " return nullptr;\n"; out << "}\n\n";