diff --git a/Lib/python/director_h.swg b/Lib/python/director_h.swg index 7913a2408..361543242 100644 --- a/Lib/python/director_h.swg +++ b/Lib/python/director_h.swg @@ -13,15 +13,33 @@ #include -extern "C" { - struct swig_type_info; -} - +/* + Use -DSWIG_DIRECTOR_STATIC if you prefer to avoid the use of the + 'Swig' namespace. This could be usefull for multi-modules projects. +*/ #ifdef SWIG_DIRECTOR_STATIC /* Force anonymous (static) namespace */ #define Swig #endif + +/* + Use -DSWIG_DIRECTOR_NORTTI if you prefer to avoid the use of RTTI + and dynamic_cast<>. But be aware that directors could stop working + when using this option. +*/ + +#if defined(SWIG_DIRECTOR_NORTTI) +# define SWIG_DIRECTOR_CAST(arg) (Swig::Director*)(arg) +#else +# define SWIG_DIRECTOR_CAST(arg) dynamic_cast(arg) +#endif + + +extern "C" { + struct swig_type_info; +} + namespace Swig { /* base class for director exceptions */ class DirectorException { diff --git a/Lib/python/pyuserdir.swg b/Lib/python/pyuserdir.swg index fe9118ab7..d6f46a329 100644 --- a/Lib/python/pyuserdir.swg +++ b/Lib/python/pyuserdir.swg @@ -122,3 +122,12 @@ These methods "may be called" if needed. #endif #define %nocallback %nopythoncallback; %feature("callback","") + +/* When using -nortti, tell directors to avoid RTTI */ +#ifdef SWIG_NORTTI +%insert("runtime") %{ +#ifndef SWIG_DIRECTOR_NORTTI +#define SWIG_DIRECTOR_NORTTI +#endif +%} +#endif diff --git a/Source/Modules/python.cxx b/Source/Modules/python.cxx index b1b2e7a89..2614b049a 100644 --- a/Source/Modules/python.cxx +++ b/Source/Modules/python.cxx @@ -70,7 +70,8 @@ Python Options (available with -python)\n\ -interface - Set the lib name to \n\ -keyword - Use keyword arguments\n\ -classic - Use classic classes only\n\ - -cpluscast - Enable new C++ casting operators, useful for debugging\n\ + -cppcast - Enable new C++ casting operators, useful for debugging\n\ + -nortti - Disable the use of RTTI, useful (sometimes) with directors\n\ -modern - Use modern python features only, without compatibility code\n\ -apply - Use apply() in proxy classes\n\ -new_vwm - New value wrapper mode, use only when everything else fails \n\ @@ -155,10 +156,14 @@ public: classic = 1; apply = 1; Swig_mark_arg(i); - } else if (strcmp(argv[i],"-cpluscast") == 0) { + } else if (strcmp(argv[i],"-cppcast") == 0) { /* Turn on new value wrapper mpde */ Preprocessor_define((DOH *) "SWIG_CPLUSPLUS_CAST", 0); Swig_mark_arg(i); + } else if (strcmp(argv[i],"-nortti") == 0) { + /* Turn on no RTTI mode */ + Preprocessor_define((DOH *) "SWIG_NORTTI", 0); + Swig_mark_arg(i); } else if (strcmp(argv[i],"-modern") == 0) { apply = 0; classic = 0; @@ -1375,7 +1380,7 @@ public: if (/*directorbase &&*/ !constructor && !destructor && isVirtual && !Getattr(n,"feature:nodirector")) { Wrapper_add_local(f, "director", "Swig::Director *director = 0"); - Printf(f->code, "director = dynamic_cast(arg1);\n"); + Printf(f->code, "director = SWIG_DIRECTOR_CAST(arg1);\n"); if (dirprot_mode() && !is_public(n)) { Printf(f->code, "if (!director || !(director->swig_get_inner(\"%s\"))) {\n", name); Printf(f->code, "PyErr_SetString(PyExc_RuntimeError,\"accessing protected member %s\");\n", name); @@ -1434,7 +1439,7 @@ public: } if (unwrap) { Wrapper_add_local(f, "resultdirector", "Swig::Director *resultdirector = 0"); - Printf(f->code, "resultdirector = dynamic_cast(result);\n"); + Printf(f->code, "resultdirector = SWIG_DIRECTOR_CAST(result);\n"); Printf(f->code, "if (resultdirector) {\n"); Printf(f->code, " resultobj = resultdirector->swig_get_self();\n"); Printf(f->code, " Py_INCREF(resultobj);\n"); @@ -2691,7 +2696,7 @@ int PYTHON::classDirectorMethod(Node *n, Node *parent, String *super) { String *director = NewStringf("director_%s", mangle); Wrapper_add_localv(w, director, "Swig::Director *", director, "= 0", NIL); Wrapper_add_localv(w, source, "PyObject *", source, "= 0", NIL); - Printf(wrap_args, "%s = dynamic_cast(%s);\n", director, nonconst); + Printf(wrap_args, "%s = SWIG_DIRECTOR_CAST(%s);\n", director, nonconst); Printf(wrap_args, "if (!%s) {\n", director); Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); Printf(wrap_args, "} else {\n");