From bbd80cfa847cea59ae8c78039ab65c58e222d7e8 Mon Sep 17 00:00:00 2001 From: Gonzalo Garramuno Date: Tue, 24 Jul 2007 05:46:55 +0000 Subject: [PATCH] Moved some of the common checks for methods/procs and arity to rubyrun.swg, as they are useful even for not stl stuff. Added a fragment trait for marking STL containers, but this fragment cannot be attached due to SWIG %template limitations. git-svn-id: https://swig.svn.sourceforge.net/svnroot/swig/trunk@9874 626c5289-ae23-0410-ae9c-e8d60b6d4f22 --- Lib/ruby/rubyrun.swg | 36 ++++++++++++++++++++ Lib/ruby/rubystdcommon.swg | 64 ++++++++++++++++++++++++++++++++++++ Lib/ruby/rubystdfunctors.swg | 34 ------------------- 3 files changed, 100 insertions(+), 34 deletions(-) diff --git a/Lib/ruby/rubyrun.swg b/Lib/ruby/rubyrun.swg index 1205d590a..9f3228c01 100644 --- a/Lib/ruby/rubyrun.swg +++ b/Lib/ruby/rubyrun.swg @@ -394,6 +394,42 @@ 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, like: + + %typecheck(c_callback_t, precedence=SWIG_TYPECHECK_POINTER) { + $result = SWIG_Ruby_isCallable( $input ); + } + */ +SWIGINTERN +int SWIG_Ruby_isCallable( VALUE proc ) +{ + static VALUE call_id = rb_intern("call"); + if ( rb_respond_to( proc, call_id ) == Qtrue ) + return 1; + return 0; +} + +/* This function can be used to check the arity (number of arguments) + a proc or method can take. Usually used in a %typecheck. + Valid arities will be that equal to minimal or those < 0 + which indicate a variable number of parameters at the end. + */ +SWIGINTERN +int SWIG_Ruby_arity( VALUE proc, int minimal ) +{ + static VALUE arity_id = rb_intern("arity"); + if ( rb_respond_to( proc, arity_id ) == Qtrue ) + { + VALUE num = rb_funcall( proc, arity_id, 0 ); + int arity = NUM2INT(num); + if ( arity < 0 && (arity+1) < -minimal ) return 1; + if ( arity == minimal ) return 1; + return 1; + } + return 0; +} + #ifdef __cplusplus } diff --git a/Lib/ruby/rubystdcommon.swg b/Lib/ruby/rubystdcommon.swg index 838c54be7..4e1b9d967 100644 --- a/Lib/ruby/rubystdcommon.swg +++ b/Lib/ruby/rubystdcommon.swg @@ -199,3 +199,67 @@ namespace swig { } } } + + +// Define GC marking template traits for a container type +%define %create_mark_traits(Type) +%{ + namespace swig { + template + struct mark_traits { + typedef typename Type::const_iterator const_iterator; + + inline void operator()(const Type& c ) const + { + const_iterator i = c.begin(); + const_iterator e = c.end(); + for ( ; i != e; ++i ) + { + rb_gc_mark( swig::from( &(*i) ) ); + } + } + }; + + // Partial specializations for classes that requires no GC marking + // or a special GC marking algorithm. + template< > + struct mark_traits { + inline void operator()(const Type& c ) const {} + }; + + template< > + struct mark_traits { + inline void operator()(const Type& c ) const {} + }; + + template< > + struct mark_traits { + inline void operator()(const Type& c ) const {} + }; + + template< > + struct mark_traits { + inline void operator()(const Type& c ) const {} + }; + + template< > + struct mark_traits { + typedef Type::const_iterator const_iterator; + + inline void operator()(const Type& c ) const { + const_iterator i = c.begin(); + const_iterator e = c.end(); + for ( ; i != e; ++i ) + { + VALUE v = *i; + if ( FIXNUM_P(v) || SPECIAL_CONST_P(v) || SYMBOL_P(v) || + ( BUILTIN_TYPE(v) == T_NONE ) ) continue; + + rb_gc_mark( v ); + } + } + }; + + } +%} +%enddef diff --git a/Lib/ruby/rubystdfunctors.swg b/Lib/ruby/rubystdfunctors.swg index be23ffe84..82093823d 100644 --- a/Lib/ruby/rubystdfunctors.swg +++ b/Lib/ruby/rubystdfunctors.swg @@ -26,40 +26,6 @@ %include rubyclasses.swg -%{ -/* 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 SWIG_Ruby_isCallable( VALUE proc ) -{ - static VALUE call_id = rb_intern("call"); - if ( rb_respond_to( proc, call_id ) ) - return 1; - return 0; -} - -/* This function can be used to check the arity (number of arguments) - a proc or method can take. Usually used in a %typecheck. - Valid arities will be that equal to minimal or those < 0 - which indicate a variable number of parameters at the end. - */ -SWIGINTERN -int SWIG_Ruby_arity( VALUE proc, int minimal ) -{ - static VALUE arity_id = rb_intern("arity"); - if ( rb_respond_to( proc, arity_id ) ) - { - VALUE num = rb_funcall( proc, arity_id, 0 ); - int arity = NUM2INT(num); - if ( arity < 0 && (arity+1) < -minimal ) return 1; - if ( arity == minimal ) return 1; - return 1; - } - return 0; -} - -%} - namespace swig {