Scilab: wrap enums to Scilab variables (if %feature scilab:const")
This commit is contained in:
parent
2cf606c638
commit
ed135cb99c
4 changed files with 48 additions and 11 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
/* File : example.i */
|
/* File : example.i */
|
||||||
%module example
|
%module example
|
||||||
|
|
||||||
|
%scilabconst(1);
|
||||||
|
|
||||||
%{
|
%{
|
||||||
#include "example.h"
|
#include "example.h"
|
||||||
%}
|
%}
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,19 @@
|
||||||
lines(0);
|
lines(0);
|
||||||
exec loader.sce;
|
exec loader.sce;
|
||||||
|
SWIG_Init();
|
||||||
|
|
||||||
// Print out the value of some enums
|
// Print out the value of some enums
|
||||||
printf("*** color ***\n");
|
printf("*** color ***\n");
|
||||||
printf(" RED = %i\n", RED_get());
|
printf(" RED = %i\n", RED);
|
||||||
printf(" BLUE = %i\n", BLUE_get());
|
printf(" BLUE = %i\n", BLUE);
|
||||||
printf(" GREEN = %i\n", GREEN_get());
|
printf(" GREEN = %i\n", GREEN);
|
||||||
|
|
||||||
|
|
||||||
printf("\nTesting use of enums with functions\n");
|
printf("\nTesting use of enums with functions\n");
|
||||||
|
|
||||||
enum_test(RED_get());
|
enum_test(RED);
|
||||||
enum_test(BLUE_get());
|
enum_test(BLUE);
|
||||||
enum_test(GREEN_get());
|
enum_test(GREEN);
|
||||||
enum_test(int32(1234));
|
enum_test(int32(1234));
|
||||||
|
|
||||||
exit
|
exit
|
||||||
|
|
|
||||||
|
|
@ -378,7 +378,7 @@
|
||||||
//%apply int { size_t };
|
//%apply int { size_t };
|
||||||
|
|
||||||
/* -----------------------------------------------------------------------------*/
|
/* -----------------------------------------------------------------------------*/
|
||||||
/* Constants
|
/* Constants and enums to Scilab variables
|
||||||
/* -----------------------------------------------------------------------------*/
|
/* -----------------------------------------------------------------------------*/
|
||||||
|
|
||||||
%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) int
|
%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) int
|
||||||
|
|
@ -404,3 +404,9 @@
|
||||||
if (SWIG_CreateScilabVariable_charptr(pvApiCtx, "$result", $value) != SWIG_OK)
|
if (SWIG_CreateScilabVariable_charptr(pvApiCtx, "$result", $value) != SWIG_OK)
|
||||||
return SWIG_ERROR;
|
return SWIG_ERROR;
|
||||||
%}
|
%}
|
||||||
|
|
||||||
|
%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) enum SWIGTYPE
|
||||||
|
%{
|
||||||
|
if (SWIG_CreateScilabVariable_int(pvApiCtx, "$result", $value) != SWIG_OK)
|
||||||
|
return SWIG_ERROR;
|
||||||
|
%}
|
||||||
|
|
|
||||||
|
|
@ -564,15 +564,19 @@ public:
|
||||||
String *constantValue = rawValue ? rawValue : Getattr(node, "value");
|
String *constantValue = rawValue ? rawValue : Getattr(node, "value");
|
||||||
String *constantTypemap = NULL;
|
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 (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);
|
constantTypemap = Swig_typemap_lookup("scilabconstcode", node, nodeName, 0);
|
||||||
if (constantTypemap != NULL) {
|
if (constantTypemap != NULL) {
|
||||||
//String *wrapName = NewString("");
|
|
||||||
//Printf(wrapName, "Swig%s", constantName);
|
|
||||||
Setattr(node, "wrap:name", constantName);
|
Setattr(node, "wrap:name", constantName);
|
||||||
Replaceall(constantTypemap, "$result", constantName);
|
Replaceall(constantTypemap, "$result", constantName);
|
||||||
|
if (isEnum) {
|
||||||
|
constantValue = Getattr(node, "enumvalue");
|
||||||
|
}
|
||||||
Replaceall(constantTypemap, "$value", constantValue);
|
Replaceall(constantTypemap, "$value", constantValue);
|
||||||
emit_action_code(node, variablesCode, constantTypemap);
|
emit_action_code(node, variablesCode, constantTypemap);
|
||||||
Delete(constantTypemap);
|
Delete(constantTypemap);
|
||||||
|
|
@ -626,6 +630,30 @@ public:
|
||||||
* enumvalueDeclaration()
|
* enumvalueDeclaration()
|
||||||
* --------------------------------------------------------------------- */
|
* --------------------------------------------------------------------- */
|
||||||
virtual int enumvalueDeclaration(Node *node) {
|
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) */
|
/* Force type to be an enum (See scitypemaps.swg) */
|
||||||
Setattr(node, "type", "enum SWIG");
|
Setattr(node, "type", "enum SWIG");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue