PageRenderTime 57ms CodeModel.GetById 2ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/rel-1.3.35/Lib/python/pystdcommon.swg

#
Unknown | 259 lines | 232 code | 27 blank | 0 comment | 0 complexity | edc1ebdc1db99466505e576fbd78ce29 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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 PyObject *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 PyObject *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 PyObject *from(Type* val) {
  19. return traits_from_ptr<Type>::from(val, 0);
  20. }
  21. };
  22. template <class Type> struct traits_from<const Type *> {
  23. static PyObject *from(const Type* val) {
  24. return traits_from_ptr<Type>::from(const_cast<Type*>(val), 0);
  25. }
  26. };
  27. template <class Type>
  28. inline PyObject *from(const Type& val) {
  29. return traits_from<Type>::from(val);
  30. }
  31. template <class Type>
  32. inline PyObject *from_ptr(Type* val, int owner) {
  33. return traits_from_ptr<Type>::from(val, owner);
  34. }
  35. /*
  36. Traits that provides the asval/as/check method
  37. */
  38. template <class Type>
  39. struct traits_asptr {
  40. static int asptr(PyObject *obj, Type **val) {
  41. Type *p;
  42. int res = (SWIG_ConvertPtr(obj, (void**)&p, type_info<Type>(), 0) == SWIG_OK) ? SWIG_OLDOBJ : 0;
  43. if (SWIG_IsOK(res)) {
  44. if (val) *val = p;
  45. }
  46. return res;
  47. }
  48. };
  49. template <class Type>
  50. inline int asptr(PyObject *obj, Type **vptr) {
  51. return traits_asptr<Type>::asptr(obj, vptr);
  52. }
  53. template <class Type>
  54. struct traits_asval {
  55. static int asval(PyObject *obj, Type *val) {
  56. if (val) {
  57. Type *p = 0;
  58. int res = traits_asptr<Type>::asptr(obj, &p);
  59. if (!SWIG_IsOK(res)) return res;
  60. if (p) {
  61. typedef typename noconst_traits<Type>::noconst_type noconst_type;
  62. *(const_cast<noconst_type*>(val)) = *p;
  63. if (SWIG_IsNewObj(res)){
  64. %delete(p);
  65. res = SWIG_DelNewMask(res);
  66. }
  67. return res;
  68. } else {
  69. return SWIG_ERROR;
  70. }
  71. } else {
  72. return traits_asptr<Type>::asptr(obj, (Type **)(0));
  73. }
  74. }
  75. };
  76. template <class Type> struct traits_asval<Type*> {
  77. static int asval(PyObject *obj, Type **val) {
  78. if (val) {
  79. typedef typename noconst_traits<Type>::noconst_type noconst_type;
  80. noconst_type *p = 0;
  81. int res = traits_asptr<noconst_type>::asptr(obj, &p);
  82. if (SWIG_IsOK(res)) {
  83. *(const_cast<noconst_type**>(val)) = p;
  84. }
  85. return res;
  86. } else {
  87. return traits_asptr<Type>::asptr(obj, (Type **)(0));
  88. }
  89. }
  90. };
  91. template <class Type>
  92. inline int asval(PyObject *obj, Type *val) {
  93. return traits_asval<Type>::asval(obj, val);
  94. }
  95. template <class Type>
  96. struct traits_as<Type, value_category> {
  97. static Type as(PyObject *obj, bool throw_error) {
  98. Type v;
  99. int res = asval(obj, &v);
  100. if (!obj || !SWIG_IsOK(res)) {
  101. if (!PyErr_Occurred()) {
  102. %type_error(swig::type_name<Type>());
  103. }
  104. if (throw_error) throw std::invalid_argument("bad type");
  105. }
  106. return v;
  107. }
  108. };
  109. template <class Type>
  110. struct traits_as<Type, pointer_category> {
  111. static Type as(PyObject *obj, bool throw_error) {
  112. Type *v = 0;
  113. int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
  114. if (SWIG_IsOK(res) && v) {
  115. if (SWIG_IsNewObj(res)) {
  116. Type r(*v);
  117. %delete(v);
  118. return r;
  119. } else {
  120. return *v;
  121. }
  122. } else {
  123. // Uninitialized return value, no Type() constructor required.
  124. static Type *v_def = (Type*) malloc(sizeof(Type));
  125. if (!PyErr_Occurred()) {
  126. %type_error(swig::type_name<Type>());
  127. }
  128. if (throw_error) throw std::invalid_argument("bad type");
  129. memset(v_def,0,sizeof(Type));
  130. return *v_def;
  131. }
  132. }
  133. };
  134. template <class Type>
  135. struct traits_as<Type*, pointer_category> {
  136. static Type* as(PyObject *obj, bool throw_error) {
  137. Type *v = 0;
  138. int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
  139. if (SWIG_IsOK(res)) {
  140. return v;
  141. } else {
  142. if (!PyErr_Occurred()) {
  143. %type_error(swig::type_name<Type>());
  144. }
  145. if (throw_error) throw std::invalid_argument("bad type");
  146. return 0;
  147. }
  148. }
  149. };
  150. template <class Type>
  151. inline Type as(PyObject *obj, bool te = false) {
  152. return traits_as<Type, typename traits<Type>::category>::as(obj, te);
  153. }
  154. template <class Type>
  155. struct traits_check<Type, value_category> {
  156. static bool check(PyObject *obj) {
  157. int res = obj ? asval(obj, (Type *)(0)) : SWIG_ERROR;
  158. return SWIG_IsOK(res) ? true : false;
  159. }
  160. };
  161. template <class Type>
  162. struct traits_check<Type, pointer_category> {
  163. static bool check(PyObject *obj) {
  164. int res = obj ? asptr(obj, (Type **)(0)) : SWIG_ERROR;
  165. return SWIG_IsOK(res) ? true : false;
  166. }
  167. };
  168. template <class Type>
  169. inline bool check(PyObject *obj) {
  170. return traits_check<Type, typename traits<Type>::category>::check(obj);
  171. }
  172. }
  173. }
  174. //
  175. // Backward compatibility
  176. //
  177. #ifdef SWIG_PYTHON_BACKWARD_COMP
  178. %{
  179. #include <string>
  180. PyObject* SwigInt_FromBool(bool b) {
  181. return PyInt_FromLong(b ? 1L : 0L);
  182. }
  183. double SwigNumber_Check(PyObject* o) {
  184. return PyFloat_Check(o) || PyInt_Check(o) || PyLong_Check(o);
  185. }
  186. double SwigNumber_AsDouble(PyObject* o) {
  187. return PyFloat_Check(o) ? PyFloat_AsDouble(o)
  188. : (PyInt_Check(o) ? double(PyInt_AsLong(o))
  189. : double(PyLong_AsLong(o)));
  190. }
  191. PyObject* SwigString_FromString(const std::string& s) {
  192. return PyString_FromStringAndSize(s.data(),s.size());
  193. }
  194. std::string SwigString_AsString(PyObject* o) {
  195. return std::string(PyString_AsString(o));
  196. }
  197. %}
  198. #endif
  199. %define %specialize_std_container(Type,Check,As,From)
  200. %{
  201. namespace swig {
  202. template <> struct traits_asval<Type > {
  203. typedef Type value_type;
  204. static int asval(PyObject *obj, value_type *val) {
  205. if (Check(obj)) {
  206. if (val) *val = As(obj);
  207. return SWIG_OK;
  208. }
  209. return SWIG_ERROR;
  210. }
  211. };
  212. template <> struct traits_from<Type > {
  213. typedef Type value_type;
  214. static PyObject *from(const value_type& val) {
  215. return From(val);
  216. }
  217. };
  218. template <>
  219. struct traits_check<Type, value_category> {
  220. static int check(PyObject *obj) {
  221. int res = Check(obj);
  222. return obj && res ? res : 0;
  223. }
  224. };
  225. }
  226. %}
  227. %enddef
  228. #define specialize_std_vector(Type,Check,As,From) %specialize_std_container(%arg(Type),Check,As,From)
  229. #define specialize_std_list(Type,Check,As,From) %specialize_std_container(%arg(Type),Check,As,From)
  230. #define specialize_std_deque(Type,Check,As,From) %specialize_std_container(%arg(Type),Check,As,From)
  231. #define specialize_std_set(Type,Check,As,From) %specialize_std_container(%arg(Type),Check,As,From)
  232. #define specialize_std_multiset(Type,Check,As,From) %specialize_std_container(%arg(Type),Check,As,From)