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.

207 lines
5.2 KiB

  1. /*
  2. Pairs
  3. */
  4. %include <rubystdcommon.swg>
  5. //#define SWIG_STD_PAIR_ASVAL
  6. %fragment("StdPairTraits","header",fragment="StdTraits") {
  7. namespace swig {
  8. template <class T, class U >
  9. struct traits_asval<std::pair<T,U> > {
  10. typedef std::pair<T,U> value_type;
  11. static int get_pair(VALUE first, VALUE second,
  12. std::pair<T,U> *val)
  13. {
  14. if (val) {
  15. T *pfirst = &(val->first);
  16. int res1 = swig::asval((VALUE)first, pfirst);
  17. if (!SWIG_IsOK(res1)) return res1;
  18. U *psecond = &(val->second);
  19. int res2 = swig::asval((VALUE)second, psecond);
  20. if (!SWIG_IsOK(res2)) return res2;
  21. return res1 > res2 ? res1 : res2;
  22. } else {
  23. T *pfirst = 0;
  24. int res1 = swig::asval((VALUE)first, pfirst);
  25. if (!SWIG_IsOK(res1)) return res1;
  26. U *psecond = 0;
  27. int res2 = swig::asval((VALUE)second, psecond);
  28. if (!SWIG_IsOK(res2)) return res2;
  29. return res1 > res2 ? res1 : res2;
  30. }
  31. }
  32. static int asval(VALUE obj, std::pair<T,U> *val) {
  33. int res = SWIG_ERROR;
  34. if ( TYPE(obj) == T_ARRAY ) {
  35. if (RARRAY_LEN(obj) == 2) {
  36. VALUE first = rb_ary_entry(obj,0);
  37. VALUE second = rb_ary_entry(obj,1);
  38. res = get_pair(first, second, val);
  39. }
  40. } else {
  41. value_type *p;
  42. res = SWIG_ConvertPtr(obj,(void**)&p,
  43. swig::type_info<value_type>(),0);
  44. if (SWIG_IsOK(res) && val) *val = *p;
  45. }
  46. return res;
  47. }
  48. };
  49. template <class T, class U >
  50. struct traits_asptr<std::pair<T,U> > {
  51. typedef std::pair<T,U> value_type;
  52. static int get_pair(VALUE first, VALUE second,
  53. std::pair<T,U> **val)
  54. {
  55. if (val) {
  56. value_type *vp = %new_instance(std::pair<T,U>);
  57. T *pfirst = &(vp->first);
  58. int res1 = swig::asval((VALUE)first, pfirst);
  59. if (!SWIG_IsOK(res1)) return res1;
  60. U *psecond = &(vp->second);
  61. int res2 = swig::asval((VALUE)second, psecond);
  62. if (!SWIG_IsOK(res2)) return res2;
  63. *val = vp;
  64. return SWIG_AddNewMask(res1 > res2 ? res1 : res2);
  65. } else {
  66. T *pfirst = 0;
  67. int res1 = swig::asval((VALUE)first, pfirst);
  68. if (!SWIG_IsOK(res1)) return res1;
  69. U *psecond = 0;
  70. int res2 = swig::asval((VALUE)second, psecond);
  71. if (!SWIG_IsOK(res2)) return res2;
  72. return res1 > res2 ? res1 : res2;
  73. }
  74. }
  75. static int asptr(VALUE obj, std::pair<T,U> **val) {
  76. int res = SWIG_ERROR;
  77. if ( TYPE(obj) == T_ARRAY ) {
  78. if ( RARRAY_LEN(obj) == 2) {
  79. VALUE first = rb_ary_entry(obj,0);
  80. VALUE second = rb_ary_entry(obj,1);
  81. res = get_pair(first, second, val);
  82. }
  83. } else {
  84. value_type *p;
  85. res = SWIG_ConvertPtr(obj,(void**)&p,
  86. swig::type_info<value_type>(),0);
  87. if (SWIG_IsOK(res) && val) *val = p;
  88. }
  89. return res;
  90. }
  91. };
  92. template <class T, class U >
  93. struct traits_from<std::pair<T,U> > {
  94. static VALUE _wrap_pair_second( VALUE self )
  95. {
  96. std::pair< typename swig::noconst_traits<T >::noconst_type,U>* p = NULL;
  97. swig::asptr( self, &p );
  98. return swig::from( p->second );
  99. }
  100. static VALUE _wrap_pair_second_eq( VALUE self, VALUE arg )
  101. {
  102. std::pair< typename swig::noconst_traits<T >::noconst_type,U>* p = NULL;
  103. swig::asptr( self, &p );
  104. return swig::from( p->second );
  105. }
  106. static VALUE from(const std::pair<T,U>& val) {
  107. VALUE obj = rb_ary_new2(2);
  108. RARRAY_PTR(obj)[0] = swig::from<
  109. typename swig::noconst_traits<T >::noconst_type>(val.first);
  110. RARRAY_PTR(obj)[1] = swig::from(val.second);
  111. RARRAY_LEN(obj) = 2;
  112. rb_define_singleton_method(obj, "second",
  113. VALUEFUNC(_wrap_pair_second), 0 );
  114. rb_define_singleton_method(obj, "second=",
  115. VALUEFUNC(_wrap_pair_second_eq), 1 );
  116. rb_obj_freeze(obj); // treat as immutable tuple
  117. return obj;
  118. }
  119. };
  120. }
  121. }
  122. // Missing typemap
  123. %typemap(in) std::pair* (int res) {
  124. res = swig::asptr( $input, &$1 );
  125. if (!SWIG_IsOK(res))
  126. %argument_fail(res, "$1_type", $symname, $argnum);
  127. }
  128. %define %swig_pair_methods(pair...)
  129. %extend {
  130. VALUE inspect() const
  131. {
  132. VALUE tmp;
  133. VALUE str = rb_str_new2( swig::type_name< pair >() );
  134. str = rb_str_cat2( str, " (" );
  135. tmp = swig::from( $self->first );
  136. tmp = rb_obj_as_string( tmp );
  137. str = rb_str_buf_append( str, tmp );
  138. str = rb_str_cat2( str, "," );
  139. tmp = swig::from( $self->second );
  140. tmp = rb_obj_as_string( tmp );
  141. str = rb_str_buf_append( str, tmp );
  142. str = rb_str_cat2( str, ")" );
  143. return str;
  144. }
  145. VALUE to_s() const
  146. {
  147. VALUE tmp;
  148. VALUE str = rb_str_new2( "(" );
  149. tmp = swig::from( $self->first );
  150. tmp = rb_obj_as_string( tmp );
  151. str = rb_str_buf_append( str, tmp );
  152. str = rb_str_cat2( str, "," );
  153. tmp = swig::from( $self->second );
  154. tmp = rb_obj_as_string( tmp );
  155. str = rb_str_buf_append( str, tmp );
  156. str = rb_str_cat2( str, ")" );
  157. return str;
  158. }
  159. VALUE __getitem__( int index )
  160. {
  161. if (( index % 2 ) == 0 )
  162. return swig::from( $self->first );
  163. else
  164. return swig::from( $self->second );
  165. }
  166. VALUE __setitem__( int index, VALUE obj )
  167. {
  168. int res;
  169. if (( index % 2 ) == 0 )
  170. {
  171. res = swig::asval( obj, &($self->first) );
  172. }
  173. else
  174. {
  175. res = swig::asval(obj, &($self->second) );
  176. }
  177. if (!SWIG_IsOK(res))
  178. rb_raise( rb_eArgError, "invalid item for " #pair );
  179. return obj;
  180. }
  181. } // extend
  182. %enddef
  183. %include <std/std_pair.i>