Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.2 KiB

  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. /*
  9. Ruby does not have native complex numbers. They are an extension in the
  10. STD library.
  11. */
  12. %{
  13. static VALUE swig_rb_cComplex = Qnil;
  14. static ID swig_real_id = 0;
  15. static ID swig_imag_id = 0;
  16. int Ruby_Is_Complex( VALUE obj )
  17. {
  18. return ( (rb_respond_to( obj, swig_real_id ) == Qtrue) &&
  19. (rb_respond_to( obj, swig_imag_id ) == Qtrue) );
  20. }
  21. %}
  22. %init {
  23. rb_require("complex");
  24. swig_rb_cComplex = rb_const_get( rb_cObject, rb_intern("Complex") );
  25. if( swig_rb_cComplex == Qnil )
  26. rb_warn("Ruby's complex.so not found");
  27. swig_real_id = rb_intern("real");
  28. swig_imag_id = rb_intern("imag");
  29. }
  30. /* the common from conversor */
  31. %define %swig_fromcplx_conv(Type, Real, Imag)
  32. %fragment(SWIG_From_frag(Type),"header")
  33. {
  34. SWIGINTERNINLINE VALUE
  35. SWIG_From(Type)(%ifcplusplus(const Type&, Type) c)
  36. {
  37. VALUE args[] = {
  38. rb_float_new(Real(c)),
  39. rb_float_new(Imag(c))
  40. };
  41. return rb_class_new_instance(2, args, swig_rb_cComplex);
  42. }
  43. }
  44. %enddef
  45. /* the double case */
  46. %define %swig_cplxdbl_conv(Type, Constructor, Real, Imag)
  47. %fragment(SWIG_AsVal_frag(Type),"header",
  48. fragment=SWIG_AsVal_frag(double))
  49. {
  50. SWIGINTERN int
  51. SWIG_AsVal(Type) (VALUE o, Type* val)
  52. {
  53. if ( Ruby_Is_Complex( o ) ) {
  54. if (val) {
  55. VALUE real = rb_funcall(o, swig_real_id, 0 );
  56. VALUE imag = rb_funcall(o, swig_imag_id, 0 );
  57. double re = 0;
  58. SWIG_AsVal_double( real, &re );
  59. double im = 0;
  60. SWIG_AsVal_double( imag, &im );
  61. *val = Constructor(re, im);
  62. }
  63. return SWIG_OK;
  64. } else {
  65. double d;
  66. int res = SWIG_AddCast(SWIG_AsVal(double)(o, &d));
  67. if (SWIG_IsOK(res)) {
  68. if (val) *val = Constructor(d, 0.0);
  69. return res;
  70. }
  71. }
  72. return SWIG_TypeError;
  73. }
  74. }
  75. %swig_fromcplx_conv(Type, Real, Imag);
  76. %enddef
  77. /* the float case */
  78. %define %swig_cplxflt_conv(Type, Constructor, Real, Imag)
  79. %fragment(SWIG_AsVal_frag(Type),"header",
  80. fragment=SWIG_AsVal_frag(float),
  81. fragment=SWIG_AsVal_frag(double)) {
  82. SWIGINTERN int
  83. SWIG_AsVal(Type)(VALUE o, Type *val)
  84. {
  85. if ( Ruby_Is_Complex( o ) ) {
  86. VALUE real = rb_funcall(o, swig_real_id, 0 );
  87. VALUE imag = rb_funcall(o, swig_imag_id, 0 );
  88. double re = 0;
  89. SWIG_AsVal_double( real, &re );
  90. double im = 0;
  91. SWIG_AsVal_double( imag, &im );
  92. if ((-FLT_MAX <= re && re <= FLT_MAX) &&
  93. (-FLT_MAX <= im && im <= FLT_MAX)) {
  94. if (val) *val = Constructor(%numeric_cast(re, float),
  95. %numeric_cast(im, float));
  96. return SWIG_OK;
  97. } else {
  98. return SWIG_OverflowError;
  99. }
  100. } else {
  101. float re;
  102. int res = SWIG_AddCast(SWIG_AsVal(float)(o, &re));
  103. if (SWIG_IsOK(res)) {
  104. if (val) *val = Constructor(re, 0.0);
  105. return res;
  106. }
  107. }
  108. return SWIG_TypeError;
  109. }
  110. }
  111. %swig_fromcplx_conv(Type, Real, Imag);
  112. %enddef
  113. #define %swig_cplxflt_convn(Type, Constructor, Real, Imag) \
  114. %swig_cplxflt_conv(Type, Constructor, Real, Imag)
  115. #define %swig_cplxdbl_convn(Type, Constructor, Real, Imag) \
  116. %swig_cplxdbl_conv(Type, Constructor, Real, Imag)