rubyhead.swg 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <ruby.h>
  2. /* Ruby 1.9.1 has a "memoisation optimisation" when compiling with GCC which
  3. * breaks using rb_intern as an lvalue, as SWIG does. We work around this
  4. * issue for now by disabling this.
  5. * https://sourceforge.net/tracker/?func=detail&aid=2859614&group_id=1645&atid=101645
  6. */
  7. #ifdef rb_intern
  8. # undef rb_intern
  9. #endif
  10. /* Remove global macros defined in Ruby's win32.h */
  11. #ifdef write
  12. # undef write
  13. #endif
  14. #ifdef read
  15. # undef read
  16. #endif
  17. #ifdef bind
  18. # undef bind
  19. #endif
  20. #ifdef close
  21. # undef close
  22. #endif
  23. #ifdef connect
  24. # undef connect
  25. #endif
  26. /* Ruby 1.7 defines NUM2LL(), LL2NUM() and ULL2NUM() macros */
  27. #ifndef NUM2LL
  28. #define NUM2LL(x) NUM2LONG((x))
  29. #endif
  30. #ifndef LL2NUM
  31. #define LL2NUM(x) INT2NUM((long) (x))
  32. #endif
  33. #ifndef ULL2NUM
  34. #define ULL2NUM(x) UINT2NUM((unsigned long) (x))
  35. #endif
  36. /* Ruby 1.7 doesn't (yet) define NUM2ULL() */
  37. #ifndef NUM2ULL
  38. #ifdef HAVE_LONG_LONG
  39. #define NUM2ULL(x) rb_num2ull((x))
  40. #else
  41. #define NUM2ULL(x) NUM2ULONG(x)
  42. #endif
  43. #endif
  44. /* RSTRING_LEN, etc are new in Ruby 1.9, but ->ptr and ->len no longer work */
  45. /* Define these for older versions so we can just write code the new way */
  46. #ifndef RSTRING_LEN
  47. # define RSTRING_LEN(x) RSTRING(x)->len
  48. #endif
  49. #ifndef RSTRING_PTR
  50. # define RSTRING_PTR(x) RSTRING(x)->ptr
  51. #endif
  52. #ifndef RSTRING_END
  53. # define RSTRING_END(x) (RSTRING_PTR(x) + RSTRING_LEN(x))
  54. #endif
  55. #ifndef RARRAY_LEN
  56. # define RARRAY_LEN(x) RARRAY(x)->len
  57. #endif
  58. #ifndef RARRAY_PTR
  59. # define RARRAY_PTR(x) RARRAY(x)->ptr
  60. #endif
  61. #ifndef RFLOAT_VALUE
  62. # define RFLOAT_VALUE(x) RFLOAT(x)->value
  63. #endif
  64. #ifndef DOUBLE2NUM
  65. # define DOUBLE2NUM(x) rb_float_new(x)
  66. #endif
  67. #ifndef RHASH_TBL
  68. # define RHASH_TBL(x) (RHASH(x)->tbl)
  69. #endif
  70. #ifndef RHASH_ITER_LEV
  71. # define RHASH_ITER_LEV(x) (RHASH(x)->iter_lev)
  72. #endif
  73. #ifndef RHASH_IFNONE
  74. # define RHASH_IFNONE(x) (RHASH(x)->ifnone)
  75. #endif
  76. #ifndef RHASH_SIZE
  77. # define RHASH_SIZE(x) (RHASH(x)->tbl->num_entries)
  78. #endif
  79. #ifndef RHASH_EMPTY_P
  80. # define RHASH_EMPTY_P(x) (RHASH_SIZE(x) == 0)
  81. #endif
  82. #ifndef RSTRUCT_LEN
  83. # define RSTRUCT_LEN(x) RSTRUCT(x)->len
  84. #endif
  85. #ifndef RSTRUCT_PTR
  86. # define RSTRUCT_PTR(x) RSTRUCT(x)->ptr
  87. #endif
  88. /*
  89. * Need to be very careful about how these macros are defined, especially
  90. * when compiling C++ code or C code with an ANSI C compiler.
  91. *
  92. * VALUEFUNC(f) is a macro used to typecast a C function that implements
  93. * a Ruby method so that it can be passed as an argument to API functions
  94. * like rb_define_method() and rb_define_singleton_method().
  95. *
  96. * VOIDFUNC(f) is a macro used to typecast a C function that implements
  97. * either the "mark" or "free" stuff for a Ruby Data object, so that it
  98. * can be passed as an argument to API functions like Data_Wrap_Struct()
  99. * and Data_Make_Struct().
  100. */
  101. #ifdef __cplusplus
  102. # ifndef RUBY_METHOD_FUNC /* These definitions should work for Ruby 1.4.6 */
  103. # define PROTECTFUNC(f) ((VALUE (*)()) f)
  104. # define VALUEFUNC(f) ((VALUE (*)()) f)
  105. # define VOIDFUNC(f) ((void (*)()) f)
  106. # else
  107. # ifndef ANYARGS /* These definitions should work for Ruby 1.6 */
  108. # define PROTECTFUNC(f) ((VALUE (*)()) f)
  109. # define VALUEFUNC(f) ((VALUE (*)()) f)
  110. # define VOIDFUNC(f) ((RUBY_DATA_FUNC) f)
  111. # else /* These definitions should work for Ruby 1.7+ */
  112. # define PROTECTFUNC(f) ((VALUE (*)(VALUE)) f)
  113. # define VALUEFUNC(f) ((VALUE (*)(ANYARGS)) f)
  114. # define VOIDFUNC(f) ((RUBY_DATA_FUNC) f)
  115. # endif
  116. # endif
  117. #else
  118. # define VALUEFUNC(f) (f)
  119. # define VOIDFUNC(f) (f)
  120. #endif
  121. /* Don't use for expressions have side effect */
  122. #ifndef RB_STRING_VALUE
  123. #define RB_STRING_VALUE(s) (TYPE(s) == T_STRING ? (s) : (*(volatile VALUE *)&(s) = rb_str_to_str(s)))
  124. #endif
  125. #ifndef StringValue
  126. #define StringValue(s) RB_STRING_VALUE(s)
  127. #endif
  128. #ifndef StringValuePtr
  129. #define StringValuePtr(s) RSTRING_PTR(RB_STRING_VALUE(s))
  130. #endif
  131. #ifndef StringValueLen
  132. #define StringValueLen(s) RSTRING_LEN(RB_STRING_VALUE(s))
  133. #endif
  134. #ifndef SafeStringValue
  135. #define SafeStringValue(v) do {\
  136. StringValue(v);\
  137. rb_check_safe_str(v);\
  138. } while (0)
  139. #endif
  140. #ifndef HAVE_RB_DEFINE_ALLOC_FUNC
  141. #define rb_define_alloc_func(klass, func) rb_define_singleton_method((klass), "new", VALUEFUNC((func)), -1)
  142. #define rb_undef_alloc_func(klass) rb_undef_method(CLASS_OF((klass)), "new")
  143. #endif
  144. static VALUE _mSWIG = Qnil;