From 4cd98d3865e427d12aa68d3eda62aa776813917e Mon Sep 17 00:00:00 2001 From: Gonzalo Garramuno Date: Fri, 4 May 2007 13:12:31 +0000 Subject: [PATCH] Improved algorithm of renaming of methods with numbers at the end. Fixed some const issues. Improved report on overloaded function error. Fixed some minor iterator potential problems. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9770 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Examples/test-suite/li_std_vector.i | 8 +++++ Examples/test-suite/ruby/Makefile.in | 2 +- .../test-suite/ruby/li_std_vector_runme.rb | 19 +++++++++- Lib/ruby/rubyclasses.swg | 5 +++ Lib/ruby/rubycontainer.swg | 8 ++--- Lib/ruby/rubyiterators.swg | 35 +++++++++++++++---- Lib/ruby/rubymacros.swg | 4 +++ Lib/ruby/rubyrun.swg | 13 +++++++ Lib/ruby/rubystdcommon.swg | 4 --- Lib/ruby/rubytypemaps.swg | 1 - Lib/ruby/std_set.i | 6 ++-- Source/Modules/ruby.cxx | 2 +- Source/Swig/misc.c | 15 +++++--- 13 files changed, 96 insertions(+), 26 deletions(-) diff --git a/Examples/test-suite/li_std_vector.i b/Examples/test-suite/li_std_vector.i index 75e5f21b3..587ea2217 100644 --- a/Examples/test-suite/li_std_vector.i +++ b/Examples/test-suite/li_std_vector.i @@ -114,3 +114,11 @@ SWIG_STD_VECTOR_SPECIALIZE(MyClass, MyClass *) }; } #endif + +#if defined(SWIGRUBY) +%template(LanguageVector) std::vector< swig::LANGUAGE_OBJ >; + +%inline { + std::vector< swig::LANGUAGE_OBJ > LanguageVector; +} +#endif diff --git a/Examples/test-suite/ruby/Makefile.in b/Examples/test-suite/ruby/Makefile.in index cecd0f639..590aa0244 100644 --- a/Examples/test-suite/ruby/Makefile.in +++ b/Examples/test-suite/ruby/Makefile.in @@ -34,7 +34,7 @@ C_TEST_CASES += \ include $(srcdir)/../common.mk # Overridden variables here -SWIGOPT += -noautorename -features autodoc=4 +SWIGOPT += -w801 -noautorename -features autodoc=4 # Rules for the different types of tests diff --git a/Examples/test-suite/ruby/li_std_vector_runme.rb b/Examples/test-suite/ruby/li_std_vector_runme.rb index 797448236..a32abd598 100755 --- a/Examples/test-suite/ruby/li_std_vector_runme.rb +++ b/Examples/test-suite/ruby/li_std_vector_runme.rb @@ -90,7 +90,9 @@ dv.kind_of? DoubleVector halved = [] halved = dv.map { |x| x / 2 } halve_in_place(dv) -halved == dv.to_a +p halved.to_a +p dv.to_a +halved.to_a == dv.to_a sv = StructVector.new sv << Li_std_vector::Struct.new sv[0].class == Li_std_vector::Struct @@ -98,3 +100,18 @@ sv[1] = Li_std_vector::Struct.new EOF + +swig_assert_each_line(<<'EOF', binding) +lv = LanguageVector.new +lv << 1 +lv << [1,2] +lv << 'asd' +lv[0], lv[1] = lv[1], lv[0] +EOF + + +# this should assert +begin + lv = LanguageVector.new('crapola') +rescue +end diff --git a/Lib/ruby/rubyclasses.swg b/Lib/ruby/rubyclasses.swg index 704edf3b7..17d58d2d2 100644 --- a/Lib/ruby/rubyclasses.swg +++ b/Lib/ruby/rubyclasses.swg @@ -43,8 +43,13 @@ namespace swig { %exception GC_VALUE {}; + %apply VALUE {GC_VALUE}; + // Make sure this is the last typecheck done + %typecheck(999999,noblock=1) GC_VALUE, GC_VALUE&, + const GC_VALUE& { $1 = 1; }; + /* For input */ %typemap(in,noblock=1) GC_VALUE* (GC_VALUE r), GC_VALUE& (GC_VALUE r) { r = $input; $1 = &r; diff --git a/Lib/ruby/rubycontainer.swg b/Lib/ruby/rubycontainer.swg index ba99cdfde..5ac43f554 100644 --- a/Lib/ruby/rubycontainer.swg +++ b/Lib/ruby/rubycontainer.swg @@ -615,14 +615,15 @@ namespace swig return r; } + Sequence* each() { if ( !rb_block_given_p() ) rb_raise( rb_eArgError, "no block given"); VALUE r; - Sequence::iterator i = self->begin(); - Sequence::iterator e = self->end(); + Sequence::const_iterator i = self->begin(); + Sequence::const_iterator e = self->end(); for ( ; i != e; ++i ) { r = swig::from(*i); @@ -650,7 +651,6 @@ namespace swig return r; } - %rename("reject!") reject_bang; %alias reject_bang "delete_if"; Sequence* reject_bang() { @@ -971,7 +971,7 @@ namespace swig { typedef T value_type; static int asptr(VALUE obj, sequence **seq) { - if (rb_obj_is_kind_of(obj, rb_cArray)) { + if (rb_obj_is_kind_of(obj, rb_cArray) == Qtrue) { try { RubySequence_Cont rubyseq(obj); if (seq) { diff --git a/Lib/ruby/rubyiterators.swg b/Lib/ruby/rubyiterators.swg index eb9705778..1743d1519 100644 --- a/Lib/ruby/rubyiterators.swg +++ b/Lib/ruby/rubyiterators.swg @@ -34,6 +34,9 @@ namespace swig { // Access iterator method, required by Ruby virtual VALUE value() const = 0; +// // Set referenced object +// virtual bool set(VALUE v) = 0; + // Forward iterator method, required by Ruby virtual RubySwigIterator *incr(size_t n = 1) = 0; @@ -133,7 +136,7 @@ namespace swig { typedef typename std::iterator_traits::value_type value_type; typedef RubySwigIterator_T self_type; - RubySwigIterator_T(out_iterator curr, VALUE seq) + RubySwigIterator_T(out_iterator curr, VALUE seq = Qnil) : RubySwigIterator(seq), current(curr) { } @@ -191,14 +194,22 @@ namespace swig { typedef RubySwigIterator_T base; typedef RubySwigIteratorOpen_T self_type; - RubySwigIteratorOpen_T(out_iterator curr, VALUE seq) + RubySwigIteratorOpen_T(out_iterator curr, VALUE seq = Qnil) : RubySwigIterator_T(curr, seq) { } - VALUE value() const { + virtual VALUE value() const { return from(static_cast(*(base::current))); } + +// virtual bool set(VALUE v) { +// value_type val; +// if ( !asval(v, &val) ) return SWIG_ERROR; +// *(base::current) = val; +// return SWIG_OK; +// } + RubySwigIterator *copy() const { @@ -234,12 +245,12 @@ namespace swig { typedef RubySwigIterator_T base; typedef RubySwigIteratorClosed_T self_type; - RubySwigIteratorClosed_T(out_iterator curr, out_iterator first, out_iterator last, VALUE seq) + RubySwigIteratorClosed_T(out_iterator curr, out_iterator first, out_iterator last, VALUE seq = Qnil) : RubySwigIterator_T(curr, seq), begin(first), end(last) { } - VALUE value() const { + virtual VALUE value() const { if (base::current == end) { throw stop_iteration(); } else { @@ -247,6 +258,13 @@ namespace swig { } } +// virtual bool set(VALUE v) { +// value_type val; +// if ( !asval(v, &val) ) return SWIG_ERROR; +// *(base::current) = val; +// return SWIG_OK; +// } + RubySwigIterator *copy() const { return new self_type(*this); @@ -283,14 +301,14 @@ namespace swig { template inline RubySwigIterator* - make_output_iterator(const OutIter& current, const OutIter& begin,const OutIter& end, VALUE seq = 0) + make_output_iterator(const OutIter& current, const OutIter& begin,const OutIter& end, VALUE seq = Qnil) { return new RubySwigIteratorClosed_T(current, begin, end, seq); } template inline RubySwigIterator* - make_output_iterator(const OutIter& current, VALUE seq = 0) + make_output_iterator(const OutIter& current, VALUE seq = Qnil) { return new RubySwigIteratorOpen_T(current, seq); } @@ -347,6 +365,9 @@ namespace swig // Access iterator method, required by Ruby virtual VALUE value() const = 0; +// // Set referenced object, required by Ruby +// virtual bool set(VALUE val) = 0; + // Forward iterator method, required by Ruby virtual RubySwigIterator *incr(size_t n = 1) = 0; diff --git a/Lib/ruby/rubymacros.swg b/Lib/ruby/rubymacros.swg index 8af6822df..de2a52ba7 100644 --- a/Lib/ruby/rubymacros.swg +++ b/Lib/ruby/rubymacros.swg @@ -5,5 +5,9 @@ #define %argnullref_fmt(_type,_name,_argn) Ruby_Format_TypeError(%nullref_fmt(), _type, #_name, _argn, $input) +%{ +#define SWIG_RUBY_THREAD_BEGIN_BLOCK +#define SWIG_RUBY_THREAD_END_BLOCK +%} %include diff --git a/Lib/ruby/rubyrun.swg b/Lib/ruby/rubyrun.swg index 9848e4a3a..090baac65 100644 --- a/Lib/ruby/rubyrun.swg +++ b/Lib/ruby/rubyrun.swg @@ -394,6 +394,19 @@ SWIG_Ruby_SetModule(swig_module_info *pointer) rb_define_readonly_variable("$swig_runtime_data_type_pointer" SWIG_RUNTIME_VERSION SWIG_TYPE_TABLE_NAME, &swig_runtime_data_type_pointer); } +/* This function can be used to check whether a proc or method or similarly + callable function has been passed. Usually used in a %typecheck. */ +SWIGINTERN +int rb_is_callable( VALUE proc ) +{ + static VALUE call_id = Qnil; + if ( call_id == Qnil ) call_id = rb_intern("call"); + if ( rb_respond_to( proc, call_id ) ) + return 1; + return 0; +} + + #ifdef __cplusplus } #endif diff --git a/Lib/ruby/rubystdcommon.swg b/Lib/ruby/rubystdcommon.swg index 1f4f6f5e8..d9ee0031e 100644 --- a/Lib/ruby/rubystdcommon.swg +++ b/Lib/ruby/rubystdcommon.swg @@ -200,7 +200,3 @@ namespace swig { } } - -#define SWIG_RUBY_THREAD_BEGIN_BLOCK -#define SWIG_RUBY_THREAD_END_BLOCK - diff --git a/Lib/ruby/rubytypemaps.swg b/Lib/ruby/rubytypemaps.swg index 68a2f8834..336ee977a 100644 --- a/Lib/ruby/rubytypemaps.swg +++ b/Lib/ruby/rubytypemaps.swg @@ -56,7 +56,6 @@ $1 = &self; } - /* Include the unified typemap library */ %include diff --git a/Lib/ruby/std_set.i b/Lib/ruby/std_set.i index 0e79b488c..a66d037a8 100644 --- a/Lib/ruby/std_set.i +++ b/Lib/ruby/std_set.i @@ -21,7 +21,7 @@ template struct traits_asptr > { - static int asptr(VALUE obj, std::set **s) { + static int asptr(VALUE obj, std::set **s) { return traits_asptr_stdseq >::asptr(obj, s); } }; @@ -40,13 +40,13 @@ %extend { %alias push "<<"; - value_type push(value_type x) { + value_type push(const value_type& x) { self->insert(x); return x; } %rename("include?") __contains__; - bool __contains__(value_type x) { + bool __contains__(const value_type& x) { return self->find(x) != self->end(); } diff --git a/Source/Modules/ruby.cxx b/Source/Modules/ruby.cxx index 784d27325..1e5dd9d7d 100644 --- a/Source/Modules/ruby.cxx +++ b/Source/Modules/ruby.cxx @@ -2012,7 +2012,7 @@ public: String* methodName = NewString(""); if ( isMethod ) Printv( methodName, Getattr(parentNode(sibl),"sym:name"), ".", NIL ); - Append( methodName, Getattr(sibl,"name" ) ); + Append( methodName, Getattr(sibl,"sym:name" ) ); if ( isCtor ) Append( methodName, ".new" ); // Generate prototype list diff --git a/Source/Swig/misc.c b/Source/Swig/misc.c index 6e9549c9a..21ec537b3 100644 --- a/Source/Swig/misc.c +++ b/Source/Swig/misc.c @@ -235,26 +235,33 @@ String *Swig_string_lccase(String *s) { * * This is the reverse case of ccase, ie * - * CamelCase -> camel_case + * CamelCase -> camel_case + * get2D -> get_2d + * asFloat2 -> as_float2 * ----------------------------------------------------------------------------- */ String *Swig_string_ucase(String *s) { String *ns; int c; int lastC = 0; + int nextC = 0; int underscore = 0; ns = NewStringEmpty(); /* We insert a underscore when: 1. Lower case char followed by upper case char getFoo > get_foo; getFOo > get_foo; GETFOO > getfoo - 2. Number proceded by char - get2D > get_2d; get22D > get_22d; GET2D > get_2d */ + 2. Number proceded by char and not end of string + get2D > get_2d; get22D > get_22d; GET2D > get_2d + but: + asFloat2 > as_float2 + */ Seek(s, 0, SEEK_SET); while ((c = Getc(s)) != EOF) { - if (isdigit(c) && isalpha(lastC)) + nextC = Getc(s); Ungetc(nextC, s); + if (isdigit(c) && isalpha(lastC) && nextC != EOF) underscore = 1; else if (isupper(c) && isalpha(lastC) && !isupper(lastC)) underscore = 1;