Support for parameter passing by value, pointer and reference.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10604 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
f84342a301
commit
f03e17b405
4 changed files with 45 additions and 13 deletions
|
|
@ -10,7 +10,7 @@ public:
|
|||
int get() { return x; }
|
||||
};
|
||||
|
||||
/*void foo_by_val(Bar bar);
|
||||
void foo_by_ref(Bar& bar);*/
|
||||
void foo_by_val(Bar bar);
|
||||
void foo_by_ref(Bar& bar);
|
||||
void foo_by_ptr(Bar* bar);
|
||||
|
||||
|
|
|
|||
|
|
@ -2,10 +2,19 @@
|
|||
|
||||
#include "example_proxy.h"
|
||||
|
||||
void test(Bar * bar) {
|
||||
printf("value of x is %d\n", Bar_get(bar));
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Bar * bar = new_Bar();
|
||||
test(bar);
|
||||
foo_by_val(bar);
|
||||
test(bar);
|
||||
foo_by_ptr(bar);
|
||||
|
||||
test(bar);
|
||||
foo_by_ref(bar);
|
||||
test(bar);
|
||||
delete_Bar(bar);
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
12
Lib/c/c.swg
12
Lib/c/c.swg
|
|
@ -12,11 +12,21 @@
|
|||
%typemap(ctype) float "float"
|
||||
%typemap(ctype) double "double"
|
||||
%typemap(ctype) bool "_Bool"
|
||||
%typemap(ctype) SWIGTYPE "struct $*1_typeObj *"
|
||||
%typemap(ctype) SWIGTYPE "struct $1_typeObj *"
|
||||
%typemap(ctype) SWIGTYPE * "struct $*1_typeObj *"
|
||||
%typemap(ctype) SWIGTYPE & "struct $*1_ltypeObj *"
|
||||
|
||||
%typemap(in) short, int, long, char, float, double, bool "$1 = $input;"
|
||||
|
||||
%typemap(in) SWIGTYPE {
|
||||
$1 = * ($1_type *) ($input->obj);
|
||||
}
|
||||
|
||||
%typemap(in) SWIGTYPE * {
|
||||
$1 = ($1_type) $input->obj;
|
||||
}
|
||||
|
||||
%typemap(in) SWIGTYPE & {
|
||||
$1 = ($*1_ltype *) $input->obj;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -258,6 +258,7 @@ public:
|
|||
ParmList *parms = Getattr(n, "parms");
|
||||
Parm *p;
|
||||
String* tm;
|
||||
String* proto = NewString("");
|
||||
|
||||
// create new function wrapper object
|
||||
Wrapper *wrapper = NewWrapper();
|
||||
|
|
@ -291,24 +292,33 @@ public:
|
|||
SwigType* type = Getattr(p, "type");
|
||||
String* lname = Getattr(p, "lname");
|
||||
String* c_parm_type = NewString("");
|
||||
String* shadow_parm_type = NewString("");
|
||||
String* arg_name = NewString("");
|
||||
|
||||
Printf(arg_name, "c%s", lname);
|
||||
|
||||
if ((tm = Getattr(p, "tmap:ctype"))) {
|
||||
if (Cmp(Getattr(p, "c:immutable"), "1") == 0) {
|
||||
Printv(c_parm_type, SwigType_str(type, 0), NIL);
|
||||
}
|
||||
else {
|
||||
Printv(c_parm_type, tm, NIL);
|
||||
}
|
||||
if (Cmp(Getattr(p, "c:immutable"), "1") == 0) {
|
||||
Printv(c_parm_type, SwigType_str(type, 0), NIL);
|
||||
}
|
||||
else if ((tm = Getattr(p, "tmap:ctype"))) {
|
||||
Printv(c_parm_type, tm, NIL);
|
||||
}
|
||||
else {
|
||||
Swig_warning(WARN_C_TYPEMAP_CTYPE_UNDEF, input_file, line_number, "No ctype typemap defined for %s\n", SwigType_str(type, 0));
|
||||
}
|
||||
|
||||
// use shadow-type for parameter if supplied
|
||||
String* stype = Getattr(p, "c:stype");
|
||||
if (stype) {
|
||||
Printv(shadow_parm_type, SwigType_str(stype, 0), NIL);
|
||||
}
|
||||
else {
|
||||
Printv(shadow_parm_type, c_parm_type, NIL);
|
||||
}
|
||||
|
||||
Printv(arg_names, gencomma ? ", " : "", Getattr(p, "name"), NIL);
|
||||
Printv(wrapper->def, gencomma ? ", " : "", c_parm_type, " ", arg_name, NIL);
|
||||
Printv(proto, gencomma ? ", " : "", shadow_parm_type, " ", Getattr(p, "name"), NIL);
|
||||
gencomma = 1;
|
||||
|
||||
if ((tm = Getattr(p, "tmap:in"))) {
|
||||
|
|
@ -328,6 +338,7 @@ public:
|
|||
p = nextSibling(p);
|
||||
}
|
||||
Delete(arg_name);
|
||||
Delete(shadow_parm_type);
|
||||
Delete(c_parm_type);
|
||||
}
|
||||
|
||||
|
|
@ -347,8 +358,8 @@ public:
|
|||
|
||||
// take care of shadow function
|
||||
if (shadow_flag) {
|
||||
// use shadow-type for parameter if supplied
|
||||
String* proto;
|
||||
|
||||
/*
|
||||
if (parms) {
|
||||
String* stype = Getattr(parms, "c:stype");
|
||||
if (stype) {
|
||||
|
|
@ -364,6 +375,7 @@ public:
|
|||
else {
|
||||
proto = empty_string;
|
||||
}
|
||||
*/
|
||||
|
||||
// use shadow-type for return type if supplied
|
||||
SwigType* shadow_type = Getattr(n, "c:stype");
|
||||
|
|
@ -407,6 +419,7 @@ public:
|
|||
}
|
||||
|
||||
// cleanup
|
||||
Delete(proto);
|
||||
Delete(arg_names);
|
||||
Delete(wname);
|
||||
DelWrapper(wrapper);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue