Commit c8f41fc4 authored by Karl Wette's avatar Karl Wette
Browse files

SWIG: refactor dynamic array macros

- new macro %swiglal_array_dynamic_size() for creating size members
- allow array lengths NI, NJ to be constants as well as struct members
Original: 4abddb3ea3b9c54d30e55e0a867e08b2967897d0
parent d7f2dbc6
......@@ -66,7 +66,8 @@
%swiglal_call_dtor(gsl_vector##NAME##_free, $self);
}
}
%swiglal_array_dynamic_1D(TYPE, size_t, data, size, arg1->stride);
%swiglal_array_dynamic_size(size_t, size);
%swiglal_array_dynamic_1D(TYPE, size_t, data, arg1->size, arg1->stride);
} gsl_vector##NAME;
// GSL matrix of type NAME.
......@@ -84,7 +85,9 @@
%swiglal_call_dtor(gsl_matrix##NAME##_free, $self);
}
}
%swiglal_array_dynamic_2D(TYPE, size_t, data, size1, size2, arg1->tda, 1);
%swiglal_array_dynamic_size(size_t, size1);
%swiglal_array_dynamic_size(size_t, size2);
%swiglal_array_dynamic_2D(TYPE, size_t, data, arg1->size1, arg1->size2, arg1->tda, 1);
} gsl_matrix##NAME;
%enddef // %lalswig_gsl_vector_matrix
......
......@@ -615,6 +615,18 @@ if (swiglal_release_parent(PTR)) {
%typemap(swiglal_dynarr_isptr) SWIGTYPE* "true";
%typemap(swiglal_dynarr_tinfo) SWIGTYPE* "$descriptor";
// Create immutable members for accessing the array's dimensions.
// NI is the name of the dimension member, and SIZET is its type.
%define %swiglal_array_dynamic_size(SIZET, NI)
%feature("action") NI {
result = %static_cast(arg1->NI, SIZET);
}
%extend {
const SIZET NI;
}
%feature("action", "") NI;
%enddef // %swiglal_array_dynamic_size()
// Check that array dimensions and strides are non-zero, otherwise fail.
%define %swiglal_array_dynamic_check_dims_strides(DATA, I)
if (dims[I-1] == 0 || strides[I-1] == 0) {
......@@ -628,22 +640,15 @@ if (swiglal_release_parent(PTR)) {
// comprising the array are defined. The DATA and N{I,J} members give
// the array data and dimensions, TYPE and SIZET give their respective
// types. The S{I,J} give the strides of the array, in number of elements.
// If the strides are members of the struct, 'arg1->' should be used to
// access the struct itself.
// If the sizes or strides are members of the struct, 'arg1->' should be
// used to access the struct itself.
// 1-D arrays:
%define %swiglal_array_dynamic_1D(TYPE, SIZET, DATA, NI, SI)
// Create immutable members for the array's dimensions.
%feature("action") NI {result = %static_cast(arg1->NI, SIZET);}
%extend {
const SIZET NI;
}
%feature("action", "") NI;
// Typemaps which convert to/from the dynamically-allocated array.
%typemap(in, noblock=1) TYPE* DATA {
if (arg1) {
const size_t dims[] = {arg1->NI};
const size_t dims[] = {NI};
const size_t strides[] = {SI};
%swiglal_array_dynamic_check_dims_strides(DATA, 1);
$1 = %reinterpret_cast(arg1->DATA, TYPE*);
......@@ -659,7 +664,7 @@ if (swiglal_release_parent(PTR)) {
}
%typemap(out, noblock=1) TYPE* DATA {
if (arg1) {
const size_t dims[] = {arg1->NI};
const size_t dims[] = {NI};
const size_t strides[] = {SI};
%swiglal_array_dynamic_check_dims_strides(DATA, 1);
$1 = %reinterpret_cast(arg1->DATA, TYPE*);
......@@ -692,20 +697,10 @@ if (swiglal_release_parent(PTR)) {
// 2-D arrays:
%define %swiglal_array_dynamic_2D(TYPE, SIZET, DATA, NI, NJ, SI, SJ)
// Create immutable members for the array's dimensions.
%feature("action") NI {result = %static_cast(arg1->NI, SIZET);}
%feature("action") NJ {result = %static_cast(arg1->NJ, SIZET);}
%extend {
const SIZET NI;
const SIZET NJ;
}
%feature("action", "") NI;
%feature("action", "") NJ;
// Typemaps which convert to/from the dynamically-allocated array.
%typemap(in, noblock=1) TYPE* DATA {
if (arg1) {
const size_t dims[] = {arg1->NI, arg1->NJ};
const size_t dims[] = {NI, NJ};
const size_t strides[] = {SI, SJ};
%swiglal_array_dynamic_check_dims_strides(DATA, 1);
%swiglal_array_dynamic_check_dims_strides(DATA, 2);
......@@ -722,7 +717,7 @@ if (swiglal_release_parent(PTR)) {
}
%typemap(out, noblock=1) TYPE* DATA {
if (arg1) {
const size_t dims[] = {arg1->NI, arg1->NJ};
const size_t dims[] = {NI, NJ};
const size_t strides[] = {SI, SJ};
%swiglal_array_dynamic_check_dims_strides(DATA, 1);
%swiglal_array_dynamic_check_dims_strides(DATA, 2);
......@@ -756,19 +751,26 @@ if (swiglal_release_parent(PTR)) {
// These macros should be called from within the definitions of
// LAL structs containing dynamically-allocated arrays.
// 1-D arrays:
// 1-D arrays, e.g:
// SIZET NI;
// TYPE* DATA;
%define %swiglal_public_1D_ARRAY(TYPE, DATA, SIZET, NI)
%swiglal_array_dynamic_1D(TYPE, SIZET, DATA, NI, 1);
%ignore DATA;
%ignore NI;
%swiglal_array_dynamic_size(SIZET, NI);
%swiglal_array_dynamic_1D(TYPE, SIZET, DATA, arg1->NI, 1);
%ignore DATA;
%ignore NI;
%enddef
#define %swiglal_public_clear_1D_ARRAY(TYPE, DATA, SIZET, NI)
// 2-D arrays:
// 2-D arrays, e.g:
// SIZET NI, NJ;
// TYPE* DATA;
%define %swiglal_public_2D_ARRAY(TYPE, DATA, SIZET, NI, NJ)
%swiglal_array_dynamic_2D(TYPE, SIZET, DATA, NI, NJ, arg1->NJ, 1);
%ignore DATA;
%ignore NI;
%ignore NJ;
%swiglal_array_dynamic_size(SIZET, NI);
%swiglal_array_dynamic_size(SIZET, NJ);
%swiglal_array_dynamic_2D(TYPE, SIZET, DATA, arg1->NI, arg1->NJ, arg1->NJ, 1);
%ignore DATA;
%ignore NI;
%ignore NJ;
%enddef
#define %swiglal_public_clear_2D_ARRAY(TYPE, DATA, SIZET, NI, NJ)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment