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:
parent
b7340dd4d6
commit
542c21b6fe
7 changed files with 84 additions and 41 deletions
|
|
@ -4,7 +4,10 @@ Bar foo_ret_val() {
|
|||
return Bar();
|
||||
}
|
||||
|
||||
Bar* foo_ret_ptr(Bar* bar) {
|
||||
Bar *foo_ret_ptr(Bar *bar) {
|
||||
return bar;
|
||||
}
|
||||
|
||||
Bar &foo_ret_ref(Bar &bar) {
|
||||
return bar;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,6 @@ public:
|
|||
};
|
||||
|
||||
Bar foo_ret_val();
|
||||
//Bar& foo_ret_ref(Bar* bar);
|
||||
Bar& foo_ret_ref(Bar& bar);
|
||||
Bar* foo_ret_ptr(Bar* bar);
|
||||
|
||||
|
|
|
|||
|
|
@ -3,9 +3,13 @@
|
|||
#include "example_proxy.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Bar b = foo_ret_val();
|
||||
//Bar * bp = foo_ret_ptr(NULL);
|
||||
printf("x = %d\n", Bar_get(&b));
|
||||
Bar *b = foo_ret_val();
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -3,15 +3,18 @@
|
|||
#include "c_arguments/c_arguments_proxy.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
A* a = new_A();
|
||||
if (A_foo(a, 0) != 1)
|
||||
fprintf(stderr, "call to A_foo() failed\n");
|
||||
A *a = new_A();
|
||||
|
||||
A *a1, *a2, *a3;
|
||||
|
||||
a1 = foo_1(a);
|
||||
a2 = foo_2(a);
|
||||
a3 = foo_3(a);
|
||||
|
||||
delete_A(a);
|
||||
|
||||
int ***array = (int ***) 0;
|
||||
|
||||
foo_1(array);
|
||||
|
||||
delete_A(a1);
|
||||
delete_A(a2);
|
||||
delete_A(a3);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,17 +4,20 @@
|
|||
|
||||
class A {
|
||||
public:
|
||||
A() {}
|
||||
int foo(int i) {
|
||||
return i+1;
|
||||
}
|
||||
A() : i(0) {}
|
||||
int i;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
%}
|
||||
|
|
|
|||
57
Lib/c/c.swg
57
Lib/c/c.swg
|
|
@ -25,7 +25,7 @@
|
|||
%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_ltype *"
|
||||
%typemap(ctype) SWIGCLASSTYPE & "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;"
|
||||
|
|
@ -41,21 +41,31 @@
|
|||
}
|
||||
|
||||
%typemap(in) SWIGCLASSTYPE * {
|
||||
$1 = ($1_type) $input->obj;
|
||||
if ($input)
|
||||
$1 = ($1_type) $input->obj;
|
||||
else
|
||||
$1 = ($1_type) 0;
|
||||
}
|
||||
|
||||
%typemap(in) SWIGCLASSTYPE * [ANY] {
|
||||
$1 = ($1_basetype*) malloc($1_dim0 * sizeof($1_basetype));
|
||||
int i;
|
||||
for (i = 0; i < $1_dim0; ++i)
|
||||
if ($input[i])
|
||||
$1[i] = ($1_basetype) $input[i]->obj;
|
||||
else
|
||||
$1[i] = ($1_basetype) 0;
|
||||
if ($input) {
|
||||
$1 = ($1_basetype*) malloc($1_dim0 * sizeof($1_basetype));
|
||||
int i;
|
||||
for (i = 0; i < $1_dim0; ++i)
|
||||
if ($input[i])
|
||||
$1[i] = ($1_basetype) $input[i]->obj;
|
||||
else
|
||||
$1[i] = ($1_basetype) 0;
|
||||
}
|
||||
else
|
||||
$1 = ($1_basetype*) 0;
|
||||
}
|
||||
|
||||
%typemap(in) SWIGCLASSTYPE & {
|
||||
$1 = ($*1_ltype *) $input->obj;
|
||||
if ($input)
|
||||
$1 = ($1_basetype *) $input->obj;
|
||||
else
|
||||
$1 = ($1_basetype *) 0;
|
||||
}
|
||||
|
||||
// typemaps for return values
|
||||
|
|
@ -64,22 +74,35 @@
|
|||
%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) bool "_Bool"
|
||||
%typemap(couttype) SWIGTYPE "struct SwigObj$1_type"
|
||||
%typemap(couttype) SWIGTYPE * "struct SwigObj$*1_type *"
|
||||
%typemap(couttype) SWIGCLASSTYPE "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) void*, short*, int*, long*, char*, float*, double*, bool* "$result = $1;"
|
||||
%typemap(out) void ""
|
||||
|
||||
%typemap(out) SWIGTYPE {
|
||||
$result.obj = (void*) &$1;
|
||||
%typemap(out) SWIGCLASSTYPE {
|
||||
$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;
|
||||
#if SWIG_C_RUNTIME
|
||||
$result->typenames[0] = 0; // FIXME
|
||||
#endif
|
||||
}
|
||||
|
||||
%typemap(out) SWIGTYPE & {
|
||||
// return by reference
|
||||
%typemap(out) SWIGCLASSTYPE & {
|
||||
$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
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ public:
|
|||
C() :
|
||||
empty_string(NewString("")),
|
||||
proxy_flag(true),
|
||||
runtime_flag(true) {
|
||||
runtime_flag(false) {
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------
|
||||
|
|
@ -52,6 +52,8 @@ public:
|
|||
|
||||
// add a symbol to the parser for conditional compilation
|
||||
Preprocessor_define("SWIGC 1", 0);
|
||||
if (runtime_flag)
|
||||
Preprocessor_define("SWIG_C_RUNTIME 1", 0);
|
||||
|
||||
// add typemap definitions
|
||||
SWIG_typemap_lang("c");
|
||||
|
|
@ -103,10 +105,10 @@ public:
|
|||
|
||||
Swig_banner(f_runtime);
|
||||
|
||||
Printf(f_header, "#include <malloc.h>\n");
|
||||
Printf(f_header, "#include <stdlib.h>\n");
|
||||
if (runtime_flag) {
|
||||
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 <setjmp.h>\n\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 (SwigType_isconst(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
|
||||
|
|
@ -464,6 +467,10 @@ public:
|
|||
String *action = emit_action(n);
|
||||
Replaceall(action, "$cppresult", "cppresult");
|
||||
|
||||
// return-by-reference hack
|
||||
if (SwigType_isreference(type))
|
||||
Replaceall(action, "&)", "*)&");
|
||||
|
||||
// emit output typemap if needed
|
||||
if (!is_void_return && (Cmp(Getattr(n, "c:objstruct"), "1") != 0)) {
|
||||
if ((tm = Swig_typemap_lookup_out("out", n, "cppresult", wrapper, action))) {
|
||||
|
|
@ -612,7 +619,7 @@ public:
|
|||
if (runtime_flag)
|
||||
Printf(sobj, " const char* typenames[%d];\n}", Len(baselist) + 2);
|
||||
else
|
||||
Printf(sobj, "};\n\n");
|
||||
Printf(sobj, "}");
|
||||
|
||||
Printv(f_header, sobj, ";\n\n", NIL);
|
||||
Printv(f_header, "const char* Swig_typename_", name, " = \"", name, "\";\n\n", NIL);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue