PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/typemaps/cpointer.swg

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