PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/python/multimap/example.i

#
Swig | 121 lines | 91 code | 21 blank | 9 comment | 0 complexity | 474fc528d7ee6d68608aa9d3b543b212 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* File : example.i */
  2. %module example
  3. %{
  4. extern int gcd(int x, int y);
  5. extern int gcdmain(int argc, char *argv[]);
  6. extern int count(char *bytes, int len, char c);
  7. extern void capitalize (char *str, int len);
  8. extern void circle (double cx, double cy);
  9. extern int squareCubed (int n, int *OUTPUT);
  10. %}
  11. %include exception.i
  12. %include typemaps.i
  13. extern int gcd(int x, int y);
  14. %typemap(in,fragment="t_output_helper") (int argc, char *argv[]) {
  15. int i;
  16. if (!PyList_Check($input)) {
  17. SWIG_exception(SWIG_ValueError, "Expecting a list");
  18. }
  19. $1 = PyList_Size($input);
  20. if ($1 == 0) {
  21. SWIG_exception(SWIG_ValueError, "List must contain at least 1 element");
  22. }
  23. $2 = (char **) malloc(($1+1)*sizeof(char *));
  24. for (i = 0; i < $1; i++) {
  25. PyObject *s = PyList_GetItem($input,i);
  26. %#if PY_VERSION_HEX >= 0x03000000
  27. if (!PyUnicode_Check(s))
  28. %#else
  29. if (!PyString_Check(s))
  30. %#endif
  31. {
  32. free($2);
  33. SWIG_exception(SWIG_ValueError, "List items must be strings");
  34. }
  35. %#if PY_VERSION_HEX >= 0x03000000
  36. {
  37. int l;
  38. $2[i] = PyUnicode_AsStringAndSize(s, &l);
  39. }
  40. %#else
  41. $2[i] = PyString_AsString(s);
  42. %#endif
  43. }
  44. $2[i] = 0;
  45. }
  46. extern int gcdmain(int argc, char *argv[]);
  47. %typemap(in) (char *bytes, int len) {
  48. %#if PY_VERSION_HEX >= 0x03000000
  49. if (!PyUnicode_Check($input)) {
  50. PyErr_SetString(PyExc_ValueError,"Expected a string");
  51. return NULL;
  52. }
  53. $1 = PyUnicode_AsStringAndSize($input, &$2);
  54. %#else
  55. if (!PyString_Check($input)) {
  56. PyErr_SetString(PyExc_ValueError,"Expected a string");
  57. return NULL;
  58. }
  59. $1 = PyString_AsString($input);
  60. $2 = PyString_Size($input);
  61. %#endif
  62. }
  63. extern int count(char *bytes, int len, char c);
  64. /* This example shows how to wrap a function that mutates a string */
  65. /* Since str is modified, we make a copy of the Python object
  66. so that we don't violate it's mutability */
  67. %typemap(in) (char *str, int len) {
  68. %#if PY_VERSION_HEX >= 0x03000000
  69. $2 = PyUnicode_GetSize($input);
  70. $1 = (char *) malloc($2+1);
  71. memmove($1,PyUnicode_AsString($input),$2);
  72. %#else
  73. $2 = PyString_Size($input);
  74. $1 = (char *) malloc($2+1);
  75. memmove($1,PyString_AsString($input),$2);
  76. %#endif
  77. }
  78. /* Return the mutated string as a new object. The t_output_helper
  79. function takes an object and appends it to the output object
  80. to create a tuple */
  81. %typemap(argout) (char *str, int len) {
  82. PyObject *o;
  83. %#if PY_VERSION_HEX >= 0x03000000
  84. o = PyUnicode_FromStringAndSize($1,$2);
  85. %#else
  86. o = PyString_FromStringAndSize($1,$2);
  87. %#endif
  88. $result = t_output_helper($result,o);
  89. free($1);
  90. }
  91. extern void capitalize(char *str, int len);
  92. /* A multi-valued constraint. Force two arguments to lie
  93. inside the unit circle */
  94. %typemap(check) (double cx, double cy) {
  95. double a = $1*$1 + $2*$2;
  96. if (a > 1.0) {
  97. SWIG_exception(SWIG_ValueError,"$1_name and $2_name must be in unit circle");
  98. }
  99. }
  100. extern void circle(double cx, double cy);