Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
3.6 KiB

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