Scilab: add STL list example

This commit is contained in:
Simon Marchetto 2013-07-19 18:01:18 +02:00
commit ef48783589
5 changed files with 144 additions and 0 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,61 @@
/* File : example.cpp */
#include "example.hxx"
#include <iostream>
#include <iterator>
#include <algorithm>
#include <numeric>
#include <sstream>
template <typename T>
std::list<T> concat_list(const std::list<T> list, const std::list<T> other_list)
{
std::list<T> out_list(list);
out_list.insert(out_list.end(), other_list.begin(), other_list.end());
return out_list;
}
// int lists
std::list<int> create_integer_list(const int rangemin, const int rangemax)
{
std::list<int> out_list;
for (int i = rangemin; i <= rangemax; i++)
{
out_list.push_back(i);
}
return out_list;
}
int sum_integer_list(const std::list<int>& list)
{
return std::accumulate(list.begin(), list.end(), 0);
}
std::list<int> concat_integer_list(const std::list<int> list, const std::list<int> other_list)
{
return concat_list<int>(list, other_list);
}
// string lists
std::list<std::string> create_string_list(const char* svalue)
{
std::list<std::string> out_list;
std::string str(svalue);
std::istringstream iss(str);
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::inserter<std::list<std::string> >(out_list, out_list.begin()));
return out_list;
}
std::list<std::string> concat_string_list(const std::list<std::string> list, const std::list<std::string> other_list)
{
return concat_list<std::string>(list, other_list);
}

View file

@ -0,0 +1,14 @@
/* File : example.hxx */
#include <list>
#include <string>
// integer lists
std::list<int> create_integer_list(const int size, const int value);
int sum_integer_list(const std::list<int>& list);
std::list<int> concat_integer_list(const std::list<int> list, const std::list<int> other_list);
// string lists
std::list<std::string> create_string_list(const char* value);
std::list<std::string> concat_string_list(const std::list<std::string> list, const std::list<std::string> other_list);

View file

@ -0,0 +1,18 @@
/* File : example.i */
%module example
%{
#include "example.hxx"
%}
%include stl.i
/* instantiate the required template specializations */
namespace std
{
%template(IntList) list<int>;
%template(StringList) list<std::string>;
}
%include "example.hxx"

View file

@ -0,0 +1,31 @@
exec loader.sce;
SWIG_Init();
// This example shows how to use C++ fonctions with STL lists arguments
// Here, STL lists are converted from/to Scilab matrices (SWIG_SCILAB_EXTRA_NATIVE_CONTAINERS is not defined)
// integer lists
disp("Example of passing matrices of int as list arguments of C++ functions.");
disp("get a list of int {1...4} from create_integer_list():");
is = create_integer_list(1, 4);
disp(is);
disp("get the sum of this list elements with sum_integer_list():")
sum = sum_integer_list(is);
disp(is);
is2 = create_integer_list(3, 6);
disp("concat this list with the list of int {3...6} with concat_integer_list():");
is3 = concat_integer_list(is, is2);
disp(is3);
// string lists
disp("Example of passing matrices of string as list arguments of C++ functions.");
disp("get a list of string {''aa'', ''bb'', ''cc'', ''dd''} with create_string_list():");
ss = create_string_list("aa bb cc dd");
disp(ss);
ss2 = create_string_list("cc dd ee ff");
disp("concat this list with the list of string {''cc'', ''dd'', ''ee'', ''ff''} with concat_string_list():");
ss3 = concat_string_list(ss, ss2);
disp(ss3);