PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Lib/csharp/arrays_csharp.i

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