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

@ -0,0 +1,20 @@
TOP = ../../..
SWIG = $(TOP)/../preinst-swig
SRCS = example.cpp
TARGET = example
INTERFACE = example.i
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
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);