Better handling of return values. Some bugfixes.
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2008-maciekd@10618 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
ea05035844
commit
f74439e215
16 changed files with 208 additions and 58 deletions
|
|
@ -1,4 +1,4 @@
|
|||
/* File : example.cxx */
|
||||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
|
@ -12,8 +12,7 @@ void Shape::move(double dx, double dy) {
|
|||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) {
|
||||
double xxx = M_PI*radius*radius;
|
||||
return xxx;
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
|
|
@ -27,4 +26,3 @@ double Square::area(void) {
|
|||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,5 @@
|
|||
/* File : example.h */
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
|
|
@ -35,3 +33,7 @@ public:
|
|||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -3,17 +3,35 @@
|
|||
#include "example_proxy.h"
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
Circle* pc = new_Circle(10.0);
|
||||
Square* ps = new_Square(10.0);
|
||||
printf("Creating some objects:\n");
|
||||
Circle* c = new_Circle(10);
|
||||
printf(" Created circle\n");
|
||||
Square* s = new_Square(10);
|
||||
printf(" Created square\n");
|
||||
|
||||
printf("Circle perim.=%f, area=%f\n", Circle_perimeter(pc), Circle_area(pc));
|
||||
printf("Square perim.=%f, area=%f\n", Square_perimeter(ps), Square_area(ps));
|
||||
printf("\nA total of %d shapes were created\n", 0);
|
||||
|
||||
printf("Circle pos.=(%f, %f)\n", Shape_get_x(pc), Shape_get_y(pc));
|
||||
Shape_move(pc, 5.0, -5.0);
|
||||
printf("After move pos.=(%f, %f)\n", Shape_get_x(pc), Shape_get_y(pc));
|
||||
|
||||
delete_Square(ps);
|
||||
delete_Circle(pc);
|
||||
printf("\nHere are some properties of the shapes:\n");
|
||||
Shape shapes[] = {c, s};
|
||||
int i;
|
||||
/*
|
||||
* TODO: support for virtual functions
|
||||
for (i = 0; i < 2; i++) {
|
||||
printf(" area = %f\n", Shape_area(shapes[i]));
|
||||
printf(" perimeter = %f\n", Shape_perimeter(shapes[i]));
|
||||
}
|
||||
*/
|
||||
|
||||
// call methods directly by now
|
||||
printf("Circle perim.=%f, area=%f\n", Circle_perimeter(c), Circle_area(c));
|
||||
printf("Square perim.=%f, area=%f\n", Square_perimeter(s), Square_area(s));
|
||||
|
||||
printf("\nGuess I'll clean up now\n");
|
||||
|
||||
delete_Square(s);
|
||||
delete_Circle(c);
|
||||
|
||||
printf("%d shapes remain\n", 0);
|
||||
printf("Goodbye\n");
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig -debug-module 4
|
||||
SWIG = $(TOP)/../preinst-swig -debug-module 4 > tree.txt
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
|
|
|||
|
|
@ -2,18 +2,18 @@
|
|||
|
||||
#include "example.h"
|
||||
|
||||
void foo_by_val(Bar foo) {
|
||||
foo.set(123);
|
||||
printf("inside foo_by_val: %d\n", foo.get());
|
||||
void foo_by_val(Bar bar) {
|
||||
bar.set(123);
|
||||
printf("inside foo_by_val: %d\n", bar.get());
|
||||
}
|
||||
|
||||
void foo_by_ref(Bar& foo) {
|
||||
foo.set(123);
|
||||
printf("inside foo_by_ref: %d\n", foo.get());
|
||||
void foo_by_ref(Bar& bar) {
|
||||
bar.set(123);
|
||||
printf("inside foo_by_ref: %d\n", bar.get());
|
||||
}
|
||||
|
||||
void foo_by_ptr(Bar* foo) {
|
||||
foo->set(123);
|
||||
printf("inside foo_by_ptr: %d\n", foo->get());
|
||||
void foo_by_ptr(Bar* bar) {
|
||||
bar->set(123);
|
||||
printf("inside foo_by_ptr: %d\n", bar->get());
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -7,7 +7,7 @@ void test(Bar * bar) {
|
|||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
Bar * bar = new_Bar();
|
||||
Bar* bar = new_Bar();
|
||||
test(bar);
|
||||
foo_by_val(bar);
|
||||
test(bar);
|
||||
|
|
|
|||
14
Examples/c/return/Makefile
Normal file
14
Examples/c/return/Makefile
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig -debug-module 4 > tree.txt
|
||||
CXXSRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
MAIN = test.c
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' MAIN='$(MAIN)' c_cpp
|
||||
|
||||
clean:
|
||||
rm -f *.o *.out *.so *.a *.dll *.exe *_wrap* *_proxy* *~
|
||||
|
||||
10
Examples/c/return/example.cxx
Normal file
10
Examples/c/return/example.cxx
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include "example.h"
|
||||
|
||||
Bar foo_ret_val() {
|
||||
return Bar();
|
||||
}
|
||||
|
||||
Bar* foo_ret_ptr(Bar* bar) {
|
||||
return bar;
|
||||
}
|
||||
|
||||
16
Examples/c/return/example.h
Normal file
16
Examples/c/return/example.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#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);
|
||||
|
||||
8
Examples/c/return/example.i
Normal file
8
Examples/c/return/example.i
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
%include "example.h"
|
||||
|
||||
10
Examples/c/return/test.c
Normal file
10
Examples/c/return/test.c
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#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));
|
||||
}
|
||||
|
||||
|
|
@ -7,7 +7,7 @@ double *Foo_ptr = &Foo;
|
|||
char *my_str = "hello, world!";
|
||||
char *array_of_strs[] = { "one", "two" };
|
||||
|
||||
char *get_str(int i) {
|
||||
char *get_str(int i, void* ptr, float ff) {
|
||||
return array_of_strs[i];
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ extern double Foo;
|
|||
extern double *Foo_ptr;
|
||||
extern char *my_str;
|
||||
extern char **array_of_strs;
|
||||
extern char *get_str(int i);
|
||||
extern char *get_str(int i, void* ptr, float ff);
|
||||
extern int gcd(int x, int y);
|
||||
%}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,8 +9,7 @@ int main(int argc, char **argv) {
|
|||
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), get_str(1));
|
||||
printf("const Val = %d\n", Val);
|
||||
printf("array_of_strs contains %s and %s\n", get_str(0, 0, 0), get_str(1, 0, 0));
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue