PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/python/pybuffer.i

#
Swig | 107 lines | 45 code | 11 blank | 51 comment | 0 complexity | e9a347c68372c564c8283bc4337bab2f MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* Implementing buffer protocol typemaps */
  2. /* %pybuffer_mutable_binary(TYPEMAP, SIZE)
  3. *
  4. * Macro for functions accept mutable buffer pointer with a size.
  5. * This can be used for both input and output. For example:
  6. *
  7. * %pybuffer_mutable_binary(char *buff, int size);
  8. * void foo(char *buff, int size) {
  9. * for(int i=0; i<size; ++i)
  10. * buff[i]++;
  11. * }
  12. */
  13. %define %pybuffer_mutable_binary(TYPEMAP, SIZE)
  14. %typemap(in) (TYPEMAP, SIZE)
  15. (int res, Py_ssize_t size = 0, void *buf = 0) {
  16. res = PyObject_AsWriteBuffer($input, &buf, &size);
  17. if (res<0) {
  18. PyErr_Clear();
  19. %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
  20. }
  21. $1 = ($1_ltype) buf;
  22. $2 = ($2_ltype) (size/sizeof($*1_type));
  23. }
  24. %enddef
  25. /* %pybuffer_mutable_string(TYPEMAP, SIZE)
  26. *
  27. * Macro for functions accept mutable zero terminated string pointer.
  28. * This can be used for both input and output. For example:
  29. *
  30. * %pybuffer_mutable_string(char *str);
  31. * void foo(char *str) {
  32. * while(*str) {
  33. * *str = toupper(*str);
  34. * str++;
  35. * }
  36. */
  37. %define %pybuffer_mutable_string(TYPEMAP)
  38. %typemap(in) (TYPEMAP)
  39. (int res, Py_ssize_t size = 0, void *buf = 0) {
  40. res = PyObject_AsWriteBuffer($input, &buf, &size);
  41. if (res<0) {
  42. PyErr_Clear();
  43. %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
  44. }
  45. $1 = ($1_ltype) buf;
  46. }
  47. %enddef
  48. /* pybuffer_binary(TYPEMAP, SIZE)
  49. *
  50. * Macro for functions accept read only buffer pointer with a size.
  51. * This must be used for input. For example:
  52. *
  53. * %pybuffer_binary(char *buff, int size);
  54. * int foo(char *buff, int size) {
  55. * int count = 0;
  56. * for(int i=0; i<size; ++i)
  57. * if (0==buff[i]) count++;
  58. * return count;
  59. * }
  60. */
  61. %define %pybuffer_binary(TYPEMAP, SIZE)
  62. %typemap(in) (TYPEMAP, SIZE)
  63. (int res, Py_ssize_t size = 0, const void *buf = 0) {
  64. res = PyObject_AsReadBuffer($input, &buf, &size);
  65. if (res<0) {
  66. PyErr_Clear();
  67. %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
  68. }
  69. $1 = ($1_ltype) buf;
  70. $2 = ($2_ltype) (size / sizeof($*1_type));
  71. }
  72. %enddef
  73. /* %pybuffer_string(TYPEMAP, SIZE)
  74. *
  75. * Macro for functions accept read only zero terminated string pointer.
  76. * This can be used for input. For example:
  77. *
  78. * %pybuffer_string(char *str);
  79. * int foo(char *str) {
  80. * int count = 0;
  81. * while(*str) {
  82. * if (isalnum(*str))
  83. * count++;
  84. * str++;
  85. * }
  86. */
  87. %define %pybuffer_string(TYPEMAP)
  88. %typemap(in) (TYPEMAP)
  89. (int res, Py_ssize_t size = 0, const void *buf = 0) {
  90. res = PyObject_AsReadBuffer($input, &buf, &size);
  91. if (res<0) {
  92. %argument_fail(res, "(TYPEMAP, SIZE)", $symname, $argnum);
  93. }
  94. $1 = ($1_ltype) buf;
  95. }
  96. %enddef