carrays.i library modified to use size_t instead of int

in the %array_functions and %array_class macros.

Affects C#, D, Go, Guile, Java, Javascript, Lua, Ocaml, R, Racket.
Closes #1680

If the old types are required for backwards compatibility, use %apply to
restore the old types as follows:

  %include "carrays.i"
  %apply int { size_t nelements, size_t index }
  ... %array_functions and %array_class ...
  %clear size_t nelements, size_t index; # To be safe in case used elsewhere
This commit is contained in:
William S Fulton 2023-10-09 20:40:29 +01:00
parent e66ce11814
commit ecaa052f3d
11 changed files with 92 additions and 84 deletions

View File

@ -7,6 +7,21 @@ the issue number to the end of the URL: https://github.com/swig/swig/issues/
Version 4.2.0 (in progress)
===========================
2023-10-11: wsfulton
[C#, D, Go, Guile, Java, Javascript, Lua, Ocaml, R, Racket] #1680
carrays.i library modified to use size_t instead of int for the functions
provided by %array_functions and %array_class.
If the old types are required for backwards compatibility, use %apply to
restore the old types as follows:
%include "carrays.i"
%apply int { size_t nelements, size_t index }
... %array_functions and %array_class ...
%clear size_t nelements, size_t index; # To be safe in case used elsewhere
*** POTENTIAL INCOMPATIBILITY ***
2023-10-11: olly
[PHP] #2685 Fix testcase director_finalizer to work with PHP 8.3.

View File

@ -5427,10 +5427,10 @@ destroy arrays and operate on elements. In this case, the functions are as foll
<div class="code">
<pre>
int *new_intArray(int nelements);
int *new_intArray(size_t nelements);
void delete_intArray(int *x);
int intArray_getitem(int *x, int index);
void intArray_setitem(int *x, int index, int value);
int intArray_getitem(int *x, size_t index);
void intArray_setitem(int *x, size_t index, int value);
</pre>
</div>

View File

@ -393,7 +393,7 @@ raw C array data.
<p>Creates four functions.</p>
<p>
<tt>type *new_name(int nelements)</tt>
<tt>type *new_name(size_t nelements)</tt>
</p>
<div class="indent"><p>
@ -410,7 +410,7 @@ Deletes an array. In C, <tt>free()</tt> is used. In C++, <tt>delete []</tt> is
</p></div>
<p>
<tt>type name_getitem(type *ary, int index)</tt>
<tt>type name_getitem(type *ary, size_t index)</tt>
</p>
<div class="indent"><p>
@ -418,7 +418,7 @@ Returns the value <tt>ary[index]</tt>.
</p></div>
<p>
<tt>void name_setitem(type *ary, int index, type value)</tt>
<tt>void name_setitem(type *ary, size_t index, type value)</tt>
</p>
<div class="indent"><p>
@ -491,10 +491,10 @@ interface is as follows:
<div class="code">
<pre>
struct name {
name(int nelements); // Create an array
name(size_t nelements); // Create an array
~name(); // Delete array
type getitem(int index); // Return item
void setitem(int index, type value); // Set item
type getitem(size_t index); // Return item
void setitem(size_t index, type value); // Set item
type *cast(); // Cast to original type
static name *frompointer(type *); // Create class wrapper from
// existing pointer

View File

@ -1624,8 +1624,8 @@ does SWIG have any indication of how large an array should be. However with the
arrays for convenient usage.</p>
<p>Given the functions:</p>
<div class="code"><pre>extern void sort_int(int* arr, int len);
extern void sort_double(double* arr, int len);
<div class="code"><pre>extern void sort_int(int* arr, size_t len);
extern void sort_double(double* arr, size_t len);
</pre></div>
<p>There are basically two ways that SWIG can deal with this. The first way, uses the <tt>&lt;carrays.i&gt;</tt> library
@ -1641,13 +1641,13 @@ More details can be found in the <a href="Library.html#Library_carrays">carrays.
// this declares a batch of functions for manipulating C integer arrays
%array_functions(int, int)
extern void sort_int(int* arr, int len); // the function to wrap
extern void sort_int(int* arr, size_t len); // the function to wrap
// using typemaps
%include &lt;typemaps.i&gt;
%apply (double *INOUT, int) {(double* arr, int len)};
%apply (double *INOUT, int) {(double* arr, size_t len)};
extern void sort_double(double* arr, int len); // the function to wrap
extern void sort_double(double* arr, size_t len); // the function to wrap
</pre></div>
<p>Once wrapped, the functions can both be called, though with different ease of use:</p>

View File

@ -17,16 +17,16 @@
* Generates functions for creating and accessing elements of a C array
* (as pointers). Creates the following functions:
*
* TYPE *new_NAME(int nelements)
* TYPE *new_NAME(size_t nelements)
* void delete_NAME(TYPE *);
* TYPE NAME_getitem(TYPE *, int index);
* void NAME_setitem(TYPE *, int index, TYPE value);
*
* TYPE NAME_getitem(TYPE *, size_t index);
* void NAME_setitem(TYPE *, size_t index, TYPE value);
*
* ----------------------------------------------------------------------------- */
%define %array_functions(TYPE,NAME)
%{
static TYPE *new_##NAME(int nelements) { %}
static TYPE *new_##NAME(size_t nelements) { %}
#ifdef __cplusplus
%{ return new TYPE[nelements](); %}
#else
@ -42,18 +42,18 @@ static void delete_##NAME(TYPE *ary) { %}
#endif
%{}
static TYPE NAME##_getitem(TYPE *ary, int index) {
static TYPE NAME##_getitem(TYPE *ary, size_t index) {
return ary[index];
}
static void NAME##_setitem(TYPE *ary, int index, TYPE value) {
static void NAME##_setitem(TYPE *ary, size_t index, TYPE value) {
ary[index] = value;
}
%}
TYPE *new_##NAME(int nelements);
TYPE *new_##NAME(size_t nelements);
void delete_##NAME(TYPE *ary);
TYPE NAME##_getitem(TYPE *ary, int index);
void NAME##_setitem(TYPE *ary, int index, TYPE value);
TYPE NAME##_getitem(TYPE *ary, size_t index);
void NAME##_setitem(TYPE *ary, size_t index, TYPE value);
%enddef
@ -65,13 +65,13 @@ void NAME##_setitem(TYPE *ary, int index, TYPE value);
* interface:
*
* struct NAME {
* NAME(int nelements);
* NAME(size_t nelements);
* ~NAME();
* TYPE getitem(int index);
* void setitem(int index, TYPE value);
* TYPE getitem(size_t index);
* void setitem(size_t index, TYPE value);
* TYPE * cast();
* static NAME *frompointer(TYPE *t);
* }
* }
*
* ----------------------------------------------------------------------------- */
@ -86,14 +86,14 @@ typedef struct {
%extend NAME {
#ifdef __cplusplus
NAME(int nelements) {
NAME(size_t nelements) {
return new TYPE[nelements]();
}
~NAME() {
delete [] self;
}
#else
NAME(int nelements) {
NAME(size_t nelements) {
return (TYPE *) calloc(nelements,sizeof(TYPE));
}
~NAME() {
@ -101,10 +101,10 @@ NAME(int nelements) {
}
#endif
TYPE getitem(int index) {
TYPE getitem(size_t index) {
return self[index];
}
void setitem(int index, TYPE value) {
void setitem(size_t index, TYPE value) {
self[index] = value;
}
TYPE * cast() {

View File

@ -10,16 +10,16 @@
* Generates functions for creating and accessing elements of a C array
* (as pointers). Creates the following functions:
*
* TYPE *new_NAME(int nelements)
* TYPE *new_NAME(size_t nelements)
* void delete_NAME(TYPE *);
* TYPE NAME_getitem(TYPE *, int index);
* void NAME_setitem(TYPE *, int index, TYPE value);
* TYPE NAME_getitem(TYPE *, size_t index);
* void NAME_setitem(TYPE *, size_t index, TYPE value);
*
* ----------------------------------------------------------------------------- */
%define %array_functions(TYPE,NAME)
%{
static TYPE *new_##NAME(int nelements) { %}
static TYPE *new_##NAME(size_t nelements) { %}
#ifdef __cplusplus
%{ return new TYPE[nelements](); %}
#else
@ -35,18 +35,18 @@ static void delete_##NAME(TYPE *ary) { %}
#endif
%{}
static TYPE NAME##_getitem(TYPE *ary, int index) {
static TYPE NAME##_getitem(TYPE *ary, size_t index) {
return ary[index];
}
static void NAME##_setitem(TYPE *ary, int index, TYPE value) {
static void NAME##_setitem(TYPE *ary, size_t index, TYPE value) {
ary[index] = value;
}
%}
TYPE *new_##NAME(int nelements);
TYPE *new_##NAME(size_t nelements);
void delete_##NAME(TYPE *ary);
TYPE NAME##_getitem(TYPE *ary, int index);
void NAME##_setitem(TYPE *ary, int index, TYPE value);
TYPE NAME##_getitem(TYPE *ary, size_t index);
void NAME##_setitem(TYPE *ary, size_t index, TYPE value);
%enddef
@ -58,13 +58,13 @@ void NAME##_setitem(TYPE *ary, int index, TYPE value);
* interface:
*
* struct NAME {
* NAME(int nelements);
* NAME(size_t nelements);
* ~NAME();
* TYPE getitem(int index);
* void setitem(int index, TYPE value);
* TYPE getitem(size_t index);
* void setitem(size_t index, TYPE value);
* TYPE * ptr();
* static NAME *frompointer(TYPE *t);
* }
* }
*
* ----------------------------------------------------------------------------- */
@ -76,34 +76,36 @@ typedef TYPE NAME;
typedef struct {} NAME;
%extend NAME {
#ifdef __cplusplus
NAME(int nelements) {
return new TYPE[nelements]();
}
~NAME() {
delete [] self;
}
NAME(size_t nelements) {
return new TYPE[nelements]();
}
~NAME() {
delete [] self;
}
#else
NAME(int nelements) {
return (TYPE *) calloc(nelements,sizeof(TYPE));
}
~NAME() {
free(self);
}
NAME(size_t nelements) {
return (TYPE *) calloc(nelements,sizeof(TYPE));
}
~NAME() {
free(self);
}
#endif
TYPE getitem(int index) {
return self[index];
}
void setitem(int index, TYPE value) {
self[index] = value;
}
TYPE * ptr() {
return self;
}
static NAME *frompointer(TYPE *t) {
return (NAME *) t;
}
TYPE getitem(size_t index) {
return self[index];
}
void setitem(size_t index, TYPE value) {
self[index] = value;
}
TYPE * ptr() {
return self;
}
static NAME *frompointer(TYPE *t) {
return (NAME *) t;
}
};
%types(NAME = TYPE);

View File

@ -1,2 +1 @@
%include <typemaps/carrays.swg>

View File

@ -7,7 +7,3 @@
%enddef
%include <typemaps/carrays.swg>

View File

@ -3,4 +3,3 @@
%enddef
%include <typemaps/carrays.swg>

View File

@ -1,4 +1 @@
%include <typemaps/carrays.swg>

View File

@ -11,10 +11,10 @@
* Generates functions for creating and accessing elements of a C array
* (as pointers). Creates the following functions:
*
* TYPE *new_NAME(int nelements)
* TYPE *new_NAME(size_t nelements)
* void delete_NAME(TYPE *);
* TYPE NAME_getitem(TYPE *, int index);
* void NAME_setitem(TYPE *, int index, TYPE value);
* TYPE NAME_getitem(TYPE *, size_t index);
* void NAME_setitem(TYPE *, size_t index, TYPE value);
*
* ----------------------------------------------------------------------------- */
@ -51,10 +51,10 @@ void NAME##_setitem(TYPE *ary, size_t index, TYPE value);
* interface:
*
* struct NAME {
* NAME(int nelements);
* NAME(size_t nelements);
* ~NAME();
* TYPE getitem(int index);
* void setitem(int index, TYPE value);
* TYPE getitem(size_t index);
* void setitem(size_t index, TYPE value);
* TYPE * cast();
* static NAME *frompointer(TYPE *t);
* }