cpointer.i 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* -----------------------------------------------------------------------------
  2. * cpointer.i
  3. *
  4. * SWIG library file containing macros that can be used to manipulate simple
  5. * pointer objects.
  6. * ----------------------------------------------------------------------------- */
  7. /* -----------------------------------------------------------------------------
  8. * %pointer_class(type,name)
  9. *
  10. * Places a simple proxy around a simple type like 'int', 'float', or whatever.
  11. * The proxy provides this interface:
  12. *
  13. * class type {
  14. * public:
  15. * type();
  16. * ~type();
  17. * type value();
  18. * void assign(type value);
  19. * };
  20. *
  21. * Example:
  22. *
  23. * %pointer_class(int, intp);
  24. *
  25. * int add(int *x, int *y) { return *x + *y; }
  26. *
  27. * In python (with proxies)
  28. *
  29. * >>> a = intp()
  30. * >>> a.assign(10)
  31. * >>> a.value()
  32. * 10
  33. * >>> b = intp()
  34. * >>> b.assign(20)
  35. * >>> print add(a,b)
  36. * 30
  37. *
  38. * As a general rule, this macro should not be used on class/structures that
  39. * are already defined in the interface.
  40. * ----------------------------------------------------------------------------- */
  41. %define %pointer_class(TYPE, NAME)
  42. %{
  43. typedef TYPE NAME;
  44. %}
  45. typedef struct {
  46. } NAME;
  47. %extend NAME {
  48. #ifdef __cplusplus
  49. NAME() {
  50. return new TYPE();
  51. }
  52. ~NAME() {
  53. if ($self) delete $self;
  54. }
  55. #else
  56. NAME() {
  57. return (TYPE *) calloc(1,sizeof(TYPE));
  58. }
  59. ~NAME() {
  60. if ($self) free($self);
  61. }
  62. #endif
  63. }
  64. %extend NAME {
  65. void assign(TYPE value) {
  66. *$self = value;
  67. }
  68. TYPE value() {
  69. return *$self;
  70. }
  71. TYPE * cast() {
  72. return $self;
  73. }
  74. static NAME * frompointer(TYPE *t) {
  75. return (NAME *) t;
  76. }
  77. }
  78. %types(NAME = TYPE);
  79. %enddef
  80. /* -----------------------------------------------------------------------------
  81. * %pointer_functions(type,name)
  82. *
  83. * Create functions for allocating/deallocating pointers. This can be used
  84. * if you don't want to create a proxy class or if the pointer is complex.
  85. *
  86. * %pointer_functions(int, intp)
  87. *
  88. * int add(int *x, int *y) { return *x + *y; }
  89. *
  90. * In python (with proxies)
  91. *
  92. * >>> a = copy_intp(10)
  93. * >>> intp_value(a)
  94. * 10
  95. * >>> b = new_intp()
  96. * >>> intp_assign(b,20)
  97. * >>> print add(a,b)
  98. * 30
  99. * >>> delete_intp(a)
  100. * >>> delete_intp(b)
  101. *
  102. * ----------------------------------------------------------------------------- */
  103. %define %pointer_functions(TYPE,NAME)
  104. %{
  105. static TYPE *new_##NAME() { %}
  106. #ifdef __cplusplus
  107. %{ return new TYPE(); %}
  108. #else
  109. %{ return (TYPE *) calloc(1,sizeof(TYPE)); %}
  110. #endif
  111. %{}
  112. static TYPE *copy_##NAME(TYPE value) { %}
  113. #ifdef __cplusplus
  114. %{ return new TYPE(value); %}
  115. #else
  116. %{ TYPE *obj = (TYPE *) calloc(1,sizeof(TYPE));
  117. *obj = value;
  118. return obj; %}
  119. #endif
  120. %{}
  121. static void delete_##NAME(TYPE *obj) { %}
  122. #ifdef __cplusplus
  123. %{ if (obj) delete obj; %}
  124. #else
  125. %{ if (obj) free(obj); %}
  126. #endif
  127. %{}
  128. static void NAME ##_assign(TYPE *obj, TYPE value) {
  129. *obj = value;
  130. }
  131. static TYPE NAME ##_value(TYPE *obj) {
  132. return *obj;
  133. }
  134. %}
  135. TYPE *new_##NAME();
  136. TYPE *copy_##NAME(TYPE value);
  137. void delete_##NAME(TYPE *obj);
  138. void NAME##_assign(TYPE *obj, TYPE value);
  139. TYPE NAME##_value(TYPE *obj);
  140. %enddef
  141. /* -----------------------------------------------------------------------------
  142. * %pointer_cast(type1,type2,name)
  143. *
  144. * Generates a pointer casting function.
  145. * ----------------------------------------------------------------------------- */
  146. %define %pointer_cast(TYPE1,TYPE2,NAME)
  147. %inline %{
  148. TYPE2 NAME(TYPE1 x) {
  149. return (TYPE2) x;
  150. }
  151. %}
  152. %enddef