Home | History | Annotate | Download | only in octave
      1 /*
      2   Defines the As/From conversors for double/float complex, you need to
      3   provide complex Type, the Name you want to use in the conversors,
      4   the complex Constructor method, and the Real and Imag complex
      5   accesor methods.
      6 
      7   See the std_complex.i and ccomplex.i for concrete examples.
      8 */
      9 
     10 /* the common from conversor */
     11 %define %swig_fromcplx_conv(Type, OctConstructor, Real, Imag)
     12      %fragment(SWIG_From_frag(Type),"header")
     13 {
     14   SWIGINTERNINLINE octave_value
     15     SWIG_From(Type)(const Type& c)
     16     {
     17       return octave_value(OctConstructor(Real(c), Imag(c)));
     18     }
     19 }
     20 %enddef
     21 
     22 // the double case
     23 %define %swig_cplxdbl_conv(Type, Constructor, Real, Imag)
     24      %fragment(SWIG_AsVal_frag(Type),"header",
     25 	       fragment=SWIG_AsVal_frag(double))
     26 {
     27   SWIGINTERN int
     28     SWIG_AsVal(Type) (const octave_value& ov, Type* val)
     29     {
     30       if (ov.is_complex_scalar()) {
     31 	if (val) {
     32 	  Complex c(ov.complex_value());
     33 	  *val=Constructor(c.real(),c.imag());
     34 	}
     35 	return SWIG_OK;
     36       } else {
     37 	double d;
     38 	int res = SWIG_AddCast(SWIG_AsVal(double)(ov, &d));
     39 	if (SWIG_IsOK(res)) {
     40 	  if (val)
     41 	    *val = Constructor(d, 0.0);
     42 	  return res;
     43 	}
     44       }
     45       return SWIG_TypeError;
     46     }
     47 }
     48 %swig_fromcplx_conv(Type, Complex, Real, Imag);
     49 %enddef
     50 
     51 // the float case
     52 %define %swig_cplxflt_conv(Type, Constructor, Real, Imag)
     53      %fragment(SWIG_AsVal_frag(Type),"header",
     54 	       fragment=SWIG_AsVal_frag(float)) {
     55   SWIGINTERN int
     56     SWIG_AsVal(Type) (const octave_value& ov, Type* val)
     57     {
     58       if (ov.is_complex_scalar()) {
     59 	if (val) {
     60 	  Complex c(ov.complex_value());
     61 	  double re = c.real();
     62 	  double im = c.imag();
     63 	  if ((-FLT_MAX <= re && re <= FLT_MAX) && (-FLT_MAX <= im && im <= FLT_MAX)) {
     64 	    if (val)
     65 	      *val = Constructor(%numeric_cast(re, float),
     66 				 %numeric_cast(im, float));
     67 	    return SWIG_OK;
     68 	  } else
     69 	    return SWIG_OverflowError;
     70 	}
     71       } else {
     72 	float d;
     73 	int res = SWIG_AddCast(SWIG_AsVal(float)(ov, &d));
     74 	if (SWIG_IsOK(res)) {
     75 	  if (val)
     76 	    *val = Constructor(d, 0.0);
     77 	  return res;
     78 	}
     79       }
     80       return SWIG_TypeError;
     81     }
     82 }
     83 
     84 %swig_fromcplx_conv(Type, FloatComplex, Real, Imag);
     85 %enddef
     86 
     87 #define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \
     88 %swig_cplxflt_conv(Type, Constructor, Real, Imag)
     89 
     90 
     91 #define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \
     92 %swig_cplxdbl_conv(Type, Constructor, Real, Imag)
     93