scilab: enum example same as in other languages

This commit is contained in:
Simon Marchetto 2014-07-24 17:09:19 +02:00
commit e37319b9be
6 changed files with 70 additions and 31 deletions

View file

@ -1,6 +1,6 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.c
SRCS = example.cxx
TARGET = example
INTERFACE = example.i
@ -9,7 +9,7 @@ check: build
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' scilab_clean

View file

@ -1,16 +0,0 @@
/* File : example.c */
#include "example.h"
#include <stdio.h>
void enum_test(color c) {
if (c == RED) {
printf("color = RED\n");
} else if (c == BLUE) {
printf("color = BLUE\n");
} else if (c == GREEN) {
printf("color = GREEN\n");
} else {
printf("color = Unknown color!\n");
}
}

View file

@ -0,0 +1,37 @@
/* File : example.c */
#include "example.h"
#include <stdio.h>
void Foo::enum_test(speed s) {
if (s == IMPULSE) {
printf("IMPULSE speed\n");
} else if (s == WARP) {
printf("WARP speed\n");
} else if (s == LUDICROUS) {
printf("LUDICROUS speed\n");
} else {
printf("Unknown speed\n");
}
}
void enum_test(color c, Foo::speed s) {
if (c == RED) {
printf("color = RED, ");
} else if (c == BLUE) {
printf("color = BLUE, ");
} else if (c == GREEN) {
printf("color = GREEN, ");
} else {
printf("color = Unknown color!, ");
}
if (s == Foo::IMPULSE) {
printf("speed = IMPULSE speed\n");
} else if (s == Foo::WARP) {
printf("speed = WARP speed\n");
} else if (s == Foo::LUDICROUS) {
printf("speed = LUDICROUS speed\n");
} else {
printf("speed = Unknown speed!\n");
}
}

View file

@ -2,5 +2,12 @@
typedef enum { RED, BLUE, GREEN } color;
void enum_test(color c);
class Foo {
public:
Foo() { }
enum speed { IMPULSE, WARP, LUDICROUS };
void enum_test(speed s);
};
void enum_test(color c, Foo::speed s);

View file

@ -5,9 +5,6 @@
#include "example.h"
%}
/* Forces to wrap enums as Scilab variables (instead of functions) */
%scilabconst(1);
%include "example.h"

View file

@ -3,16 +3,30 @@ ilib_verbose(0);
exec loader.sce;
example_Init();
printf("\nTesting use of enums (wrapped as Scilab variables)\n");
printf("\nTest enums\n");
printf("*** color ***\n");
printf(" RED = %i\n", RED);
printf(" BLUE = %i\n", BLUE);
printf(" GREEN = %i\n", GREEN);
printf(" RED_get() = %i\n", RED_get());
printf(" BLUE_get() = %i\n", BLUE_get());
printf(" GREEN_get() = %i\n", GREEN_get());
enum_test(RED);
enum_test(BLUE);
enum_test(GREEN);
enum_test(int32(1234));
printf("\n*** Foo::speed ***\n")
printf(" Foo_IMPULSE = %i\n", Foo_IMPULSE_get());
printf(" Foo_WARP = %i\n", Foo_WARP_get());
printf(" Foo_LUDICROUS = %i\n", Foo_LUDICROUS_get());
printf("\nTest enums as argument of functions\n");
enum_test(RED_get(), Foo_IMPULSE_get());
enum_test(BLUE_get(), Foo_WARP_get());
enum_test(GREEN_get(), Foo_LUDICROUS_get());
enum_test(1234, 5678);
printf("\nTest enums as argument of class methods\n");
f = new_Foo();
Foo_enum_test(f, Foo_IMPULSE_get());
Foo_enum_test(f, Foo_WARP_get());
Foo_enum_test(f, Foo_LUDICROUS_get());
delete_Foo(f);
exit