Skip to content

Commit

Permalink
Issue #68 Emitting varin and varout typemaps
Browse files Browse the repository at this point in the history
  • Loading branch information
jaeandersson committed Jun 2, 2016
1 parent f092882 commit 7232c0d
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 92 deletions.
58 changes: 2 additions & 56 deletions Examples/test-suite/matlab/Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,62 +11,8 @@ srcdir = @srcdir@
top_srcdir = @top_srcdir@
top_builddir = @top_builddir@

#CPP_TEST_CASES += \
# li_std_pair_extra \
# li_std_string_extra \
# matlab_cell_deref\
# matlab_dim

# CPP_TEST_BROKEN += \
# allprotected \
# apply_signed_char \
# apply_strings \
# nested_directors \
# special_variables \
# typemap_directorout \
# director_abstract \
# director_alternating \
# director_basic \
# director_binary_string \
# director_classes \
# director_classic \
# director_constructor \
# director_default \
# director_detect \
# director_enum \
# director_exception \
# director_extend \
# director_finalizer \
# director_frob \
# director_ignore \
# director_keywords \
# director_namespace_clash \
# director_nested \
# director_nspace_director_name_collision \
# director_nspace \
# director_overload2 \
# director_overload \
# director_primitives \
# director_profile \
# director_protected \
# director_protected_overloaded \
# director_redefined \
# director_smartptr \
# director_stl \
# director_string \
# director_thread \
# director_unroll \
# director_using \
# director_wombat \
# director_wstring

#C_TEST_CASES +=

#
# This test only works with modern C compilers
#
#C_TEST_CASES += \
# complextest
CPP_TEST_BROKEN += \
arrays_global

include $(srcdir)/../common.mk

Expand Down
29 changes: 13 additions & 16 deletions Examples/test-suite/matlab/arrays_global_runme.m
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
import arrays_global.*
arrays_global.array_i(arrays_global.array_const_i);

arrays_global.cvar.array_i = arrays_global.cvar.array_const_i;
arrays_global.BeginString_FIX44a;
arrays_global.BeginString_FIX44b;
arrays_global.BeginString_FIX44c;
arrays_global.BeginString_FIX44d;
arrays_global.BeginString_FIX44d;
arrays_global.BeginString_FIX44b(strcat('12','\0','45'));
arrays_global.BeginString_FIX44b;
arrays_global.BeginString_FIX44d;
arrays_global.BeginString_FIX44e;
arrays_global.BeginString_FIX44f;

cvar.BeginString_FIX44a;
cvar.BeginString_FIX44b;
cvar.BeginString_FIX44c;
cvar.BeginString_FIX44d;
cvar.BeginString_FIX44d;
cvar.BeginString_FIX44b = strcat('12','\0','45');
cvar.BeginString_FIX44b;
cvar.BeginString_FIX44d;
cvar.BeginString_FIX44e;
cvar.BeginString_FIX44f;

test_a('hello','hi','chello','chi');

test_b('1234567','hi');
arrays_global.test_a('hello','hi','chello','chi');

arrays_global.test_b('1234567','hi');
94 changes: 74 additions & 20 deletions Source/Modules/matlab.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1293,19 +1293,22 @@ int MATLAB::variableWrapper(Node *n) {
#ifdef MATLABPRINTFUNCTIONENTRY
Printf(stderr,"Entering variableWrapper\n");
#endif
String *name = Getattr(n, "name");
String *iname = Getattr(n, "sym:name");
SwigType *t = Getattr(n, "type");

// Skip if inside class
if (class_name) return Language::variableWrapper(n);
if (!addSymbol(iname, n))
return SWIG_ERROR;

if (Language::variableWrapper(n)!=SWIG_OK) return SWIG_ERROR;
// Skip if inside class // FIXME(@jaeandersson) ignores varin, varout
if (class_name) return Language::variableWrapper(n);

// Name of variable
String *symname = Getattr(n, "sym:name");
checkValidSymName(n);

// Create MATLAB proxy
String* mfile = NewString("");
Printf(mfile, "%s/%s.m", pkg_name_fullpath, symname);
Printf(mfile, "%s/%s.m", pkg_name_fullpath, iname);
if (f_wrap_m)
SWIG_exit(EXIT_FAILURE);
f_wrap_m = NewFile(mfile, "w", SWIG_output_files());
Expand All @@ -1315,44 +1318,95 @@ int MATLAB::variableWrapper(Node *n) {
}

// Add getter function
String *getname = Swig_name_get(NSPACE_TODO, symname);
String *getname = Swig_name_get(NSPACE_TODO, iname);
String *getwname = Swig_name_wrapper(getname);
int gw_ind_get = toGateway(getname,getwname);

// varout typemap
String *tm;
Wrapper *getf = NewWrapper();
int addfail = 0;
Setattr(n, "wrap:name", getname);
Printf(getf->def, "SWIGINTERN int %s (int resc, mxArray *resv[], int argc, mxArray *argv[]) {", getwname);
if ((tm = Swig_typemap_lookup("varout", n, name, 0))) {
Replaceall(tm, "$source", name);
Replaceall(tm, "$target", "resv[0]");
Replaceall(tm, "$result", "resv[0]");
addfail = emit_action_code(n, getf->code, tm);
Delete(tm);
} else {
Swig_warning(WARN_TYPEMAP_VAROUT_UNDEF, input_file, line_number, "Unable to read variable of type %s\n", SwigType_str(t, 0));
}
Append(getf->code, " return 0;\n");
if (addfail) {
Append(getf->code, "fail:\n");
Append(getf->code, " return 1;\n");
}
Append(getf->code, "}\n");
Wrapper_print(getf, f_wrappers);

// Add getter/setter function
if (!is_assignable(n)) {
// Only getter
Printf(f_wrap_m,"function v = %s()\n",symname);
Printf(f_wrap_m," v = %s(%d);\n",mex_name,gw_ind_get);
Printf(f_wrap_m,"end\n");
if (!class_name) {
Printf(f_wrap_m,"function v = %s()\n",iname);
Printf(f_wrap_m," v = %s(%d);\n",mex_name,gw_ind_get);
Printf(f_wrap_m,"end\n");
}
} else {
// Add setter function
String *setname = Swig_name_set(NSPACE_TODO, symname);
String *setname = Swig_name_set(NSPACE_TODO, iname);
String *setwname = Swig_name_wrapper(setname);
int gw_ind_set = toGateway(setname,setwname);

// varin typemap
Wrapper *setf = NewWrapper();
Setattr(n, "wrap:name", setname);
Printf(setf->def, "SWIGINTERN int %s (int resc, mxArray *resv[], int argc, mxArray *argv[]) {", setwname);
if ((tm = Swig_typemap_lookup("varin", n, name, 0))) {
Replaceall(tm, "$source", "argv[0]");
Replaceall(tm, "$target", name);
Replaceall(tm, "$input", "argv[0]");
if (Getattr(n, "tmap:varin:implicitconv")) {
Replaceall(tm, "$implicitconv", get_implicitconv_flag(n));
}
emit_action_code(n, setf->code, tm);
Delete(tm);
} else {
Swig_warning(WARN_TYPEMAP_VARIN_UNDEF, input_file, line_number, "Unable to set variable of type %s.\n", SwigType_str(t, 0));
}
Printf(setf->code, "return 0;\n");
Append(setf->code, "fail:\n");
Printf(setf->code, "return 1;\n");
Append(setf->code, "}\n");
Wrapper_print(setf, f_wrappers);

// Getter and setter
Printf(f_wrap_m,"function varargout = %s(varargin)\n",symname);
Printf(f_wrap_m," narginchk(0,1)\n");
Printf(f_wrap_m," if nargin==0\n");
Printf(f_wrap_m," nargoutchk(0,1)\n");
Printf(f_wrap_m," varargout{1} = %s(%d);\n",mex_name,gw_ind_get);
Printf(f_wrap_m," else\n");
Printf(f_wrap_m," nargoutchk(0,0)\n");
Printf(f_wrap_m," %s(%d,varargin{1});\n",mex_name,gw_ind_set);
Printf(f_wrap_m," end\n");
Printf(f_wrap_m,"end\n");
if (!class_name) {
Printf(f_wrap_m,"function varargout = %s(varargin)\n",iname);
Printf(f_wrap_m," narginchk(0,1)\n");
Printf(f_wrap_m," if nargin==0\n");
Printf(f_wrap_m," nargoutchk(0,1)\n");
Printf(f_wrap_m," varargout{1} = %s(%d);\n",mex_name,gw_ind_get);
Printf(f_wrap_m," else\n");
Printf(f_wrap_m," nargoutchk(0,0)\n");
Printf(f_wrap_m," %s(%d,varargin{1});\n",mex_name,gw_ind_set);
Printf(f_wrap_m," end\n");
Printf(f_wrap_m,"end\n");
}

// Tidy up
Delete(setname);
Delete(setwname);
DelWrapper(setf);
}

// Tidy up
Delete(getname);
Delete(getwname);
Delete(mfile);
Delete(f_wrap_m);
DelWrapper(getf);
f_wrap_m = 0;

return SWIG_OK;
Expand Down

0 comments on commit 7232c0d

Please sign in to comment.