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.

162 lines
4.1 KiB

  1. /**
  2. * @file rubystdfunctors.swg
  3. * @date Sun May 6 00:44:33 2007
  4. *
  5. * @brief This file provides unary and binary functors for STL
  6. * containers, that will invoke a Ruby proc or method to do
  7. * their operation.
  8. *
  9. * You can use them in a swig file like:
  10. *
  11. * %include <std_set.i>
  12. * %include <std_functors.i>
  13. *
  14. * %template< IntSet > std::set< int, swig::BinaryPredicate<> >;
  15. *
  16. *
  17. * which will then allow calling them from Ruby either like:
  18. *
  19. * # order of set is defined by C++ default
  20. * a = IntSet.new
  21. *
  22. * # sort order defined by Ruby proc
  23. * b = IntSet.new( proc { |a,b| a > b } )
  24. *
  25. */
  26. %include rubyclasses.swg
  27. namespace swig {
  28. %apply GC_VALUE { UnaryPredicate, BinaryPredicate, UnaryFunction,
  29. BinaryFunction };
  30. %typecheck(SWIG_TYPECHECK_POINTER,noblock=1)
  31. UnaryPredicate, UnaryPredicate&, UnaryFunction, UnaryFunction&
  32. {
  33. $1 = SWIG_Ruby_isCallable($input) && SWIG_Ruby_arity($input, 1);
  34. }
  35. %typecheck(SWIG_TYPECHECK_POINTER,noblock=1)
  36. BinaryPredicate, BinaryPredicate&, BinaryFunction, BinaryFunction& {
  37. $1 = SWIG_Ruby_isCallable($input) && SWIG_Ruby_arity($input, 2);
  38. }
  39. %typemap(in,noblock=1) BinaryFunction&, BinaryFunction {
  40. $1 = new swig::BinaryFunction< >($input);
  41. }
  42. %typemap(in,noblock=1) UnaryFunction&, UnaryFunction {
  43. $1 = new swig::UnaryFunction< >($input);
  44. }
  45. %typemap(in,noblock=1) BinaryPredicate&, BinaryPredicate {
  46. $1 = new swig::BinaryPredicate<>($input);
  47. }
  48. %typemap(in,noblock=1) UnaryPredicate&, UnaryPredicate {
  49. $1 = new swig::UnaryPredicate< >($input);
  50. }
  51. %ignore BinaryFunction;
  52. template< class _T = GC_VALUE >
  53. struct BinaryFunction {
  54. };
  55. %ignore UnaryFunction;
  56. template< class _T = GC_VALUE >
  57. struct UnaryFunction {
  58. };
  59. %ignore BinaryPredicate;
  60. template< class _T = GC_VALUE >
  61. struct BinaryPredicate {
  62. };
  63. %ignore UnaryPredicate;
  64. template< class _T = GC_VALUE >
  65. struct UnaryPredicate {
  66. };
  67. }
  68. %fragment("StdFunctors","header",fragment="StdTraits")
  69. {
  70. namespace swig {
  71. static ID call_id = rb_intern("call");
  72. template <class _T = GC_VALUE, class _DefaultFunc = std::less<GC_VALUE> >
  73. struct BinaryPredicate : GC_VALUE, std::binary_function< _T, _T, bool >
  74. {
  75. BinaryPredicate(VALUE obj = Qnil) : GC_VALUE(obj) { }
  76. bool operator()(_T a, _T b) const
  77. {
  78. if (_obj != Qnil) {
  79. SWIG_RUBY_THREAD_BEGIN_BLOCK;
  80. VALUE arg1 = swig::from(a);
  81. VALUE arg2 = swig::from(b);
  82. VALUE res = rb_funcall( _obj, swig::call_id, 2, arg1, arg2);
  83. SWIG_RUBY_THREAD_END_BLOCK;
  84. return RTEST(res);
  85. } else {
  86. return _DefaultFunc()(a, b);
  87. }
  88. }
  89. };
  90. template <class _T = GC_VALUE, class _DefaultFunc = std::less< _T > >
  91. struct BinaryFunction : GC_VALUE, std::binary_function< _T, _T, _T >
  92. {
  93. BinaryFunction(VALUE obj = Qnil) : GC_VALUE(obj) { }
  94. _T operator()(_T a, _T b) const
  95. {
  96. if (_obj != Qnil) {
  97. SWIG_RUBY_THREAD_BEGIN_BLOCK;
  98. VALUE arg1 = swig::from(a);
  99. VALUE arg2 = swig::from(b);
  100. VALUE res = rb_funcall( _obj, swig::call_id, 2, arg1, arg2);
  101. SWIG_RUBY_THREAD_END_BLOCK;
  102. return swig::as<_T >(res);
  103. } else {
  104. return _DefaultFunc()(a, b);
  105. }
  106. }
  107. };
  108. template< class _T = GC_VALUE >
  109. struct UnaryPredicate : GC_VALUE, std::unary_function< _T, bool >
  110. {
  111. UnaryPredicate(VALUE obj = Qnil) : GC_VALUE(obj) { }
  112. bool operator()(_T a) const
  113. {
  114. SWIG_RUBY_THREAD_BEGIN_BLOCK;
  115. VALUE arg1 = swig::from<_T >(a);
  116. VALUE res = rb_funcall( _obj, swig::call_id, 1, arg1);
  117. SWIG_RUBY_THREAD_END_BLOCK;
  118. return RTEST(res);
  119. }
  120. };
  121. template< class _T = GC_VALUE >
  122. struct UnaryFunction : GC_VALUE, std::unary_function< _T, _T >
  123. {
  124. UnaryFunction(VALUE obj = Qnil) : GC_VALUE(obj) { }
  125. _T operator()(_T a) const
  126. {
  127. SWIG_RUBY_THREAD_BEGIN_BLOCK;
  128. VALUE arg1 = swig::from(a);
  129. VALUE res = rb_funcall( _obj, swig::call_id, 1, VALUE(arg1));
  130. SWIG_RUBY_THREAD_END_BLOCK;
  131. return swig::as< _T >(res);
  132. }
  133. };
  134. } // namespace swig
  135. }