Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: swig/swig
...
head fork: swig/swig
Checking mergeability… Don't worry, you can still create the pull request.
  • 5 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
View
92 Doc/Manual/Scilab.html
@@ -408,9 +408,8 @@
<H3><a name="Scilab_wrapping_constants"></a>37.3.7 Constants</H3>
-
<p>
- C constants are not really constant in Scilab. When dealing with the constants, a get function will be generated. For example given some constants:
+There is no constant in Scilab. By default, C/C++ constants are wrapped as getter functions. For example for the following constants:
</p>
<div class="code"><pre>
@@ -423,7 +422,9 @@
#define SCONST2 "\"Hello World\""
</pre></div>
-<p>It is easy to use them in Scilab:</p>
+<p>
+The following getter functions are generated:
+</p>
<div class="targetlang"><pre>
--&gt; exec loader.sce;
@@ -448,33 +449,98 @@
ans= 3.14
</pre></div>
-<H3><a name="Scilab_wrapping_enums"></a>37.3.8 Enums</H3>
+<p>
+There is another mode in which constants are wrapped as Scilab variables.
+The variables are easier to use than functions, but the little drawback is that variables are not constant and so can be modified.
+This mode can be enabled/disabled at any time in the interface file with the feature <tt>%scilabconst()</tt> (argument value "1" to enable, "0" to disable).
+For example in this mode the previous constants:
+</p>
+<div class="code"><pre>
+%module example
-<p> The way SWIG deals with the enums is similar to constants. For example:
+%scilabconst(1);
+#define ICONST 42
+#define FCONST 2.1828
+#define CCONST 'x'
+#define CCONST2 '\n'
+#define SCONST "Hello World"
+#define SCONST2 "\"Hello World\""
+</pre></div>
+
+<p>
+Are mapped to Scilab variables, with the same name:
</p>
-<div class="code"><pre>%module example
-typedef enum { RED, BLUE, GREEN } color;
+<div class="targetlang"><pre>
+--&gt; exec loader.sce;
+--&gt; ICONST;
+ans= 42
+--&gt; FCONST;
+ans= 2.1828
+--&gt; CCONST;
+ans=x
+--&gt; CCONST2;
+ans=
+
+--&gt; SCONST;
+ans= Hello World
+--&gt; SCONST2;
+ans= "Hello World"
+--&gt; EXPR;
+ans= 48.5484
+--&gt; iconst;
+ans= 37
+--&gt; fconst;
+ans= 3.14
</pre></div>
+<H3><a name="Scilab_wrapping_enums"></a>37.3.8 Enums</H3>
+
<p>
- Some code like RED_get(), BLUE_get(),GREEN_get() will be generated. It can be used as the following:
+The wrapping of enums is quite the same as for constants.
+In the default mode, the enums are wrapped as getter functions.
+For example on the following enumeration:
</p>
+<div class="code"><pre>%module example
+typedef enum { RED, BLUE, GREEN } color;
+</pre></div>
+
+<p>
+A getter function will be generated for each value of the enumeration:
+</p>
<div class="targetlang"><pre>
--&gt; exec loader.sce;
--&gt; printf(" RED = %i\n", RED_get());
- RED = 0
-
+ RED = 0.
--&gt; printf(" BLUE = %i\n", BLUE_get());
- BLUE = 1
-
+ BLUE = 1.
--&gt; printf(" GREEN = %i\n", GREEN_get());
- GREEN = 2
+ GREEN = 2.
+</pre></div>
+
+<p>
+The feature <tt>%scilabconst()</tt> is also available for enumerations:
+</p>
+
+<div class="code"><pre>%module example
+%scilabconst(1);
+typedef enum { RED, BLUE, GREEN } color;
</pre></div>
+<p>
+<div class="targetlang"><pre>
+--&gt; exec loader.sce;
+--&gt; printf(" RED = %i\n", RED);
+ RED = 0.
+--&gt; printf(" BLUE = %i\n", BLUE);
+ BLUE = 1.
+--&gt; printf(" GREEN = %i\n", GREEN);
+ GREEN = 2.
+</pre></div>
+</p>
<H3><a name="Scilab_wrapping_pointers"></a>37.3.9 Pointers</H3>
View
28 Examples/scilab/constants/example.i
@@ -1,29 +1,17 @@
/* File : example.i */
%module example
-/* Forces to wrap constants as Scilab variables (instead of functions) */
+/* Wraps constants as Scilab variables (instead of getter functions) */
%scilabconst(1);
-#define ICONST 42
-#define FCONST 2.1828
-#define CCONST 'x'
-#define CCONST2 '\n'
-#define SCONST "Hello World"
-#define SCONST2 "\"Hello World\""
+#define ICONST 42
+#define FCONST 2.1828
+#define SCONST "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 */
+// Constants expressions are also accepted
+#define EXPR ICONST + 3*FCONST
+// SWIG also offers to define constants
%constant int iconst = 37;
-%constant double fconst = 3.14;
-
-
+%constant double fconst = 42.2;
View
16 Examples/scilab/constants/runme.sci
@@ -6,23 +6,9 @@ example_Init();
printf("\nConstants are wrapped by functions:\n");
printf("ICONST = %i (should be 42)\n", ICONST);
printf("FCONST = %5.4f (should be 2.1828)\n", FCONST);
-printf("CCONST = ''%c'' (should be ''x'')\n", CCONST);
-printf("CCONST2 = %s (this should be on a new line)\n", CCONST2);
printf("SCONST = ''%s'' (should be ''Hello World'')\n", SCONST);
-printf("SCONST2 = ''%s'' (should be "'""Hello World"""')\n", SCONST2);
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);
-
-try
- printf("EXTERN = %s (Arg! This should not printf(anything)\n", EXTERN);
-catch
- printf("EXTERN is not defined (good)\n");
-end
-try
- printf("FOO = %i (Arg! This should not printf(anything)\n", FOO);
-catch
- printf("FOO is not defined (good)\n");
-end
+printf("fconst = %3.2f (should be 42.20)\n", fconst);
exit
View
3  Examples/test-suite/scilab/Makefile.in
@@ -11,7 +11,8 @@ top_srcdir = $(abspath @top_srcdir@)
top_builddir = $(abspath @top_builddir@)
C_TEST_CASES += \
- scilab_enums
+ scilab_enums \
+ scilab_consts \
CPP_STD_TEST_CASES += \
primitive_types \
View
20 Examples/test-suite/scilab/bools_runme.sci
@@ -0,0 +1,20 @@
+exec("swigtest.start", -1);
+
+function checkBool(bool_val, expected_bool_val)
+ if typeof(bool_val) <> "boolean" then swigtesterror(); end
+ if bool_val <> expected_bool_val then swigtesterror(); end
+endfunction
+
+checkBool(constbool_get(), %f);
+
+checkBool(bool1_get(), %t);
+checkBool(bool2_get(), %f);
+
+checkBool(bo(%t), %t);
+checkBool(bo(%f), %f);
+
+bs = new_BoolStructure();
+checkBool(BoolStructure_m_bool1_get(bs), %t);
+checkBool(BoolStructure_m_bool2_get(bs), %f);
+
+exec("swigtest.quit", -1);
View
32 Examples/test-suite/scilab/scilab_consts_runme.sci
@@ -0,0 +1,32 @@
+exec("swigtest.start", -1);
+
+function checkConst(const_val, expected_type, expected_const_val)
+ if typeof(const_val) <> expected_type then swigtesterror(); end
+ if const_val <> expected_const_val then swigtesterror(); end
+endfunction
+
+checkConst(ICONST0_get(), "constant", 42);
+checkConst(FCONST0_get(), "constant", 2.1828);
+checkConst(CCONST0_get(), "string", "x");
+//checkConst(CCONST0_2_get(), "string", "\n");
+checkConst(SCONST0_get(), "string", "Hello World");
+checkConst(SCONST0_2_get(), "string", """Hello World""");
+checkConst(EXPR0_get(), "constant", 48.5484);
+checkConst(iconst0_get(), "constant", 37);
+checkConst(fconst0_get(), "constant", 42.2);
+
+if isdef('BAR0') then swigtesterror(); end
+
+checkConst(ICONST1, "int32", 42);
+checkConst(FCONST1, "constant", 2.1828);
+checkConst(CCONST1, "string", "x");
+//checkConst(CCONST1_2, "string", "\n");
+checkConst(SCONST1, "string", "Hello World");
+checkConst(SCONST1_2, "string", """Hello World""");
+checkConst(EXPR1, "constant", 48.5484);
+checkConst(iconst0_get(), "constant", 37);
+checkConst(fconst0_get(), "constant", 42.2);
+
+if isdef('BAR1') then swigtesterror(); end
+
+exec("swigtest.quit", -1);
View
16 Examples/test-suite/scilab/struct_value_runme.sci
@@ -0,0 +1,16 @@
+exec("swigtest.start", -1);
+
+foo = new_Foo();
+Foo_x_set(foo, 1);
+if Foo_x_get(foo) <> 1 then swigtesterror(); end
+
+bar = new_Bar();
+Bar_a_set(bar, foo);
+a = Bar_a_get(bar);
+if Foo_x_get(a) <> 1 then swigtesterror(); end
+
+Bar_b_set(bar, foo);
+b = Bar_b_get(bar);
+if Foo_x_get(b) <> 1 then swigtesterror(); end
+
+exec("swigtest.quit", -1);
View
42 Examples/test-suite/scilab_consts.i
@@ -0,0 +1,42 @@
+%module scilab_consts
+
+/* Default mode: constants are wrapped as getter functions */
+%scilabconst(0);
+
+#define ICONST0 42
+#define FCONST0 2.1828
+#define CCONST0 'x'
+#define CCONST0_2 '\n'
+#define SCONST0 "Hello World"
+#define SCONST0_2 "\"Hello World\""
+
+/* Expressions should work too */
+#define EXPR0 ICONST0 + 3*FCONST0
+
+/* This shouldn't do anything, bar is not defined */
+#define BAR0 bar
+
+/* SWIG directive %constant produces constants too */
+%constant int iconst0 = 37;
+%constant double fconst0 = 42.2;
+
+
+/* Alternative mode: constants are wrapped as variables */
+%scilabconst(1);
+
+#define ICONST1 42
+#define FCONST1 2.1828
+#define CCONST1 'x'
+#define CCONST1_2 '\n'
+#define SCONST1 "Hello World"
+#define SCONST1_2 "\"Hello World\""
+
+/* Expressions should work too */
+#define EXPR1 ICONST1 + 3*FCONST1
+
+/* This shouldn't do anything, bar is not defined */
+#define BAR1 bar
+
+/* SWIG directive %constant produces constants too */
+%constant int iconst1 = 37;
+%constant double fconst1 = 42.2;

No commit comments for this range

Something went wrong with that request. Please try again.