Scilab: std_vector_as_argument example converted to test

This commit is contained in:
Simon Marchetto 2013-09-06 18:34:57 +02:00
commit 84f1e3d3e2
7 changed files with 136 additions and 189 deletions

View file

@ -1,15 +0,0 @@
TOP = ../../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.cpp
TARGET = example_wrap.cxx
INTERFACE = example.i
check: build
$(MAKE) -f $(TOP)/Makefile scilab_run
build:
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean:
$(MAKE) -f $(TOP)/Makefile scilab_clean

View file

@ -1,105 +0,0 @@
/* File : example.cpp */
#include "example.hxx"
#include <iostream>
#include <iterator>
#include <algorithm>
#include <numeric>
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 objects) vectors
std::vector<classA*> create_classAPtr_vector(const int size, const int value)
{
std::vector<classA*> out_vector;
for (int i=0; i<size; i++)
{
classA* objA = new classA(value);
out_vector.push_back(objA);
}
return out_vector;
}
void print_classAPtr_vector(const std::vector<classA*>& vector)
{
std::vector<classA*>::const_iterator it;
std::cout << std::endl;
for (it = vector.begin(); it != vector.end(); ++it)
{
std::cout << "<classA* a:" << (*it)->a << ">" << std::endl;
}
}
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 file

@ -1,38 +0,0 @@
/* File : example.hxx */
#include <vector>
#include <string>
// double vectors
std::vector<double> create_double_vector(const int size, const double value);
double sum_double_vector(const std::vector<double>& vector);
std::vector<double> concat_double_vector(const std::vector<double> vector, const std::vector<double> other_vector);
// integer vectors
std::vector<int> create_integer_vector(const int size, const int value);
int sum_integer_vector(const std::vector<int>& vector);
std::vector<int> concat_integer_vector(const std::vector<int> vector, const std::vector<int> other_vector);
// string vectors
std::vector<std::string> create_string_vector(const int size, const char* value);
std::vector<std::string> concat_string_vector(const std::vector<std::string> vector, const std::vector<std::string> other_vector);
// bool vectors
std::vector<bool> create_bool_vector(const int size, const bool value);
std::vector<bool> concat_bool_vector(const std::vector<bool> vector, const std::vector<bool> other_vector);
// pointer (on object) vectors
class classA
{
public:
classA() : a(0) {}
classA(int _a) : a(_a) {}
classA(const classA& c) : a(c.a) {}
int a;
};
std::vector<classA*> create_classAPtr_vector(const int size, const int value);
void print_classAPtr_vector(const std::vector<classA*>& pvector);
std::vector<classA*> concat_classAPtr_vector(const std::vector<classA*> vector, const std::vector<classA*> other_vector);

View file

@ -1,21 +0,0 @@
/* File : example.i */
%module example
%{
#include "example.hxx"
%}
%include stl.i
/* instantiate the required template specializations */
namespace std
{
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
%template(StringVector) vector<std::string>;
%template(BoolVector) vector<bool>;
%template(ClassAPtrVector) vector<classA*>;
}
%include "example.hxx"

View file

@ -1,70 +0,0 @@
lines(0);
exec loader.sce;
SWIG_Init();
// This example shows how to use C++ fonctions with STL vectors arguments
// Here, STL vectors are converted from/to Scilab matrices (SWIG_SCILAB_EXTRA_NATIVE_CONTAINERS is not defined)
// double vectors
disp("Example of passing matrices of double as vector arguments of C++ functions.");
disp("get a vector of double {2.0, 2.0, 2.0, 2.0} from create_double_vector():");
dv = create_double_vector(4, 2.0);
disp(dv);
disp("get the sum of this vector elements with sum_double_vector():")
ds = sum_double_vector(dv);
disp(ds);
dv2 = create_double_vector(2, 5.0);
disp("concat this vector with the vector of double {5.0, 5.0} with concat_double_vector():");
dv3 = concat_double_vector(dv, dv2);
disp(dv3);
// integer vectors
disp("Example of passing matrices of int as vector arguments of C++ functions.");
disp("get a vector of int {3, 3, 3, 3} from create_integer_vector():");
iv = create_integer_vector(3, 3);
disp(iv);
disp("get the sum of this vector elements with sum_integer_vector():")
is = sum_integer_vector(iv);
disp(is);
iv2 = create_integer_vector(2, 1);
disp("concat this vector with the vector of int {1, 1} with concat_integer_vector():");
iv3 = concat_integer_vector(iv, iv2);
disp(iv3);
// string vectors
disp("Example of passing matrices of string as vector arguments of C++ functions.");
disp("get a vector of string {''aa'', ''aa''} with create_string_vector():");
sv = create_string_vector(2, "aa");
disp(sv);
sv2 = create_string_vector(2, "bb");
disp("concat this vector with the vector of string {''bb'', ''bb''} with concat_string_vector():");
sv3 = concat_string_vector(sv, sv2);
disp(sv3);
// bool vectors
disp("Example of passing matrices of bool as vector arguments of C++ functions.");
disp("get a vector of bool {true, true} with create_bool_vector():");
bv = create_bool_vector(2, %T);
disp(bv);
bv2 = create_bool_vector(3, %F);
disp("concat this vector with the vector of bool {false, false, false} with concat_bool_vector():");
bv3 = concat_bool_vector(bv, bv2);
disp(bv3);
// pointer (on object) vectors
disp("Example of passing lists of pointers on object as vector of pointers on objects arguments of C++ functions.");
disp("get a vector of pointers on object {<classA* a:1>, <classA* a:1>, <classA* a:1>} with create_classAPtr_vector():");
pv = create_classAPtr_vector(3, 1);
print_classAPtr_vector(pv);
pv2 = create_classAPtr_vector(2, 5);
disp("concat this vector with the vector of pointers on object {<classA* a:5>, <classA* a:5>} with concat_classAPtr_vector():");
pv3 = concat_classAPtr_vector(pv, pv2);
print_classAPtr_vector(pv3);
exit