Scilab: wrap enums to Scilab variables (if %feature scilab:const")

This commit is contained in:
Simon Marchetto 2013-08-29 18:14:59 +02:00
commit f1d289925a
4 changed files with 48 additions and 11 deletions

View file

@ -1,6 +1,8 @@
/* File : example.i */
%module example
%scilabconst(1);
%{
#include "example.h"
%}

View file

@ -1,18 +1,19 @@
lines(0);
exec loader.sce;
SWIG_Init();
// Print out the value of some enums
printf("*** color ***\n");
printf(" RED = %i\n", RED_get());
printf(" BLUE = %i\n", BLUE_get());
printf(" GREEN = %i\n", GREEN_get());
printf(" RED = %i\n", RED);
printf(" BLUE = %i\n", BLUE);
printf(" GREEN = %i\n", GREEN);
printf("\nTesting use of enums with functions\n");
enum_test(RED_get());
enum_test(BLUE_get());
enum_test(GREEN_get());
enum_test(RED);
enum_test(BLUE);
enum_test(GREEN);
enum_test(int32(1234));
exit

View file

@ -378,7 +378,7 @@
//%apply int { size_t };
/* -----------------------------------------------------------------------------*/
/* Constants
/* Constants and enums to Scilab variables
/* -----------------------------------------------------------------------------*/
%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) int
@ -404,3 +404,9 @@
if (SWIG_CreateScilabVariable_charptr(pvApiCtx, "$result", $value) != SWIG_OK)
return SWIG_ERROR;
%}
%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) enum SWIGTYPE
%{
if (SWIG_CreateScilabVariable_int(pvApiCtx, "$result", $value) != SWIG_OK)
return SWIG_ERROR;
%}

View file

@ -568,15 +568,19 @@ public:
String *constantValue = rawValue ? rawValue : Getattr(node, "value");
String *constantTypemap = NULL;
// Constants of simple type are wrapped to Scilab variables
// If feature scilab:const enabled, constants & enums are wrapped to Scilab variables
if (GetFlag(node, "feature:scilab:const")) {
if ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)) {
bool isConstant = ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING));
bool isEnum = (Cmp(nodeType(node), "enumitem") == 0);
if (isConstant || isEnum) {
constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0);
if (constantTypemap != NULL) {
//String *wrapName = NewString("");
//Printf(wrapName, "Swig%s", constantName);
Setattr(node, "wrap:name", constantName);
Replaceall(constantTypemap, "$result", constantName);
if (isEnum) {
constantValue = Getattr(node, "enumvalue");
}
Replaceall(constantTypemap, "$value", constantValue);
emit_action_code(node, variablesCode, constantTypemap);
Delete(constantTypemap);
@ -630,6 +634,30 @@ public:
* enumvalueDeclaration()
* --------------------------------------------------------------------- */
virtual int enumvalueDeclaration(Node *node) {
static int iPreviousEnumValue = 0;
if (GetFlag(node, "feature:scilab:const")) {
// Compute the "absolute" value of enum if needed
// (most of time enum values are a linked list of relative values)
String *enumValue = Getattr(node, "enumvalue");
if (!enumValue) {
String *enumValueEx = Getattr(node, "enumvalueex");
if (enumValueEx) {
String *firstenumitem = Getattr(node, "firstenumitem");
if (firstenumitem) {
// First node, value is in enumValueEx
Setattr(node, "enumvalue", enumValueEx);
iPreviousEnumValue = atoi(Char(enumValueEx));
}
else {
enumValue = NewString("");
iPreviousEnumValue = iPreviousEnumValue + 1;
Printf(enumValue, "%d", iPreviousEnumValue);
Setattr(node, "enumvalue", enumValue);
}
}
}
}
/* Force type to be an enum (See scitypemaps.swg) */
Setattr(node, "type", "enum SWIG");