Fixes to std_map and multimap. There's still a problem
of const correctness in the std swig STL library. Need to bring it up in the swig-devel list. Added new functions to swig_assert. Changed some tests to reflect these changes. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9730 626c5289-ae23-0410-ae9c-e8d60b6d4f22
This commit is contained in:
parent
b72d6a93af
commit
a100f16bbd
22 changed files with 366 additions and 135 deletions
|
|
@ -42,10 +42,9 @@ namespace swig {
|
|||
};
|
||||
|
||||
%apply VALUE {GC_VALUE};
|
||||
%apply VALUE const& {GC_VALUE const&};
|
||||
|
||||
/* For input */
|
||||
%typemap(in,noblock=1) GC_VALUE* (GC_VALUE r) {
|
||||
%typemap(in,noblock=1) GC_VALUE* (GC_VALUE r), GC_VALUE& (GC_VALUE r) {
|
||||
r = $input; $1 = &r;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -519,15 +519,14 @@ namespace swig
|
|||
/**** The Ruby container methods ****/
|
||||
|
||||
|
||||
%define %swig_container_methods(Container...)
|
||||
|
||||
%define %swig_container_printing_methods(Container...)
|
||||
|
||||
%extend {
|
||||
|
||||
VALUE inspect()
|
||||
{
|
||||
Container::iterator i = $self->begin();
|
||||
Container::iterator e = $self->end();
|
||||
Container::const_iterator i = $self->begin();
|
||||
Container::const_iterator e = $self->end();
|
||||
VALUE str = rb_str_new2( swig::type_name< Container >() );
|
||||
str = rb_str_cat2( str, " [" );
|
||||
bool comma = false;
|
||||
|
|
@ -535,7 +534,7 @@ namespace swig
|
|||
for ( ; i != e; ++i, comma = true )
|
||||
{
|
||||
if (comma) str = rb_str_cat2( str, "," );
|
||||
tmp = swig::from<Container::value_type>( *i );
|
||||
tmp = swig::from( *i );
|
||||
tmp = rb_obj_as_string( tmp );
|
||||
str = rb_str_buf_append( str, tmp );
|
||||
}
|
||||
|
|
@ -545,13 +544,13 @@ namespace swig
|
|||
|
||||
VALUE to_a()
|
||||
{
|
||||
Container::iterator i = $self->begin();
|
||||
Container::iterator e = $self->end();
|
||||
Container::const_iterator i = $self->begin();
|
||||
Container::const_iterator e = $self->end();
|
||||
VALUE ary = rb_ary_new2( std::distance( i, e ) );
|
||||
VALUE tmp;
|
||||
for ( ; i != e; ++i )
|
||||
{
|
||||
tmp = swig::from<Container::value_type>( *i );
|
||||
tmp = swig::from( *i );
|
||||
rb_ary_push( ary, tmp );
|
||||
}
|
||||
return ary;
|
||||
|
|
@ -565,13 +564,16 @@ namespace swig
|
|||
VALUE tmp;
|
||||
for ( ; i != e; ++i )
|
||||
{
|
||||
tmp = swig::from<Container::value_type>( *i );
|
||||
tmp = swig::from( *i );
|
||||
tmp = rb_obj_as_string( tmp );
|
||||
str = rb_str_buf_append( str, tmp );
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
}
|
||||
%enddef
|
||||
|
||||
%define %swig_container_methods(Container...)
|
||||
|
||||
%enddef
|
||||
|
||||
|
|
|
|||
|
|
@ -132,8 +132,8 @@
|
|||
}
|
||||
|
||||
%define %swig_map_common(Map...)
|
||||
%swig_sequence_iterator(Map);
|
||||
%swig_container_methods(Map)
|
||||
// %swig_sequence_methods_common(Map);
|
||||
// %swig_sequence_iterator(Map);
|
||||
|
||||
%extend {
|
||||
VALUE __getitem__(const key_type& key) const {
|
||||
|
|
@ -178,6 +178,23 @@
|
|||
}
|
||||
return ary;
|
||||
}
|
||||
|
||||
Map* each_key()
|
||||
{
|
||||
if ( !rb_block_given_p() )
|
||||
rb_raise( rb_eArgError, "no block given");
|
||||
|
||||
VALUE r;
|
||||
Map::iterator i = self->begin();
|
||||
Map::iterator e = self->end();
|
||||
for ( ; i != e; ++i )
|
||||
{
|
||||
r = swig::from( i->first );
|
||||
rb_yield(r);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
VALUE values() {
|
||||
Map::size_type size = self->size();
|
||||
|
|
@ -197,6 +214,23 @@
|
|||
return ary;
|
||||
}
|
||||
|
||||
Map* each_value()
|
||||
{
|
||||
if ( !rb_block_given_p() )
|
||||
rb_raise( rb_eArgError, "no block given");
|
||||
|
||||
VALUE r;
|
||||
Map::iterator i = self->begin();
|
||||
Map::iterator e = self->end();
|
||||
for ( ; i != e; ++i )
|
||||
{
|
||||
r = swig::from( i->second );
|
||||
rb_yield(r);
|
||||
}
|
||||
|
||||
return self;
|
||||
}
|
||||
|
||||
VALUE entries() {
|
||||
Map::size_type size = self->size();
|
||||
int rubysize = (size <= (Map::size_type) INT_MAX) ? (int) size : -1;
|
||||
|
|
@ -210,7 +244,8 @@
|
|||
Map::const_iterator i = self->begin();
|
||||
Map::const_iterator e = self->end();
|
||||
for ( ; i != e; ++i ) {
|
||||
rb_ary_push( ary, swig::from(*i) );
|
||||
rb_ary_push( ary, swig::from<std::pair<Map::key_type,
|
||||
Map::mapped_type> >(*i) );
|
||||
}
|
||||
return ary;
|
||||
}
|
||||
|
|
@ -236,9 +271,65 @@
|
|||
%define %swig_map_methods(Map...)
|
||||
%swig_map_common(Map)
|
||||
%extend {
|
||||
|
||||
void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
|
||||
(*self)[key] = x;
|
||||
}
|
||||
|
||||
VALUE inspect()
|
||||
{
|
||||
Map::const_iterator i = $self->begin();
|
||||
Map::const_iterator e = $self->end();
|
||||
VALUE str = rb_str_new2( swig::type_name< Map >() );
|
||||
str = rb_str_cat2( str, " {" );
|
||||
bool comma = false;
|
||||
VALUE tmp;
|
||||
for ( ; i != e; ++i, comma = true )
|
||||
{
|
||||
if (comma) str = rb_str_cat2( str, "," );
|
||||
// @todo: improve -- this should just be swig::from(*i)
|
||||
tmp = swig::from< std::pair<Map::key_type,
|
||||
Map::mapped_type> >( *i );
|
||||
tmp = rb_obj_as_string( tmp );
|
||||
str = rb_str_buf_append( str, tmp );
|
||||
}
|
||||
str = rb_str_cat2( str, "}" );
|
||||
return str;
|
||||
}
|
||||
|
||||
VALUE to_a()
|
||||
{
|
||||
Map::const_iterator i = $self->begin();
|
||||
Map::const_iterator e = $self->end();
|
||||
VALUE ary = rb_ary_new2( std::distance( i, e ) );
|
||||
VALUE tmp;
|
||||
for ( ; i != e; ++i )
|
||||
{
|
||||
// @todo: improve -- this should just be swig::from(*i)
|
||||
tmp = swig::from< std::pair<Map::key_type,
|
||||
Map::mapped_type> >( *i );
|
||||
rb_ary_push( ary, tmp );
|
||||
}
|
||||
return ary;
|
||||
}
|
||||
|
||||
VALUE to_s()
|
||||
{
|
||||
Map::iterator i = $self->begin();
|
||||
Map::iterator e = $self->end();
|
||||
VALUE str = rb_str_new2( "" );
|
||||
VALUE tmp;
|
||||
for ( ; i != e; ++i )
|
||||
{
|
||||
// @todo: improve -- this should just be swig::from(*i)
|
||||
tmp = swig::from< std::pair<Map::key_type,
|
||||
Map::mapped_type> >( *i );
|
||||
tmp = rb_obj_as_string( tmp );
|
||||
str = rb_str_buf_append( str, tmp );
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
||||
}
|
||||
%enddef
|
||||
|
||||
|
|
|
|||
|
|
@ -85,11 +85,108 @@
|
|||
}
|
||||
}
|
||||
|
||||
%define %swig_multimap_methods(Type...)
|
||||
%swig_map_common(Type);
|
||||
%define %swig_multimap_methods(MultiMap...)
|
||||
%swig_map_common(MultiMap);
|
||||
|
||||
%extend {
|
||||
void __setitem__(const key_type& key, const mapped_type& x) throw (std::out_of_range) {
|
||||
self->insert(Type::value_type(key,x));
|
||||
self->insert(MultiMap::value_type(key,x));
|
||||
}
|
||||
|
||||
VALUE inspect()
|
||||
{
|
||||
MultiMap::iterator i = $self->begin();
|
||||
MultiMap::iterator e = $self->end();
|
||||
VALUE str = rb_str_new2( swig::type_name< MultiMap >() );
|
||||
str = rb_str_cat2( str, " {" );
|
||||
VALUE tmp;
|
||||
while ( i != e )
|
||||
{
|
||||
const MultiMap::key_type& key = i->first;
|
||||
const MultiMap::key_type& oldkey = key;
|
||||
tmp = swig::from( key );
|
||||
str = rb_str_buf_append( str, rb_inspect(tmp) );
|
||||
|
||||
VALUE vals = rb_ary_new();
|
||||
for ( ; i != e && key == oldkey; ++i )
|
||||
{
|
||||
const MultiMap::mapped_type& val = i->second;
|
||||
tmp = swig::from( val );
|
||||
rb_ary_push( vals, tmp );
|
||||
}
|
||||
|
||||
if ( RARRAY_LEN(vals) == 1 )
|
||||
{
|
||||
str = rb_str_buf_append( str, rb_inspect(tmp) );
|
||||
}
|
||||
else
|
||||
{
|
||||
str = rb_str_buf_append( str, rb_inspect(vals) );
|
||||
}
|
||||
}
|
||||
str = rb_str_cat2( str, "}" );
|
||||
return str;
|
||||
}
|
||||
|
||||
VALUE to_a()
|
||||
{
|
||||
MultiMap::const_iterator i = $self->begin();
|
||||
MultiMap::const_iterator e = $self->end();
|
||||
VALUE ary = rb_ary_new2( std::distance( i, e ) );
|
||||
VALUE tmp;
|
||||
while ( i != e )
|
||||
{
|
||||
const MultiMap::key_type& key = i->first;
|
||||
const MultiMap::key_type& oldkey = key;
|
||||
tmp = swig::from( key );
|
||||
rb_ary_push( ary, tmp );
|
||||
|
||||
VALUE vals = rb_ary_new();
|
||||
for ( ; i != e && key == oldkey; ++i )
|
||||
{
|
||||
const MultiMap::mapped_type& val = i->second;
|
||||
tmp = swig::from( val );
|
||||
rb_ary_push( vals, tmp );
|
||||
}
|
||||
|
||||
if ( RARRAY_LEN(vals) == 1 )
|
||||
{
|
||||
rb_ary_push( ary, tmp );
|
||||
}
|
||||
else
|
||||
{
|
||||
rb_ary_push( ary, vals );
|
||||
}
|
||||
}
|
||||
return ary;
|
||||
}
|
||||
|
||||
VALUE to_s()
|
||||
{
|
||||
MultiMap::iterator i = $self->begin();
|
||||
MultiMap::iterator e = $self->end();
|
||||
VALUE str = rb_str_new2( "" );
|
||||
VALUE tmp;
|
||||
while ( i != e )
|
||||
{
|
||||
const MultiMap::key_type& key = i->first;
|
||||
const MultiMap::key_type& oldkey = key;
|
||||
tmp = swig::from( key );
|
||||
tmp = rb_obj_as_string( tmp );
|
||||
str = rb_str_buf_append( str, tmp );
|
||||
|
||||
VALUE vals = rb_ary_new();
|
||||
for ( ; i != e && key == oldkey; ++i )
|
||||
{
|
||||
const MultiMap::mapped_type& val = i->second;
|
||||
tmp = swig::from( val );
|
||||
rb_ary_push( vals, tmp );
|
||||
}
|
||||
|
||||
tmp = rb_obj_as_string( vals );
|
||||
str = rb_str_buf_append( str, tmp );
|
||||
}
|
||||
return str;
|
||||
}
|
||||
}
|
||||
%enddef
|
||||
|
|
|
|||
|
|
@ -91,7 +91,8 @@
|
|||
}
|
||||
} else {
|
||||
value_type *p;
|
||||
res = SWIG_ConvertPtr(obj,(void**)&p,swig::type_info<value_type>(),0);
|
||||
res = SWIG_ConvertPtr(obj,(void**)&p,
|
||||
swig::type_info<value_type>(),0);
|
||||
if (SWIG_IsOK(res) && val) *val = p;
|
||||
}
|
||||
return res;
|
||||
|
|
@ -146,8 +147,9 @@
|
|||
|
||||
|
||||
%define %swig_pair_methods(pair...)
|
||||
|
||||
%extend {
|
||||
VALUE inspect()
|
||||
VALUE inspect() const
|
||||
{
|
||||
VALUE tmp;
|
||||
VALUE str = rb_str_new2( swig::type_name< pair >() );
|
||||
|
|
@ -163,7 +165,7 @@
|
|||
return str;
|
||||
}
|
||||
|
||||
VALUE to_s()
|
||||
VALUE to_s() const
|
||||
{
|
||||
VALUE tmp;
|
||||
VALUE str = rb_str_new2( "(" );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue