octstdcommon.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 octave_value from(Type *val, int owner = 0) {
  7. return SWIG_NewPointerObj(val, type_info<Type>(), owner);
  8. }
  9. };
  10. template <class Type> struct traits_from {
  11. static octave_value 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 octave_value 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 octave_value from(const Type* val) {
  22. return traits_from_ptr<Type>::from(const_cast<Type*>(val), 0);
  23. }
  24. };
  25. template <class Type>
  26. inline octave_value from(const Type& val) {
  27. return traits_from<Type>::from(val);
  28. }
  29. template <class Type>
  30. inline octave_value 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 octave_value& 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 octave_value& 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 octave_value& obj, Type *val) {
  52. if (val) {
  53. Type *p = 0;
  54. int res = traits_asptr<Type>::asptr(obj, &p);
  55. if (!SWIG_IsOK(res)) return res;
  56. if (p) {
  57. typedef typename noconst_traits<Type>::noconst_type noconst_type;
  58. *(const_cast<noconst_type*>(val)) = *p;
  59. if (SWIG_IsNewObj(res)){
  60. %delete(p);
  61. res = SWIG_DelNewMask(res);
  62. }
  63. return res;
  64. } else {
  65. return SWIG_ERROR;
  66. }
  67. } else {
  68. return traits_asptr<Type>::asptr(obj, (Type **)(0));
  69. }
  70. }
  71. };
  72. template <class Type> struct traits_asval<Type*> {
  73. static int asval(const octave_value& obj, Type **val) {
  74. if (val) {
  75. typedef typename noconst_traits<Type>::noconst_type noconst_type;
  76. noconst_type *p = 0;
  77. int res = traits_asptr<noconst_type>::asptr(obj, &p);
  78. if (SWIG_IsOK(res)) {
  79. *(const_cast<noconst_type**>(val)) = p;
  80. }
  81. return res;
  82. } else {
  83. return traits_asptr<Type>::asptr(obj, (Type **)(0));
  84. }
  85. }
  86. };
  87. template <class Type>
  88. inline int asval(const octave_value& obj, Type *val) {
  89. return traits_asval<Type>::asval(obj, val);
  90. }
  91. template <class Type>
  92. struct traits_as<Type, value_category> {
  93. static Type as(const octave_value& obj, bool throw_error) {
  94. Type v;
  95. int res = asval(obj, &v);
  96. if (!obj.is_defined() || !SWIG_IsOK(res)) {
  97. if (!Octave_Error_Occurred()) {
  98. %type_error(swig::type_name<Type>());
  99. }
  100. if (throw_error) throw std::invalid_argument("bad type");
  101. }
  102. return v;
  103. }
  104. };
  105. template <class Type>
  106. struct traits_as<Type, pointer_category> {
  107. static Type as(const octave_value& obj, bool throw_error) {
  108. Type *v = 0;
  109. int res = traits_asptr<Type>::asptr(obj, &v);
  110. if (SWIG_IsOK(res) && v) {
  111. if (SWIG_IsNewObj(res)) {
  112. Type r(*v);
  113. %delete(v);
  114. return r;
  115. } else {
  116. return *v;
  117. }
  118. } else {
  119. // Uninitialized return value, no Type() constructor required.
  120. static Type *v_def = (Type*) malloc(sizeof(Type));
  121. if (!Octave_Error_Occurred()) {
  122. %type_error(swig::type_name<Type>());
  123. }
  124. if (throw_error) throw std::invalid_argument("bad type");
  125. memset(v_def,0,sizeof(Type));
  126. return *v_def;
  127. }
  128. }
  129. };
  130. template <class Type>
  131. struct traits_as<Type*, pointer_category> {
  132. static Type* as(const octave_value& obj, bool throw_error) {
  133. Type *v = 0;
  134. int res = traits_asptr<Type>::asptr(obj, &v);
  135. if (SWIG_IsOK(res)) {
  136. return v;
  137. } else {
  138. if (!Octave_Error_Occurred()) {
  139. %type_error(swig::type_name<Type>());
  140. }
  141. if (throw_error) throw std::invalid_argument("bad type");
  142. return 0;
  143. }
  144. }
  145. };
  146. template <class Type>
  147. inline Type as(const octave_value& 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 octave_value& 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 octave_value& 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 octave_value& 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 octave_value& 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 octave_value 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 octave_value& 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)