15
15
#include < numeric>
16
16
%}
17
17
18
+ /* -------------------------------------------------------------------------
19
+ * Macros
20
+ * ------------------------------------------------------------------------- */
18
21
%define %flc_cmp_algorithm(RETURN_TYPE, FUNCNAME, ARGS, CALL)
19
22
20
23
%inline {
@@ -39,29 +42,17 @@ static RETURN_TYPE FUNCNAME##_cmp(ARGS, bool (*cmp)(T, T)) {
39
42
40
43
%enddef
41
44
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...)
47
47
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) };
63
52
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);
65
56
66
57
%enddef
67
58
@@ -85,28 +76,12 @@ typedef int index_int;
85
76
// Apply array-to-C translation for numeric values
86
77
%apply (SWIGTYPE *DATA, size_t SIZE) { (index_int *IDX, size_t IDXSIZE) };
87
78
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 * )
110
85
111
86
/* -------------------------------------------------------------------------
112
87
* Sorting routines
0 commit comments