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.

265 lines
6.5 KiB

  1. /* ------------------------------------------------------------
  2. * The Ruby classes, for C++
  3. * ------------------------------------------------------------ */
  4. %include <rubyclasses.swg>
  5. %fragment("StdTraits","header",fragment="StdTraitsCommon")
  6. {
  7. namespace swig {
  8. /*
  9. Traits that provides the from method
  10. */
  11. template <class Type> struct traits_from_ptr {
  12. static VALUE from(Type *val, int owner = 0) {
  13. return SWIG_NewPointerObj(val, type_info<Type>(), owner);
  14. }
  15. };
  16. template <class Type> struct traits_from {
  17. static VALUE from(const Type& val) {
  18. return traits_from_ptr<Type>::from(new Type(val), 1);
  19. }
  20. };
  21. template <class Type> struct traits_from<Type *> {
  22. static VALUE from(Type* val) {
  23. return traits_from_ptr<Type>::from(val, 0);
  24. }
  25. };
  26. template <class Type> struct traits_from<const Type *> {
  27. static VALUE from(const Type* val) {
  28. return traits_from_ptr<Type>::from(const_cast<Type*>(val), 0);
  29. }
  30. };
  31. template <class Type>
  32. inline VALUE from(const Type& val) {
  33. return traits_from<Type>::from(val);
  34. }
  35. template <class Type>
  36. inline VALUE from_ptr(Type* val, int owner) {
  37. return traits_from_ptr<Type>::from(val, owner);
  38. }
  39. /*
  40. Traits that provides the asval/as/check method
  41. */
  42. template <class Type>
  43. struct traits_asptr {
  44. static int asptr(VALUE obj, Type **val) {
  45. Type *p;
  46. int res = SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0);
  47. if (SWIG_IsOK(res)) {
  48. if (val) *val = p;
  49. }
  50. return res;
  51. }
  52. };
  53. template <class Type>
  54. inline int asptr(VALUE obj, Type **vptr) {
  55. return traits_asptr<Type>::asptr(obj, vptr);
  56. }
  57. template <class Type>
  58. struct traits_asval {
  59. static int asval(VALUE obj, Type *val) {
  60. if (val) {
  61. Type *p = 0;
  62. int res = traits_asptr<Type>::asptr(obj, &p);
  63. if (!SWIG_IsOK(res)) return res;
  64. if (p) {
  65. typedef typename noconst_traits<Type>::noconst_type noconst_type;
  66. *(const_cast<noconst_type*>(val)) = *p;
  67. if (SWIG_IsNewObj(res)){
  68. %delete(p);
  69. res = SWIG_DelNewMask(res);
  70. }
  71. return res;
  72. } else {
  73. return SWIG_ERROR;
  74. }
  75. } else {
  76. return traits_asptr<Type>::asptr(obj, (Type **)(0));
  77. }
  78. }
  79. };
  80. template <class Type> struct traits_asval<Type*> {
  81. static int asval(VALUE obj, Type **val) {
  82. if (val) {
  83. typedef typename noconst_traits<Type>::noconst_type noconst_type;
  84. noconst_type *p = 0;
  85. int res = traits_asptr<noconst_type>::asptr(obj, &p);
  86. if (SWIG_IsOK(res)) {
  87. *(const_cast<noconst_type**>(val)) = p;
  88. }
  89. return res;
  90. } else {
  91. return traits_asptr<Type>::asptr(obj, (Type **)(0));
  92. }
  93. }
  94. };
  95. template <class Type>
  96. inline int asval(VALUE obj, Type *val) {
  97. return traits_asval<Type>::asval(obj, val);
  98. }
  99. template <class Type>
  100. struct traits_as<Type, value_category> {
  101. static Type as(VALUE obj, bool throw_error) {
  102. Type v;
  103. int res = asval(obj, &v);
  104. if (!obj || !SWIG_IsOK(res)) {
  105. if (throw_error) throw std::invalid_argument("bad type");
  106. VALUE lastErr = rb_gv_get("$!");
  107. if (lastErr == Qnil) {
  108. %type_error(swig::type_name<Type>());
  109. }
  110. }
  111. return v;
  112. }
  113. };
  114. template <class Type>
  115. struct traits_as<Type, pointer_category> {
  116. static Type as(VALUE obj, bool throw_error) {
  117. Type *v = 0;
  118. int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
  119. if (SWIG_IsOK(res) && v) {
  120. if (SWIG_IsNewObj(res)) {
  121. Type r(*v);
  122. %delete(v);
  123. return r;
  124. } else {
  125. return *v;
  126. }
  127. } else {
  128. // Uninitialized return value, no Type() constructor required.
  129. if (throw_error) throw std::invalid_argument("bad type");
  130. VALUE lastErr = rb_gv_get("$!");
  131. if (lastErr == Qnil) {
  132. %type_error(swig::type_name<Type>());
  133. }
  134. static Type *v_def = (Type*) malloc(sizeof(Type));
  135. memset(v_def,0,sizeof(Type));
  136. return *v_def;
  137. }
  138. }
  139. };
  140. template <class Type>
  141. struct traits_as<Type*, pointer_category> {
  142. static Type* as(VALUE obj, bool throw_error) {
  143. Type *v = 0;
  144. int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
  145. if (SWIG_IsOK(res)) {
  146. return v;
  147. } else {
  148. if (throw_error) throw std::invalid_argument("bad type");
  149. VALUE lastErr = rb_gv_get("$!");
  150. if (lastErr == Qnil) {
  151. %type_error(swig::type_name<Type>());
  152. }
  153. return 0;
  154. }
  155. }
  156. };
  157. template <class Type>
  158. inline Type as(VALUE obj, bool te = false) {
  159. return traits_as< Type, typename traits< Type >::category >::as(obj, te);
  160. }
  161. template <class Type>
  162. struct traits_check<Type, value_category> {
  163. static bool check(VALUE obj) {
  164. int res = obj ? asval(obj, (Type *)(0)) : SWIG_ERROR;
  165. return SWIG_IsOK(res) ? true : false;
  166. }
  167. };
  168. template <class Type>
  169. struct traits_check<Type, pointer_category> {
  170. static bool check(VALUE obj) {
  171. int res = obj ? asptr(obj, (Type **)(0)) : SWIG_ERROR;
  172. return SWIG_IsOK(res) ? true : false;
  173. }
  174. };
  175. template <class Type>
  176. inline bool check(VALUE obj) {
  177. return traits_check<Type, typename traits<Type>::category>::check(obj);
  178. }
  179. }
  180. }
  181. // Define GC marking template traits for a container type
  182. %define %create_mark_traits(Type)
  183. %{
  184. namespace swig {
  185. template <class T>
  186. struct mark_traits {
  187. typedef typename Type<T >::const_iterator const_iterator;
  188. inline void operator()(const Type<T >& c ) const
  189. {
  190. const_iterator i = c.begin();
  191. const_iterator e = c.end();
  192. for ( ; i != e; ++i )
  193. {
  194. rb_gc_mark( swig::from( &(*i) ) );
  195. }
  196. }
  197. };
  198. // Partial specializations for classes that requires no GC marking
  199. // or a special GC marking algorithm.
  200. template< >
  201. struct mark_traits<bool> {
  202. inline void operator()(const Type<bool >& c ) const {}
  203. };
  204. template< >
  205. struct mark_traits<char> {
  206. inline void operator()(const Type<char >& c ) const {}
  207. };
  208. template< >
  209. struct mark_traits<int> {
  210. inline void operator()(const Type<int >& c ) const {}
  211. };
  212. template< >
  213. struct mark_traits<unsigned> {
  214. inline void operator()(const Type<unsigned >& c ) const {}
  215. };
  216. template< >
  217. struct mark_traits<GC_VALUE> {
  218. typedef Type<GC_VALUE >::const_iterator const_iterator;
  219. inline void operator()(const Type<GC_VALUE >& c ) const {
  220. const_iterator i = c.begin();
  221. const_iterator e = c.end();
  222. for ( ; i != e; ++i )
  223. {
  224. VALUE v = *i;
  225. if ( FIXNUM_P(v) || SPECIAL_CONST_P(v) || SYMBOL_P(v) ||
  226. ( BUILTIN_TYPE(v) == T_NONE ) ) continue;
  227. rb_gc_mark( v );
  228. }
  229. }
  230. };
  231. }
  232. %}
  233. %enddef