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.

216 lines
5.4 KiB

  1. %fragment("StdTraits","header",fragment="StdTraitsCommon")
  2. {
  3. namespace swig {
  4. /*
  5. Traits that provides the from method
  6. */
  7. template <class Type> struct traits_from_ptr {
  8. static SWIG_Object from(Type *val, int owner = 0) {
  9. return SWIG_NewPointerObj(val, type_info<Type>(), owner);
  10. }
  11. };
  12. template <class Type> struct traits_from {
  13. static SWIG_Object from(const Type& val) {
  14. return traits_from_ptr<Type>::from(new Type(val), 1);
  15. }
  16. };
  17. template <class Type> struct traits_from<Type *> {
  18. static SWIG_Object from(Type* val) {
  19. return traits_from_ptr<Type>::from(val, 0);
  20. }
  21. };
  22. template <class Type>
  23. inline SWIG_Object from(const Type& val) {
  24. return traits_from<Type>::from(val);
  25. }
  26. template <class Type>
  27. inline SWIG_Object from_ptr(Type* val, int owner) {
  28. return traits_from_ptr<Type>::from(val, owner);
  29. }
  30. /*
  31. Traits that provides the asval/as/check method
  32. */
  33. template <class Type>
  34. struct traits_asptr {
  35. static int asptr(SWIG_Object obj, Type **val) {
  36. Type *p;
  37. int res = (SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0) == SWIG_OK) ? SWIG_OLDOBJ : 0;
  38. if (SWIG_IsOK(res)) {
  39. if (val) *val = p;
  40. }
  41. return res;
  42. }
  43. };
  44. template <class Type>
  45. inline int asptr(SWIG_Object obj, Type **vptr) {
  46. return traits_asptr<Type>::asptr(obj, vptr);
  47. }
  48. template <class Type>
  49. struct traits_asval {
  50. static int asval(SWIG_Object obj, Type *val) {
  51. if (val) {
  52. Type *p = 0;
  53. int res = traits_asptr<Type>::asptr(obj, &p);
  54. if (!SWIG_IsOK(res)) return res;
  55. if (p) {
  56. typedef typename noconst_traits<Type>::noconst_type noconst_type;
  57. *(const_cast<noconst_type*>(val)) = *p;
  58. if (SWIG_IsNewObj(res)){
  59. %delete(p);
  60. res = SWIG_DelNewMask(res);
  61. }
  62. return res;
  63. } else {
  64. return SWIG_ERROR;
  65. }
  66. } else {
  67. return traits_asptr<Type>::asptr(obj, (Type **)(0));
  68. }
  69. }
  70. };
  71. template <class Type> struct traits_asval<Type*> {
  72. static int asval(SWIG_Object obj, Type **val) {
  73. if (val) {
  74. typedef typename noconst_traits<Type>::noconst_type noconst_type;
  75. noconst_type *p = 0;
  76. int res = traits_asptr<noconst_type>::asptr(obj, &p);
  77. if (SWIG_IsOK(res)) {
  78. *(const_cast<noconst_type**>(val)) = p;
  79. }
  80. return res;
  81. } else {
  82. return traits_asptr<Type>::asptr(obj, (Type **)(0));
  83. }
  84. }
  85. };
  86. template <class Type>
  87. inline int asval(SWIG_Object obj, Type *val) {
  88. return traits_asval<Type>::asval(obj, val);
  89. }
  90. template <class Type>
  91. struct traits_as<Type, value_category> {
  92. static Type as(SWIG_Object obj, bool throw_error) {
  93. Type v;
  94. int res = asval(obj, &v);
  95. if (!obj || !SWIG_IsOK(res)) {
  96. // if (!PyErr_Occurred()) {
  97. // %type_error(swig::type_name<Type>());
  98. // }
  99. if (throw_error) throw std::invalid_argument("bad type");
  100. }
  101. return v;
  102. }
  103. };
  104. template <class Type>
  105. struct traits_as<Type, pointer_category> {
  106. static Type as(SWIG_Object obj, bool throw_error) {
  107. Type *v = 0;
  108. int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
  109. if (SWIG_IsOK(res) && v) {
  110. if (SWIG_IsNewObj(res)) {
  111. Type r(*v);
  112. %delete(v);
  113. return r;
  114. } else {
  115. return *v;
  116. }
  117. } else {
  118. // Uninitialized return value, no Type() constructor required.
  119. static Type *v_def = (Type*) malloc(sizeof(Type));
  120. // if (!PyErr_Occurred()) {
  121. // %type_error(swig::type_name<Type>());
  122. // }
  123. if (throw_error) throw std::invalid_argument("bad type");
  124. memset(v_def,0,sizeof(Type));
  125. return *v_def;
  126. }
  127. }
  128. };
  129. template <class Type>
  130. struct traits_as<Type*, pointer_category> {
  131. static Type* as(SWIG_Object obj, bool throw_error) {
  132. Type *v = 0;
  133. int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
  134. if (SWIG_IsOK(res)) {
  135. return v;
  136. } else {
  137. // if (!PyErr_Occurred()) {
  138. // %type_error(swig::type_name<Type>());
  139. // }
  140. if (throw_error) throw std::invalid_argument("bad type");
  141. return 0;
  142. }
  143. }
  144. };
  145. template <class Type>
  146. inline Type as(SWIG_Object obj, bool te = false) {
  147. return traits_as<Type, typename traits<Type>::category>::as(obj, te);
  148. }
  149. template <class Type>
  150. struct traits_check<Type, value_category> {
  151. static bool check(SWIG_Object obj) {
  152. int res = obj ? asval(obj, (Type *)(0)) : SWIG_ERROR;
  153. return SWIG_IsOK(res) ? true : false;
  154. }
  155. };
  156. template <class Type>
  157. struct traits_check<Type, pointer_category> {
  158. static bool check(SWIG_Object obj) {
  159. int res = obj ? asptr(obj, (Type **)(0)) : SWIG_ERROR;
  160. return SWIG_IsOK(res) ? true : false;
  161. }
  162. };
  163. template <class Type>
  164. inline bool check(SWIG_Object obj) {
  165. return traits_check<Type, typename traits<Type>::category>::check(obj);
  166. }
  167. }
  168. }
  169. %define %specialize_std_container(Type,Check,As,From)
  170. %{
  171. namespace swig {
  172. template <> struct traits_asval<Type > {
  173. typedef Type value_type;
  174. static int asval(SWIG_Object obj, value_type *val) {
  175. if (Check(obj)) {
  176. if (val) *val = As(obj);
  177. return SWIG_OK;
  178. }
  179. return SWIG_ERROR;
  180. }
  181. };
  182. template <> struct traits_from<Type > {
  183. typedef Type value_type;
  184. static SWIG_Object from(const value_type& val) {
  185. return From(val);
  186. }
  187. };
  188. template <>
  189. struct traits_check<Type, value_category> {
  190. static int check(SWIG_Object obj) {
  191. int res = Check(obj);
  192. return obj && res ? res : 0;
  193. }
  194. };
  195. }
  196. %}
  197. %enddef