scistdcommon.swg 6.0 KB

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