| 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 concret examples. |
| 8 | */ |
| 9 | |
| 10 | /* the common from conversor */ |
| 11 | %define %swig_fromcplx_conv(Type, Real, Imag) |
| 12 | %fragment(SWIG_From_frag(Type),"header") |
| 13 | %{ |
| 14 | SWIGINTERNINLINE PyObject* |
| 15 | SWIG_From(Type)(SWIG_cplusplus(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), |
| 32 | PyComplex_ImagAsDouble(o)); |
| 33 | return 1; |
| 34 | } else { |
| 35 | double d; |
| 36 | if (SWIG_AsVal(double)(o, &d)) { |
| 37 | if (val) *val = Constructor(d, 0.0); |
| 38 | return 1; |
| 39 | } else { |
| 40 | PyErr_Clear(); |
| 41 | } |
| 42 | } |
| 43 | if (val) { |
| 44 | SWIG_type_error("Type", o); |
| 45 | } |
| 46 | return 0; |
| 47 | } |
| 48 | %} |
| 49 | %swig_fromcplx_conv(Type, Real, Imag); |
| 50 | %enddef |
| 51 | |
| 52 | /* the float case */ |
| 53 | %define %swig_cplxflt_conv(Type, Constructor, Real, Imag) |
| 54 | %fragment(SWIG_AsVal_frag(Type),"header", |
| 55 | fragment="SWIG_CheckDoubleInRange", |
| 56 | fragment=SWIG_AsVal_frag(float)) { |
| 57 | SWIGINTERN int |
| 58 | SWIG_AsVal(Type)(PyObject *o, Type *val) |
| 59 | { |
| 60 | const char* errmsg = val ? #Type : 0; |
| 61 | if (PyComplex_Check(o)) { |
| 62 | double re = PyComplex_RealAsDouble(o); |
| 63 | double im = PyComplex_ImagAsDouble(o); |
| 64 | if (SWIG_CheckDoubleInRange(re, -FLT_MAX, FLT_MAX, errmsg) |
| 65 | && SWIG_CheckDoubleInRange(im, -FLT_MAX, FLT_MAX, errmsg)) { |
| 66 | if (val) *val = Constructor(SWIG_numeric_cast(re, float), |
| 67 | SWIG_numeric_cast(im, float)); |
| 68 | return 1; |
| 69 | } else { |
| 70 | return 0; |
| 71 | } |
| 72 | } else { |
| 73 | double re; |
| 74 | if (SWIG_AsVal(double)(o, &re)) { |
| 75 | if (SWIG_CheckDoubleInRange(re, -FLT_MAX, FLT_MAX, errmsg)) { |
| 76 | if (val) *val = Constructor(SWIG_numeric_cast(re,float), 0.0); |
| 77 | return 1; |
| 78 | } else { |
| 79 | return 0; |
| 80 | } |
| 81 | } else { |
| 82 | PyErr_Clear(); |
| 83 | } |
| 84 | } |
| 85 | if (val) { |
| 86 | SWIG_type_error("Type", o); |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | %swig_fromcplx_conv(Type, Real, Imag); |
| 93 | %enddef |
| 94 | |
| 95 | #define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \ |
| 96 | %swig_cplxflt_conv(Type, Constructor, Real, Imag) |
| 97 | |
| 98 | |
| 99 | #define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \ |
| 100 | %swig_cplxdbl_conv(Type, Constructor, Real, Imag) |
| 101 | |
| 102 | |