Few C typemaps fixes (still using experimental approach).

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10658 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Maciej Drwal 2008-07-11 16:12:14 +00:00
commit 542c21b6fe
7 changed files with 84 additions and 41 deletions

View file

@ -4,7 +4,10 @@ Bar foo_ret_val() {
return Bar(); return Bar();
} }
Bar* foo_ret_ptr(Bar* bar) { Bar *foo_ret_ptr(Bar *bar) {
return bar; return bar;
} }
Bar &foo_ret_ref(Bar &bar) {
return bar;
}

View file

@ -11,6 +11,6 @@ public:
}; };
Bar foo_ret_val(); Bar foo_ret_val();
//Bar& foo_ret_ref(Bar* bar); Bar& foo_ret_ref(Bar& bar);
Bar* foo_ret_ptr(Bar* bar); Bar* foo_ret_ptr(Bar* bar);

View file

@ -3,9 +3,13 @@
#include "example_proxy.h" #include "example_proxy.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
Bar b = foo_ret_val(); Bar *b = foo_ret_val();
//Bar * bp = foo_ret_ptr(NULL); printf("x = %d\n", Bar_get(b));
printf("x = %d\n", Bar_get(&b)); Bar *bp = foo_ret_ptr(NULL);
Bar *br = foo_ret_ref(new_Bar());
delete_Bar(b);
delete_Bar(br);
return 0; return 0;
} }

View file

@ -3,15 +3,18 @@
#include "c_arguments/c_arguments_proxy.h" #include "c_arguments/c_arguments_proxy.h"
int main(int argc, char **argv) { int main(int argc, char **argv) {
A* a = new_A(); A *a = new_A();
if (A_foo(a, 0) != 1)
fprintf(stderr, "call to A_foo() failed\n"); A *a1, *a2, *a3;
a1 = foo_1(a);
a2 = foo_2(a);
a3 = foo_3(a);
delete_A(a); delete_A(a);
delete_A(a1);
int ***array = (int ***) 0; delete_A(a2);
delete_A(a3);
foo_1(array);
return 0; return 0;
} }

View file

@ -4,17 +4,20 @@
class A { class A {
public: public:
A() {} A() : i(0) {}
int foo(int i) { int i;
return i+1;
}
}; };
void foo(A a, A &ra, A *pa, char c, char *str, char **pstr, char ***ppstri) { A foo_1(A *a) {
return *a;
} }
void foo_1(int ***a) { A * foo_2(A *a) {
return a;
}
A & foo_3(A *a) {
return *a;
} }
%} %}

View file

@ -25,7 +25,7 @@
%typemap(ctype) SWIGTYPE * [ANY] "$1_basetype*" %typemap(ctype) SWIGTYPE * [ANY] "$1_basetype*"
%typemap(ctype) SWIGCLASSTYPE "struct SwigObj$1_type *" %typemap(ctype) SWIGCLASSTYPE "struct SwigObj$1_type *"
%typemap(ctype) SWIGCLASSTYPE * "struct SwigObj$1_type" %typemap(ctype) SWIGCLASSTYPE * "struct SwigObj$1_type"
%typemap(ctype) SWIGCLASSTYPE & "struct SwigObj$*1_ltype *" %typemap(ctype) SWIGCLASSTYPE & "struct SwigObj$1_basetype *"
%typemap(ctype) SWIGCLASSTYPE * [ANY] "struct SwigObj$1_basetype *" %typemap(ctype) SWIGCLASSTYPE * [ANY] "struct SwigObj$1_basetype *"
%typemap(in) short, int, long, char, float, double, bool "$1 = ($1_basetype) $input;" %typemap(in) short, int, long, char, float, double, bool "$1 = ($1_basetype) $input;"
@ -41,21 +41,31 @@
} }
%typemap(in) SWIGCLASSTYPE * { %typemap(in) SWIGCLASSTYPE * {
$1 = ($1_type) $input->obj; if ($input)
$1 = ($1_type) $input->obj;
else
$1 = ($1_type) 0;
} }
%typemap(in) SWIGCLASSTYPE * [ANY] { %typemap(in) SWIGCLASSTYPE * [ANY] {
$1 = ($1_basetype*) malloc($1_dim0 * sizeof($1_basetype)); if ($input) {
int i; $1 = ($1_basetype*) malloc($1_dim0 * sizeof($1_basetype));
for (i = 0; i < $1_dim0; ++i) int i;
if ($input[i]) for (i = 0; i < $1_dim0; ++i)
$1[i] = ($1_basetype) $input[i]->obj; if ($input[i])
else $1[i] = ($1_basetype) $input[i]->obj;
$1[i] = ($1_basetype) 0; else
$1[i] = ($1_basetype) 0;
}
else
$1 = ($1_basetype*) 0;
} }
%typemap(in) SWIGCLASSTYPE & { %typemap(in) SWIGCLASSTYPE & {
$1 = ($*1_ltype *) $input->obj; if ($input)
$1 = ($1_basetype *) $input->obj;
else
$1 = ($1_basetype *) 0;
} }
// typemaps for return values // typemaps for return values
@ -64,22 +74,35 @@
%typemap(couttype) void*, short*, int*, long*, char*, float*, double* "$1_type" %typemap(couttype) void*, short*, int*, long*, char*, float*, double* "$1_type"
%typemap(couttype) const short, const int, const long, const char, const float, const double "$1_basetype" %typemap(couttype) const short, const int, const long, const char, const float, const double "$1_basetype"
%typemap(couttype) bool "_Bool" %typemap(couttype) bool "_Bool"
%typemap(couttype) SWIGTYPE "struct SwigObj$1_type" %typemap(couttype) SWIGCLASSTYPE "struct SwigObj$1_type *"
%typemap(couttype) SWIGTYPE * "struct SwigObj$*1_type *" %typemap(couttype) SWIGCLASSTYPE * "struct SwigObj$1_type"
%typemap(couttype) SWIGCLASSTYPE & "struct SwigObj$1_basetype *"
%typemap(out) short, int, long, char, float, double, bool "$result = $1;" %typemap(out) short, int, long, char, float, double, bool "$result = $1;"
%typemap(out) void*, short*, int*, long*, char*, float*, double*, bool* "$result = $1;" %typemap(out) void*, short*, int*, long*, char*, float*, double*, bool* "$result = $1;"
%typemap(out) void "" %typemap(out) void ""
%typemap(out) SWIGTYPE { %typemap(out) SWIGCLASSTYPE {
$result.obj = (void*) &$1; $result = (struct SwigObj$1_type *) malloc(sizeof(struct SwigObj$1_type));
$result->obj = (void*) &$1;
#if SWIG_C_RUNTIME
$result->typenames[0] = 0; // FIXME
#endif
} }
%typemap(out) SWIGTYPE * { %typemap(out) SWIGCLASSTYPE * {
$result = (struct SwigObj$1_type) malloc(sizeof(struct SwigObj$*1_type));
$result->obj = (void*) $1; $result->obj = (void*) $1;
#if SWIG_C_RUNTIME
$result->typenames[0] = 0; // FIXME
#endif
} }
%typemap(out) SWIGTYPE & { %typemap(out) SWIGCLASSTYPE & {
// return by reference $result = (struct SwigObj$1_basetype *) malloc(sizeof(struct SwigObj$1_basetype));
$result->obj = (void*) $1;
#if SWIG_C_RUNTIME
$result->typenames[0] = 0; // FIXME
#endif
} }

View file

@ -39,7 +39,7 @@ public:
C() : C() :
empty_string(NewString("")), empty_string(NewString("")),
proxy_flag(true), proxy_flag(true),
runtime_flag(true) { runtime_flag(false) {
} }
/* ------------------------------------------------------------ /* ------------------------------------------------------------
@ -52,6 +52,8 @@ public:
// add a symbol to the parser for conditional compilation // add a symbol to the parser for conditional compilation
Preprocessor_define("SWIGC 1", 0); Preprocessor_define("SWIGC 1", 0);
if (runtime_flag)
Preprocessor_define("SWIG_C_RUNTIME 1", 0);
// add typemap definitions // add typemap definitions
SWIG_typemap_lang("c"); SWIG_typemap_lang("c");
@ -103,10 +105,10 @@ public:
Swig_banner(f_runtime); Swig_banner(f_runtime);
Printf(f_header, "#include <malloc.h>\n"); Printf(f_header, "#include <stdlib.h>\n");
if (runtime_flag) { if (runtime_flag) {
Printf(f_header, "#include <stdio.h>\n"); Printf(f_header, "#include <stdio.h>\n");
Printf(f_header, "#include <stdlib.h>\n"); //Printf(f_header, "#include <stdlib.h>\n");
Printf(f_header, "#include <string.h>\n"); Printf(f_header, "#include <string.h>\n");
Printf(f_header, "#include <setjmp.h>\n\n"); Printf(f_header, "#include <setjmp.h>\n\n");
Printf(f_header, "static jmp_buf Swig_rt_env;\n"); Printf(f_header, "static jmp_buf Swig_rt_env;\n");
@ -384,7 +386,8 @@ public:
if (!is_void_return && (Cmp(Getattr(n, "c:objstruct"), "1") != 0)) { if (!is_void_return && (Cmp(Getattr(n, "c:objstruct"), "1") != 0)) {
if (SwigType_isconst(type)) if (SwigType_isconst(type))
SwigType_del_qualifier(type); SwigType_del_qualifier(type);
Wrapper_add_localv(wrapper, "cppresult", SwigType_str(type, 0), "cppresult", NIL); SwigType *return_var_type = SwigType_isreference(type) ? SwigType_add_pointer(SwigType_base(type)) : type;
Wrapper_add_localv(wrapper, "cppresult", SwigType_str(return_var_type, 0), "cppresult", NIL);
} }
// create wrapper function prototype // create wrapper function prototype
@ -464,6 +467,10 @@ public:
String *action = emit_action(n); String *action = emit_action(n);
Replaceall(action, "$cppresult", "cppresult"); Replaceall(action, "$cppresult", "cppresult");
// return-by-reference hack
if (SwigType_isreference(type))
Replaceall(action, "&)", "*)&");
// emit output typemap if needed // emit output typemap if needed
if (!is_void_return && (Cmp(Getattr(n, "c:objstruct"), "1") != 0)) { if (!is_void_return && (Cmp(Getattr(n, "c:objstruct"), "1") != 0)) {
if ((tm = Swig_typemap_lookup_out("out", n, "cppresult", wrapper, action))) { if ((tm = Swig_typemap_lookup_out("out", n, "cppresult", wrapper, action))) {
@ -612,7 +619,7 @@ public:
if (runtime_flag) if (runtime_flag)
Printf(sobj, " const char* typenames[%d];\n}", Len(baselist) + 2); Printf(sobj, " const char* typenames[%d];\n}", Len(baselist) + 2);
else else
Printf(sobj, "};\n\n"); Printf(sobj, "}");
Printv(f_header, sobj, ";\n\n", NIL); Printv(f_header, sobj, ";\n\n", NIL);
Printv(f_header, "const char* Swig_typename_", name, " = \"", name, "\";\n\n", NIL); Printv(f_header, "const char* Swig_typename_", name, " = \"", name, "\";\n\n", NIL);