PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Lib/typemaps/primtypes.swg

#
Unknown | 321 lines | 259 code | 62 blank | 0 comment | 0 complexity | 5d6ad7a673ca01fa10a12c2b5bdbf652 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* ------------------------------------------------------------
  2. * Primitive type fragments and macros
  3. * ------------------------------------------------------------ */
  4. /*
  5. This file provide fragments and macros for the C/C++ primitive types.
  6. The file defines default fragments for the following types:
  7. bool
  8. signed char
  9. unsigned char
  10. signed wchar_t // in C++
  11. unsigned wchar_t // in C++
  12. short
  13. unsigned short
  14. int
  15. unsigned int
  16. float
  17. size_t
  18. ptrdiff_t
  19. which can always be redefined in the swig target language if needed.
  20. The fragments for the following types, however, always need to be
  21. defined in the target language:
  22. long
  23. unsigned long
  24. long long
  25. unsigned long long
  26. double
  27. If they are not provided, an #error directive will appear in the
  28. wrapped code.
  29. --------------------------------------------------------------------
  30. This file provides the macro
  31. %typemaps_primitive(CheckCode, Type)
  32. which generates the typemaps for a primitive type with a given
  33. checkcode. It is assumed that the primitive type is 'normalized' and
  34. the corresponding SWIG_AsVal(Type) and SWIG_From(Type) methods are
  35. provided via fragments.
  36. The following auxiliary macros (explained with bash pseudo code) are
  37. also defined:
  38. %apply_ctypes(Macro)
  39. for i in C Type
  40. do
  41. Macro($i)
  42. done
  43. %apply_cpptypes(Macro)
  44. for i in C++ Type
  45. do
  46. Macro($i)
  47. done
  48. %apply_ctypes_2(Macro2)
  49. for i in C Type
  50. do
  51. for j in C Type
  52. do
  53. Macro_2($i, $j)
  54. done
  55. done
  56. %apply_cpptypes_2(Macro2)
  57. for i in C++ Type
  58. do
  59. for j in C++ Type
  60. do
  61. Macro_2($i, $j)
  62. done
  63. done
  64. %apply_checkctypes(Macro2)
  65. for i in Check Type
  66. do
  67. Macro2(%checkcode($i), $i)
  68. done
  69. */
  70. /* ------------------------------------------------------------
  71. * Primitive type fragments
  72. * ------------------------------------------------------------ */
  73. /* boolean */
  74. %fragment(SWIG_From_frag(bool),"header",fragment=SWIG_From_frag(long)) {
  75. SWIGINTERN SWIG_Object
  76. SWIG_From_dec(bool)(bool value)
  77. {
  78. return SWIG_From(long)(value ? 1 : 0);
  79. }
  80. }
  81. %fragment(SWIG_AsVal_frag(bool),"header",fragment=SWIG_AsVal_frag(long)) {
  82. SWIGINTERN int
  83. SWIG_AsVal_dec(bool)(SWIG_Object obj, bool *val)
  84. {
  85. long v;
  86. int res = SWIG_AsVal(long)(obj, val ? &v : 0);
  87. if (SWIG_IsOK(res)) {
  88. if (val) *val = v ? true : false;
  89. return res;
  90. }
  91. return SWIG_TypeError;
  92. }
  93. }
  94. /* signed/unsigned char */
  95. %numeric_slong(signed char, "<limits.h>", SCHAR_MIN, SCHAR_MAX)
  96. %numeric_ulong(unsigned char, "<limits.h>", UCHAR_MAX)
  97. /* short/unsigned short */
  98. %numeric_slong(short, "<limits.h>", SHRT_MIN, SHRT_MAX)
  99. %numeric_ulong(unsigned short, "<limits.h>", USHRT_MAX)
  100. /* int/unsigned int */
  101. %numeric_slong(int, "<limits.h>", INT_MIN, INT_MAX)
  102. %numeric_ulong(unsigned int, "<limits.h>", UINT_MAX)
  103. /* signed/unsigned wchar_t */
  104. #ifdef __cplusplus
  105. %numeric_slong(signed wchar_t, "<wchar.h>", WCHAR_MIN, WCHAR_MAX)
  106. %numeric_ulong(unsigned wchar_t, "<wchar.h>", UWCHAR_MAX)
  107. #endif
  108. /* float */
  109. %numeric_double(float, "<float.h>", -FLT_MAX, FLT_MAX)
  110. /* long/unsigned long */
  111. %ensure_type_fragments(long)
  112. %ensure_type_fragments(unsigned long)
  113. /* long long/unsigned long long */
  114. %ensure_type_fragments(long long)
  115. %ensure_type_fragments(unsigned long long)
  116. /* double */
  117. %ensure_type_fragments(double)
  118. /* size_t */
  119. %fragment(SWIG_From_frag(size_t),"header",fragment=SWIG_From_frag(unsigned long)) {
  120. SWIGINTERNINLINE SWIG_Object
  121. SWIG_From_dec(size_t)(size_t value)
  122. {
  123. return SWIG_From(unsigned long)(%numeric_cast(value, unsigned long));
  124. }
  125. }
  126. %fragment(SWIG_AsVal_frag(size_t),"header",fragment=SWIG_AsVal_frag(unsigned long)) {
  127. SWIGINTERNINLINE int
  128. SWIG_AsVal_dec(size_t)(SWIG_Object obj, size_t *val)
  129. {
  130. unsigned long v;
  131. int res = SWIG_AsVal(unsigned long)(obj, val ? &v : 0);
  132. if (SWIG_IsOK(res) && val) *val = %numeric_cast(v, size_t);
  133. return res;
  134. }
  135. }
  136. /* ptrdiff_t */
  137. %fragment(SWIG_From_frag(ptrdiff_t),"header",fragment=SWIG_From_frag(long)) {
  138. SWIGINTERNINLINE SWIG_Object
  139. SWIG_From_dec(ptrdiff_t)(ptrdiff_t value)
  140. {
  141. return SWIG_From(long)(%numeric_cast(value,long));
  142. }
  143. }
  144. %fragment(SWIG_AsVal_frag(ptrdiff_t),"header",fragment=SWIG_AsVal_frag(long)) {
  145. SWIGINTERNINLINE int
  146. SWIG_AsVal_dec(ptrdiff_t)(SWIG_Object obj, ptrdiff_t *val)
  147. {
  148. long v;
  149. int res = SWIG_AsVal(long)(obj, val ? &v : 0);
  150. if (SWIG_IsOK(res) && val) *val = %numeric_cast(v, ptrdiff_t);
  151. return res;
  152. }
  153. }
  154. %fragment("SWIG_CanCastAsInteger","header",
  155. fragment=SWIG_AsVal_frag(double),
  156. fragment="<float.h>",
  157. fragment="<math.h>") {
  158. SWIGINTERNINLINE int
  159. SWIG_CanCastAsInteger(double *d, double min, double max) {
  160. double x = *d;
  161. if ((min <= x && x <= max)) {
  162. double fx = floor(x);
  163. double cx = ceil(x);
  164. double rd = ((x - fx) < 0.5) ? fx : cx; /* simple rint */
  165. if ((errno == EDOM) || (errno == ERANGE)) {
  166. errno = 0;
  167. } else {
  168. double summ, reps, diff;
  169. if (rd < x) {
  170. diff = x - rd;
  171. } else if (rd > x) {
  172. diff = rd - x;
  173. } else {
  174. return 1;
  175. }
  176. summ = rd + x;
  177. reps = diff/summ;
  178. if (reps < 8*DBL_EPSILON) {
  179. *d = rd;
  180. return 1;
  181. }
  182. }
  183. }
  184. return 0;
  185. }
  186. }
  187. /* ------------------------------------------------------------
  188. * Generate the typemaps for primitive type
  189. * ------------------------------------------------------------ */
  190. #define %typemaps_primitive(Code, Type) %typemaps_asvalfromn(%arg(Code), Type)
  191. /* ------------------------------------------------------------
  192. * Primitive Type Macros
  193. * ------------------------------------------------------------ */
  194. /* useful macros to derive typemap declarations from primitive types */
  195. %define _apply_macro(macro, arg2, arg1...)
  196. #if #arg1 != ""
  197. macro(%arg(arg1),arg2);
  198. #else
  199. macro(arg2);
  200. #endif
  201. %enddef
  202. /* Apply macro to the C-types */
  203. %define %apply_ctypes(Macro, Arg2...)
  204. _apply_macro(Macro, bool , Arg2);
  205. _apply_macro(Macro, signed char , Arg2);
  206. _apply_macro(Macro, unsigned char , Arg2);
  207. _apply_macro(Macro, short , Arg2);
  208. _apply_macro(Macro, unsigned short , Arg2);
  209. _apply_macro(Macro, int , Arg2);
  210. _apply_macro(Macro, unsigned int , Arg2);
  211. _apply_macro(Macro, long , Arg2);
  212. _apply_macro(Macro, unsigned long , Arg2);
  213. _apply_macro(Macro, long long , Arg2);
  214. _apply_macro(Macro, unsigned long long , Arg2);
  215. _apply_macro(Macro, float , Arg2);
  216. _apply_macro(Macro, double , Arg2);
  217. _apply_macro(Macro, char , Arg2);
  218. _apply_macro(Macro, wchar_t , Arg2);
  219. _apply_macro(Macro, size_t , Arg2);
  220. _apply_macro(Macro, ptrdiff_t , Arg2);
  221. %enddef
  222. /* apply the Macro2(Type1, Type2) to all C types */
  223. #define %apply_ctypes_2(Macro2) %apply_ctypes(%apply_ctypes, Macro2)
  224. /* apply the Macro(Type) to all C++ types */
  225. %define %apply_cpptypes(Macro, Arg2...)
  226. %apply_ctypes(Macro, Arg2)
  227. _apply_macro(Macro, std::size_t, Arg2);
  228. _apply_macro(Macro, std::ptrdiff_t, Arg2);
  229. _apply_macro(Macro, std::string, Arg2);
  230. _apply_macro(Macro, std::wstring, Arg2);
  231. _apply_macro(Macro, std::complex<float>, Arg2);
  232. _apply_macro(Macro, std::complex<double>, Arg2);
  233. %enddef
  234. /* apply the Macro2(Type1, Type2) to all C++ types */
  235. #define %apply_cpptypes_2(Macro2) %apply_cpptypes(%apply_cpptypes, Macro2)
  236. /* apply the Macro2(CheckCode,Type) to all Checked Types */
  237. %define %apply_checkctypes(Macro2)
  238. Macro2(%checkcode(BOOL), bool);
  239. Macro2(%checkcode(INT8), signed char);
  240. Macro2(%checkcode(UINT8), unsigned char);
  241. Macro2(%checkcode(INT16), short);
  242. Macro2(%checkcode(UINT16), unsigned short);
  243. Macro2(%checkcode(INT32), int);
  244. Macro2(%checkcode(UINT32), unsigned int);
  245. Macro2(%checkcode(INT64), long);
  246. Macro2(%checkcode(UINT64), unsigned long);
  247. Macro2(%checkcode(INT128), long long);
  248. Macro2(%checkcode(UINT128), unsigned long long);
  249. Macro2(%checkcode(FLOAT), float);
  250. Macro2(%checkcode(DOUBLE), double);
  251. Macro2(%checkcode(CHAR), char);
  252. Macro2(%checkcode(UNICHAR), wchar_t);
  253. Macro2(%checkcode(SIZE), size_t);
  254. Macro2(%checkcode(PTRDIFF), ptrdiff_t);
  255. %enddef
  256. /* ------------------------------------------------------------
  257. * Generate the typemaps for all the primitive types with checkcode
  258. * ------------------------------------------------------------ */
  259. %apply_checkctypes(%typemaps_primitive);