diff --git a/Examples/scilab/template/Makefile b/Examples/scilab/template/Makefile index ee565de91..e4badf9cf 100644 --- a/Examples/scilab/template/Makefile +++ b/Examples/scilab/template/Makefile @@ -1,6 +1,6 @@ TOP = ../.. SWIG = $(TOP)/../preinst-swig -SRCS = example.cxx +SRCS = TARGET = example INTERFACE = example.i diff --git a/Examples/scilab/template/example.cxx b/Examples/scilab/template/example.cxx deleted file mode 100644 index 72410315d..000000000 --- a/Examples/scilab/template/example.cxx +++ /dev/null @@ -1,43 +0,0 @@ -/* File : example.c */ - -#include "example.h" -#define M_PI 3.14159265358979323846 - -template -void Shape::move(T dx, T dy) { - x += dx; - y += dy; -} - -template -int Shape::nbshapes = 0; - -template -int Shape::getNbShapes() { - return Shape::nbshapes; -} - -template -T Circle::area() { - return M_PI*radius*radius; -} - -template -T Circle::perimeter() { - return 2*M_PI*radius; -} - -template -T Square::area() { - return width*width; -} - -template -T Square::perimeter() { - return 4*width; -} - -template class Shape; -template class Square; -template class Circle; - diff --git a/Examples/scilab/template/example.h b/Examples/scilab/template/example.h index a825631b3..7401df650 100644 --- a/Examples/scilab/template/example.h +++ b/Examples/scilab/template/example.h @@ -1,36 +1,32 @@ /* File : example.h */ -template -class Shape { -private: - static int nbshapes; -public: - Shape() { x = 0; y = 0; nbshapes++; } - virtual ~Shape() { nbshapes--; }; - T x, y; - void move(T dx, T dy); - virtual T area() = 0; - virtual T perimeter() = 0; - static int getNbShapes(); -}; - -template -class Circle : public Shape { -private: - T radius; -public: - Circle(T r) : Shape() { radius = r; }; - virtual T area(); - virtual T perimeter(); -}; - -template -class Square : public Shape { -private: - T width; -public: - Square(T w) : Shape() { width = w; }; - virtual T area(); - virtual T perimeter(); +// Some template definitions + +template T max(T a, T b) { return a>b ? a : b; } + +template class vector { + T *v; + int sz; + public: + vector(int _sz) { + v = new T[_sz]; + sz = _sz; + } + T &get(int index) { + return v[index]; + } + void set(int index, T &val) { + v[index] = val; + } +#ifdef SWIG + %extend { + T getitem(int index) { + return $self->get(index); + } + void setitem(int index, T val) { + $self->set(index,val); + } + } +#endif }; diff --git a/Examples/scilab/template/example.i b/Examples/scilab/template/example.i index 9732ebe2b..8f94c4da1 100644 --- a/Examples/scilab/template/example.i +++ b/Examples/scilab/template/example.i @@ -8,7 +8,10 @@ /* Let's just grab the original header file here */ %include "example.h" -%template(ShapeDouble) Shape; -%template(CircleDouble) Circle; -%template(SquareDouble) Square; +/* Now instantiate some specific template declarations */ + +%template(maxint) max; +%template(maxdouble) max; +%template(vecint) vector; +%template(vecdouble) vector; diff --git a/Examples/scilab/template/runme.sci b/Examples/scilab/template/runme.sci index 55a17820c..3fa8c0efe 100644 --- a/Examples/scilab/template/runme.sci +++ b/Examples/scilab/template/runme.sci @@ -1,36 +1,39 @@ lines(0); ilib_verbose(0); exec loader.sce; +example_Init(); -function printShape(shape, name) - printf("\nShape %s position:\n", name); - printf(" (x, y) = (%f, %f)\n", ShapeDouble_x_get(shape), ShapeDouble_y_get(shape)) +// Call some templated functions +printf("maxint(3, 7) = %i\n", maxint(3, 7)); +printf("maxdouble(3.14, 2.18) = %3.2f\n", maxdouble(3.14, 2.18)); - printf("\nShape %s properties:\n", name); - printf(" area = %f\n", ShapeDouble_area(shape)); - printf(" perimeter = %f\n", ShapeDouble_perimeter(shape)); +// Create some class - printf("\n"); -endfunction +iv = new_vecint(100); +dv = new_vecdouble(1000); -printf("Creating some objects:\n"); -c = new_CircleDouble(10); -s = new_SquareDouble(10); +for i = 0:100 + vecint_setitem(iv, i, 2*i); +end -printf("\nA total of %i shapes were created\n", ShapeDouble_getNbShapes()); +for i = 0:100 + vecdouble_setitem(dv, i, 1.0/(i+1)); +end -ShapeDouble_move(c, 20, 30); -ShapeDouble_move(s, -10, 0); +isum = 0 +for i = 0:100 + isum = isum + vecint_getitem(iv, i); +end -printShape(c, "circle"); -printShape(s, "square"); +printf("isum = %i\n", isum); -printf("\nGuess I will clean up now\n"); +dsum = 0 +for i = 0:100 + dsum = dsum + vecdouble_getitem(dv, i); +end -delete_CircleDouble(c); -delete_SquareDouble(s); +printf("dsum = %3.2f\n", dsum); -printf("%i shapes remain\n", ShapeDouble_getNbShapes()); - -exit +delete_vecint(iv); +delete_vecdouble(dv);