swig/Lib/ruby/rubycomplex.swg
Thomas Reitmayr 5a10e10399 Ruby: Fix warnings in generated code about missing parameter in variadic macro
The Ruby C API function 'rb_funcall' is used in various places in generated
code for invoking a Ruby method without parameters. The C function uses a
variadic parameter list for the arguments passed to Ruby, therefore in these
cases the list of variadic parameters is empty.
As an optimization Ruby may implement the 'rb_funcall' function as a macro
which however will not accept an empty list of arguments for '...' as of
C99 and C++11.
In order to prevent compiler warnings, this commit replaces all such
occurrences with a call to 'rb_funcall2' (which in its current name
'rb_funcallv' is invoked by the 'rb_funcall' macro anyway, at least for
Ruby 2.6.6).
2022-01-29 11:07:57 +13:00

148 lines
3.8 KiB
Text

/*
Defines the As/From conversors for double/float complex, you need to
provide complex Type, the Name you want to use in the converters,
the complex Constructor method, and the Real and Imag complex
accessor methods.
See the std_complex.i and ccomplex.i for concrete examples.
*/
%fragment("rb_complex_new","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. */
SWIGINTERN 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);
}
%#endif
}
%fragment("SWIG_Complex_Numbers","header")
{
%#if !defined(T_COMPLEX)
SWIGINTERN 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 ) ) &&
(rb_respond_to( obj, imag_id ) ) );
}
%#else
SWIGINTERN int SWIG_Is_Complex( VALUE obj ) {
return TYPE(obj) == T_COMPLEX;
}
%#endif
SWIGINTERN VALUE SWIG_Complex_Real(VALUE obj) {
static ID real_id = rb_intern("real");
return rb_funcall2(obj, real_id, 0, 0);
}
SWIGINTERN VALUE SWIG_Complex_Imaginary(VALUE obj) {
static ID imag_id = rb_intern("imag");
return rb_funcall2(obj, imag_id, 0, 0);
}
}
%init {
%#if !defined(T_COMPLEX)
rb_require("complex");
%#endif
}
/* the common from converter */
%define %swig_fromcplx_conv(Type, Real, Imag)
%fragment(SWIG_From_frag(Type),"header",fragment="rb_complex_new")
{
SWIGINTERNINLINE VALUE
SWIG_From(Type)(%ifcplusplus(const Type&, Type) c)
{
VALUE re = rb_float_new(Real(c));
VALUE im = rb_float_new(Imag(c));
return rb_complex_new(re, im);
}
}
%enddef
/* the double case */
%define %swig_cplxdbl_conv(Type, Constructor, Real, Imag)
%fragment(SWIG_AsVal_frag(Type),"header",
fragment=SWIG_AsVal_frag(double),
fragment="SWIG_Complex_Numbers")
{
SWIGINTERN int
SWIG_AsVal(Type) (VALUE o, Type* val)
{
if ( SWIG_Is_Complex( o ) ) {
if (val) {
VALUE real = SWIG_Complex_Real(o);
VALUE imag = SWIG_Complex_Imaginary(o);
double re = 0;
SWIG_AsVal_double( real, &re );
double im = 0;
SWIG_AsVal_double( imag, &im );
*val = Constructor(re, im);
}
return SWIG_OK;
} else {
double d;
int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d));
if (SWIG_IsOK(res)) {
if (val) *val = Constructor(d, 0.0);
return res;
}
}
return SWIG_TypeError;
}
}
%swig_fromcplx_conv(Type, Real, Imag);
%enddef
/* the float case */
%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_Complex_Numbers") {
SWIGINTERN int
SWIG_AsVal(Type)(VALUE o, Type *val)
{
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;
SWIG_AsVal_double( imag, &im );
if ((-FLT_MAX <= re && re <= FLT_MAX) &&
(-FLT_MAX <= im && im <= FLT_MAX)) {
if (val) *val = Constructor(%numeric_cast(re, float),
%numeric_cast(im, float));
return SWIG_OK;
} else {
return SWIG_OverflowError;
}
} else {
float re;
int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re));
if (SWIG_IsOK(res)) {
if (val) *val = Constructor(re, 0.0f);
return res;
}
}
return SWIG_TypeError;
}
}
%swig_fromcplx_conv(Type, Real, Imag);
%enddef
#define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \
%swig_cplxflt_conv(Type, Constructor, Real, Imag)
#define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \
%swig_cplxdbl_conv(Type, Constructor, Real, Imag)