New example for templates
This commit is contained in:
parent
0390648862
commit
0ac03d9fe9
5 changed files with 142 additions and 0 deletions
17
Examples/scilab/template/Makefile
Normal file
17
Examples/scilab/template/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
|
||||||
43
Examples/scilab/template/example.cxx
Normal file
43
Examples/scilab/template/example.cxx
Normal file
|
|
@ -0,0 +1,43 @@
|
||||||
|
/* 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>;
|
||||||
|
|
||||||
36
Examples/scilab/template/example.h
Normal file
36
Examples/scilab/template/example.h
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
/* 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();
|
||||||
|
};
|
||||||
|
|
||||||
|
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<typename T>
|
||||||
|
class Square : public Shape<T> {
|
||||||
|
private:
|
||||||
|
T width;
|
||||||
|
public:
|
||||||
|
Square(T w) : Shape<T>() { width = w; };
|
||||||
|
virtual T area();
|
||||||
|
virtual T perimeter();
|
||||||
|
};
|
||||||
|
|
||||||
14
Examples/scilab/template/example.i
Normal file
14
Examples/scilab/template/example.i
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
/* File : example.i */
|
||||||
|
%module example
|
||||||
|
|
||||||
|
%{
|
||||||
|
#include "example.h"
|
||||||
|
%}
|
||||||
|
|
||||||
|
/* Let's just grab the original header file here */
|
||||||
|
%include "example.h"
|
||||||
|
|
||||||
|
%template(ShapeDouble) Shape<double>;
|
||||||
|
%template(CircleDouble) Circle<double>;
|
||||||
|
%template(SquareDouble) Square<double>;
|
||||||
|
|
||||||
32
Examples/scilab/template/runme.sci
Normal file
32
Examples/scilab/template/runme.sci
Normal file
|
|
@ -0,0 +1,32 @@
|
||||||
|
function printShape(shape, name)
|
||||||
|
printf("\nShape %s position:\n", name);
|
||||||
|
printf(" (x, y) = (%f, %f)\n", ShapeDouble_x_get(shape), ShapeDouble_y_get(shape))
|
||||||
|
|
||||||
|
printf("\nShape %s properties:\n", name);
|
||||||
|
printf(" area = %f\n", ShapeDouble_area(shape));
|
||||||
|
printf(" perimeter = %f\n", ShapeDouble_perimeter(shape));
|
||||||
|
|
||||||
|
printf("\n");
|
||||||
|
endfunction
|
||||||
|
|
||||||
|
exec loader.sce;
|
||||||
|
|
||||||
|
printf("Creating some objects:\n");
|
||||||
|
c = new_CircleDouble(10);
|
||||||
|
s = new_SquareDouble(10);
|
||||||
|
|
||||||
|
printf("\nA total of %i shapes were created\n", ShapeDouble_getNbShapes());
|
||||||
|
|
||||||
|
ShapeDouble_move(c, 20, 30);
|
||||||
|
ShapeDouble_move(s, -10, 0);
|
||||||
|
|
||||||
|
printShape(c, "circle");
|
||||||
|
printShape(s, "square");
|
||||||
|
|
||||||
|
printf("\nGuess I will clean up now\n");
|
||||||
|
|
||||||
|
delete_CircleDouble(c);
|
||||||
|
delete_SquareDouble(s);
|
||||||
|
|
||||||
|
printf("%i shapes remain\n", ShapeDouble_getNbShapes());
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue