Fixed a bug with argout values being incorrectly

returned when the first argout/returned element
was an array.

Made std::pair and container returned elements be
frozen (like tuples in python).

Added the std_containers.i test from python and
created a ruby runme file for it.




git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9746 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
Gonzalo Garramuno 2007-05-01 22:40:55 +00:00
commit 1b349d4749
6 changed files with 360 additions and 4 deletions

View file

@ -1,6 +1,14 @@
Version 1.3.32 (in progress)
============================
05/07/2007: gga
[Ruby]
Docstrings are now supported.
%feature("autodoc") and %feature("docstring") are now
properly supported in Ruby. These features will generate
a _wrap.cxx file with rdoc comments in them.
TODO: add doxygen -> rdoc conversion support.
05/05/2007: gga
[Ruby]
STL files have been upgraded to follow the new swig/python
@ -10,8 +18,8 @@ Version 1.3.32 (in progress)
supported, including their iterators, support for containing
ruby objects (swig::GC_VALUE) and several other ruby
enhancements.
std::ios, std::iostream, std::iostreambuf and std::sstream
are now also supported.
std::complex, std::ios, std::iostream, std::iostreambuf and
std::sstream are now also supported.
std::wstring, std::wios, std::wiostream, std::wiostreambuf
and std::wsstream are supported verbatim with no unicode
conversion.
@ -25,6 +33,12 @@ Version 1.3.32 (in progress)
exceptions. Now, nil is returned instead, following ruby's
standard Array behavior.
05/02/2007: gga
[Ruby]
Fixed a subtle bug in multiple argouts that could get triggered if
the user returned two or more arguments and the first one was an
array.
05/01/2007: gga
[Ruby]
Improved the documentation to document the new features

View file

@ -0,0 +1,119 @@
#!/usr/bin/env ruby
#
# Standard containers test suite. Tests:
# std::complex, std::vector, std::set and std::map,
# and IN/OUT functions for them.
#
# Author:: gga
# Copyright:: 2007
# License:: Ruby
#
require 'swig_assert'
require 'std_containers'
include Std_containers
swig_assert_each_line(<<'EOF', binding)
cube = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]
icube = cident(cube)
icube.each_index { |i| swig_assert_equal("cube[#{i}]","icube[#{i}]", binding, 'cident') }
p = [1,2]
p == pident(p)
v = [1,2,3,4,5,6]
iv = vident(v)
iv.each_index { |i| swig_assert_equal("iv[#{i}]","v[#{i}]", binding, 'vident') }
iv = videntu(v)
iv.each_index { |i| swig_assert_equal("iv[#{i}]","v[#{i}]", binding, 'videntu') }
vu = Vector_ui.new(v)
vu[2] == videntu(vu)[2]
v[0,3][1] == vu[0,3][1]
m = [[1,2,3],[2,3],[3,4]]
im = midenti(m)
0.upto(m.size-1){ |i| 0.upto(m[i].size-1) { |j| swig_assert_equal("m[#{i}][#{j}]","im[#{i}][#{j}]", binding, 'getslice') } }
m = [[1,0,1],[1,1],[1,1]]
im = midentb(m)
0.upto(m.size-1){ |i| 0.upto(m[i].size-1) { |j| swig_assert_equal("(m[#{i}][#{j}]==1)","im[#{i}][#{j}]", binding, 'getslice') } }
mi = Imatrix.new(m)
mc = Cmatrix.new(m)
mi[0][1] == mc[0][1] # or bad matrix
map ={}
map['hello'] = 1
map['hi'] = 2
map['3'] = 2
imap = Std_containers.mapident(map)
map.each_key { |k| swig_assert_equal("map['#{k}']", "imap['#{k}']", binding) }
mapc ={}
c1 = C.new
c2 = C.new
mapc[1] = c1
mapc[2] = c2
mapidentc(mapc)
vi = Vector_i.new([2,2,3,4])
v1 = vi.dup
v1.class == vi.class
v1 != vi
v1.object_id != vi.object_id
v = [1,2]
v1 = v_inout(vi)
vi[1] == v1[1]
# vi.class == v1.class # only if SWIG_RUBY_EXTRA_NATIVE_CONTAINERS was set
v1,v2 = [[1,2],[3,4]]
v1,v2 = v_inout2(v1,v2)
v2 == [1,2]
v1 == [3,4]
a1 = A.new(1)
a2 = A.new(2)
p1 = [1,a1]
p2 = [2,a2]
v = [p1,p2]
v2 = pia_vident(v)
# v2[0][1].a
# v2[1][1].a
# v3 = Std_containers.vector_piA(v2)
# v3[0][1].a
# v3[1][1].a
s = Set_i.new
s.push(1)
s.push(2)
s.push(3)
j = 1
s.each { |i| swig_assert_equal("#{i}","#{j}", binding, "for s[#{i}]"); j += 1 }
EOF

View file

@ -0,0 +1,207 @@
%module std_containers
//
// STL containers test suite.
// Tests:
// std::complex, std::string, std::vector, std::set, std::deque,
// std::pair, std::map, std::multiset, std::multimap and IN/OUT functions
// for them, with C++ types.
//
%{
#include <set>
%}
%include std_vector.i
%include std_string.i
%include std_deque.i
%include std_list.i
%include std_set.i
%include std_multiset.i
%include std_pair.i
%include std_map.i
%include std_multimap.i
%include std_complex.i
%template() std::vector<double>;
%template() std::pair<std::string, int>;
%template() std::pair<int,double>;
%template() std::vector< std::vector<double > > ;
%template(ccube) std::vector< std::vector< std::vector<double > > >;
%inline
{
typedef
std::vector<std::vector<std::vector<double > > >
ccube;
ccube cident(const ccube& c)
{
return c;
}
struct C
{
};
}
%template(map_si) std::map<std::string, int>;
%template(pair_iC) std::pair<int, C*>;
%template(map_iC) std::map<int, C*>;
%template(mmap_si) std::multimap<std::string, int>;
%template(set_i) std::set<int>;
%template(multiset_i) std::multiset<int>;
%template(list_i) std::list<int>;
%template(deque_i) std::deque<int>;
%template(vector_b) std::vector<bool>;
%template(vector_i) std::vector<int>;
%template(vector_c) std::vector<std::complex<double> >;
%template(vector_ui) std::vector<unsigned int>;
%template(bmatrix) std::vector<std::vector<bool> >;
%template(imatrix) std::vector<std::vector<int> >;
%template(cmatrix) std::vector<std::vector<std::complex<double> > >;
%apply std::vector<int> *INOUT {std::vector<int> *INOUT2};
%inline
{
typedef std::vector<std::vector<int> > imatrix;
imatrix midenti(const imatrix& v)
{
return v;
}
typedef std::vector<std::vector<bool> > bmatrix;
bmatrix midentb(const bmatrix& v)
{
return v;
}
std::map<int,C*> mapidentc(const std::map<int,C*>& v)
{
return v;
}
std::map<int,int> mapidenti(const std::map<int,int>& v)
{
return v;
}
std::map<std::string,int> mapident(const std::map<std::string,int>& v)
{
return v;
}
std::multimap<std::string,int> mapident(const std::multimap<std::string,int>& v)
{
return v;
}
std::vector<int> vident(const std::vector<int>& v)
{
return v;
}
std::set<int> sident(const std::set<int>& v)
{
return v;
}
std::vector<unsigned int> videntu(const std::vector<unsigned int>& v)
{
return v;
}
int get_elem(const std::vector<int>& v, int index)
{
return v[index];
}
std::pair<int,double> pident(const std::pair<int,double>& p)
{
return p;
}
void
v_inout(std::vector<int> *INOUT) {
*INOUT = *INOUT;
}
void
v_inout2(std::vector<int> *INOUT, std::vector<int> *INOUT2) {
std::swap(*INOUT, *INOUT2);
}
}
%{
template <class C> struct Param
{
};
%}
template <class C> struct Param
{
};
%template(Param_c) Param<std::complex<double> >;
%inline
{
int hello(Param<std::complex<double> > c)
{
return 0;
}
}
%inline
{
struct A
{
A(int aa = 0) : a(aa)
{
}
int a;
};
}
%template() std::pair<A,int>;
%template(pair_iA) std::pair<int,A>;
%template(vector_piA) std::vector<std::pair<int,A> >;
%inline {
std::pair<A,int> ident(std::pair<int,A> a, const std::pair<int,int>& b)
{
return std::pair<A,int>();
}
std::vector<std::pair<int,A> > pia_vident(std::vector<std::pair<int,A> > a )
{
return a;
}
struct Foo
{
Foo(int i) {
}
};
}
%std_nodefconst_type(Foo);
%template(vector_Foo) std::vector<Foo>;
%template(deque_Foo) std::deque<Foo>;
%template(list_Foo) std::list<Foo>;

View file

@ -1007,6 +1007,7 @@ namespace swig {
RARRAY_PTR(obj)[i] = swig::from<value_type>(*it);
}
RARRAY_LEN(obj) = size;
rb_obj_freeze(obj); // treat as immutable result
return obj;
} else {
rb_raise(rb_eRangeError,"sequence size not valid in ruby");

View file

@ -124,13 +124,15 @@
static VALUE from(const std::pair<T,U>& val) {
VALUE obj = rb_ary_new2(2);
RARRAY_PTR(obj)[0] = swig::from< typename swig::noconst_traits<T >::noconst_type>(val.first);
RARRAY_PTR(obj)[0] = swig::from<
typename swig::noconst_traits<T >::noconst_type>(val.first);
RARRAY_PTR(obj)[1] = swig::from(val.second);
RARRAY_LEN(obj) = 2;
rb_define_singleton_method(obj, "second",
VALUEFUNC(_wrap_pair_second), 0 );
rb_define_singleton_method(obj, "second=",
VALUEFUNC(_wrap_pair_second_eq), 1 );
rb_obj_freeze(obj); // treat as immutable tuple
return obj;
}
};

View file

@ -942,7 +942,7 @@ public:
Replaceall(tm, "$input", Getattr(p, "emit:input"));
Printv(outarg, tm, "\n", NIL);
need_result = 1;
need_result += 1;
p = Getattr(p, "tmap:argout:next");
} else {
p = nextSibling(p);
@ -1239,6 +1239,19 @@ public:
need_result = 1;
// Printf(f->code, "DATA_PTR(self) = result;\n");
}
else
{
if ( need_result > 1 ) {
if ( SwigType_type(t) == T_VOID )
Printf(f->code, "vresult = rb_ary_new();\n");
else
{
Printf(f->code, "if (vresult == Qnil) vresult = rb_ary_new();\n");
Printf(f->code, "else vresult = SWIG_Ruby_AppendOutput( "
"rb_ary_new(), vresult);\n");
}
}
}
/* Dump argument output code; */
Printv(f->code, outarg, NIL);