PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/r/rstdcommon.swg

#
Unknown | 210 lines | 191 code | 19 blank | 0 comment | 0 complexity | 5b123c349c495ec3ea8459962ed81a38 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 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);
  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 (throw_error)
  97. throw std::invalid_argument("bad type");
  98. }
  99. return v;
  100. }
  101. };
  102. template <class Type>
  103. struct traits_as<Type, pointer_category> {
  104. static Type as(SWIG_Object obj, bool throw_error) {
  105. Type *v = 0;
  106. int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
  107. if (SWIG_IsOK(res) && v) {
  108. if (SWIG_IsNewObj(res)) {
  109. Type r(*v);
  110. %delete(v);
  111. return r;
  112. } else {
  113. return *v;
  114. }
  115. } else {
  116. // Uninitialized return value, no Type() constructor required.
  117. static Type *v_def = (Type*) malloc(sizeof(Type));
  118. if (throw_error)
  119. throw std::invalid_argument("bad type");
  120. memset(v_def,0,sizeof(Type));
  121. return *v_def;
  122. }
  123. }
  124. };
  125. template <class Type>
  126. struct traits_as<Type*, pointer_category> {
  127. static Type* as(SWIG_Object obj, bool throw_error) {
  128. Type *v = 0;
  129. int res = (obj ? traits_asptr<Type>::asptr(obj, &v) : SWIG_ERROR);
  130. if (SWIG_IsOK(res)) {
  131. return v;
  132. } else {
  133. if (throw_error)
  134. throw std::invalid_argument("bad type");
  135. return 0;
  136. }
  137. }
  138. };
  139. template <class Type>
  140. inline Type as(SWIG_Object obj, bool te = false) {
  141. return traits_as<Type, typename traits<Type>::category>::as(obj, te);
  142. }
  143. template <class Type>
  144. struct traits_check<Type, value_category> {
  145. static bool check(SWIG_Object obj) {
  146. int res = obj ? asval(obj, (Type *)(0)) : SWIG_ERROR;
  147. return SWIG_IsOK(res) ? true : false;
  148. }
  149. };
  150. template <class Type>
  151. struct traits_check<Type, pointer_category> {
  152. static bool check(SWIG_Object obj) {
  153. int res = obj ? asptr(obj, (Type **)(0)) : SWIG_ERROR;
  154. return SWIG_IsOK(res) ? true : false;
  155. }
  156. };
  157. template <class Type>
  158. inline bool check(SWIG_Object obj) {
  159. return traits_check<Type, typename traits<Type>::category>::check(obj);
  160. }
  161. }
  162. }
  163. %define %specialize_std_container(Type,Check,As,From)
  164. %{
  165. namespace swig {
  166. template <> struct traits_asval<Type > {
  167. typedef Type value_type;
  168. static int asval(SWIG_Object obj, value_type *val) {
  169. if (Check(obj)) {
  170. if (val) *val = As(obj);
  171. return SWIG_OK;
  172. }
  173. return SWIG_ERROR;
  174. }
  175. };
  176. template <> struct traits_from<Type > {
  177. typedef Type value_type;
  178. static SWIG_Object from(const value_type& val) {
  179. return From(val);
  180. }
  181. };
  182. template <>
  183. struct traits_check<Type, value_category> {
  184. static int check(SWIG_Object obj) {
  185. int res = Check(obj);
  186. return obj && res ? res : 0;
  187. }
  188. };
  189. }
  190. %}
  191. %enddef