ruby_functor.hpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Pingus - A free Lemmings clone
  2. // Copyright (C) 2002 Ingo Ruhnke <grumbel@gmx.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_RUBY_FUNCTOR_HXX
  17. #define HEADER_RUBY_FUNCTOR_HXX
  18. #include <iostream>
  19. #include "ruby.h"
  20. #include "ruby_object.hpp"
  21. #include "flexlay_wrap.hpp"
  22. /** */
  23. class RubyFunctor
  24. {
  25. private:
  26. RubyObject val;
  27. public:
  28. RubyFunctor(const RubyObject& val_);
  29. ~RubyFunctor();
  30. void operator()();
  31. void operator()(int i);
  32. void operator()(int x, int y);
  33. /** Print backtrace in case of error */
  34. static void print_error();
  35. // FIXME: Protect these function calls somehow
  36. template<class C> void operator()(const C& c)
  37. {
  38. if (1) {
  39. rb_funcall(val.ptr(), rb_intern("call"), 1,
  40. convert_to_ruby_value(c));
  41. } else {
  42. //VALUE arg1 = convert_to_ruby_value(c);
  43. //rb_funcall(val.ptr(), rb_intern("call"), 1, arg1);
  44. int state = 0;
  45. VALUE args[2];
  46. args[0] = reinterpret_cast<VALUE>(this);
  47. args[1] = convert_to_ruby_value(c);
  48. rb_protect(&RubyFunctor::funcall_protect1, reinterpret_cast<VALUE>(args), &state);
  49. if (state)
  50. print_error();
  51. }
  52. }
  53. template<class C, class D> void operator()(const C& c, const D& d)
  54. {
  55. if (1) {
  56. rb_funcall(val.ptr(), rb_intern("call"), 2,
  57. convert_to_ruby_value(c),
  58. convert_to_ruby_value(d));
  59. } else {
  60. std::cout << "Calling operator() with two args" << std::endl;
  61. int state = 0;
  62. VALUE args[2];
  63. args[0] = reinterpret_cast<VALUE>(this);
  64. args[1] = convert_to_ruby_value(c);
  65. args[2] = convert_to_ruby_value(d);
  66. rb_protect(&RubyFunctor::funcall_protect2, reinterpret_cast<VALUE>(args), &state);
  67. if (state)
  68. print_error();
  69. }
  70. }
  71. static VALUE funcall_protect(VALUE self);
  72. static VALUE funcall_protect1(VALUE self);
  73. static VALUE funcall_protect2(VALUE self);
  74. };
  75. #endif
  76. /* EOF */