carray.i 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. %insert(mli) %{
  2. type _value = c_obj
  3. %}
  4. %insert(ml) %{
  5. type _value = c_obj
  6. %}
  7. %define %array_tmap_out(type,what,out_f)
  8. %typemap(type) what [ANY] {
  9. int i;
  10. /* $*1_type */
  11. $result = caml_array_new($1_dim0);
  12. for( i = 0; i < $1_dim0; i++ ) {
  13. caml_array_set($result,i,out_f($1[i]));
  14. }
  15. }
  16. %enddef
  17. %define %array_tmap_in(type,what,in_f)
  18. %typemap(type) what [ANY] {
  19. int i;
  20. /* $*1_type */
  21. $1 = ($*1_type *)malloc( $1_size );
  22. for( i = 0; i < $1_dim0 && i < caml_array_len($input); i++ ) {
  23. $1[i] = in_f(caml_array_nth($input,i));
  24. }
  25. }
  26. %typemap(free) what [ANY] {
  27. free( (void *)$1 );
  28. }
  29. %enddef
  30. %define %make_simple_array_typemap(type,out_f,in_f)
  31. %array_tmap_out(out,type,out_f);
  32. %array_tmap_out(varout,type,out_f);
  33. %array_tmap_out(directorin,type,out_f);
  34. %array_tmap_in(in,type,in_f);
  35. %array_tmap_in(varin,type,in_f);
  36. %array_tmap_in(directorout,type,in_f);
  37. %enddef
  38. %make_simple_array_typemap(bool,caml_val_bool,caml_long_val);
  39. %make_simple_array_typemap(short,caml_val_short,caml_long_val);
  40. %make_simple_array_typemap(unsigned short,caml_val_ushort,caml_long_val);
  41. %make_simple_array_typemap(int,caml_val_int,caml_long_val);
  42. %make_simple_array_typemap(unsigned int,caml_val_uint,caml_long_val);
  43. %make_simple_array_typemap(long,caml_val_long,caml_long_val);
  44. %make_simple_array_typemap(unsigned long,caml_val_ulong,caml_long_val);
  45. %make_simple_array_typemap(size_t,caml_val_int,caml_long_val);
  46. %make_simple_array_typemap(float,caml_val_float,caml_double_val);
  47. %make_simple_array_typemap(double,caml_val_double,caml_double_val);
  48. #ifdef __cplusplus
  49. %typemap(in) SWIGTYPE [] {
  50. int i;
  51. /* $*1_type */
  52. $1 = new $*1_type [$1_dim0];
  53. for( i = 0; i < $1_dim0 && i < caml_array_len($input); i++ ) {
  54. $1[i] = *(($*1_ltype *)
  55. caml_ptr_val(caml_array_nth($input,i),
  56. $*1_descriptor)) ;
  57. }
  58. }
  59. #else
  60. %typemap(in) SWIGTYPE [] {
  61. int i;
  62. /* $*1_type */
  63. $1 = ($*1_type *)malloc( $1_size );
  64. for( i = 0; i < $1_dim0 && i < caml_array_len($input); i++ ) {
  65. $1[i] = *(($*1_ltype)
  66. caml_ptr_val(caml_array_nth($input),
  67. $*1_descriptor));
  68. }
  69. }
  70. #endif
  71. %typemap(out) SWIGTYPE [] {
  72. int i;
  73. CAML_VALUE *fromval = caml_named_value("create_$ntype_from_ptr");
  74. $result = caml_array_new($1_dim0);
  75. for( i = 0; i < $1_dim0; i++ ) {
  76. if( fromval ) {
  77. caml_array_set
  78. ($result,
  79. i,
  80. callback(*fromval,caml_val_ptr((void *)&$1[i],$*1_descriptor)));
  81. } else {
  82. caml_array_set
  83. ($result,
  84. i,
  85. caml_val_ptr ((void *)&$1[i],$&1_descriptor));
  86. }
  87. }
  88. }
  89. %typemap(in) enum SWIGTYPE [] {
  90. int i;
  91. /* $*1_type */
  92. $1 = ($*1_type *)malloc( $1_size );
  93. for( i = 0; i < $1_dim0 && i < caml_array_len($input); i++ ) {
  94. $1[i] = ($type)
  95. caml_long_val_full(caml_array_nth($input),
  96. "$type_marker");
  97. }
  98. }
  99. %typemap(out) enum SWIGTYPE [] {
  100. int i;
  101. $result = caml_array_new($1_dim0);
  102. for( i = 0; i < $1_dim0; i++ ) {
  103. caml_array_set
  104. ($result,
  105. i,
  106. callback2(*caml_named_value(SWIG_MODULE "_int_to_enum"),
  107. *caml_named_value("$type_marker"),
  108. Val_int($1[i])));
  109. }
  110. }
  111. #ifdef __cplusplus
  112. %typemap(freearg) SWIGTYPE [ANY] {
  113. delete [] $1;
  114. }
  115. #else
  116. %typemap(freearg) SWIGTYPE [ANY] {
  117. free( (void *)$1 );
  118. }
  119. #endif