1 /* 2 Defines the As/From converters for double/float complex, you need to 3 provide complex Type, the Name you want to use in the converters, 4 the complex Constructor method, and the Real and Imag complex 5 accessor methods. 6 7 See the std_complex.i and ccomplex.i for concret examples. 8 */ 9 10 /* the common from converter */ 11 %define %swig_fromcplx_conv(Type, Real, Imag) 12 %fragment(SWIG_From_frag(Type),"header") 13 { 14 SWIGINTERNINLINE PyObject* 15 SWIG_From(Type)(%ifcplusplus(const Type&, Type) c) 16 { 17 return PyComplex_FromDoubles(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) (PyObject *o, Type* val) 29 { 30 if (PyComplex_Check(o)) { 31 if (val) *val = Constructor(PyComplex_RealAsDouble(o), PyComplex_ImagAsDouble(o)); 32 return SWIG_OK; 33 } else { 34 double d; 35 int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d)); 36 if (SWIG_IsOK(res)) { 37 if (val) *val = Constructor(d, 0.0); 38 return res; 39 } 40 } 41 return SWIG_TypeError; 42 } 43 } 44 %swig_fromcplx_conv(Type, Real, Imag); 45 %enddef 46 47 /* the float case */ 48 %define %swig_cplxflt_conv(Type, Constructor, Real, Imag) 49 %fragment(SWIG_AsVal_frag(Type),"header", 50 fragment=SWIG_AsVal_frag(float)) { 51 SWIGINTERN int 52 SWIG_AsVal(Type)(PyObject *o, Type *val) 53 { 54 if (PyComplex_Check(o)) { 55 double re = PyComplex_RealAsDouble(o); 56 double im = PyComplex_ImagAsDouble(o); 57 if ((-FLT_MAX <= re && re <= FLT_MAX) && (-FLT_MAX <= im && im <= FLT_MAX)) { 58 if (val) *val = Constructor(%numeric_cast(re, float), 59 %numeric_cast(im, float)); 60 return SWIG_OK; 61 } else { 62 return SWIG_OverflowError; 63 } 64 } else { 65 float re; 66 int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re)); 67 if (SWIG_IsOK(res)) { 68 if (val) *val = Constructor(re, 0.0); 69 return res; 70 } 71 } 72 return SWIG_TypeError; 73 } 74 } 75 76 %swig_fromcplx_conv(Type, Real, Imag); 77 %enddef 78 79 #define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \ 80 %swig_cplxflt_conv(Type, Constructor, Real, Imag) 81 82 83 #define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \ 84 %swig_cplxdbl_conv(Type, Constructor, Real, Imag) 85 86 87