PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/python/pycomplex.swg

#
Unknown | 86 lines | 76 code | 10 blank | 0 comment | 0 complexity | 49897beacb2dfe883d5c9614238a8dbf MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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. See the std_complex.i and ccomplex.i for concret examples.
  7. */
  8. /* the common from converter */
  9. %define %swig_fromcplx_conv(Type, Real, Imag)
  10. %fragment(SWIG_From_frag(Type),"header")
  11. {
  12. SWIGINTERNINLINE PyObject*
  13. SWIG_From(Type)(%ifcplusplus(const Type&, Type) c)
  14. {
  15. return PyComplex_FromDoubles(Real(c), Imag(c));
  16. }
  17. }
  18. %enddef
  19. /* the double case */
  20. %define %swig_cplxdbl_conv(Type, Constructor, Real, Imag)
  21. %fragment(SWIG_AsVal_frag(Type),"header",
  22. fragment=SWIG_AsVal_frag(double))
  23. {
  24. SWIGINTERN int
  25. SWIG_AsVal(Type) (PyObject *o, Type* val)
  26. {
  27. if (PyComplex_Check(o)) {
  28. if (val) *val = Constructor(PyComplex_RealAsDouble(o), PyComplex_ImagAsDouble(o));
  29. return SWIG_OK;
  30. } else {
  31. double d;
  32. int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d));
  33. if (SWIG_IsOK(res)) {
  34. if (val) *val = Constructor(d, 0.0);
  35. return res;
  36. }
  37. }
  38. return SWIG_TypeError;
  39. }
  40. }
  41. %swig_fromcplx_conv(Type, Real, Imag);
  42. %enddef
  43. /* the float case */
  44. %define %swig_cplxflt_conv(Type, Constructor, Real, Imag)
  45. %fragment(SWIG_AsVal_frag(Type),"header",
  46. fragment=SWIG_AsVal_frag(float)) {
  47. SWIGINTERN int
  48. SWIG_AsVal(Type)(PyObject *o, Type *val)
  49. {
  50. if (PyComplex_Check(o)) {
  51. double re = PyComplex_RealAsDouble(o);
  52. double im = PyComplex_ImagAsDouble(o);
  53. if ((-FLT_MAX <= re && re <= FLT_MAX) && (-FLT_MAX <= im && im <= FLT_MAX)) {
  54. if (val) *val = Constructor(%numeric_cast(re, float),
  55. %numeric_cast(im, float));
  56. return SWIG_OK;
  57. } else {
  58. return SWIG_OverflowError;
  59. }
  60. } else {
  61. float re;
  62. int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re));
  63. if (SWIG_IsOK(res)) {
  64. if (val) *val = Constructor(re, 0.0);
  65. return res;
  66. }
  67. }
  68. return SWIG_TypeError;
  69. }
  70. }
  71. %swig_fromcplx_conv(Type, Real, Imag);
  72. %enddef
  73. #define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \
  74. %swig_cplxflt_conv(Type, Constructor, Real, Imag)
  75. #define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \
  76. %swig_cplxdbl_conv(Type, Constructor, Real, Imag)