diff options
author | William S Fulton <wsf@fultondesigns.co.uk> | 2013-04-02 20:14:51 +0100 |
---|---|---|
committer | William S Fulton <wsf@fultondesigns.co.uk> | 2013-04-02 20:14:51 +0100 |
commit | 9aaf4ad03cc31eb976d8bd16301286f0ac83ff4c (patch) | |
tree | 54c1388344517b29ee446efc32bacf9a6ed7ee5c /Lib/ruby | |
parent | e13e1cba9e6dd024f713821558ae084cc9dc3507 (diff) | |
download | swig-9aaf4ad03cc31eb976d8bd16301286f0ac83ff4c.tar.gz |
Fixes for Ruby 1.9 std::complex wrappers.
New native Ruby complex numbers are used.
Diffstat (limited to 'Lib/ruby')
-rw-r--r-- | Lib/ruby/rubycomplex.swg | 84 |
1 files changed, 49 insertions, 35 deletions
diff --git a/Lib/ruby/rubycomplex.swg b/Lib/ruby/rubycomplex.swg index afdb15e7e..8a7486865 100644 --- a/Lib/ruby/rubycomplex.swg +++ b/Lib/ruby/rubycomplex.swg @@ -1,49 +1,61 @@ /* Defines the As/From conversors for double/float complex, you need to - provide complex Type, the Name you want to use in the conversors, + provide complex Type, the Name you want to use in the converters, the complex Constructor method, and the Real and Imag complex - accesor methods. + accessor methods. See the std_complex.i and ccomplex.i for concrete examples. */ -/* - Ruby does not have native complex numbers. They are an extension in the - STD library. -*/ -%{ - static VALUE swig_rb_cComplex = Qnil; - static ID swig_real_id = 0; - static ID swig_imag_id = 0; +%fragment("SWIG_Complex_Numbers","header") +{ +%#if !defined(T_COMPLEX) +/* Ruby versions prior to 1.9 did not have native complex numbers. They were an extension in the STD library. */ +VALUE rb_complex_new(VALUE x, VALUE y) { + static ID new_id = rb_intern("new"); + static VALUE cComplex = rb_const_get(rb_cObject, rb_intern("Complex")); + return rb_funcall(cComplex, new_id, 2, x, y); +} - int Ruby_Is_Complex( VALUE obj ) - { - return ( (rb_respond_to( obj, swig_real_id ) == Qtrue) && - (rb_respond_to( obj, swig_imag_id ) == Qtrue) ); - } -%} +static int SWIG_Is_Complex( VALUE obj ) { + static ID real_id = rb_intern("real"); + static ID imag_id = rb_intern("imag"); + return ( (rb_respond_to( obj, real_id ) == Qtrue) && + (rb_respond_to( obj, imag_id ) == Qtrue) ); +} +%#else +static int SWIG_Is_Complex( VALUE obj ) { + return TYPE(obj) == T_COMPLEX; +} +%#endif + +VALUE SWIG_Complex_Real(VALUE obj) { + static ID real_id = rb_intern("real"); + return rb_funcall(obj, real_id, 0); +} + +VALUE SWIG_Complex_Imaginary(VALUE obj) { + static ID imag_id = rb_intern("imag"); + return rb_funcall(obj, imag_id, 0); +} +} %init { +%#if !defined(T_COMPLEX) rb_require("complex"); - swig_rb_cComplex = rb_const_get( rb_cObject, rb_intern("Complex") ); - if( swig_rb_cComplex == Qnil ) - rb_warn("Ruby's complex.so not found"); - swig_real_id = rb_intern("real"); - swig_imag_id = rb_intern("imag"); +%#endif } -/* the common from conversor */ +/* the common from converter */ %define %swig_fromcplx_conv(Type, Real, Imag) %fragment(SWIG_From_frag(Type),"header") { SWIGINTERNINLINE VALUE SWIG_From(Type)(%ifcplusplus(const Type&, Type) c) { - VALUE args[] = { - rb_float_new(Real(c)), - rb_float_new(Imag(c)) - }; - return rb_class_new_instance(2, args, swig_rb_cComplex); + VALUE re = rb_float_new(Real(c)); + VALUE im = rb_float_new(Imag(c)); + return rb_complex_new(re, im); } } %enddef @@ -51,15 +63,16 @@ SWIG_From(Type)(%ifcplusplus(const Type&, Type) c) /* the double case */ %define %swig_cplxdbl_conv(Type, Constructor, Real, Imag) %fragment(SWIG_AsVal_frag(Type),"header", - fragment=SWIG_AsVal_frag(double)) + fragment=SWIG_AsVal_frag(double), + fragment="SWIG_Complex_Numbers") { SWIGINTERN int SWIG_AsVal(Type) (VALUE o, Type* val) { - if ( Ruby_Is_Complex( o ) ) { + if ( SWIG_Is_Complex( o ) ) { if (val) { - VALUE real = rb_funcall(o, swig_real_id, 0 ); - VALUE imag = rb_funcall(o, swig_imag_id, 0 ); + VALUE real = SWIG_Complex_Real(o); + VALUE imag = SWIG_Complex_Imaginary(o); double re = 0; SWIG_AsVal_double( real, &re ); double im = 0; @@ -85,13 +98,14 @@ SWIG_AsVal(Type) (VALUE o, Type* val) %define %swig_cplxflt_conv(Type, Constructor, Real, Imag) %fragment(SWIG_AsVal_frag(Type),"header", fragment=SWIG_AsVal_frag(float), - fragment=SWIG_AsVal_frag(double)) { + fragment=SWIG_AsVal_frag(double), + fragment="SWIG_Complex_Numbers") { SWIGINTERN int SWIG_AsVal(Type)(VALUE o, Type *val) { - if ( Ruby_Is_Complex( o ) ) { - VALUE real = rb_funcall(o, swig_real_id, 0 ); - VALUE imag = rb_funcall(o, swig_imag_id, 0 ); + if ( SWIG_Is_Complex( o ) ) { + VALUE real = SWIG_Complex_Real(o); + VALUE imag = SWIG_Complex_Imaginary(o); double re = 0; SWIG_AsVal_double( real, &re ); double im = 0; |