Skip to content

Commit 207c621

Browse files
authored
Merge pull request #6 from swig-fortran/update-swig
Use new SWIG %fortrancallback feature
2 parents 16ebae5 + 1eb13c9 commit 207c621

File tree

2 files changed

+101
-103
lines changed

2 files changed

+101
-103
lines changed

include/flc_algorithm.i

Lines changed: 18 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include <numeric>
1616
%}
1717

18+
/* -------------------------------------------------------------------------
19+
* Macros
20+
* ------------------------------------------------------------------------- */
1821
%define %flc_cmp_algorithm(RETURN_TYPE, FUNCNAME, ARGS, CALL)
1922

2023
%inline {
@@ -39,29 +42,17 @@ static RETURN_TYPE FUNCNAME##_cmp(ARGS, bool (*cmp)(T, T)) {
3942

4043
%enddef
4144

42-
// >>> Create a native function pointer interface for the given comparator.
43-
44-
%define %flc_cmp_funptr(CTYPE, FTYPE)
45-
46-
#define flc_cmp_funptr flc_cmp_funptr_ ## %mangle(CTYPE)
45+
/* ------------------------------------------------------------------------- */
46+
%define %flc_typemaps(NAME, TYPE...)
4747

48-
// Define an abstract interface that gets inserted into the module
49-
%fragment("flc_cmp_funptr"{CTYPE}, "fdecl", noblock=1)
50-
{ abstract interface
51-
function flc_cmp_funptr(left, right) bind(C) &
52-
result(fresult)
53-
use, intrinsic :: ISO_C_BINDING
54-
FTYPE, intent(in), value :: left, right
55-
logical(C_BOOL) :: fresult
56-
end function
57-
end interface}
58-
59-
%apply bool (*)(SWIGTYPE, SWIGTYPE) { bool (*)(CTYPE, CTYPE) };
60-
%typemap(ftype, in={procedure(flc_cmp_funptr)},
61-
fragment="flc_cmp_funptr"{CTYPE}, noblock=1) bool (*)(CTYPE, CTYPE)
62-
{procedure(flc_cmp_funptr), pointer}
48+
// Apply array conversion typemap
49+
%apply (const SWIGTYPE *DATA, size_t SIZE) {
50+
(TYPE const *DATA1, size_t DATASIZE1),
51+
(TYPE const *DATA2, size_t DATASIZE2) };
6352

64-
#undef SWIG_cmp_funptr
53+
// Explicitly declare function interface for callbacks
54+
%fortrancallback("%s") flc_cmp_##NAME;
55+
extern "C" bool flc_cmp_##NAME(TYPE left, TYPE right);
6556

6657
%enddef
6758

@@ -85,28 +76,12 @@ typedef int index_int;
8576
// Apply array-to-C translation for numeric values
8677
%apply (SWIGTYPE *DATA, size_t SIZE) { (index_int *IDX, size_t IDXSIZE) };
8778

88-
%apply (const SWIGTYPE *DATA, size_t SIZE) {
89-
(int32_t const *DATA1, size_t DATASIZE1),
90-
(int64_t const *DATA1, size_t DATASIZE1),
91-
(double const *DATA1, size_t DATASIZE1),
92-
(void* const *DATA1, size_t DATASIZE1),
93-
(int32_t const *DATA2, size_t DATASIZE2),
94-
(int64_t const *DATA2, size_t DATASIZE2),
95-
(double const *DATA2, size_t DATASIZE2),
96-
(void* const *DATA2, size_t DATASIZE2)};
97-
98-
99-
// Make function pointers available as generic types
100-
%typemap(fin) bool (*)(SWIGTYPE, SWIGTYPE)
101-
"$1 = c_funloc($input)"
102-
%typemap(fout) bool (*)(CTYPE, CTYPE)
103-
"call c_f_procpointer($1, $result)"
104-
105-
%flc_cmp_funptr(int32_t, integer(C_INT32_T))
106-
%flc_cmp_funptr(int64_t, integer(C_INT64_T))
107-
%flc_cmp_funptr(double, real(C_DOUBLE))
108-
%flc_cmp_funptr(index_int, integer(INDEX_INT))
109-
%flc_cmp_funptr(void*, type(C_PTR))
79+
// Apply array and callback typemaps
80+
%flc_typemaps(int4 , int32_t )
81+
%flc_typemaps(int8 , int64_t )
82+
%flc_typemaps(real8, double )
83+
%flc_typemaps(index, index_int )
84+
%flc_typemaps(ptr , void* )
11085

11186
/* -------------------------------------------------------------------------
11287
* Sorting routines

0 commit comments

Comments
 (0)