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.

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