octcomplex.swg 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. See the std_complex.i and ccomplex.i for concrete examples.
  7. */
  8. /* the common from conversor */
  9. %define %swig_fromcplx_conv(Type, OctConstructor, Real, Imag)
  10. %fragment(SWIG_From_frag(Type),"header")
  11. {
  12. SWIGINTERNINLINE octave_value
  13. SWIG_From(Type)(const Type& c)
  14. {
  15. return octave_value(OctConstructor(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) (const octave_value& ov, Type* val)
  26. {
  27. if (ov.is_complex_scalar()) {
  28. if (val) {
  29. Complex c(ov.complex_value());
  30. *val=Constructor(c.real(),c.imag());
  31. }
  32. return SWIG_OK;
  33. } else {
  34. double d;
  35. int res = SWIG_AddCast(SWIG_AsVal(double)(ov, &d));
  36. if (SWIG_IsOK(res)) {
  37. if (val)
  38. *val = Constructor(d, 0.0);
  39. return res;
  40. }
  41. }
  42. return SWIG_TypeError;
  43. }
  44. }
  45. %swig_fromcplx_conv(Type, Complex, Real, Imag);
  46. %enddef
  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) (const octave_value& ov, Type* val)
  53. {
  54. if (ov.is_complex_scalar()) {
  55. if (val) {
  56. Complex c(ov.complex_value());
  57. double re = c.real();
  58. double im = c.imag();
  59. if ((-FLT_MAX <= re && re <= FLT_MAX) && (-FLT_MAX <= im && im <= FLT_MAX)) {
  60. if (val)
  61. *val = Constructor(%numeric_cast(re, float),
  62. %numeric_cast(im, float));
  63. return SWIG_OK;
  64. } else
  65. return SWIG_OverflowError;
  66. }
  67. } else {
  68. float d;
  69. int res = SWIG_AddCast(SWIG_AsVal(float)(ov, &d));
  70. if (SWIG_IsOK(res)) {
  71. if (val)
  72. *val = Constructor(d, 0.0);
  73. return res;
  74. }
  75. }
  76. return SWIG_TypeError;
  77. }
  78. }
  79. %swig_fromcplx_conv(Type, FloatComplex, Real, Imag);
  80. %enddef
  81. #define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \
  82. %swig_cplxflt_conv(Type, Constructor, Real, Imag)
  83. #define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \
  84. %swig_cplxdbl_conv(Type, Constructor, Real, Imag)