Permalink
Browse files

scilab: template example same as in other languages

  • Loading branch information...
1 parent f602d62 commit 98d823facebb3b962a26174bdac7dc3ceeb01e76 @smarchetto smarchetto committed Jul 24, 2014
@@ -1,6 +1,6 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
-SRCS = example.cxx
+SRCS =
TARGET = example
INTERFACE = example.i
@@ -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>;
-
@@ -1,36 +1,32 @@
/* File : example.h */
-template<typename T>
-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();
-};
+// Some template definitions
-template<typename T>
-class Circle : public Shape<T> {
-private:
- T radius;
-public:
- Circle(T r) : Shape<T>() { radius = r; };
- virtual T area();
- virtual T perimeter();
-};
+template<class T> T max(T a, T b) { return a>b ? a : b; }
-template<typename T>
-class Square : public Shape<T> {
-private:
- T width;
-public:
- Square(T w) : Shape<T>() { width = w; };
- virtual T area();
- virtual T perimeter();
+template<class T> 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
};
@@ -8,7 +8,10 @@
/* Let's just grab the original header file here */
%include "example.h"
-%template(ShapeDouble) Shape<double>;
-%template(CircleDouble) Circle<double>;
-%template(SquareDouble) Square<double>;
+/* Now instantiate some specific template declarations */
+
+%template(maxint) max<int>;
+%template(maxdouble) max<double>;
+%template(vecint) vector<int>;
+%template(vecdouble) vector<double>;
@@ -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);

0 comments on commit 98d823f

Please sign in to comment.