Scilab: rewrite STL vector example (vector argument passing example)

Fixes:
- use generic support of vectors
- add vector of object example
- improve example messages
- simplify cpp code
- move example code to "std_vector" folder
This commit is contained in:
Simon Marchetto 2013-07-17 16:54:20 +02:00
commit 2472fe492a
9 changed files with 211 additions and 143 deletions

View file

@ -1,16 +1,20 @@
TOP = ../..
TOP = ../../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.cpp
TARGET = example
INTERFACE = example.i
all:
all: run
loader.sce:
$(MAKE) -f $(TOP)/Makefile SRCS='$(SRCS)' SWIG='$(SWIG)' \
TARGET='$(TARGET)' INTERFACE='$(INTERFACE)' scilab_cpp
clean:
$(MAKE) -f $(TOP)/Makefile scilab_clean
rm -f *.sce *.so lib*lib.c *_wrap.cxx
check: all
run: loader.sce
$(MAKE) -f $(TOP)/Makefile scilab_run
debug: loader.sce
$(MAKE) -f $(TOP)/Makefile scilab_debug

View file

@ -0,0 +1,93 @@
/* 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);
}
// pointer (on objects) vectors
std::vector<classA*> create_classA_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_classA_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_classA_vector(const std::vector<classA*> vector, const std::vector<classA*> other_vector)
{
return concat_vector<classA*>(vector, other_vector);
}

View file

@ -0,0 +1,33 @@
/* 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);
// pointer (on objects) vectors
class classA
{
public:
classA() : a(0) {}
classA(int _a) : a(_a) {}
int a;
};
std::vector<classA*> create_classA_vector(const int size, const int value);
void print_classA_vector(const std::vector<classA*>& pvector);
std::vector<classA*> concat_classA_vector(const std::vector<classA*> vector, const std::vector<classA*> other_vector);

View file

@ -0,0 +1,20 @@
/* 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(ClassAVector) vector<classA*>;
}
%include "example.hxx"

View file

@ -0,0 +1,57 @@
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);
// pointer (on object) vectors
disp("Example of passing list of objects as vector arguments of C++ functions.");
disp("get a vector of objects {<classA a:1>, <classA a:1>, <classA a:1>} with create_classA_vector():");
pv = create_classA_vector(3, 1);
print_classA_vector(pv);
pv2 = create_classA_vector(2, 5);
disp("concat this vector with the vector of objects {<classA a:5>, <classA a:5>} with concat_classA_vector():");
pv3 = concat_classA_vector(pv, pv2);
print_classA_vector(pv3);

View file

@ -1,69 +0,0 @@
/* File : example.cpp */
#include "example.hxx"
#include <iterator>
#include <iostream>
#include <algorithm>
// double vectors
std::vector<double> create_dvector(const int size, const double value)
{
return std::vector<double>(size, value);
}
double sum_dvector(const std::vector<double> in_dvector)
{
double sum = 0;
for (int i = 0; i < in_dvector.size(); i++)
{
sum += in_dvector[i];
}
return sum;
}
void concat_dvector(std::vector<double>& inout_dvector, const std::vector<double>& in_dvector)
{
inout_dvector.insert(inout_dvector.end(), in_dvector.begin(), in_dvector.end());
}
// int vectors
std::vector<int> create_ivector(const int size, const int value)
{
return std::vector<int>(size, value);
}
int sum_ivector(const std::vector<int> in_ivector)
{
int sum = 0;
for (int i = 0; i < in_ivector.size(); i++)
{
sum += in_ivector[i];
}
return sum;
}
void concat_ivector(std::vector<int>& inout_ivector, const std::vector<int>& in_ivector)
{
inout_ivector.insert(inout_ivector.end(), in_ivector.begin(), in_ivector.end());
}
// string vectors
std::vector<std::string> create_svector(const int size, const char* value)
{
return std::vector<std::string>(size, value);
}
void print_svector(const std::vector<std::string> in_svector)
{
std::copy(in_svector.begin(), in_svector.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
}
void concat_svector(std::vector<std::string>& inout_svector, const std::vector<std::string>& in_svector)
{
inout_svector.insert(inout_svector.end(), in_svector.begin(), in_svector.end());
}

View file

@ -1,18 +0,0 @@
/* File : example.hxx */
#include <vector>
#include <string>
std::vector<double> create_dvector(const int size, const double value);
double sum_dvector(const std::vector<double> in_vector);
void concat_dvector(std::vector<double>& inout_dvector, const std::vector<double>& in_dvector);
std::vector<int> create_ivector(const int size, const int value);
int sum_ivector(const std::vector<int> in_ivector);
void concat_ivector(std::vector<int>& inout_ivector, const std::vector<int>& in_ivector);
std::vector<std::string> create_svector(const int size, const char* value);
void print_svector(const std::vector<std::string> in_svector);
void concat_svector(std::vector<std::string>& inout_svector, const std::vector<std::string>& in_svector);

View file

@ -1,15 +0,0 @@
/* File : example.i */
%module example
%{
#include "example.hxx"
%}
%include "std_vector.i"
%apply std::vector<double> &INOUT { std::vector<double>& inout_dvector };
%apply std::vector<int> &INOUT { std::vector<int>& inout_ivector };
%apply std::vector<std::string> &INOUT { std::vector<std::string>& inout_svector };
%include "example.hxx"

View file

@ -1,37 +0,0 @@
exec loader.sce;
disp("this is an example with vectors of double.");
disp("create the vector of double {2.0, 2.0, 2.0, 2.0}:");
dv = create_dvector(4, 2.0);
disp(dv);
ds = sum_dvector(dv);
disp("sum of this vector elements is:")
disp(ds);
dv2 = create_dvector(2, 5.0);
disp("concat this vector with the vector of double {5.0, 5.0}:");
dv3 = concat_dvector(dv, dv2);
disp(dv3);
disp("now an example with vectors of int.");
disp("create the vector of int {3, 3, 3}:");
iv = create_ivector(3, 3);
disp(iv);
is = sum_ivector(iv);
disp("sum of this vector elements is:");
disp(is);
iv2 = create_ivector(2, 1);
disp("concat this vector with the vector of ints {1, 1}:");
iv3 = concat_ivector(iv, iv2);
disp(iv3);
disp("now an example with vectors of string.");
disp("create the vector of string {''aa'', ''aa''}:");
sv = create_svector(2, "aa");
print_svector(sv);
sv2 = create_svector(2, "bb");
disp("concat this vector with the vector of string {''bb'', ''bb''}:");
sv3 = concat_svector(sv, sv2);
print_svector(sv3);