arrays_csharp.i 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * arrays_csharp.i
  6. *
  7. * This file contains a two approaches to marshaling arrays. The first uses
  8. * default p/invoke marshaling and the second uses pinning of the arrays.
  9. *
  10. * Default marshaling approach
  11. * ----------------------------
  12. * Array typemaps using default p/invoke marshaling. The data is copied to a separately
  13. * allocated buffer when passing over the managed-native boundary.
  14. *
  15. * There are separate typemaps for in, out and inout arrays to enable avoiding
  16. * unnecessary copying.
  17. *
  18. * Example usage:
  19. *
  20. * %include "arrays_csharp.i"
  21. * %apply int INPUT[] { int* sourceArray }
  22. * %apply int OUTPUT[] { int* targetArray }
  23. * void myArrayCopy( int* sourceArray, int* targetArray, int nitems );
  24. *
  25. * %apply int INOUT[] { int* array1, int *array2 }
  26. * void myArraySwap( int* array1, int* array2, int nitems );
  27. *
  28. * If handling large arrays you should consider using the pinning array typemaps
  29. * described next.
  30. *
  31. * Pinning approach
  32. * ----------------
  33. * Array typemaps using pinning. These typemaps pin the managed array given
  34. * as parameter and pass a pointer to it to the c/c++ side. This is very
  35. * efficient as no copying is done (unlike in the default array marshaling),
  36. * but it makes garbage collection more difficult. When considering using
  37. * these typemaps, think carefully whether you have callbacks that may cause
  38. * the control to re-enter the managed side from within the call (and produce
  39. * garbage for the gc) or whether other threads may produce enough garbage to
  40. * trigger gc while the call is being executed. In those cases it may be
  41. * wiser to use the default marshaling typemaps.
  42. *
  43. * Please note that when using fixed arrays, you have to mark your corresponding
  44. * module class method unsafe using
  45. * %csmethodmodifiers "public unsafe"
  46. * (the visibility of the method is up to you).
  47. *
  48. * Example usage:
  49. *
  50. * %include "arrays_csharp.i"
  51. * %apply int FIXED[] { int* sourceArray, int *targetArray }
  52. * %csmethodmodifiers myArrayCopy "public unsafe";
  53. * void myArrayCopy( int *sourceArray, int* targetArray, int nitems );
  54. *
  55. * ----------------------------------------------------------------------------- */
  56. %define CSHARP_ARRAYS( CTYPE, CSTYPE )
  57. // input only arrays
  58. %typemap(ctype) CTYPE INPUT[] "CTYPE*"
  59. %typemap(cstype) CTYPE INPUT[] "CSTYPE[]"
  60. %typemap(imtype, inattributes="[In, MarshalAs(UnmanagedType.LPArray)]") CTYPE INPUT[] "CSTYPE[]"
  61. %typemap(csin) CTYPE INPUT[] "$csinput"
  62. %typemap(in) CTYPE INPUT[] "$1 = $input;"
  63. %typemap(freearg) CTYPE INPUT[] ""
  64. %typemap(argout) CTYPE INPUT[] ""
  65. // output only arrays
  66. %typemap(ctype) CTYPE OUTPUT[] "CTYPE*"
  67. %typemap(cstype) CTYPE OUTPUT[] "CSTYPE[]"
  68. %typemap(imtype, inattributes="[Out, MarshalAs(UnmanagedType.LPArray)]") CTYPE OUTPUT[] "CSTYPE[]"
  69. %typemap(csin) CTYPE OUTPUT[] "$csinput"
  70. %typemap(in) CTYPE OUTPUT[] "$1 = $input;"
  71. %typemap(freearg) CTYPE OUTPUT[] ""
  72. %typemap(argout) CTYPE OUTPUT[] ""
  73. // inout arrays
  74. %typemap(ctype) CTYPE INOUT[] "CTYPE*"
  75. %typemap(cstype) CTYPE INOUT[] "CSTYPE[]"
  76. %typemap(imtype, inattributes="[In, Out, MarshalAs(UnmanagedType.LPArray)]") CTYPE INOUT[] "CSTYPE[]"
  77. %typemap(csin) CTYPE INOUT[] "$csinput"
  78. %typemap(in) CTYPE INOUT[] "$1 = $input;"
  79. %typemap(freearg) CTYPE INOUT[] ""
  80. %typemap(argout) CTYPE INOUT[] ""
  81. %enddef // CSHARP_ARRAYS
  82. CSHARP_ARRAYS(signed char, sbyte)
  83. CSHARP_ARRAYS(unsigned char, byte)
  84. CSHARP_ARRAYS(short, short)
  85. CSHARP_ARRAYS(unsigned short, ushort)
  86. CSHARP_ARRAYS(int, int)
  87. CSHARP_ARRAYS(unsigned int, uint)
  88. // FIXME - on Unix 64 bit, long is 8 bytes but is 4 bytes on Windows 64 bit.
  89. // How can this be handled sensibly?
  90. // See e.g. http://www.xml.com/ldd/chapter/book/ch10.html
  91. CSHARP_ARRAYS(long, int)
  92. CSHARP_ARRAYS(unsigned long, uint)
  93. CSHARP_ARRAYS(long long, long)
  94. CSHARP_ARRAYS(unsigned long long, ulong)
  95. CSHARP_ARRAYS(float, float)
  96. CSHARP_ARRAYS(double, double)
  97. %define CSHARP_ARRAYS_FIXED( CTYPE, CSTYPE )
  98. %typemap(ctype) CTYPE FIXED[] "CTYPE*"
  99. %typemap(imtype) CTYPE FIXED[] "IntPtr"
  100. %typemap(cstype) CTYPE FIXED[] "CSTYPE[]"
  101. %typemap(csin,
  102. pre= " fixed ( CSTYPE* swig_ptrTo_$csinput = $csinput ) {",
  103. terminator=" }")
  104. CTYPE FIXED[] "(IntPtr)swig_ptrTo_$csinput"
  105. %typemap(in) CTYPE FIXED[] "$1 = $input;"
  106. %typemap(freearg) CTYPE FIXED[] ""
  107. %typemap(argout) CTYPE FIXED[] ""
  108. %enddef // CSHARP_ARRAYS_FIXED
  109. CSHARP_ARRAYS_FIXED(signed char, sbyte)
  110. CSHARP_ARRAYS_FIXED(unsigned char, byte)
  111. CSHARP_ARRAYS_FIXED(short, short)
  112. CSHARP_ARRAYS_FIXED(unsigned short, ushort)
  113. CSHARP_ARRAYS_FIXED(int, int)
  114. CSHARP_ARRAYS_FIXED(unsigned int, uint)
  115. CSHARP_ARRAYS_FIXED(long, int)
  116. CSHARP_ARRAYS_FIXED(unsigned long, uint)
  117. CSHARP_ARRAYS_FIXED(long long, long)
  118. CSHARP_ARRAYS_FIXED(unsigned long long, ulong)
  119. CSHARP_ARRAYS_FIXED(float, float)
  120. CSHARP_ARRAYS_FIXED(double, double)
粤ICP备19079148号