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
  • 3 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
1  Examples/test-suite/common.mk
@@ -493,7 +493,6 @@ CPP_STD_TEST_CASES += \
li_std_set_as_argument \
li_std_string \
li_std_vector \
- li_std_vector_as_argument \
li_std_vector_enum \
li_std_vector_member_var\
naturalvar \
View
118 Examples/test-suite/li_std_sequence_container_typemaps.i
@@ -0,0 +1,118 @@
+%module li_std_sequence_container_typemaps
+
+%include stl.i
+
+%{
+#include <vector>
+#include <list>
+#include <deque>
+#include <iostream>
+#include <iterator>
+#include <algorithm>
+#include <numeric>
+%}
+
+%inline %{
+class ClassA
+{
+public:
+ ClassA() : a(0) {}
+ ClassA(int _a) : a(_a) {}
+ ClassA(const ClassA& c) : a(c.a) {}
+ int a;
+};
+
+typedef ClassA* ClassAPtr;
+
+namespace std {
+ template<typename T> T binaryOperation(T x, T y) {
+ return x + y;
+ }
+
+ template<> bool binaryOperation(bool x, bool y) {
+ return x | y;
+ }
+
+ template<> ClassAPtr binaryOperation(ClassAPtr x, ClassAPtr y) {
+ x->a += y->a;
+ return x;
+ }
+
+ template<typename SeqCont>
+ struct sequence_container {
+ typedef typename SeqCont::value_type value_type;
+
+ static SeqCont ret_container(const int size, const value_type value) {
+ return SeqCont(size, value);
+ }
+
+ static value_type val_container(const SeqCont container) {
+ return std::accumulate(container.begin(), container.end(), container.front(),
+ binaryOperation<value_type>);
+ }
+
+ static value_type ref_container(const SeqCont& container) {
+ return std::accumulate(container.begin(), container.end(), container.front(),
+ binaryOperation<value_type>);
+ }
+
+ /*SeqCont ret_val_containers(const SeqCont container,
+ const SeqCont other_container) {
+ SeqCont out_container(container);
+ out_container.insert(out_container.end(), other_container.begin(),
+ other_container.end());
+ return out_container;
+ }*/
+ };
+
+ template<typename T>
+ std::vector<T> ret_vector(const int size, const T value) {
+ return sequence_container<std::vector<T> >::ret_container(size, value);
+ }
+ template<typename T>
+ T val_vector(const std::vector<T> container) {
+ return sequence_container<std::vector<T> >::val_container(container);
+ }
+ template<typename T>
+ T ref_vector(const std::vector<T>& container) {
+ return sequence_container<std::vector<T> >::ref_container(container);
+ }
+
+ template<typename T>
+ std::list<T> ret_list(const int size, const T value) {
+ return sequence_container<std::list<T> >::ret_container(size, value);
+ }
+ template<typename T>
+ T val_list(const std::list<T> container) {
+ return sequence_container<std::list<T> >::val_container(container);
+ }
+ template<typename T>
+ T ref_list(const std::list<T>& container) {
+ return sequence_container<std::list<T> >::ref_container(container);
+ }
+}
+%}
+
+%define instantiate_containers_templates(TYPE...)
+namespace std
+{
+ %template(TYPE ##_## vector) std::vector<TYPE >;
+ %template(ret_ ## TYPE ##_## vector) ret_vector<TYPE>;
+ %template(val_ ## TYPE ##_## vector) val_vector<TYPE>;
+ %template(ref_ ## TYPE ##_## vector) ref_vector<TYPE>;
+
+ %template(TYPE ##_## list) std::list<TYPE >;
+ %template(ret_ ## TYPE ##_## list) ret_list<TYPE>;
+ %template(val_ ## TYPE ##_## list) val_list<TYPE>;
+ %template(ref_ ## TYPE ##_## list) ref_list<TYPE>;
+}
+%enddef
+
+
+instantiate_containers_templates(int)
+instantiate_containers_templates(double)
+instantiate_containers_templates(bool)
+instantiate_containers_templates(string)
+instantiate_containers_templates(ClassAPtr)
+
+
View
115 Examples/test-suite/li_std_vector_as_argument.i
@@ -1,115 +0,0 @@
-%module li_std_vector_as_argument
-
-%{
-#include <vector>
-#include <iostream>
-#include <iterator>
-#include <algorithm>
-#include <numeric>
-
-%}
-
-%{
-class classA
-{
-public:
- classA() : a(0) {}
- classA(int _a) : a(_a) {}
- classA(const classA& c) : a(c.a) {}
- int a;
-};
-%}
-
-%include stl.i
-
-namespace std
-{
- %template(IntVector) vector<int>;
- %template(DoubleVector) vector<double>;
- %template(StringVector) vector<std::string>;
- %template(BoolVector) vector<bool>;
- %template(ClassAPtrVector) vector<classA*>;
-}
-
-%ignore concat_vector;
-
-class classA
-{
-public:
- classA() : a(0) {}
- classA(int _a) : a(_a) {}
- classA(const classA& c) : a(c.a) {}
- int a;
-};
-
-%inline %{
-template <typename T>
-std::vector<T> concat_vector(const std::vector<T> vector, const std::vector<T> other_vector) {
- std::vector<T> out_vector(vector);
- out_vector.insert(out_vector.end(), other_vector.begin(), other_vector.end());
- return out_vector;
-}
-
-// double vectors
-
-std::vector<double> create_double_vector(const int size, const double value) {
- return std::vector<double>(size, value);
-}
-
-double sum_double_vector(const std::vector<double>& vector) {
- return std::accumulate(vector.begin(), vector.end(), 0);
-}
-
-std::vector<double> concat_double_vector(const std::vector<double> vector, const std::vector<double> other_vector) {
- return concat_vector<double>(vector, other_vector);
-}
-
-// int vectors
-
-std::vector<int> create_integer_vector(const int size, const int value) {
- return std::vector<int>(size, value);
-}
-
-int sum_integer_vector(const std::vector<int>& vector) {
- return std::accumulate(vector.begin(), vector.end(), 0);
-}
-
-std::vector<int> concat_integer_vector(const std::vector<int> vector, const std::vector<int> other_vector) {
- return concat_vector<int>(vector, other_vector);
-}
-
-// string vectors
-
-std::vector<std::string> create_string_vector(const int size, const char *value) {
- return std::vector<std::string>(size, value);
-}
-
-std::vector<std::string> concat_string_vector(const std::vector<std::string> vector, const std::vector<std::string> other_vector) {
- return concat_vector<std::string>(vector, other_vector);
-}
-
-// bool vectors
-
-std::vector<bool> create_bool_vector(const int size, const bool value) {
- return std::vector<bool>(size, value);
-}
-
-std::vector<bool> concat_bool_vector(const std::vector<bool> vector, const std::vector<bool> other_vector) {
- return concat_vector<bool>(vector, other_vector);
-}
-
-// pointer (on object) vectors
-
-std::vector<classA*> create_classAPtr_vector(const int size, classA *aClassAPtr) {
- std::vector<classA*> out_vector;
- for (int i=0; i<size; i++) {
- out_vector.push_back(aClassAPtr);
- }
- return out_vector;
-}
-
-std::vector<classA*> concat_classAPtr_vector(const std::vector<classA*> vector, const std::vector<classA*> other_vector) {
- return concat_vector<classA*>(vector, other_vector);
-}
-%}
-
View
1  Examples/test-suite/scilab/Makefile.in
@@ -17,6 +17,7 @@ C_TEST_CASES += \
CPP_STD_TEST_CASES += \
primitive_types \
inout \
+ li_std_sequence_container_typemaps \
TEST_DIR = $*.dir
RUNME_SCRIPT = $(srcdir)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX)
View
101 Examples/test-suite/scilab/li_std_sequence_container_typemaps_runme.sci
@@ -0,0 +1,101 @@
+// test STL sequence containers typemaps
+
+exec("swigtest.start", -1);
+
+// test sequence containers of pointers
+// -container: type of container: "vector", "list"...
+// -value, count: value to store count times in container
+// -expected_accumulate_value: expected value of an accumulation function
+// computed on the container
+function testSequenceContainerPtr(container, count, value, expected_accumulate_value)
+ // test sequence container returned from fonction (expected a list)
+ classAPtr = new_ClassA(value);
+ cmd = msprintf("l = ret_ClassAPtr_%s(count, classAPtr);", container);
+ ierr = execstr(cmd, "errcatch");
+ if ierr <> 0 then swigtesterror(); end
+ if ~exists('l') | (size(l) <> count) | (ClassA_a_get(l(1)) <> value) then swigtesterror(); end
+ l2 = l;
+
+ // test sequence container passed as value of function
+ cmd = msprintf("classAPtr = val_ClassAPtr_%s(l);", container);
+ ierr = execstr(cmd, "errcatch");
+ if ierr <> 0 then swigtesterror(); end
+ if ClassA_a_get(classAPtr) <> expected_accumulate_value then swigtesterror(); end
+
+ // recreate a container
+ classAPtr = new_ClassA(value);
+ cmd = msprintf("l = ret_ClassAPtr_%s(count, classAPtr);", container);
+ ierr = execstr(cmd, "errcatch");
+ if ierr <> 0 then swigtesterror(); end
+ if ~exists('l') | (size(l) <> count) | (ClassA_a_get(l(1)) <> value) then swigtesterror(); end
+
+ // test sequence container passed as refererence of function
+ cmd = msprintf("classAPtr = ref_ClassAPtr_%s(l);", container);
+ ierr = execstr(cmd, "errcatch");
+ if ierr <> 0 then swigtesterror(); end
+ if ClassA_a_get(classAPtr) <> expected_accumulate_value then swigtesterror(); end
+endfunction
+
+// test sequence containers of primitive type
+// -container: type of container: "vector", "list"...
+// -value_type: type of element stored in container: "int", ...
+// -value, count: value to store count times in container
+// -expected_accumulate_value: expected value of an accumulation function
+// computed on the container
+function testSequenceContainer(container, value_type, value, count, expected_accumulate_value)
+ // test sequence container returned from fonction (expect a row matrix)
+ if value_type = "string"
+ cmd = msprintf("c = ret_%s_%s(count, ''%s'');", value_type, container, value);
+ elseif value_type = "bool" then
+ cmd = msprintf("c = ret_%s_%s(count, %s);", value_type, container, "%"+string(value));
+ else
+ cmd = msprintf("c = ret_%s_%s(count, %d);", value_type, container, value);
+ end
+ ierr = execstr(cmd, "errcatch");
+ if ierr <> 0 then swigtesterror(); end
+ if ~isdef('c') | c <> repmat(value, 1, count) then swigtesterror(); end
+
+ // test sequence container passed as value of function
+ cmd = msprintf("s = val_%s_%s(c);", value_type, container);
+ ierr = execstr(cmd, "errcatch");
+ if ierr <> 0 then swigtesterror(); end
+ if s <> expected_accumulate_value then swigtesterror(); end
+
+ // test sequence container passed as matrix as value of function
+ //m = repmat(value, 1, count);
+ //cmd = msprintf("s = val_%s_%s(m);", value_type, container);
+ //ierr = execstr(cmd, "errcatch");
+ //if ierr <> 0 then swigtesterror(); end
+ //if s <> expected_accumulate_value then swigtesterror(); end
+
+ // test sequence container passed as reference of function
+ cmd = msprintf("s = ref_%s_%s(c);", value_type, container);
+ ierr = execstr(cmd, "errcatch");
+ if ierr <> 0 then swigtesterror(); end
+ if s <> expected_accumulate_value then swigtesterror(); end
+
+ // test sequence container passed as matrix as reference of function
+ //m = repmat(value, 1, count);
+ //cmd = msprintf("s = val_%s_%s(m);", value_type, container);
+ //ierr = execstr(cmd, "errcatch");
+ //if ierr <> 0 then swigtesterror(); end
+ //if s <> expected_accumulate_value then swigtesterror(); end
+endfunction
+
+// test vector
+testSequenceContainer("vector", "int", 2, 4, 10);
+testSequenceContainer("vector", "double", 2., 3., 8.);
+testSequenceContainer("vector", "string", "a", 4, "aaaaa");
+testSequenceContainer("vector", "bool", %T, 2, %T);
+testSequenceContainerPtr("vector", 1, 3, 6.0);
+
+// test list
+testSequenceContainer("list", "int", 2, 3, 8);
+testSequenceContainer("list", "double", 2., 4., 10.);
+testSequenceContainer("list", "string", "a", 4, "aaaaa");
+testSequenceContainer("list", "bool", %T, 2, %T);
+testSequenceContainerPtr("list", 1, 3, 6.0);
+
+exec("swigtest.quit", -1);
+
+
View
78 Examples/test-suite/scilab/li_std_vector_as_argument_runme.sci
@@ -1,78 +0,0 @@
-// Tests C++ fonctions with STL vectors as arguments
-
-exec("swigtest.start", -1);
-
-// double vectors
-
-// Test of using vector<double> arguments of C++ functions
-// Get a vector of double {2.0, 2.0, 2.0, 2.0} from create_double_vector()
-dv = create_double_vector(4, 2.0);
-if ~exists("dv") | (dv <> [2. 2. 2. 2.]) then swigtesterror(); end
-// Get sum this vector elements with sum_double_vector()
-ds = sum_double_vector(dv);
-if ~exists("ds") | (ds <> 8.) then swigtesterror(); end
-// Get a vector of double {5.0, 5.0} from create_double_vector()
-dv2 = create_double_vector(2, 5.0);
-if ~exists("dv2") | (dv2 <> [5. 5.]) then swigtesterror(); end
-// Concat the two vectors with concat_double_vector()
-dv3 = concat_double_vector(dv, dv2);
-if ~exists("dv3") | (dv3 <> [dv dv2]) then swigtesterror(); end
-
-// integer vectors
-
-// Test of using vector<int> arguments of C++ functions.");
-// Get a vector of int {3, 3, 3, 3} from create_integer_vector()
-iv = create_integer_vector(3, 3);
-if ~exists("iv") | (iv <> int32([3 3 3])) then swigtesterror(); end
-// Get the sum of this vector elements with sum_integer_vector()
-is = sum_integer_vector(iv);
-if ~exists("is") | (is <> int32(9)) then swigtesterror(); end
-// Get a vector of int {1, 1} from create_integer_vector()
-iv2 = create_integer_vector(2, 1);
-// Concat the two vectors with concat_integer_vector()
-iv3 = concat_integer_vector(iv, iv2);
-if ~exists("iv3") | (iv3 <> int32([iv iv2])) then swigtesterror(); end
-
-// std::string vectors
-
-// Test of using vector<std::string> arguments of C++ functions.
-// Get a vector of string {''aa'', ''aa''} with create_string_vector()
-sv = create_string_vector(2, "aa");
-if ~exists("sv") | (sv <> ["aa"; "aa"]) then swigtesterror(); end
-// Get a vector of string {''bb'', ''bb''} with create_string_vector()
-sv2 = create_string_vector(2, "bb");
-if ~exists("sv2") | (sv2 <> ["bb"; "bb"]) then swigtesterror(); end
-// Concat the two vectors with concat_string_vector()
-sv3 = concat_string_vector(sv, sv2);
-if ~exists("sv3") | (sv3 <> [sv; sv2]) then swigtesterror(); end
-
-// bool vectors
-
-// Test of using vector<bool> arguments of C++ functions.
-// Get a vector of bool {true, true} with create_bool_vector()
-bv = create_bool_vector(2, %T);
-if ~exists("bv") | (bv <> [%T %T]) then swigtesterror(); end
-// Get a vector of bool {false, false, false} with create_bool_vector()
-bv2 = create_bool_vector(3, %F);
-if ~exists("bv2") | (bv2 <> [%F %F %F]) then swigtesterror(); end
-// Concat the two vectors with concat_bool_vector()
-bv3 = concat_bool_vector(bv, bv2);
-if ~exists("bv3") | (bv3 <> [bv bv2]) then swigtesterror(); end
-
-// Pointer (on object) vectors
-
-// Test of using vector of pointers (on object) arguments of C++ functions.
-// Get a vector of pointers on object {<classA* a:10>, <classA* a:10>, <classA* a:10>} with create_classAPtr_vector()
-classA_1 = new_classA(10);
-pv = create_classAPtr_vector(3, classA_1);
-if ~exists("pv") | (size(pv) <> 3) | (classA_a_get(pv(1)) <> 10) then swigtesterror(); end
-// Get a vector of pointers on object {<classA* a:5>, <classA* a:5>} with create_classAPtr_vector()
-classA_2 = new_classA(5);
-pv2 = create_classAPtr_vector(2, classA_2);
-if ~exists("pv2") | (size(pv2) <> 2) | (classA_a_get(pv2(1)) <> 5) then swigtesterror(); end
-// Concat the two vectors with concat_classAPtr_vector()
-pv3 = concat_classAPtr_vector(pv, pv2);
-if ~exists("pv3") | (size(pv3) <> 5) | (classA_a_get(pv3(1)) <> 10) | (classA_a_get(pv3(4)) <> 5) then swigtesterror(); end
-
-exec("swigtest.quit", -1);
-
View
2  Lib/scilab/scichar.swg
@@ -237,7 +237,7 @@ SwigScilabStringFromCharPtrArray(void *_pvApiCtx, int _iVarOut, char **_charPtrA
return SWIG_ERROR;
}
- sciErr = createMatrixOfString(_pvApiCtx, SWIG_NbInputArgument(pvApiCtx) + _iVarOut, _charPtrArraySize, 1, _charPtrArray);
+ sciErr = createMatrixOfString(_pvApiCtx, SWIG_NbInputArgument(pvApiCtx) + _iVarOut, 1, _charPtrArraySize, _charPtrArray);
if (sciErr.iErr) {
printError(&sciErr, 0);
return SWIG_ERROR;

No commit comments for this range

Something went wrong with that request. Please try again.