Have just one Scilab constants example

Same example as other languages, but using %scilabconst(1)
This commit is contained in:
William S Fulton 2014-09-02 20:29:45 +01:00
commit d3a54e50f8
8 changed files with 30 additions and 145 deletions

View file

@ -7,7 +7,6 @@ funcptr
matrix
matrix2
pointer
scilab_const
simple
std_list
std_vector

View file

@ -1,14 +1,30 @@
/* File : example.i */
%module example
#define ICONST 42
#define FCONST 2.1828
#define SCONST "Hello World"
/* Wraps enums and constants as Scilab variables (instead of functions) */
%scilabconst(1);
// Constants expressions are also accepted
#define EXPR ICONST + 3*FCONST
/* A few preprocessor macros */
#define ICONST 42
#define FCONST 2.1828
#define CCONST 'x'
#define CCONST2 '\n'
#define SCONST "Hello World"
#define SCONST2 "\"Hello World\""
/* This should work just fine */
#define EXPR ICONST + 3*(FCONST)
/* This shouldn't do anything */
#define EXTERN extern
/* Neither should this (BAR isn't defined) */
#define FOO (ICONST + BAR)
/* The following directives also produce constants */
// SWIG also offers to define constants
%constant int iconst = 37;
%constant double fconst = 42.2;
%constant double fconst = 3.14;

View file

@ -3,12 +3,12 @@ ilib_verbose(0);
exec loader.sce;
example_Init();
printf("\nConstants are wrapped by functions:\n");
printf("ICONST_get() = %i (should be 42)\n", ICONST_get());
printf("FCONST_get() = %5.4f (should be 2.1828)\n", FCONST_get());
printf("SCONST_get() = ''%s'' (should be ''Hello World'')\n", SCONST_get());
printf("EXPR_get() = %5.4f (should be 48.5484)\n", EXPR_get());
printf("iconst_get() = %i (should be 37)\n", iconst_get());
printf("fconst_get() = %3.2f (should be 42.20)\n", fconst_get());
printf("\nTest constants\n");
printf("ICONST = %i (should be 42)\n", ICONST);
printf("FCONST = %5.4f (should be 2.1828)\n", FCONST);
printf("SCONST = ''%s'' (should be ''Hello World'')\n", SCONST);
printf("EXPR = %5.4f (should be 48.5484)\n", EXPR);
printf("iconst = %i (should be 37)\n", iconst);
printf("fconst = %3.2f (should be 3.14)\n", fconst);
exit

View file

@ -1,15 +0,0 @@
TOP = ../..
SWIG = $(TOP)/../preinst-swig
CXXSRCS = example.cxx
TARGET = example
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' scilab_run
build:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' CXXSRCS='$(CXXSRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean:
$(MAKE) -f $(TOP)/Makefile SRCDIR='$(SRCDIR)' scilab_clean

View file

@ -1,37 +0,0 @@
/* 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

@ -1,20 +0,0 @@
/* File : example.h */
// Constants
#define ICONST 42
#define FCONST 2.1828
#define SCONST "Hello World"
#define EXPR ICONST + 3 * FCONST
// Enums
enum color { RED, BLUE, GREEN };
class Foo {
public:
Foo() { }
enum speed { IMPULSE, WARP, LUDICROUS };
void enum_test(speed s);
};
void enum_test(enum color c, Foo::speed s);

View file

@ -1,15 +0,0 @@
/* File : example.i */
%module example
%{
#include "example.h"
%}
/* Wraps enums and constants as Scilab variables (instead of functions) */
%scilabconst(1);
%include "example.h"
%constant int iconst = 37;
%constant double fconst = 42.2;

View file

@ -1,43 +0,0 @@
lines(0);
ilib_verbose(0);
exec loader.sce;
example_Init();
printf("\nTest %%scilab_const(1) feature: constants and enums are 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("\n*** Foo::speed ***\n")
printf(" Foo_IMPULSE = %i\n", Foo_IMPULSE);
printf(" Foo_WARP = %i\n", Foo_WARP);
printf(" Foo_LUDICROUS = %i\n", Foo_LUDICROUS);
printf("\nTest enums as argument of functions\n");
enum_test(RED, Foo_IMPULSE);
enum_test(BLUE, Foo_WARP);
enum_test(GREEN, Foo_LUDICROUS);
enum_test(1234, 5678);
printf("\nTest enums as argument of class methods\n");
f = new_Foo();
Foo_enum_test(f, Foo_IMPULSE);
Foo_enum_test(f, Foo_WARP);
Foo_enum_test(f, Foo_LUDICROUS);
delete_Foo(f);
printf("\nTest constants\n");
printf("ICONST = %i (should be 42)\n", ICONST);
printf("FCONST = %5.4f (should be 2.1828)\n", FCONST);
printf("SCONST = ''%s'' (should be ''Hello World'')\n", SCONST);
printf("EXPR = %5.4f (should be 48.5484)\n", EXPR);
printf("iconst = %i (should be 37)\n", iconst);
printf("fconst = %3.2f (should be 42.20)\n", fconst);
exit