tr_macro.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * This file contains macros to help generate the bindings
  3. * of functionality to MRuby.
  4. */
  5. #ifndef TR_MACRO_H_INCLUDED
  6. #define TR_MACRO_H_INCLUDED
  7. #define RH_WRAP(RUBY, TYPE, OBJECT) \
  8. mrb_obj_value(Data_Wrap_Struct(RUBY, \
  9. RUBY->object_class, &toruby_##TYPE, OBJECT));
  10. #define TR_NAME(NAME) tr_##NAME
  11. #define TR_PAIR_HELPER(NAME) TR_NAME(NAME), NAME
  12. #define TR_PAIR(NAME) TR_PAIR_HELPER(NAME)
  13. #define TR_MACRO_AID(MACRO, ARGS) MACRO ARGS
  14. #define TR_PAIR_DO_AID(MACRO, NAME) TR_MACRO_AID(MACRO, (TR_PAIR(NAME)))
  15. #define TR_PAIR_DO(MACRO, NAME) TR_PAIR_DO_AID(MACRO, NAME)
  16. #define TORUBY_0_ICALLER(NAME, TOCALL) \
  17. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  18. (void) self; (void) mrb; \
  19. return mrb_fixnum_value(TOCALL()); \
  20. }
  21. #define TORUBY_0_FGETTER(NAME, TOCALL) \
  22. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  23. (void) self; \
  24. return mrb_float_value(mrb, TOCALL()); \
  25. }
  26. #define TR_WRAP_NOARG_BOOL(NAME, TOCALL) \
  27. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  28. (void) self; (void) mrb; \
  29. return rh_bool_value(TOCALL()); \
  30. }
  31. #define TR_WRAP_I_BOOL(NAME, TOCALL) \
  32. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  33. mrb_int i1; \
  34. (void) self; \
  35. mrb_get_args(mrb, "i", &i1); \
  36. return rh_bool_value(TOCALL(i1)); \
  37. }
  38. #define TR_WRAP_B_BOOL(NAME, TOCALL) \
  39. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  40. mrb_value b1; \
  41. (void) self; \
  42. mrb_get_args(mrb, "o", &b1); \
  43. return rh_bool_value(TOCALL(rh_tobool(b1))); \
  44. }
  45. #define TR_WRAP_IZ_INT(NAME, TOCALL) \
  46. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  47. mrb_int i1 = -1; \
  48. char * str = NULL; \
  49. (void) self; \
  50. mrb_get_args(mrb, "iz", &i1, &str); \
  51. return mrb_fixnum_value(TOCALL(i1, str)); \
  52. }
  53. #define TR_WRAP_I_INT(NAME, TOCALL) \
  54. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  55. mrb_int i1; \
  56. mrb_get_args(mrb, "i", &i1); \
  57. (void) self; \
  58. return mrb_fixnum_value(TOCALL(i1)); \
  59. }
  60. #define TR_WRAP_II_INT(NAME, TOCALL) \
  61. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  62. mrb_int i1, i2; \
  63. mrb_get_args(mrb, "ii", &i1, &i2); \
  64. (void) self; \
  65. return mrb_fixnum_value(TOCALL(i1, i2)); \
  66. }
  67. #define TR_WRAP_III_INT(NAME, TOCALL) \
  68. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  69. mrb_int i1, i2, i3; \
  70. mrb_get_args(mrb, "iii", &i1, &i2, &i3) \
  71. (void) self; \
  72. return mrb_fixnum_value(TOCALL(i1, i2, i3)); \
  73. }
  74. #define TR_WRAP_IIII_INT(NAME, TOCALL) \
  75. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  76. mrb_int i1, i2, i3, i4; \
  77. mrb_get_args(mrb, "iiii", &i1, &i2, &i3, &i4); \
  78. (void) self; \
  79. return mrb_fixnum_value(TOCALL(i1, i2, i3, i4)); \
  80. }
  81. #define TR_WRAP_IIIIB_INT(NAME, TOCALL) \
  82. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  83. mrb_int i1, i2, i3, i4; \
  84. mrb_value b5; \
  85. (void) self; \
  86. mrb_get_args(mrb, "iiiib", &i1, &i2, &i3, &i4, &b5); \
  87. return mrb_fixnum_value(TOCALL(i1, i2, i3, i4, rh_tobool(b5))); \
  88. }
  89. #define TR_WRAP_NOARG_INT(NAME, TOCALL) \
  90. static mrb_value NAME(mrb_state * mrb, mrb_value self) { \
  91. (void) self; (void) mrb; \
  92. return mrb_fixnum_value(TOCALL()); \
  93. }
  94. #define TR_METHOD(MRB, CLASS, NAME, IMPL, FLAGS) \
  95. mrb_define_method((MRB), (CLASS), (NAME), (IMPL), (FLAGS))
  96. #define TR_METHOD_ARGC(MRB, CLASS, NAME, IMPL, ARGC)\
  97. mrb_define_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC))
  98. #define TR_METHOD_NOARG(MRB, CLASS, NAME, IMPL)\
  99. mrb_define_method((MRB), (CLASS), (NAME), (IMPL), ARGS_NONE())
  100. #define TR_METHOD_OPTARG(MRB, CLASS, NAME, IMPL, ARGC, OPTC) \
  101. mrb_define_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC) | ARGS_OPT(OPTC))
  102. #define TR_CLASS_METHOD(MRB, CLASS, NAME, IMPL, FLAGS)\
  103. mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), (FLAGS))
  104. #define TR_CLASS_METHOD_ARGC(MRB, CLASS, NAME, IMPL, ARGC)\
  105. mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC))
  106. #define TR_CLASS_METHOD_NOARG(MRB, CLASS, NAME, IMPL)\
  107. mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), ARGS_NONE())
  108. #define TR_CLASS_METHOD_OPTARG(MRB, CLASS, NAME, IMPL, ARGC, OPTC) \
  109. mrb_define_class_method((MRB), (CLASS), (NAME), (IMPL), ARGS_REQ(ARGC) | ARGS_OPT(OPTC))
  110. #define TR_CONST_INT(MRB, CLASS, NAME, VALUE) \
  111. mrb_define_const((MRB), (CLASS), (NAME), mrb_fixnum_value(VALUE))
  112. #define TR_CONST_INT_EASY(MRB, CLASS, PREFIX, NAME) \
  113. TR_CONST_INT(MRB, CLASS, #NAME, PREFIX##NAME)
  114. #define TR_CONST_INT_VALUE(MRB, CLASS, VALUE) \
  115. TR_CONST_INT(MRB, CLASS, #VALUE, VALUE)
  116. #endif // TR_H_INCLUDED