scilab: template example same as in other languages

This commit is contained in:
Simon Marchetto 2014-07-24 17:25:58 +02:00
commit 98d823face
5 changed files with 60 additions and 101 deletions

View file

@ -1,6 +1,6 @@
TOP = ../.. TOP = ../..
SWIG = $(TOP)/../preinst-swig SWIG = $(TOP)/../preinst-swig
SRCS = example.cxx SRCS =
TARGET = example TARGET = example
INTERFACE = example.i INTERFACE = example.i

View file

@ -1,43 +0,0 @@
/* File : example.c */
#include "example.h"
#define M_PI 3.14159265358979323846
template<typename T>
void Shape<T>::move(T dx, T dy) {
x += dx;
y += dy;
}
template<typename T>
int Shape<T>::nbshapes = 0;
template<typename T>
int Shape<T>::getNbShapes() {
return Shape<T>::nbshapes;
}
template<typename T>
T Circle<T>::area() {
return M_PI*radius*radius;
}
template<typename T>
T Circle<T>::perimeter() {
return 2*M_PI*radius;
}
template<typename T>
T Square<T>::area() {
return width*width;
}
template<typename T>
T Square<T>::perimeter() {
return 4*width;
}
template class Shape<double>;
template class Square<double>;
template class Circle<double>;

View file

@ -1,36 +1,32 @@
/* File : example.h */ /* File : example.h */
template<typename T> // Some template definitions
class Shape {
private: template<class T> T max(T a, T b) { return a>b ? a : b; }
static int nbshapes;
public: template<class T> class vector {
Shape() { x = 0; y = 0; nbshapes++; } T *v;
virtual ~Shape() { nbshapes--; }; int sz;
T x, y; public:
void move(T dx, T dy); vector(int _sz) {
virtual T area() = 0; v = new T[_sz];
virtual T perimeter() = 0; sz = _sz;
static int getNbShapes(); }
}; T &get(int index) {
return v[index];
template<typename T> }
class Circle : public Shape<T> { void set(int index, T &val) {
private: v[index] = val;
T radius; }
public: #ifdef SWIG
Circle(T r) : Shape<T>() { radius = r; }; %extend {
virtual T area(); T getitem(int index) {
virtual T perimeter(); return $self->get(index);
}; }
void setitem(int index, T val) {
template<typename T> $self->set(index,val);
class Square : public Shape<T> { }
private: }
T width; #endif
public:
Square(T w) : Shape<T>() { width = w; };
virtual T area();
virtual T perimeter();
}; };

View file

@ -8,7 +8,10 @@
/* Let's just grab the original header file here */ /* Let's just grab the original header file here */
%include "example.h" %include "example.h"
%template(ShapeDouble) Shape<double>; /* Now instantiate some specific template declarations */
%template(CircleDouble) Circle<double>;
%template(SquareDouble) Square<double>; %template(maxint) max<int>;
%template(maxdouble) max<double>;
%template(vecint) vector<int>;
%template(vecdouble) vector<double>;

View file

@ -1,36 +1,39 @@
lines(0); lines(0);
ilib_verbose(0); ilib_verbose(0);
exec loader.sce; exec loader.sce;
example_Init();
function printShape(shape, name) // Call some templated functions
printf("\nShape %s position:\n", name); printf("maxint(3, 7) = %i\n", maxint(3, 7));
printf(" (x, y) = (%f, %f)\n", ShapeDouble_x_get(shape), ShapeDouble_y_get(shape)) printf("maxdouble(3.14, 2.18) = %3.2f\n", maxdouble(3.14, 2.18));
printf("\nShape %s properties:\n", name); // Create some class
printf(" area = %f\n", ShapeDouble_area(shape));
printf(" perimeter = %f\n", ShapeDouble_perimeter(shape));
printf("\n"); iv = new_vecint(100);
endfunction dv = new_vecdouble(1000);
printf("Creating some objects:\n"); for i = 0:100
c = new_CircleDouble(10); vecint_setitem(iv, i, 2*i);
s = new_SquareDouble(10); 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); isum = 0
ShapeDouble_move(s, -10, 0); for i = 0:100
isum = isum + vecint_getitem(iv, i);
end
printShape(c, "circle"); printf("isum = %i\n", isum);
printShape(s, "square");
printf("\nGuess I will clean up now\n"); dsum = 0
for i = 0:100
dsum = dsum + vecdouble_getitem(dv, i);
end
delete_CircleDouble(c); printf("dsum = %3.2f\n", dsum);
delete_SquareDouble(s);
printf("%i shapes remain\n", ShapeDouble_getNbShapes()); delete_vecint(iv);
delete_vecdouble(dv);
exit