carrays.i 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* -----------------------------------------------------------------------------
  2. * carrays.i
  3. *
  4. * D-specific version of ../carrays.i.
  5. * ----------------------------------------------------------------------------- */
  6. /* -----------------------------------------------------------------------------
  7. * %array_functions(TYPE,NAME)
  8. *
  9. * Generates functions for creating and accessing elements of a C array
  10. * (as pointers). Creates the following functions:
  11. *
  12. * TYPE *new_NAME(int nelements)
  13. * void delete_NAME(TYPE *);
  14. * TYPE NAME_getitem(TYPE *, int index);
  15. * void NAME_setitem(TYPE *, int index, TYPE value);
  16. *
  17. * ----------------------------------------------------------------------------- */
  18. %define %array_functions(TYPE,NAME)
  19. %{
  20. static TYPE *new_##NAME(int nelements) { %}
  21. #ifdef __cplusplus
  22. %{ return new TYPE[nelements](); %}
  23. #else
  24. %{ return (TYPE *) calloc(nelements,sizeof(TYPE)); %}
  25. #endif
  26. %{}
  27. static void delete_##NAME(TYPE *ary) { %}
  28. #ifdef __cplusplus
  29. %{ delete [] ary; %}
  30. #else
  31. %{ free(ary); %}
  32. #endif
  33. %{}
  34. static TYPE NAME##_getitem(TYPE *ary, int index) {
  35. return ary[index];
  36. }
  37. static void NAME##_setitem(TYPE *ary, int index, TYPE value) {
  38. ary[index] = value;
  39. }
  40. %}
  41. TYPE *new_##NAME(int nelements);
  42. void delete_##NAME(TYPE *ary);
  43. TYPE NAME##_getitem(TYPE *ary, int index);
  44. void NAME##_setitem(TYPE *ary, int index, TYPE value);
  45. %enddef
  46. /* -----------------------------------------------------------------------------
  47. * %array_class(TYPE,NAME)
  48. *
  49. * Generates a class wrapper around a C array. The class has the following
  50. * interface:
  51. *
  52. * struct NAME {
  53. * NAME(int nelements);
  54. * ~NAME();
  55. * TYPE getitem(int index);
  56. * void setitem(int index, TYPE value);
  57. * TYPE * ptr();
  58. * static NAME *frompointer(TYPE *t);
  59. * }
  60. *
  61. * ----------------------------------------------------------------------------- */
  62. %define %array_class(TYPE,NAME)
  63. %{
  64. typedef TYPE NAME;
  65. %}
  66. typedef struct {} NAME;
  67. %extend NAME {
  68. #ifdef __cplusplus
  69. NAME(int nelements) {
  70. return new TYPE[nelements]();
  71. }
  72. ~NAME() {
  73. delete [] self;
  74. }
  75. #else
  76. NAME(int nelements) {
  77. return (TYPE *) calloc(nelements,sizeof(TYPE));
  78. }
  79. ~NAME() {
  80. free(self);
  81. }
  82. #endif
  83. TYPE getitem(int index) {
  84. return self[index];
  85. }
  86. void setitem(int index, TYPE value) {
  87. self[index] = value;
  88. }
  89. TYPE * ptr() {
  90. return self;
  91. }
  92. static NAME *frompointer(TYPE *t) {
  93. return (NAME *) t;
  94. }
  95. };
  96. %types(NAME = TYPE);
  97. %enddef