PageRenderTime 58ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/d/cpointer.i

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