From c4fb9f00c88c329deee9c41aeb7c8f8a2640b197 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Tue, 27 Aug 2013 11:57:43 +0200 Subject: [PATCH 1/8] Scilab: list helper getScilabListAndSize() --- Lib/scilab/scilist.swg | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/Lib/scilab/scilist.swg b/Lib/scilab/scilist.swg index fc73d463a..c585726e4 100644 --- a/Lib/scilab/scilist.swg +++ b/Lib/scilab/scilist.swg @@ -43,6 +43,27 @@ SWIG_GetScilabListSize(SciObject _obj, int *_piListSize) return SWIG_OK; } +SWIGINTERN int +SWIG_GetScilabListAndSize(SciObject _obj, int **_piListAddr, int *_piListSize) +{ + SciErr sciErr; + + sciErr = getVarAddressFromPosition(pvApiCtx, _obj, _piListAddr); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return SWIG_ERROR; + } + + sciErr = getListItemNumber(pvApiCtx, *_piListAddr, _piListSize); + if (sciErr.iErr) + { + printError(&sciErr, 0); + return SWIG_ERROR; + } + + return SWIG_OK; +} SWIGINTERN int SWIG_CheckScilabList(SciObject _obj) From 6346803d41322c7f4f085e417106a3c218968886 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Tue, 27 Aug 2013 12:53:16 +0200 Subject: [PATCH 2/8] Scilab: fix debug target --- Examples/Makefile.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Examples/Makefile.in b/Examples/Makefile.in index 48483572b..59d02caa4 100644 --- a/Examples/Makefile.in +++ b/Examples/Makefile.in @@ -1635,7 +1635,7 @@ scilab_run: # ----------------------------------------------------------------- scilab_debug: - @env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(SCILAB) $(SCILAB_STARTOPT) -f runme.sci + @env LD_LIBRARY_PATH=.:$$LD_LIBRARY_PATH SCILABPATH=$(srcdir):$$SCILABPATH $(SCILAB) $(SCILAB_STARTOPT) -debug -f runme.sci # ----------------------------------------------------------------- # Scilab version From 362c7e7bce0c3b2b29fd7910e950f35741415d81 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Wed, 28 Aug 2013 17:00:23 +0200 Subject: [PATCH 3/8] Scilab: add %feature scilab:const (constants are wrapped by Scilab variables) --- Examples/scilab/constants/example.i | 12 ++++++++++ Examples/scilab/constants/runme.sci | 30 ++++++++++++++++------- Lib/scilab/scichar.swg | 34 ++++++++++++++++++++++++++ Lib/scilab/scidouble.swg | 12 ++++++++++ Lib/scilab/sciint.swg | 12 ++++++++++ Lib/scilab/scilab.swg | 1 + Lib/scilab/scimacros.swg | 5 ++++ Lib/scilab/sciruntime.swg | 3 ++- Lib/scilab/scitypemaps.swg | 28 ++++++++++++++++++++++ Source/Modules/scilab.cxx | 37 ++++++++++++++++++++++++----- 10 files changed, 158 insertions(+), 16 deletions(-) create mode 100644 Lib/scilab/scimacros.swg diff --git a/Examples/scilab/constants/example.i b/Examples/scilab/constants/example.i index a79fb4ed9..fbdea586a 100644 --- a/Examples/scilab/constants/example.i +++ b/Examples/scilab/constants/example.i @@ -24,5 +24,17 @@ %constant int iconst = 37; %constant double fconst = 3.14; +/* Now constants are wrapped to Scilab variables */ +%scilabconst(1); + +#define ICONST2 12 +#define FCONST2 4.60 +#define CCONST3 'a' +#define CCONST4 '\n' +#define SCONST3 "Hello World" +#define SCONST4 "\"Hello World\"" + +%constant int iconst2 = 73; +%constant double fconst2 = 6.28; diff --git a/Examples/scilab/constants/runme.sci b/Examples/scilab/constants/runme.sci index 5109f857e..a1afec976 100644 --- a/Examples/scilab/constants/runme.sci +++ b/Examples/scilab/constants/runme.sci @@ -1,15 +1,17 @@ lines(0); exec loader.sce; +SWIG_Init(); -printf("ICONST = %i (should be 42)\n", ICONST_get()); -printf("FCONST = %f (should be 2.1828)\n", FCONST_get()); -printf("CCONST = %c (should be ''x'')\n", CCONST_get()); -printf("CCONST2 = %s (this should be on a new line)\n", CCONST2_get()); -printf("SCONST = %s (should be ''Hello World'')\n", SCONST_get()); -printf("SCONST2 = %s (should be "'""Hello World"""')\n", SCONST2_get()); -printf("EXPR = %f (should be 48.5484)\n", EXPR_get()); -printf("iconst = %i (should be 37)\n", iconst_get()); -printf("fconst = %f (should be 3.14)\n", fconst_get()); +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("CCONST_get() = ''%c'' (should be ''x'')\n", CCONST_get()); +printf("CCONST2_get() = %s (this should be on a new line)\n", CCONST2_get()); +printf("SCONST_get() = ''%s'' (should be ''Hello World'')\n", SCONST_get()); +printf("SCONST2_get() = ''%s'' (should be "'""Hello World"""')\n", SCONST2_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 3.14)\n", fconst_get()); try printf("EXTERN = %s (Arg! This should not printf(anything)\n", EXTERN_get()); @@ -22,4 +24,14 @@ catch printf("FOO is not defined (good)\n"); end +printf("\nNow constants are wrapped by Scilab variables (feature scilab:const):\n"); +printf("ICONST2 = %i (should be 12)\n", ICONST2); +printf("FCONST2 = %3.2f (should be 4.60)\n", FCONST2); +printf("CCONST3 = ''%c'' (should be ''a'')\n", CCONST3); +printf("CCONST4 = %s (this should be on a new line)\n", CCONST4); +printf("SCONST3 = ''%s'' (should be ''Hello World'')\n", SCONST3); +printf("SCONST4 = ''%s'' (should be "'""Hello World"""')\n", SCONST4); +printf("iconst2 = %i (should be 73)\n", iconst2); +printf("fconst2 = %3.2f (should be 6.28)\n", fconst2); + exit diff --git a/Lib/scilab/scichar.swg b/Lib/scilab/scichar.swg index 96e855929..e165b66d8 100644 --- a/Lib/scilab/scichar.swg +++ b/Lib/scilab/scichar.swg @@ -252,3 +252,37 @@ SwigScilabStringFromCharPtrArray(void *_pvApiCtx, int _iVarOut, char **_charPtrA return SWIG_NbInputArgument(pvApiCtx) + _iVarOut; } } +%fragment(SWIG_CreateScilabVariable_frag(char), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(char)(void *_pvApiCtx, const char* _psVariableName, const char _cVariableValue) { + SciErr sciErr; + char sValue[2]; + const char* psStrings[1]; + + sValue[0] = _cVariableValue; + sValue[1] = '\0'; + psStrings[0] = sValue; + + sciErr = createNamedMatrixOfString(_pvApiCtx, _psVariableName, 1, 1, psStrings); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} +%fragment(SWIG_CreateScilabVariable_frag(charptr), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(charptr)(void *_pvApiCtx, const char* _psVariableName, const char* _psVariableValue) { + SciErr sciErr; + const char* psStrings[1]; + psStrings[0] = _psVariableValue; + + sciErr = createNamedMatrixOfString(_pvApiCtx, _psVariableName, 1, 1, psStrings); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} diff --git a/Lib/scilab/scidouble.swg b/Lib/scilab/scidouble.swg index 0eb9d8ae4..94a69671a 100644 --- a/Lib/scilab/scidouble.swg +++ b/Lib/scilab/scidouble.swg @@ -118,3 +118,15 @@ SWIG_SciDouble_FromDoubleArrayAndSize(void *_pvApiCtx, int _iVarOut, int _iRows, return Rhs + _iVarOut; } } +%fragment(SWIG_CreateScilabVariable_frag(double), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(double)(void *_pvApiCtx, const char* _psVariableName, const double _dVariableValue) { + SciErr sciErr; + sciErr = createNamedMatrixOfDouble(_pvApiCtx, _psVariableName, 1, 1, &_dVariableValue); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} diff --git a/Lib/scilab/sciint.swg b/Lib/scilab/sciint.swg index f45fb8524..e2758eb74 100644 --- a/Lib/scilab/sciint.swg +++ b/Lib/scilab/sciint.swg @@ -196,3 +196,15 @@ SWIG_SciInt32_FromIntArrayAndSize(void *_pvApiCtx, int _iVarOut, int _iRows, int return Rhs + _iVarOut; } } +%fragment(SWIG_CreateScilabVariable_frag(int), "wrapper") { +SWIGINTERN int +SWIG_CreateScilabVariable_dec(int)(void *_pvApiCtx, const char* _psVariableName, const int _iVariableValue) { + SciErr sciErr; + sciErr = createNamedMatrixOfInteger32(_pvApiCtx, _psVariableName, 1, 1, &_iVariableValue); + if (sciErr.iErr) { + printError(&sciErr, 0); + return SWIG_ERROR; + } + return SWIG_OK; +} +} diff --git a/Lib/scilab/scilab.swg b/Lib/scilab/scilab.swg index ac24d159f..3b5f6e817 100644 --- a/Lib/scilab/scilab.swg +++ b/Lib/scilab/scilab.swg @@ -1,5 +1,6 @@ %include %include +%include %include %include diff --git a/Lib/scilab/scimacros.swg b/Lib/scilab/scimacros.swg new file mode 100644 index 000000000..669ca893f --- /dev/null +++ b/Lib/scilab/scimacros.swg @@ -0,0 +1,5 @@ + #define %scilabconst(flag) %feature("scilab:const","flag") + +// Create Scilab variable +#define SWIG_CreateScilabVariable_frag(Type...) %fragment_name(CreateScilabVariable, Type) +#define SWIG_CreateScilabVariable_dec(Type...) %symbol_name(CreateScilabVariable, Type) diff --git a/Lib/scilab/sciruntime.swg b/Lib/scilab/sciruntime.swg index e8b52b12b..86471bda4 100644 --- a/Lib/scilab/sciruntime.swg +++ b/Lib/scilab/sciruntime.swg @@ -78,7 +78,6 @@ SWIG_Scilab_ErrorMsg(int code, const char *mesg) /* Used for C++ enums */ //#define SWIG_AsVal_int(scilabValue, valuePointer) SWIG_SciDouble_AsInt(pvApiCtx, scilabValue, valuePointer, fname) - #if SCILAB_VERSION_54_OR_HIGHER #define SWIG_CheckInputArgument(pvApiCtx, minInputArgument, maxInputArgument) CheckInputArgument(pvApiCtx, minInputArgument, maxInputArgument) #define SWIG_CheckOutputArgument(pvApiCtx, minOutputArgument, maxOutputArgument) CheckOutputArgument(pvApiCtx, minOutputArgument, maxOutputArgument) @@ -272,6 +271,8 @@ SWIG_Scilab_SetModule(swig_module_info *swig_module) %init %{ #ifdef __cplusplus extern "C" +#endif int SWIG_Init(char *fname, unsigned long fname_len) { SWIG_InitializeModule(NULL); + SWIG_CreateScilabVariables(); %} diff --git a/Lib/scilab/scitypemaps.swg b/Lib/scilab/scitypemaps.swg index 657717cda..f6eeb27aa 100644 --- a/Lib/scilab/scitypemaps.swg +++ b/Lib/scilab/scitypemaps.swg @@ -376,3 +376,31 @@ %typecheck(SWIG_TYPECHECK_STRING_ARRAY) char ** { SCILAB_TYPECHECK(isStringType) } //%apply int { size_t }; + +/* -----------------------------------------------------------------------------*/ +/* Constants +/* -----------------------------------------------------------------------------*/ + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(int)) int +%{ + if (SWIG_CreateScilabVariable_int(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(double)) double +%{ + if (SWIG_CreateScilabVariable_double(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(char)) char +%{ + if (SWIG_CreateScilabVariable_char(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} + +%typemap(scilabconstcode, fragment=SWIG_CreateScilabVariable_frag(charptr)) char * +%{ + if (SWIG_CreateScilabVariable_charptr(pvApiCtx, "$result", $value) != SWIG_OK) + return SWIG_ERROR; +%} diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 7ec5849eb..8cb430c94 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -24,6 +24,7 @@ Scilab options\n\ -vbl sets the build verbose level (default 0)\n\n"; const char* SWIG_INIT_FUNCTION_NAME = "SWIG_Init"; +const char* SWIG_CREATE_VARIABLES_FUNCTION_NAME = "SWIG_CreateScilabVariables"; class SCILAB : public Language { protected: @@ -34,6 +35,8 @@ protected: File *wrappersSection; File *initSection; + String *variablesCode; + File *builderFile; String *builderCode; int builderFunctionCount; @@ -43,7 +46,6 @@ protected: String *ldflag; String* verboseBuildLevel; - public: /* ------------------------------------------------------------------------ * main() @@ -185,10 +187,12 @@ public: Printf(builderCode, "table = ["); - /* In C++ mode, add initialization function to builder table */ - if (CPlusPlus) { - Printf(builderCode, "\"%s\",\"%s\";", SWIG_INIT_FUNCTION_NAME, SWIG_INIT_FUNCTION_NAME); - } + /* add initialization function to builder table */ + addFunctionInBuilder(NewString(SWIG_INIT_FUNCTION_NAME), NewString(SWIG_INIT_FUNCTION_NAME)); + + // Open Scilab wrapper variables creation function + variablesCode = NewString(""); + Printf(variablesCode, "int %s() {\n", SWIG_CREATE_VARIABLES_FUNCTION_NAME); /* Emit code for children */ if (CPlusPlus) { @@ -201,6 +205,9 @@ public: Printf(wrappersSection, "}\n"); } + // Close Scilab wrapper variables creation function + Printf(variablesCode, " return SWIG_OK;\n}\n"); + /* Write all to the builder.sce file */ Printf(builderCode, "];\n"); Printf(builderCode, "if ~isempty(table) then\n"); @@ -213,13 +220,14 @@ public: Delete(builderFile); /* Close the init function (opened in sciinit.swg) */ - Printf(initSection, "return 0;\n}\n#endif\n"); + Printf(initSection, "return 0;\n}\n"); /* Write all to the wrapper file */ SwigType_emit_type_table(runtimeSection, wrappersSection); // Declare pointer types, ... (Ex: SWIGTYPE_p_p_double) Dump(runtimeSection, beginSection); Dump(headerSection, beginSection); Dump(wrappersSection, beginSection); + Dump(variablesCode, beginSection); Wrapper_pretty_print(initSection, beginSection); /* Cleanup files */ @@ -560,6 +568,23 @@ public: String *constantValue = rawValue ? rawValue : Getattr(node, "value"); String *constantTypemap = NULL; + // Constants of simple type are wrapped to Scilab variables + if (GetFlag(node, "feature:scilab:const")) { + if ((SwigType_issimple(type)) || (SwigType_type(type) == T_STRING)) { + 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); + Replaceall(constantTypemap, "$value", constantValue); + emit_action_code(node, variablesCode, constantTypemap); + Delete(constantTypemap); + return SWIG_OK; + } + } + } + /* Create variables for member pointer constants, not suppported by typemaps (like Python wrapper does) */ if (SwigType_type(type) == T_MPOINTER) { String *wname = Swig_name_wrapper(constantName); From 1b6fff9da50484cd3cfd54cf91d15224f25fef1e Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Thu, 29 Aug 2013 09:07:29 +0200 Subject: [PATCH 4/8] Scilab: fix portability issue in configure.ac --- configure.ac | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure.ac b/configure.ac index 8e1419a36..c549d92e4 100644 --- a/configure.ac +++ b/configure.ac @@ -1031,7 +1031,7 @@ if test -n "$SCILAB"; then AC_MSG_CHECKING(for Scilab startup options) SCILABSTARTOPT="-nwni -nb" if test $SCILAB_VERSION -ge 54; then - SCILABSTARTOPT+=" -noatomsautoload" + SCILABSTARTOPT="$SCILABSTARTOPT -noatomsautoload" fi AC_MSG_RESULT($SCILABSTARTOPT) fi From f1d289925a49db5c212685a89317779d9042ce58 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Thu, 29 Aug 2013 18:14:59 +0200 Subject: [PATCH 5/8] Scilab: wrap enums to Scilab variables (if %feature scilab:const") --- Examples/scilab/enum/example.i | 2 ++ Examples/scilab/enum/runme.sci | 13 ++++++------ Lib/scilab/scitypemaps.swg | 8 +++++++- Source/Modules/scilab.cxx | 36 ++++++++++++++++++++++++++++++---- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/Examples/scilab/enum/example.i b/Examples/scilab/enum/example.i index 23ee8a822..634352b03 100644 --- a/Examples/scilab/enum/example.i +++ b/Examples/scilab/enum/example.i @@ -1,6 +1,8 @@ /* File : example.i */ %module example +%scilabconst(1); + %{ #include "example.h" %} diff --git a/Examples/scilab/enum/runme.sci b/Examples/scilab/enum/runme.sci index a5f16f3f8..e03fac505 100644 --- a/Examples/scilab/enum/runme.sci +++ b/Examples/scilab/enum/runme.sci @@ -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 diff --git a/Lib/scilab/scitypemaps.swg b/Lib/scilab/scitypemaps.swg index f6eeb27aa..33cf288a6 100644 --- a/Lib/scilab/scitypemaps.swg +++ b/Lib/scilab/scitypemaps.swg @@ -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; +%} diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 8cb430c94..3bcbdd0c7 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -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"); From 74aebf252d65696a616c40f9c83bacbaee67efc0 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 30 Aug 2013 16:03:20 +0200 Subject: [PATCH 6/8] Scilab: consider int as default type for STL container of values (so it works for container of enums) --- Lib/scilab/scisequence.swg | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Lib/scilab/scisequence.swg b/Lib/scilab/scisequence.swg index 9eefd94fb..56f6eb196 100644 --- a/Lib/scilab/scisequence.swg +++ b/Lib/scilab/scisequence.swg @@ -34,6 +34,11 @@ // %fragment(SWIG_Traits_Sequence_frag(ptr), "header", + fragment=SWIG_AsCheck_Sequence_frag(int), + fragment=SWIG_AsGet_Sequence_frag(int), + fragment=SWIG_AsSize_Sequence_frag(int), + fragment=SWIG_FromCreate_Sequence_frag(int), + fragment=SWIG_FromSet_Sequence_frag(int), fragment=SWIG_AsCheck_Sequence_frag(ptr), fragment=SWIG_AsGet_Sequence_frag(ptr), fragment=SWIG_AsSize_Sequence_frag(ptr), @@ -42,29 +47,29 @@ fragment="StdTraits") { namespace swig { - // Returns an error for default (not specialized) value containers + // For sequence of values, considers int as default type (so it works for enums) template struct traits_as_sequence { static int check(SciObject obj) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsCheck_Sequence_dec(int)(obj); } static int get(SciObject obj, void **sequence) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsGet_Sequence_dec(int)(obj, (int **)sequence); } static int size(SciObject obj, int *size) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsSize_Sequence_dec(int)(obj, size); } }; template struct traits_from_sequence { static int create(int size, void **sequence) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_FromCreate_Sequence_dec(int)(size, (int **)sequence); } static SciObject set(int size, void *sequence) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_FromSet_Sequence_dec(int)(size, (int *)sequence); } }; - // But supports containers of pointers + // For sequence of pointers template struct traits_as_sequence { static int check(SciObject obj) { @@ -127,25 +132,27 @@ namespace swig { // %fragment(SWIG_Traits_SequenceItem_frag(ptr), "header", + fragment=SWIG_AsVal_SequenceItem_frag(int), + fragment=SWIG_From_SequenceItem_frag(int), fragment=SWIG_AsVal_SequenceItem_frag(ptr), fragment=SWIG_From_SequenceItem_frag(ptr), fragment="StdTraits") { namespace swig { - // Returns an error for default (not specialized) value containers + // For sequence of values, considers int as default type (so it works for enums) template struct traits_asval_sequenceitem { static int asval(SciObject obj, void *pSequence, int iItemIndex, T *pItemValue) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_AsVal_SequenceItem_dec(int)(obj, (int *)pSequence, iItemIndex, (int *)pItemValue); } }; template struct traits_from_sequenceitem { static int from(void *pSequence, int iItemIndex, T itemValue) { - SWIG_Error(SWIG_TypeError, type_name()); + return SWIG_From_SequenceItem_dec(int)((int *)pSequence, iItemIndex, (int)itemValue); } }; - // But supports containers of pointers + // Sequence of pointers template struct traits_asval_sequenceitem { static int asval(SciObject obj, void *pSequence, int iItemIndex, T **pItemValue) { From 39bab12d2ca85c023495e84fdd8100231bb2585d Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 30 Aug 2013 16:41:42 +0200 Subject: [PATCH 7/8] Scilab: add %scilabconst in enum example --- Examples/scilab/enum/example.c | 13 +++++++++ Examples/scilab/enum/example.i | 8 +++--- Examples/scilab/enum/runme.sci | 31 ++++++++++++++-------- Examples/scilab/enum/scilabconst_example.h | 3 +++ 4 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 Examples/scilab/enum/scilabconst_example.h diff --git a/Examples/scilab/enum/example.c b/Examples/scilab/enum/example.c index 6df9203ce..0dbe4cda7 100644 --- a/Examples/scilab/enum/example.c +++ b/Examples/scilab/enum/example.c @@ -1,6 +1,7 @@ /* File : example.c */ #include "example.h" +#include "scilabconst_example.h" #include void enum_test(color c) { @@ -14,3 +15,15 @@ void enum_test(color c) { printf("color = Unknown color!\n"); } } + +void scilabconst_enum_test(fruit f) { + if (f == APPLE) { + printf("fruit = APPLE\n"); + } else if (f == ORANGE) { + printf("fruit = ORANGE\n"); + } else if (f == LEMON) { + printf("fruit = LEMON\n"); + } else { + printf("fruit = Unknown fruit!\n"); + } +} diff --git a/Examples/scilab/enum/example.i b/Examples/scilab/enum/example.i index 634352b03..6a471fde0 100644 --- a/Examples/scilab/enum/example.i +++ b/Examples/scilab/enum/example.i @@ -1,13 +1,13 @@ /* File : example.i */ %module example -%scilabconst(1); - %{ #include "example.h" +#include "scilabconst_example.h" %} -/* Let's just grab the original header file here */ - %include "example.h" +%scilabconst(1); + +%include "scilabconst_example.h" diff --git a/Examples/scilab/enum/runme.sci b/Examples/scilab/enum/runme.sci index e03fac505..182fa0c61 100644 --- a/Examples/scilab/enum/runme.sci +++ b/Examples/scilab/enum/runme.sci @@ -2,19 +2,28 @@ lines(0); exec loader.sce; SWIG_Init(); -// Print out the value of some enums +printf("\nTesting use of enums wrapped as Scilab functions\n"); + printf("*** color ***\n"); -printf(" RED = %i\n", RED); -printf(" BLUE = %i\n", BLUE); -printf(" GREEN = %i\n", GREEN); +printf(" RED = %i\n", RED_get()); +printf(" BLUE = %i\n", BLUE_get()); +printf(" GREEN = %i\n", GREEN_get()); - -printf("\nTesting use of enums with functions\n"); - -enum_test(RED); -enum_test(BLUE); -enum_test(GREEN); +enum_test(RED_get()); +enum_test(BLUE_get()); +enum_test(GREEN_get()); enum_test(int32(1234)); -exit +printf("\nTesting use of enums wrapped as Scilab variables\n"); +printf("*** fruit ***\n"); +printf(" APPLE = %i\n", APPLE); +printf(" ORANGE = %i\n", ORANGE); +printf(" LEMON = %i\n", LEMON); + +scilabconst_enum_test(APPLE); +scilabconst_enum_test(ORANGE); +scilabconst_enum_test(LEMON); +scilabconst_enum_test(int32(1234)); + +exit diff --git a/Examples/scilab/enum/scilabconst_example.h b/Examples/scilab/enum/scilabconst_example.h new file mode 100644 index 000000000..92dcaaf61 --- /dev/null +++ b/Examples/scilab/enum/scilabconst_example.h @@ -0,0 +1,3 @@ +typedef enum { APPLE, ORANGE, LEMON } fruit; + +void scilabconst_enum_test(fruit f); From 1ab2bd15b17fa54cfe824e87e6e73e3d18cacb29 Mon Sep 17 00:00:00 2001 From: Simon Marchetto Date: Fri, 30 Aug 2013 16:46:45 +0200 Subject: [PATCH 8/8] Scilab: clean enum management code (no need to force enum type) --- Source/Modules/scilab.cxx | 3 --- 1 file changed, 3 deletions(-) diff --git a/Source/Modules/scilab.cxx b/Source/Modules/scilab.cxx index 3bcbdd0c7..89d39d357 100644 --- a/Source/Modules/scilab.cxx +++ b/Source/Modules/scilab.cxx @@ -659,9 +659,6 @@ public: } } - /* Force type to be an enum (See scitypemaps.swg) */ - Setattr(node, "type", "enum SWIG"); - return Language::enumvalueDeclaration(node); }