diff options
author | Joel Andersson <j.a.e.andersson@gmail.com> | 2016-07-08 13:34:30 -0500 |
---|---|---|
committer | Joel Andersson <j.a.e.andersson@gmail.com> | 2016-07-08 13:41:18 -0500 |
commit | d29613b7c94cdf61083b3d94062e829dd29d99c7 (patch) | |
tree | 9c22770d7fa4af913d9d73da108283ae377361cc | |
parent | 090946a248381a8433128489f1da177a26fc34fa (diff) | |
download | swig-d29613b7c94cdf61083b3d94062e829dd29d99c7.tar.gz |
Cleaned up indentation, matlab.cxx
-rw-r--r-- | Source/Modules/matlab.cxx | 886 |
1 files changed, 443 insertions, 443 deletions
diff --git a/Source/Modules/matlab.cxx b/Source/Modules/matlab.cxx index f14ef95e0..509692d0f 100644 --- a/Source/Modules/matlab.cxx +++ b/Source/Modules/matlab.cxx @@ -204,14 +204,14 @@ void MATLAB::main(int argc, char *argv[]) { Swig_arg_error(); } } else if (strcmp(argv[i], "-cppcast") == 0) { - cppcast = 1; - Swig_mark_arg(i); + cppcast = 1; + Swig_mark_arg(i); } else if (strcmp(argv[i], "-nocppcast") == 0) { - cppcast = 0; - Swig_mark_arg(i); + cppcast = 0; + Swig_mark_arg(i); } else if (strcmp(argv[i], "-noh") == 0) { - no_header_file = 1; - Swig_mark_arg(i); + no_header_file = 1; + Swig_mark_arg(i); } else if (strcmp(argv[i], "-pkgname") == 0) { if (argv[i+1]) { pkg_name = NewString(argv[i+1]); @@ -280,12 +280,12 @@ int MATLAB::top(Node *n) { // Set package name via module option if not set via command-line // Otherwise use module name as default (see below) if (!pkg_name && Getattr(options, "package")) { - pkg_name = Getattr(options, "package"); + pkg_name = Getattr(options, "package"); } // Set mex name via module option if not set via command-line // Otherwise use module name as default (see below) if (!mex_name && Getattr(options, "mexname")) { - mex_name = Getattr(options, "mexname"); + mex_name = Getattr(options, "mexname"); } } } @@ -334,7 +334,7 @@ int MATLAB::top(Node *n) { } /* The name of the compiled mex-wrapper is modulenameMEX by default, but this - can be overridden by setting the mexname opion */ + can be overridden by setting the mexname opion */ if (!mex_name) { mex_name=NewString(module); Append(mex_name, "MEX"); @@ -358,8 +358,8 @@ int MATLAB::top(Node *n) { if (!no_header_file) { f_runtime_h = NewFile(outfile_h, "w", SWIG_output_files()); if (!f_runtime_h) { - FileErrorDisplay(outfile_h); - SWIG_exit(EXIT_FAILURE); + FileErrorDisplay(outfile_h); + SWIG_exit(EXIT_FAILURE); } } else { f_runtime_h = f_runtime; @@ -746,27 +746,27 @@ String *matlabappend(Node *n) { } int MATLAB::importDirective(Node *n) { - String *modname = Getattr(n, "module"); - - if (modname) { - // Find the module node for this imported module. It should be the - // first child but search just in case. - Node *mod = firstChild(n); - while (mod && Strcmp(nodeType(mod), "module") != 0) - mod = nextSibling(mod); - - // If option mexname set for module, use instead - Node *options = Getattr(mod, "options"); - String *import_mexname = options ? Getattr(options, "mexname") : 0; - if (import_mexname == 0) { - import_mexname = Copy(modname); - Append(import_mexname, "MEX"); - } - - /* Add to list of modules to be imported */ - Append(l_modules, import_mexname); + String *modname = Getattr(n, "module"); + + if (modname) { + // Find the module node for this imported module. It should be the + // first child but search just in case. + Node *mod = firstChild(n); + while (mod && Strcmp(nodeType(mod), "module") != 0) + mod = nextSibling(mod); + + // If option mexname set for module, use instead + Node *options = Getattr(mod, "options"); + String *import_mexname = options ? Getattr(options, "mexname") : 0; + if (import_mexname == 0) { + import_mexname = Copy(modname); + Append(import_mexname, "MEX"); } - return Language::importDirective(n); + + /* Add to list of modules to be imported */ + Append(l_modules, import_mexname); + } + return Language::importDirective(n); } // const char *get_implicitconv_flag(Node *n) { @@ -1068,65 +1068,65 @@ int MATLAB::functionWrapper(Node *n) { } } - Setattr(n, "wrap:name", overname); + Setattr(n, "wrap:name", overname); - /* if the object is a director, and the method call originated from its - * underlying python object, resolve the call by going up the c++ - * inheritance chain. otherwise try to resolve the method in python. - * without this check an infinite loop is set up between the director and - * shadow class method calls. - */ + /* if the object is a director, and the method call originated from its + * underlying python object, resolve the call by going up the c++ + * inheritance chain. otherwise try to resolve the method in python. + * without this check an infinite loop is set up between the director and + * shadow class method calls. + */ - // NOTE: this code should only be inserted if this class is the - // base class of a director class. however, in general we haven't - // yet analyzed all classes derived from this one to see if they are - // directors. furthermore, this class may be used as the base of - // a director class defined in a completely different module at a - // later time, so this test must be included whether or not directorbase - // is true. we do skip this code if directors have not been enabled - // at the command line to preserve source-level compatibility with - // non-polymorphic swig. also, if this wrapper is for a smart-pointer - // method, there is no need to perform the test since the calling object - // (the smart-pointer) and the director object (the "pointee") are - // distinct. - - int director_method = is_member_director(n) && !is_smart_pointer() && !destructor; - if (director_method) { - Wrapper_add_local(f, "director", "Swig::Director *director = 0"); - Append(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, "SWIG_SetErrorMsg(PyExc_RuntimeError,\"accessing protected member %s\");\n", name); - //Append(f->code, "SWIG_fail;\n"); - //Append(f->code, "}\n"); - //} - - Wrapper_add_local(f, "upcall", "bool upcall = false"); - const char *self_parm = "argv[0]"; - Printf(f->code, "upcall = director;\n", self_parm); - } + // NOTE: this code should only be inserted if this class is the + // base class of a director class. however, in general we haven't + // yet analyzed all classes derived from this one to see if they are + // directors. furthermore, this class may be used as the base of + // a director class defined in a completely different module at a + // later time, so this test must be included whether or not directorbase + // is true. we do skip this code if directors have not been enabled + // at the command line to preserve source-level compatibility with + // non-polymorphic swig. also, if this wrapper is for a smart-pointer + // method, there is no need to perform the test since the calling object + // (the smart-pointer) and the director object (the "pointee") are + // distinct. - /* Emit the function call */ - if (director_method) { - Append(f->code, "try {\n"); - } + int director_method = is_member_director(n) && !is_smart_pointer() && !destructor; + if (director_method) { + Wrapper_add_local(f, "director", "Swig::Director *director = 0"); + Append(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, "SWIG_SetErrorMsg(PyExc_RuntimeError,\"accessing protected member %s\");\n", name); + //Append(f->code, "SWIG_fail;\n"); + //Append(f->code, "}\n"); + //} + Wrapper_add_local(f, "upcall", "bool upcall = false"); + const char *self_parm = "argv[0]"; + Printf(f->code, "upcall = director;\n", self_parm); + } - Swig_director_emit_dynamic_cast(n, f); + /* Emit the function call */ + if (director_method) { + Append(f->code, "try {\n"); + } - if (destructor) { - Append(f->code, "if (is_owned) {\n"); - } - String *actioncode = emit_action(n); - if (destructor) { - Append(actioncode, "}\n"); - } - if (director_method) { - Append(actioncode, "} catch (Swig::DirectorException&) {\n"); - Append(actioncode, " SWIG_fail;\n"); - Append(actioncode, "}\n"); - } + Swig_director_emit_dynamic_cast(n, f); + + if (destructor) { + Append(f->code, "if (is_owned) {\n"); + } + String *actioncode = emit_action(n); + if (destructor) { + Append(actioncode, "}\n"); + } + + if (director_method) { + Append(actioncode, "} catch (Swig::DirectorException&) {\n"); + Append(actioncode, " SWIG_fail;\n"); + Append(actioncode, "}\n"); + } Wrapper_add_local(f, "_out", "mxArray * _out"); @@ -1477,144 +1477,144 @@ int MATLAB::constantWrapper(Node *n) { return SWIG_OK; } - /* ------------------------------------------------------------ - * classDirectorConstructor() - * ------------------------------------------------------------ */ - - int MATLAB::classDirectorConstructor(Node *n) { - Node *parent = Getattr(n, "parentNode"); - String *sub = NewString(""); - String *decl = Getattr(n, "decl"); - String *supername = Swig_class_name(parent); - String *classname = NewString(""); - Printf(classname, "SwigDirector_%s", supername); - - /* insert self parameter */ - Parm *p; - ParmList *superparms = Getattr(n, "parms"); - ParmList *parms = CopyParmList(superparms); - String *type = NewString("mxArray"); - SwigType_add_pointer(type); - p = NewParm(type, NewString("self"), n); - set_nextSibling(p, parms); - parms = p; - - if (!Getattr(n, "defaultargs")) { - /* constructor */ - { - Wrapper *w = NewWrapper(); - String *call; - String *basetype = Getattr(parent, "classtype"); - String *target = Swig_method_decl(0, decl, classname, parms, 0, 0); - call = Swig_csuperclass_call(0, basetype, superparms); - Printf(w->def, "%s::%s: %s, Swig::Director(self) { \n", classname, target, call); - Printf(w->def, " SWIG_DIRECTOR_RGTR((%s *)this, this); \n", basetype); - Printf(w->def, " SWIG_Matlab_getSwigPtr(self);\n"); - Append(w->def, "}\n"); - Delete(target); - Wrapper_print(w, f_directors); - Delete(call); - DelWrapper(w); - } - - /* constructor header */ - { - String *target = Swig_method_decl(0, decl, classname, parms, 0, 1); - Printf(f_directors_h, " %s;\n", target); - Delete(target); - } - } - - - - Delete(sub); - Delete(classname); - Delete(supername); - Delete(parms); - return Language::classDirectorConstructor(n); - } +/* ------------------------------------------------------------ + * classDirectorConstructor() + * ------------------------------------------------------------ */ - /* ------------------------------------------------------------ - * classDirectorDefaultConstructor() - * ------------------------------------------------------------ */ +int MATLAB::classDirectorConstructor(Node *n) { + Node *parent = Getattr(n, "parentNode"); + String *sub = NewString(""); + String *decl = Getattr(n, "decl"); + String *supername = Swig_class_name(parent); + String *classname = NewString(""); + Printf(classname, "SwigDirector_%s", supername); - int MATLAB::classDirectorDefaultConstructor(Node *n) { - String *classname = Swig_class_name(n); + /* insert self parameter */ + Parm *p; + ParmList *superparms = Getattr(n, "parms"); + ParmList *parms = CopyParmList(superparms); + String *type = NewString("mxArray"); + SwigType_add_pointer(type); + p = NewParm(type, NewString("self"), n); + set_nextSibling(p, parms); + parms = p; + + if (!Getattr(n, "defaultargs")) { + /* constructor */ { - Node *parent = Swig_methodclass(n); - String *basetype = Getattr(parent, "classtype"); Wrapper *w = NewWrapper(); - Printf(w->def, "SwigDirector_%s::SwigDirector_%s(mxArray* self) : Swig::Director(self) { \n", classname, classname); + String *call; + String *basetype = Getattr(parent, "classtype"); + String *target = Swig_method_decl(0, decl, classname, parms, 0, 0); + call = Swig_csuperclass_call(0, basetype, superparms); + Printf(w->def, "%s::%s: %s, Swig::Director(self) { \n", classname, target, call); Printf(w->def, " SWIG_DIRECTOR_RGTR((%s *)this, this); \n", basetype); + Printf(w->def, " SWIG_Matlab_getSwigPtr(self);\n"); Append(w->def, "}\n"); + Delete(target); Wrapper_print(w, f_directors); + Delete(call); DelWrapper(w); } - Printf(f_directors_h, " SwigDirector_%s(mxArray* self);\n", classname); - Delete(classname); - return Language::classDirectorDefaultConstructor(n); + + /* constructor header */ + { + String *target = Swig_method_decl(0, decl, classname, parms, 0, 1); + Printf(f_directors_h, " %s;\n", target); + Delete(target); + } } - /* ------------------------------------------------------------ - * classDirectorInit() - * ------------------------------------------------------------ */ - int MATLAB::classDirectorInit(Node *n) { - String *declaration = Swig_director_declaration(n); - Printf(f_directors_h, "\n"); - Printf(f_directors_h, "%s\n", declaration); - Printf(f_directors_h, "public:\n"); - Delete(declaration); - return Language::classDirectorInit(n); - } + Delete(sub); + Delete(classname); + Delete(supername); + Delete(parms); + return Language::classDirectorConstructor(n); +} - /* ------------------------------------------------------------ - * classDirectorEnd() - * ------------------------------------------------------------ */ +/* ------------------------------------------------------------ + * classDirectorDefaultConstructor() + * ------------------------------------------------------------ */ - int MATLAB::classDirectorEnd(Node *n) { - if (dirprot_mode()) { - /* - This implementation uses a std::map<std::string,int>. - - It should be possible to rewrite it using a more elegant way, - like copying the Java approach for the 'override' array. - - But for now, this seems to be the least intrusive way. - */ - Printf(f_directors_h, "\n"); - Printf(f_directors_h, "/* Internal director utilities */\n"); - Printf(f_directors_h, "public:\n"); - Printf(f_directors_h, " bool swig_get_inner(const char *swig_protected_method_name) const {\n"); - Printf(f_directors_h, " std::map<std::string, bool>::const_iterator iv = swig_inner.find(swig_protected_method_name);\n"); - Printf(f_directors_h, " return (iv != swig_inner.end() ? iv->second : false);\n"); - Printf(f_directors_h, " }\n"); - - Printf(f_directors_h, " void swig_set_inner(const char *swig_protected_method_name, bool val) const {\n"); - Printf(f_directors_h, " swig_inner[swig_protected_method_name] = val;\n"); - Printf(f_directors_h, " }\n"); - Printf(f_directors_h, "private:\n"); - Printf(f_directors_h, " mutable std::map<std::string, bool> swig_inner;\n"); +int MATLAB::classDirectorDefaultConstructor(Node *n) { + String *classname = Swig_class_name(n); + { + Node *parent = Swig_methodclass(n); + String *basetype = Getattr(parent, "classtype"); + Wrapper *w = NewWrapper(); + Printf(w->def, "SwigDirector_%s::SwigDirector_%s(mxArray* self) : Swig::Director(self) { \n", classname, classname); + Printf(w->def, " SWIG_DIRECTOR_RGTR((%s *)this, this); \n", basetype); + Append(w->def, "}\n"); + Wrapper_print(w, f_directors); + DelWrapper(w); + } + Printf(f_directors_h, " SwigDirector_%s(mxArray* self);\n", classname); + Delete(classname); + return Language::classDirectorDefaultConstructor(n); +} - } - Printf(f_directors_h, "};\n\n"); - return Language::classDirectorEnd(n); - } +/* ------------------------------------------------------------ + * classDirectorInit() + * ------------------------------------------------------------ */ +int MATLAB::classDirectorInit(Node *n) { + String *declaration = Swig_director_declaration(n); + Printf(f_directors_h, "\n"); + Printf(f_directors_h, "%s\n", declaration); + Printf(f_directors_h, "public:\n"); + Delete(declaration); + return Language::classDirectorInit(n); +} - /* ------------------------------------------------------------ - * classDirectorDisown() - * ------------------------------------------------------------ */ +/* ------------------------------------------------------------ + * classDirectorEnd() + * ------------------------------------------------------------ */ - int MATLAB::classDirectorDisown(Node *n) { - int result; +int MATLAB::classDirectorEnd(Node *n) { + if (dirprot_mode()) { + /* + This implementation uses a std::map<std::string,int>. + + It should be possible to rewrite it using a more elegant way, + like copying the Java approach for the 'override' array. + + But for now, this seems to be the least intrusive way. + */ Printf(f_directors_h, "\n"); - result = Language::classDirectorDisown(n); - return result; + Printf(f_directors_h, "/* Internal director utilities */\n"); + Printf(f_directors_h, "public:\n"); + Printf(f_directors_h, " bool swig_get_inner(const char *swig_protected_method_name) const {\n"); + Printf(f_directors_h, " std::map<std::string, bool>::const_iterator iv = swig_inner.find(swig_protected_method_name);\n"); + Printf(f_directors_h, " return (iv != swig_inner.end() ? iv->second : false);\n"); + Printf(f_directors_h, " }\n"); + + Printf(f_directors_h, " void swig_set_inner(const char *swig_protected_method_name, bool val) const {\n"); + Printf(f_directors_h, " swig_inner[swig_protected_method_name] = val;\n"); + Printf(f_directors_h, " }\n"); + Printf(f_directors_h, "private:\n"); + Printf(f_directors_h, " mutable std::map<std::string, bool> swig_inner;\n"); + } + Printf(f_directors_h, "};\n\n"); + return Language::classDirectorEnd(n); +} + + +/* ------------------------------------------------------------ + * classDirectorDisown() + * ------------------------------------------------------------ */ + +int MATLAB::classDirectorDisown(Node *n) { + int result; + Printf(f_directors_h, "\n"); + result = Language::classDirectorDisown(n); + return result; +} + /* --------------------------------------------------------------- * classDirectorMethod() * @@ -1688,14 +1688,14 @@ int MATLAB::classDirectorMethod(Node *n, Node *parent, String *super) { Swig_typemap_attach_parms("throws", throw_parm_list, 0); for (p = throw_parm_list; p; p = nextSibling(p)) { if (Getattr(p, "tmap:throws")) { - if (gencomma++) { - Append(w->def, ", "); - Append(declaration, ", "); - } - String *str = SwigType_str(Getattr(p, "type"), 0); - Append(w->def, str); - Append(declaration, str); - Delete(str); + if (gencomma++) { + Append(w->def, ", "); + Append(declaration, ", "); + } + String *str = SwigType_str(Getattr(p, "type"), 0); + Append(w->def, str); + Append(declaration, str); + Delete(str); } } @@ -1721,13 +1721,13 @@ int MATLAB::classDirectorMethod(Node *n, Node *parent, String *super) { if (ignored_method) { if (!pure_virtual) { if (!is_void) - Printf(w->code, "return "); + Printf(w->code, "return "); String *super_call = Swig_method_call(super, l); Printf(w->code, "%s;\n", super_call); Delete(super_call); } else { Printf(w->code, "Swig::DirectorPureVirtualException::raise(\"Attempted to invoke pure virtual method %s::%s\");\n", SwigType_namestr(c_classname), - SwigType_namestr(name)); + SwigType_namestr(name)); } } else { /* attach typemaps to arguments (C/C++ -> MATLAB) */ @@ -1754,8 +1754,8 @@ int MATLAB::classDirectorMethod(Node *n, Node *parent, String *super) { int use_parse = 0; while (p) { if (checkAttribute(p, "tmap:in:numinputs", "0")) { - p = Getattr(p, "tmap:in:next"); - continue; + p = Getattr(p, "tmap:in:next"); + continue; } /* old style? caused segfaults without the p!=0 check @@ -1764,109 +1764,109 @@ int MATLAB::classDirectorMethod(Node *n, Node *parent, String *super) { while (Getattr(p, "tmap:ignore")) { p = Getattr(p, "tmap:ignore:next"); } - */ + */ if (Getattr(p, "tmap:directorargout") != 0) - outputs++; + outputs++; String *pname = Getattr(p, "name"); String *ptype = Getattr(p, "type"); Putc(',', arglist); if ((tm = Getattr(p, "tmap:directorin")) != 0) { - String *parse = Getattr(p, "tmap:directorin:parse"); - if (!parse) { - sprintf(source, "obj%d", idx++); - String *input = NewString(source); - Setattr(p, "emit:directorinput", input); - Replaceall(tm, "$input", input); - Delete(input); - Replaceall(tm, "$owner", "0"); - /* Wrapper_add_localv(w, source, "SwigVar_mxArray", source, "= 0", NIL); */ - Printv(wrap_args, "SwigVar_mxArray ", source, ";\n", NIL); - - Printv(wrap_args, tm, "\n", NIL); - Printv(arglist, "(mxArray *)", source, NIL); - Putc('O', parse_args); - } else { - use_parse = 1; - Append(parse_args, parse); - Setattr(p, "emit:directorinput", pname); - Replaceall(tm, "$input", pname); - Replaceall(tm, "$owner", "0"); - if (Len(tm) == 0) - Append(tm, pname); - Append(arglist, tm); - } - p = Getattr(p, "tmap:directorin:next"); - continue; + String *parse = Getattr(p, "tmap:directorin:parse"); + if (!parse) { + sprintf(source, "obj%d", idx++); + String *input = NewString(source); + Setattr(p, "emit:directorinput", input); + Replaceall(tm, "$input", input); + Delete(input); + Replaceall(tm, "$owner", "0"); + /* Wrapper_add_localv(w, source, "SwigVar_mxArray", source, "= 0", NIL); */ + Printv(wrap_args, "SwigVar_mxArray ", source, ";\n", NIL); + + Printv(wrap_args, tm, "\n", NIL); + Printv(arglist, "(mxArray *)", source, NIL); + Putc('O', parse_args); + } else { + use_parse = 1; + Append(parse_args, parse); + Setattr(p, "emit:directorinput", pname); + Replaceall(tm, "$input", pname); + Replaceall(tm, "$owner", "0"); + if (Len(tm) == 0) + Append(tm, pname); + Append(arglist, tm); + } + p = Getattr(p, "tmap:directorin:next"); + continue; } else if (Cmp(ptype, "void")) { - /* special handling for pointers to other C++ director classes. - * ideally this would be left to a typemap, but there is currently no - * way to selectively apply the dynamic_cast<> to classes that have - * directors. in other words, the type "SwigDirector_$1_lname" only exists - * for classes with directors. we avoid the problem here by checking - * module.wrap::directormap, but it's not clear how to get a typemap to - * do something similar. perhaps a new default typemap (in addition - * to SWIGTYPE) called DIRECTORTYPE? - */ - if (SwigType_ispointer(ptype) || SwigType_isreference(ptype)) { - Node *module = Getattr(parent, "module"); - Node *target = Swig_directormap(module, ptype); - sprintf(source, "obj%d", idx++); - String *nonconst = 0; - /* strip pointer/reference --- should move to Swig/stype.c */ - String *nptype = NewString(Char(ptype) + 2); - /* name as pointer */ - String *ppname = Copy(pname); - if (SwigType_isreference(ptype)) { - Insert(ppname, 0, "&"); - } - /* if necessary, cast away const since MATLAB doesn't support it! */ - if (SwigType_isconst(nptype)) { - nonconst = NewStringf("nc_tmp_%s", pname); - String *nonconst_i = NewStringf("= const_cast< %s >(%s)", SwigType_lstr(ptype, 0), ppname); - Wrapper_add_localv(w, nonconst, SwigType_lstr(ptype, 0), nonconst, nonconst_i, NIL); - Delete(nonconst_i); - Swig_warning(WARN_LANG_DISCARD_CONST, input_file, line_number, - "Target language argument '%s' discards const in director method %s::%s.\n", - SwigType_str(ptype, pname), SwigType_namestr(c_classname), SwigType_namestr(name)); - } else { - nonconst = Copy(ppname); - } - Delete(nptype); - Delete(ppname); - String *mangle = SwigType_manglestr(ptype); - if (target) { - String *director = NewStringf("director_%s", mangle); - Wrapper_add_localv(w, director, "Swig::Director *", director, "= 0", NIL); - Wrapper_add_localv(w, source, "SwigVar_mxArray", source, "= 0", NIL); - Printf(wrap_args, "%s = SWIG_DIRECTOR_CAST(%s);\n", director, nonconst); - Printf(wrap_args, "if (!%s) {\n", director); - Printf(wrap_args, "%s = SWIG_InternalNewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); - Append(wrap_args, "} else {\n"); - Printf(wrap_args, "%s = %s->swig_get_self();\n", source, director); - Printf(wrap_args, "Py_INCREF((mxArray *)%s);\n", source); - Append(wrap_args, "}\n"); - Delete(director); - Printv(arglist, source, NIL); - } else { - Wrapper_add_localv(w, source, "SwigVar_mxArray", source, "= 0", NIL); - Printf(wrap_args, "%s = SWIG_InternalNewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); - //Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE_p_%s, 0);\n", - // source, nonconst, base); - Printv(arglist, source, NIL); - } - Putc('O', parse_args); - Delete(mangle); - Delete(nonconst); - } else { - Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, - "Unable to use type %s as a function argument in director method %s::%s (skipping method).\n", SwigType_str(ptype, 0), - SwigType_namestr(c_classname), SwigType_namestr(name)); - status = SWIG_NOWRAP; - break; - } + /* special handling for pointers to other C++ director classes. + * ideally this would be left to a typemap, but there is currently no + * way to selectively apply the dynamic_cast<> to classes that have + * directors. in other words, the type "SwigDirector_$1_lname" only exists + * for classes with directors. we avoid the problem here by checking + * module.wrap::directormap, but it's not clear how to get a typemap to + * do something similar. perhaps a new default typemap (in addition + * to SWIGTYPE) called DIRECTORTYPE? + */ + if (SwigType_ispointer(ptype) || SwigType_isreference(ptype)) { + Node *module = Getattr(parent, "module"); + Node *target = Swig_directormap(module, ptype); + sprintf(source, "obj%d", idx++); + String *nonconst = 0; + /* strip pointer/reference --- should move to Swig/stype.c */ + String *nptype = NewString(Char(ptype) + 2); + /* name as pointer */ + String *ppname = Copy(pname); + if (SwigType_isreference(ptype)) { + Insert(ppname, 0, "&"); + } + /* if necessary, cast away const since MATLAB doesn't support it! */ + if (SwigType_isconst(nptype)) { + nonconst = NewStringf("nc_tmp_%s", pname); + String *nonconst_i = NewStringf("= const_cast< %s >(%s)", SwigType_lstr(ptype, 0), ppname); + Wrapper_add_localv(w, nonconst, SwigType_lstr(ptype, 0), nonconst, nonconst_i, NIL); + Delete(nonconst_i); + Swig_warning(WARN_LANG_DISCARD_CONST, input_file, line_number, + "Target language argument '%s' discards const in director method %s::%s.\n", + SwigType_str(ptype, pname), SwigType_namestr(c_classname), SwigType_namestr(name)); + } else { + nonconst = Copy(ppname); + } + Delete(nptype); + Delete(ppname); + String *mangle = SwigType_manglestr(ptype); + if (target) { + String *director = NewStringf("director_%s", mangle); + Wrapper_add_localv(w, director, "Swig::Director *", director, "= 0", NIL); + Wrapper_add_localv(w, source, "SwigVar_mxArray", source, "= 0", NIL); + Printf(wrap_args, "%s = SWIG_DIRECTOR_CAST(%s);\n", director, nonconst); + Printf(wrap_args, "if (!%s) {\n", director); + Printf(wrap_args, "%s = SWIG_InternalNewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); + Append(wrap_args, "} else {\n"); + Printf(wrap_args, "%s = %s->swig_get_self();\n", source, director); + Printf(wrap_args, "Py_INCREF((mxArray *)%s);\n", source); + Append(wrap_args, "}\n"); + Delete(director); + Printv(arglist, source, NIL); + } else { + Wrapper_add_localv(w, source, "SwigVar_mxArray", source, "= 0", NIL); + Printf(wrap_args, "%s = SWIG_InternalNewPointerObj(%s, SWIGTYPE%s, 0);\n", source, nonconst, mangle); + //Printf(wrap_args, "%s = SWIG_NewPointerObj(%s, SWIGTYPE_p_%s, 0);\n", + // source, nonconst, base); + Printv(arglist, source, NIL); + } + Putc('O', parse_args); + Delete(mangle); + Delete(nonconst); + } else { + Swig_warning(WARN_TYPEMAP_DIRECTORIN_UNDEF, input_file, line_number, + "Unable to use type %s as a function argument in director method %s::%s (skipping method).\n", SwigType_str(ptype, 0), + SwigType_namestr(c_classname), SwigType_namestr(name)); + status = SWIG_NOWRAP; + break; + } } p = nextSibling(p); } @@ -1910,7 +1910,7 @@ int MATLAB::classDirectorMethod(Node *n, Node *parent, String *super) { if (!tm) { tm = Getattr(n, "feature:director:except"); if (tm) - tm = Copy(tm); + tm = Copy(tm); } Printf(w->code, "if (error != 0) {\n", Swig_cresult_name()); Printf(w->code, "mexCallMATLAB(0, (mxArray **)NULL,1, &error, \"throw\");"); @@ -1951,50 +1951,50 @@ int MATLAB::classDirectorMethod(Node *n, Node *parent, String *super) { if (!is_void) { tm = Swig_typemap_lookup("directorout", n, Swig_cresult_name(), w); if (tm != 0) { - if (outputs > 1) { - Printf(w->code, "output = PyTuple_GetItem(%s, %d);\n", Swig_cresult_name(), idx++); - Replaceall(tm, "$input", "output"); - } else { - Replaceall(tm, "$input", Swig_cresult_name()); - } - char temp[24]; - sprintf(temp, "%d", idx); - Replaceall(tm, "$argnum", temp); - - /* TODO check this */ - if (Getattr(n, "wrap:disown")) { - Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); - } else { - Replaceall(tm, "$disown", "0"); - } - if (Getattr(n, "tmap:directorout:implicitconv")) { - Replaceall(tm, "$implicitconv", get_implicitconv_flag(n)); - } - Replaceall(tm, "$result", "c_result"); - Printv(w->code, tm, "\n", NIL); - Delete(tm); + if (outputs > 1) { + Printf(w->code, "output = PyTuple_GetItem(%s, %d);\n", Swig_cresult_name(), idx++); + Replaceall(tm, "$input", "output"); + } else { + Replaceall(tm, "$input", Swig_cresult_name()); + } + char temp[24]; + sprintf(temp, "%d", idx); + Replaceall(tm, "$argnum", temp); + + /* TODO check this */ + if (Getattr(n, "wrap:disown")) { + Replaceall(tm, "$disown", "SWIG_POINTER_DISOWN"); + } else { + Replaceall(tm, "$disown", "0"); + } + if (Getattr(n, "tmap:directorout:implicitconv")) { + Replaceall(tm, "$implicitconv", get_implicitconv_flag(n)); + } + Replaceall(tm, "$result", "c_result"); + Printv(w->code, tm, "\n", NIL); + Delete(tm); } else { - Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, - "Unable to use return type %s in director method %s::%s (skipping method).\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), - SwigType_namestr(name)); - status = SWIG_ERROR; + Swig_warning(WARN_TYPEMAP_DIRECTOROUT_UNDEF, input_file, line_number, + "Unable to use return type %s in director method %s::%s (skipping method).\n", SwigType_str(returntype, 0), SwigType_namestr(c_classname), + SwigType_namestr(name)); + status = SWIG_ERROR; } } /* marshal outputs */ for (p = l; p;) { if ((tm = Getattr(p, "tmap:directorargout")) != 0) { - if (outputs > 1) { - Printf(w->code, "output = PyTuple_GetItem(%s, %d);\n", Swig_cresult_name(), idx++); - Replaceall(tm, "$result", "output"); - } else { - Replaceall(tm, "$result", Swig_cresult_name()); - } - Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); - Printv(w->code, tm, "\n", NIL); - p = Getattr(p, "tmap:directorargout:next"); + if (outputs > 1) { + Printf(w->code, "output = PyTuple_GetItem(%s, %d);\n", Swig_cresult_name(), idx++); + Replaceall(tm, "$result", "output"); + } else { + Replaceall(tm, "$result", Swig_cresult_name()); + } + Replaceall(tm, "$input", Getattr(p, "emit:directorinput")); + Printv(w->code, tm, "\n", NIL); + p = Getattr(p, "tmap:directorargout:next"); } else { - p = nextSibling(p); + p = nextSibling(p); } } @@ -2008,9 +2008,9 @@ int MATLAB::classDirectorMethod(Node *n, Node *parent, String *super) { if (!(ignored_method && !pure_virtual)) { String *rettype = SwigType_str(returntype, 0); if (!SwigType_isreference(returntype)) { - Printf(w->code, "return (%s) c_result;\n", rettype); + Printf(w->code, "return (%s) c_result;\n", rettype); } else { - Printf(w->code, "return (%s) *c_result;\n", rettype); + Printf(w->code, "return (%s) *c_result;\n", rettype); } Delete(rettype); } @@ -2050,9 +2050,9 @@ int MATLAB::classDirectorMethod(Node *n, Node *parent, String *super) { return status; } - /* ---------------------------------------------------------------------------- - * END of C++ Director Class modifications - * ------------------------------------------------------------------------- */ +/* ---------------------------------------------------------------------------- + * END of C++ Director Class modifications + * ------------------------------------------------------------------------- */ int MATLAB::nativeWrapper(Node *n) { @@ -2088,16 +2088,16 @@ int MATLAB::classHandler(Node *n) { if (class_name) SWIG_exit(EXIT_FAILURE); class_name = Getattr(n, "sym:name"); - // Wrappers cannot be emitted - static Hash *emitted = NewHash(); - String *mangled_classname = Swig_name_mangle(Getattr(n, "name")); - if (Getattr(emitted, mangled_classname)) { - Delete(mangled_classname); - class_name=0; - return SWIG_NOWRAP; - } - Setattr(emitted, mangled_classname, "1"); - Delete(mangled_classname); + // Wrappers cannot be emitted + static Hash *emitted = NewHash(); + String *mangled_classname = Swig_name_mangle(Getattr(n, "name")); + if (Getattr(emitted, mangled_classname)) { + Delete(mangled_classname); + class_name=0; + return SWIG_NOWRAP; + } + Setattr(emitted, mangled_classname, "1"); + Delete(mangled_classname); // store class_name for use by NewPointerObj { @@ -2115,10 +2115,10 @@ int MATLAB::classHandler(Node *n) { } else { - SwigType *t = Copy(Getattr(n, "name")); - SwigType_add_pointer(t); - SwigType_remember_clientdata(t, quoted_class_name); - Delete(t); + SwigType *t = Copy(Getattr(n, "name")); + SwigType_add_pointer(t); + SwigType_remember_clientdata(t, quoted_class_name); + Delete(t); } Delete(quoted_class_name); Delete(smartptr); @@ -2155,12 +2155,12 @@ int MATLAB::classHandler(Node *n) { #if 0 // some prints for debugging { - String *tmpname = Getattr(b.item, "name"); - if (tmpname) - Printf(stderr,"BASE %s\n", tmpname); - tmpname = Getattr(b.item, "sym:name"); - if (tmpname) - Printf(stderr,"BASEsym %s\n", tmpname); + String *tmpname = Getattr(b.item, "name"); + if (tmpname) + Printf(stderr,"BASE %s\n", tmpname); + tmpname = Getattr(b.item, "sym:name"); + if (tmpname) + Printf(stderr,"BASEsym %s\n", tmpname); } #endif String *bname = Getattr(b.item, "sym:name"); @@ -2462,55 +2462,55 @@ int MATLAB::membervariableHandler(Node *n) { } void MATLAB::wrapConstructor(int gw_ind, String *symname, String *fullname, Node *n) { - Printf(f_wrap_m," function self = %s(varargin)\n",symname); - Printf(f_wrap_m,"%s",base_init); - Printf(f_wrap_m," if nargin==1 && strcmp(class(varargin{1}),'SwigRef')\n"); - Printf(f_wrap_m," if ~isnull(varargin{1})\n"); - Printf(f_wrap_m," self.swigPtr = varargin{1}.swigPtr;\n"); - Printf(f_wrap_m," end\n"); - Printf(f_wrap_m," else\n"); - if (fullname==0) { - Printf(f_wrap_m," error('No matching constructor');\n"); - } else { - // How to get working on C side? Commented out, replaced by hack below - // Printf(f_wrap_m," self.swigPtr = %s(%d, varargin{:});\n", mex_name, gw_ind); - if (have_matlabprepend(n)) - Printf(f_wrap_m, "%s\n",matlabprepend(n)); - Printf(f_wrap_m," tmp = %s(%d, varargin{:});\n", mex_name, gw_ind); - Printf(f_wrap_m," self.swigPtr = tmp.swigPtr;\n"); - Printf(f_wrap_m," tmp.swigPtr = [];\n"); - if (have_matlabappend(n)) - Printf(f_wrap_m, "%s\n",matlabappend(n)); - } - Printf(f_wrap_m," end\n"); - Printf(f_wrap_m," end\n"); + Printf(f_wrap_m," function self = %s(varargin)\n",symname); + Printf(f_wrap_m,"%s",base_init); + Printf(f_wrap_m," if nargin==1 && strcmp(class(varargin{1}),'SwigRef')\n"); + Printf(f_wrap_m," if ~isnull(varargin{1})\n"); + Printf(f_wrap_m," self.swigPtr = varargin{1}.swigPtr;\n"); + Printf(f_wrap_m," end\n"); + Printf(f_wrap_m," else\n"); + if (fullname==0) { + Printf(f_wrap_m," error('No matching constructor');\n"); + } else { + // How to get working on C side? Commented out, replaced by hack below + // Printf(f_wrap_m," self.swigPtr = %s(%d, varargin{:});\n", mex_name, gw_ind); + if (have_matlabprepend(n)) + Printf(f_wrap_m, "%s\n",matlabprepend(n)); + Printf(f_wrap_m," tmp = %s(%d, varargin{:});\n", mex_name, gw_ind); + Printf(f_wrap_m," self.swigPtr = tmp.swigPtr;\n"); + Printf(f_wrap_m," tmp.swigPtr = [];\n"); + if (have_matlabappend(n)) + Printf(f_wrap_m, "%s\n",matlabappend(n)); + } + Printf(f_wrap_m," end\n"); + Printf(f_wrap_m," end\n"); } void MATLAB::wrapConstructorDirector(int gw_ind, String *symname, String *fullname, Node *n) { - Printf(f_wrap_m," function self = %s(varargin)\n",symname); - Printf(f_wrap_m,"%s",base_init); - Printf(f_wrap_m," if nargin==1 && strcmp(class(varargin{1}),'SwigRef')\n"); - Printf(f_wrap_m," if ~isnull(varargin{1})\n"); - Printf(f_wrap_m," self.swigPtr = varargin{1}.swigPtr;\n"); + Printf(f_wrap_m," function self = %s(varargin)\n",symname); + Printf(f_wrap_m,"%s",base_init); + Printf(f_wrap_m," if nargin==1 && strcmp(class(varargin{1}),'SwigRef')\n"); + Printf(f_wrap_m," if ~isnull(varargin{1})\n"); + Printf(f_wrap_m," self.swigPtr = varargin{1}.swigPtr;\n"); + Printf(f_wrap_m," end\n"); + Printf(f_wrap_m," else\n"); + if (fullname==0) { + Printf(f_wrap_m," error('No matching constructor');\n"); + } else { + // How to get working on C side? Commented out, replaed by hack below + // Printf(f_wrap_m," self.swigPtr = %s(%d, varargin{:});\n", mex_name, gw_ind); + if (have_matlabprepend(n)) Printf(f_wrap_m, "%s\n", matlabprepend(n)); + Printf(f_wrap_m," if strcmp(class(self),'director_basic.%s')\n", symname); + Printf(f_wrap_m," tmp = %s(%d, 0, varargin{:});\n", mex_name, gw_ind); + Printf(f_wrap_m," else\n"); + Printf(f_wrap_m," tmp = %s(%d, self, varargin{:});\n", mex_name, gw_ind); Printf(f_wrap_m," end\n"); - Printf(f_wrap_m," else\n"); - if (fullname==0) { - Printf(f_wrap_m," error('No matching constructor');\n"); - } else { - // How to get working on C side? Commented out, replaed by hack below - // Printf(f_wrap_m," self.swigPtr = %s(%d, varargin{:});\n", mex_name, gw_ind); - if (have_matlabprepend(n)) Printf(f_wrap_m, "%s\n", matlabprepend(n)); - Printf(f_wrap_m," if strcmp(class(self),'director_basic.%s')\n", symname); - Printf(f_wrap_m," tmp = %s(%d, 0, varargin{:});\n", mex_name, gw_ind); - Printf(f_wrap_m," else\n"); - Printf(f_wrap_m," tmp = %s(%d, self, varargin{:});\n", mex_name, gw_ind); - Printf(f_wrap_m," end\n"); - Printf(f_wrap_m," self.swigPtr = tmp.swigPtr;\n"); - Printf(f_wrap_m," tmp.swigPtr = [];\n"); - if (have_matlabappend(n)) Printf(f_wrap_m, "%s\n",matlabappend(n)); - } - Printf(f_wrap_m," end\n"); - Printf(f_wrap_m," end\n"); + Printf(f_wrap_m," self.swigPtr = tmp.swigPtr;\n"); + Printf(f_wrap_m," tmp.swigPtr = [];\n"); + if (have_matlabappend(n)) Printf(f_wrap_m, "%s\n",matlabappend(n)); + } + Printf(f_wrap_m," end\n"); + Printf(f_wrap_m," end\n"); } int MATLAB::constructorHandler(Node *n) { @@ -2543,23 +2543,23 @@ int MATLAB::constructorHandler(Node *n) { Delete(fullname); } - if (use_director) { - Parm *parms = Getattr(n, "parms"); - Parm *self; - String *name = NewString("self"); - String *type = NewString("mxArray"); - SwigType_add_pointer(type); - self = NewParm(type, name, n); - Delete(type); - Delete(name); - Setattr(self, "lname", "O"); - if (parms) - set_nextSibling(self, parms); - Setattr(n, "parms", self); - Setattr(n, "wrap:self", "1"); - Setattr(n, "hidden", "1"); - Delete(self); - } + if (use_director) { + Parm *parms = Getattr(n, "parms"); + Parm *self; + String *name = NewString("self"); + String *type = NewString("mxArray"); + SwigType_add_pointer(type); + self = NewParm(type, name, n); + Delete(type); + Delete(name); + Setattr(self, "lname", "O"); + if (parms) + set_nextSibling(self, parms); + Setattr(n, "parms", self); + Setattr(n, "wrap:self", "1"); + Setattr(n, "hidden", "1"); + Delete(self); + } return Language::constructorHandler(n); @@ -2677,20 +2677,20 @@ int MATLAB::memberconstantHandler(Node *n) { } int MATLAB::insertDirective(Node *n) { - String *code = Getattr(n, "code"); - String *section = Getattr(n, "section"); - - if (Cmp(section, "matlab") == 0 && f_wrap_m) { - Printv(f_wrap_m, code, NIL); - } else if (Cmp(section, "matlabsetup") == 0) { - Printv(f_setup_m, code, NIL); - } else { - Language::insertDirective(n); - } + String *code = Getattr(n, "code"); + String *section = Getattr(n, "section"); - return SWIG_OK; + if (Cmp(section, "matlab") == 0 && f_wrap_m) { + Printv(f_wrap_m, code, NIL); + } else if (Cmp(section, "matlabsetup") == 0) { + Printv(f_setup_m, code, NIL); + } else { + Language::insertDirective(n); } + return SWIG_OK; +} + int MATLAB::staticmembervariableHandler(Node *n) { #ifdef MATLABPRINTFUNCTIONENTRY Printf(stderr,"Entering staticmembervariableHandler\n"); @@ -2966,12 +2966,12 @@ void MATLAB::dispatchFunction(Node *n) { Node *sibl = n; while (Getattr(sibl, "sym:previousSibling")) - sibl = Getattr(sibl, "sym:previousSibling"); // go all the way up + sibl = Getattr(sibl, "sym:previousSibling"); // go all the way up String *protoTypes = NewString(""); do { - String *fulldecl = Swig_name_decl(sibl); - Printf(protoTypes, "\n\" %s\\n\"", fulldecl); - Delete(fulldecl); + String *fulldecl = Swig_name_decl(sibl); + Printf(protoTypes, "\n\" %s\\n\"", fulldecl); + Delete(fulldecl); } while ((sibl = Getattr(sibl, "sym:nextSibling"))); Printf(f->def, "int %s (int resc, mxArray *resv[], int argc, mxArray *argv[]) {", wname); |