add 'class' example
git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/branches/gsoc2009-sploving@11581 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
0623fdff8b
commit
f844123608
7 changed files with 159 additions and 1 deletions
|
|
@ -1145,7 +1145,6 @@ SCILAB_INCLUDE= $(DEFS) @SCILABINCLUDE@
|
|||
SCILAB_LIB = @SCILABLIB@
|
||||
SCILAB = @SCILAB@
|
||||
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Build a C dynamically loadable module
|
||||
# ----------------------------------------------------------------
|
||||
|
|
@ -1156,6 +1155,16 @@ scilab: $(SRCS)
|
|||
env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(SCILAB) -nwni -nb -f builder.sce; \
|
||||
fi
|
||||
|
||||
# ----------------------------------------------------------------
|
||||
# Build a C++ dynamically loadable module
|
||||
# ----------------------------------------------------------------
|
||||
|
||||
scilab_cpp: $(SRCS)
|
||||
$(SWIG) -scilab -c++ $(SWIGOPT) $(INTERFACEPATH)
|
||||
if [ -f builder.sce ]; then \
|
||||
env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(SCILAB) -nwni -nb -f builder.sce; \
|
||||
fi
|
||||
|
||||
# -----------------------------------------------------------------
|
||||
# Running a Scilab example
|
||||
# -----------------------------------------------------------------
|
||||
|
|
|
|||
17
Examples/scilab/class/Makefile
Normal file
17
Examples/scilab/class/Makefile
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
TOP = ../..
|
||||
SWIG = $(TOP)/../preinst-swig
|
||||
SRCS = example.cxx
|
||||
TARGET = example
|
||||
INTERFACE = example.i
|
||||
|
||||
all::
|
||||
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
|
||||
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
|
||||
|
||||
|
||||
clean::
|
||||
$(MAKE) -f $(TOP)/Makefile scilab_clean
|
||||
rm -f *.sce *.so lib*lib.c *_wrap.c *_wrap.cxx
|
||||
|
||||
check: all
|
||||
$(MAKE) -f $(TOP)/Makefile scilab_run
|
||||
28
Examples/scilab/class/example.cxx
Normal file
28
Examples/scilab/class/example.cxx
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* File : example.c */
|
||||
|
||||
#include "example.h"
|
||||
#define M_PI 3.14159265358979323846
|
||||
|
||||
/* Move the shape to a new location */
|
||||
void Shape::move(double dx, double dy) {
|
||||
x += dx;
|
||||
y += dy;
|
||||
}
|
||||
|
||||
int Shape::nshapes = 0;
|
||||
|
||||
double Circle::area(void) {
|
||||
return M_PI*radius*radius;
|
||||
}
|
||||
|
||||
double Circle::perimeter(void) {
|
||||
return 2*M_PI*radius;
|
||||
}
|
||||
|
||||
double Square::area(void) {
|
||||
return width*width;
|
||||
}
|
||||
|
||||
double Square::perimeter(void) {
|
||||
return 4*width;
|
||||
}
|
||||
35
Examples/scilab/class/example.h
Normal file
35
Examples/scilab/class/example.h
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
/* File : example.h */
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
Shape() {
|
||||
nshapes++;
|
||||
}
|
||||
virtual ~Shape() {
|
||||
nshapes--;
|
||||
};
|
||||
double x, y;
|
||||
void move(double dx, double dy);
|
||||
virtual double area(void) = 0;
|
||||
virtual double perimeter(void) = 0;
|
||||
static int nshapes;
|
||||
};
|
||||
|
||||
class Circle : public Shape {
|
||||
private:
|
||||
double radius;
|
||||
public:
|
||||
Circle(double r) : radius(r) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
class Square : public Shape {
|
||||
private:
|
||||
double width;
|
||||
public:
|
||||
Square(double w) : width(w) { };
|
||||
virtual double area(void);
|
||||
virtual double perimeter(void);
|
||||
};
|
||||
|
||||
10
Examples/scilab/class/example.i
Normal file
10
Examples/scilab/class/example.i
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* File : example.i */
|
||||
%module example
|
||||
|
||||
%{
|
||||
#include "example.h"
|
||||
%}
|
||||
|
||||
/* Let's just grab the original header file here */
|
||||
%include "example.h"
|
||||
|
||||
47
Examples/scilab/class/runme.sci
Normal file
47
Examples/scilab/class/runme.sci
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// loader the *.so
|
||||
exec loader.sce;
|
||||
|
||||
// ----- Object creation -----
|
||||
|
||||
printf("Creating some objects:\n");
|
||||
c = new_Circle(10)
|
||||
s = new_Square(10)
|
||||
|
||||
// ----- Access a static member -----
|
||||
|
||||
printf("\nA total of %i shapes were created\n", Shape_nshapes_get());
|
||||
|
||||
// ----- Member data access -----
|
||||
|
||||
// Set the location of the object
|
||||
|
||||
Shape_x_set(c, 20);
|
||||
Shape_y_set(c, 30);
|
||||
|
||||
Shape_x_set(s, -10);
|
||||
Shape_y_set(s, 5);
|
||||
|
||||
printf("\nHere is their current position:\n");
|
||||
printf(" Circle = (%f, %f)\n", Shape_x_get(c), Shape_y_get(c));
|
||||
printf(" Square = (%f, %f)\n", Shape_x_get(s), Shape_y_get(s));
|
||||
|
||||
// ----- Call some methods -----
|
||||
|
||||
printf("\nHere are some properties of the shapes:\n");
|
||||
function print_shape(o)
|
||||
printf(" area = %f\n", Shape_area(o));
|
||||
printf(" perimeter = %f\n", Shape_perimeter(o));
|
||||
endfunction
|
||||
print_shape(c);
|
||||
print_shape(s);
|
||||
|
||||
printf("\nGuess I will clean up now\n");
|
||||
|
||||
// Note: this invokes the virtual destructor
|
||||
delete_Circle(c);
|
||||
delete_Square(s);
|
||||
|
||||
printf("%i shapes remain\n", Shape_nshapes_get());
|
||||
printf("Goodbye\n");
|
||||
|
||||
exit
|
||||
Loading…
Add table
Add a link
Reference in a new issue