Exceptions support for C. exception_order test shows how to use type information to achieve correct catching order. Examples cleanup.

git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10671 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Maciej Drwal 2008-07-17 00:52:11 +00:00
commit 88facfd390
27 changed files with 223 additions and 429 deletions

View file

@ -1,21 +0,0 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig -debug-module 4 > tree.txt
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
RUNME = runme.c
PROXY = example_proxy.c
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' c_cpp
run:
$(MAKE) -f $(TOP)/Makefile RUNME='$(RUNME)' PROXY='$(PROXY)' \
TARGET='$(TARGET)' c_compile
env LD_LIBRARY_PATH=. ./runme
clean:
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~
check: all

View file

@ -1,2 +0,0 @@
#include "example.h"

View file

@ -1,52 +0,0 @@
#include <stdio.h>
class A {
public:
int i;
A(int i) : i(i) {}
void foo() {
}
};
class B : public A {
public:
B(int i) : A(i) {}
};
class MyClass {
public:
void foo(A a, A* aa, A* aaa[5]) {
printf("a.i = %d\n", a.i);
printf("aa->i = %d\n", aa->i);
int i = 0;
while (aaa[i]) {
printf("aaa[%d]->i = %d\n", i, aaa[i]->i);
i++;
}
}
void bar(int x, int* px, int** xs) {
printf("x = %d\n", x);
printf("*px = %d\n", *px);
int i = 0;
while (xs[i]) {
printf("*xs[%d] = %d\n", i, *xs[i]);
i++;
}
}
void foo_1(A& a, A& b) {
printf("a.i = %d\n", a.i);
printf("b.i = %d\n", b.i);
a.i++;
b.i++;
}
void foo_2(int& x, double& d) {
printf("x = %d d = %f\n", x, d);
x++;
d++;
}
};

View file

@ -1,8 +0,0 @@
%module example
%{
#include "example.h"
%}
%include "example.h"

View file

@ -1,36 +0,0 @@
#include <stdio.h>
#include "example_proxy.h"
int main(int argc, char** argv) {
MyClass* mc = new_MyClass();
A* a = new_A(123), *b = new_A(234), *as[5];
int i, j = 321, *js[5];
double d = 55.0;
for (i = 0; i < 4; ++i) {
as[i] = new_A(20 + i);
js[i] = (int*) malloc(sizeof(int));
*js[i] = 10 + i;
}
as[i] = 0;
js[i] = 0;
MyClass_bar(mc, j, &j, js);
MyClass_foo(mc, a, b, as);
MyClass_foo_1(mc, a, b);
MyClass_foo_1(mc, a, b);
MyClass_foo_2(mc, &j, &d);
MyClass_foo_2(mc, &j, &d);
delete_A(a);
delete_A(b);
delete_MyClass(mc);
for (i = 0; i < 4; ++i)
delete_A(as[i]);
return 0;
}

View file

@ -1,4 +1,3 @@
# see top-level Makefile.in
class
reference
simple

View file

@ -38,6 +38,7 @@ int main(int argc, char **argv) {
printf("%d shapes remain\n", Shape_nshapes_get());
printf("Goodbye\n");
return 0;
SWIG_exit(0);
}

View file

@ -1,21 +0,0 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig -debug-module 4 > tree.txt
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
RUNME = runme.c
PROXY = example_proxy.c
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' c_cpp
run:
$(MAKE) -f $(TOP)/Makefile RUNME='$(RUNME)' PROXY='$(PROXY)' \
TARGET='$(TARGET)' c_compile
env LD_LIBRARY_PATH=. ./runme
clean:
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~
check: all

View file

@ -1,19 +0,0 @@
#include <stdio.h>
#include "example.h"
void foo_by_val(Bar bar) {
bar.set(123);
printf("inside foo_by_val: %d\n", bar.get());
}
void foo_by_ref(Bar& bar) {
bar.set(123);
printf("inside foo_by_ref: %d\n", bar.get());
}
void foo_by_ptr(Bar* bar) {
bar->set(123);
printf("inside foo_by_ptr: %d\n", bar->get());
}

View file

@ -1,16 +0,0 @@
#include <stdio.h>
class Bar {
private:
int x;
public:
Bar() : x(0) {}
~Bar() {}
void set(int x) { this->x = x; }
int get() { return x; }
};
void foo_by_val(Bar bar);
void foo_by_ref(Bar& bar);
void foo_by_ptr(Bar* bar);

View file

@ -1,8 +0,0 @@
%module example
%{
#include "example.h"
%}
%include "example.h"

View file

@ -1,21 +0,0 @@
#include <stdio.h>
#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;
}

View file

@ -1,21 +0,0 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig -debug-module 4 > tree.txt
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
RUNME = runme.c
PROXY = example_proxy.c
all::
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' c_cpp
run:
$(MAKE) -f $(TOP)/Makefile RUNME='$(RUNME)' PROXY='$(PROXY)' \
TARGET='$(TARGET)' c_compile
env LD_LIBRARY_PATH=. ./runme
clean:
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~
check: all

View file

@ -1,13 +0,0 @@
#include "example.h"
Bar foo_ret_val() {
return Bar();
}
Bar *foo_ret_ptr(Bar *bar) {
return bar;
}
Bar &foo_ret_ref(Bar &bar) {
return bar;
}

View file

@ -1,16 +0,0 @@
#include <stdio.h>
class Bar {
private:
int x;
public:
Bar() : x(0) {}
~Bar() {}
void set(int x) { this->x = x; }
int get() { return x; }
};
Bar foo_ret_val();
Bar& foo_ret_ref(Bar& bar);
Bar* foo_ret_ptr(Bar* bar);

View file

@ -1,8 +0,0 @@
%module example
%{
#include "example.h"
%}
%include "example.h"

View file

@ -1,18 +0,0 @@
#include <stdio.h>
#include "example_proxy.h"
int main(int argc, char** argv) {
Bar *b = foo_ret_val();
printf("x = %d\n", Bar_get(b));
Bar *c = new_Bar();
Bar *bp = foo_ret_ptr(c);
//Bar *br = foo_ret_ref(c);
delete_Bar(b);
delete_Bar(bp);
delete_Bar(c);
return 0;
}

View file

@ -1,15 +1,7 @@
/* File : example.c */
/* A global variable */
double Foo = 3.0;
double *Foo_ptr = &Foo;
char *my_str = "hello, world!";
char *array_of_strs[] = { "one", "two" };
char *get_str(int i, void* ptr, float ff) {
return array_of_strs[i];
}
double Foo = 3.0;
/* Compute the greatest common divisor of positive integers */
int gcd(int x, int y) {
@ -22,3 +14,5 @@ int gcd(int x, int y) {
}
return g;
}

View file

@ -2,11 +2,6 @@
%module example
%inline %{
extern int gcd(int x, int y);
extern double Foo;
extern double *Foo_ptr;
extern char *my_str;
extern char **array_of_strs;
extern char *get_str(int i, void* ptr, float ff);
extern int gcd(int x, int y);
%}

View file

@ -6,10 +6,6 @@ int main(int argc, char **argv) {
int a = 35;
int b = 15;
printf("Foo is %f\n", Foo);
printf("Foo by ptr is \%f\n", *Foo_ptr);
printf("my_str is: %s\n", my_str);
printf("GCD(%d, %d)=%d\n", a, b, gcd(a, b));
printf("array_of_strs contains %s and %s\n", get_str(0, 0, 0), get_str(1, 0, 0));
return 0;
SWIG_exit(0);
}

View file

@ -1,20 +0,0 @@
#include <stdio.h>
#include "c_arguments/c_arguments_proxy.h"
int main(int argc, char **argv) {
A *a = new_A();
A *a1, *a2, *a3;
a1 = foo_1(a);
a2 = foo_2(a);
a3 = foo_3(a);
delete_A(a);
delete_A(a1);
delete_A(a2);
delete_A(a3);
return 0;
}

View file

@ -0,0 +1,65 @@
#include <stdio.h>
#include "exception_order/exception_order_proxy.h"
int main() {
A* a = new_A();
SWIG_try {
A_foo(a);
}
SWIG_catch(E1) {
}
SWIG_catch(SWIG_AnyException) {
fprintf(stderr, "foo: bad exception order\n");
}
SWIG_endtry;
SWIG_try {
A_bar(a);
}
SWIG_catch(E2) {
}
SWIG_catch(SWIG_AnyException) {
fprintf(stderr, "bar: bad exception order\n");
}
SWIG_endtry;
SWIG_try {
A_foobar(a);
}
SWIG_catch(SWIG_AnyException) {
if (strcmp(SWIG_exception.msg, "postcatch unknown") != 0) {
fprintf(stderr, "bad exception order\n");
SWIG_throw_msg(SWIG_exception.klass, SWIG_exception.msg);
}
}
SWIG_endtry;
SWIG_try {
A_barfoo(a, 1);
}
SWIG_catch(E1) {
}
SWIG_catch(SWIG_AnyException) {
fprintf(stderr, "barfoo(1): bad exception order\n");
}
SWIG_endtry;
SWIG_try {
A_barfoo(a, 2);
}
SWIG_catch(E2) {
}
SWIG_catch(SWIG_AnyException) {
fprintf(stderr, "barfoo(2): bad exception order\n");
}
SWIG_endtry;
SWIG_exit(0);
}

View file

@ -1,23 +0,0 @@
%module c_arguments
%inline %{
class A {
public:
A() : i(0) {}
int i;
};
A foo_1(A *a) {
return *a;
}
A * foo_2(A *a) {
return a;
}
A & foo_3(A *a) {
return *a;
}
%}

View file

@ -33,8 +33,21 @@
}
#endif
%catches(E1,E2*,ET<int>,ET<double>,...) A::barfoo(int i);
/*
* Additional typemaps needed to handle exception order in C.
*/
#if defined(SWIGC)
%typemap(throws) SWIGTYPE {
SwigObj$1_basetype *_c_ex = _wrap_new_$1_basetype();
SWIG_CThrowException(_c_ex, "C++ $1_type exception thrown");
}
%typemap(throws) ET<int>, ET<double> {
SWIG_CThrowException(NULL, "C++ $1_type exception thrown");
}
#endif
%catches(E1,E2*,ET<int>,ET<double>,...) A::barfoo(int i);
%allowexception efoovar;
%allowexception A::efoovar;