Skip to content

Commit 9b67679

Browse files
committed
Unify formatting of interface files
1 parent 4a7c4b9 commit 9b67679

File tree

6 files changed

+112
-67
lines changed

6 files changed

+112
-67
lines changed

include/flc.i

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
#error "To import the FLC module correctly, use ``%include \"import_flc.i\"``"
1212
#endif
1313

14+
/* -------------------------------------------------------------------------
15+
* Header definition macros
16+
* ------------------------------------------------------------------------- */
17+
1418
%define %flc_add_header
1519
%insert("fbegin") %{
1620
! Flibcpp project, https://github.com/swig-fortran/flibcpp
@@ -28,26 +32,27 @@
2832

2933
%flc_add_header
3034

31-
/************************
35+
/* -------------------------------------------------------------------------
3236
* Exception handling
33-
************************/
37+
* ------------------------------------------------------------------------- */
3438

35-
/* Rename the error variables' internal C symbols */
39+
// Rename the error variables' internal C symbols
3640
#define SWIG_FORTRAN_ERROR_INT flc_ierr
3741
#define SWIG_FORTRAN_ERROR_STR flc_get_serr
3842

39-
/* Restore names in the wrapper code */
43+
// Restore names in the wrapper code
4044
%rename(ierr) flc_ierr;
4145
%rename(get_serr) flc_get_serr;
4246

43-
/* Unless we're directly building this module, delay exception handling */
47+
// Unless we're directly building this module, delay exception handling
4448
#ifndef SWIGIMPORTED
4549
%include <exception.i>
4650
#endif
4751

48-
/******************************
52+
/* -------------------------------------------------------------------------
4953
* Data types and instantiation
50-
******************************/
54+
* ------------------------------------------------------------------------- */
55+
5156
%{
5257
#include <cstdint>
5358
using std::int32_t;
@@ -62,9 +67,9 @@ using std::size_t;
6267
%template(DST) SRC<double>;
6368
%enddef
6469

65-
/************************
70+
/* -------------------------------------------------------------------------
6671
* Array view translation
67-
************************/
72+
* ------------------------------------------------------------------------- */
6873

6974
%include <typemaps.i>
7075
%apply (SWIGTYPE *DATA, size_t SIZE) {
@@ -77,17 +82,16 @@ using std::size_t;
7782
(const int64_t *DATA, size_t DATASIZE),
7883
(const double *DATA, size_t DATASIZE) };
7984

80-
/************************
85+
/* -------------------------------------------------------------------------
8186
* Version information
82-
*
83-
* Linked into auto-generated file flibcpp_version.cpp
84-
************************/
87+
* ------------------------------------------------------------------------- */
8588

8689
%apply char* { const char flibcpp_version[] };
8790
%fortranbindc flibcpp_version_major;
8891
%fortranbindc flibcpp_version_minor;
8992
%fortranbindc flibcpp_version_patch;
9093

94+
// These symbols are defined in the CMake-generated `flibcpp_version.cpp`
9195
%inline %{
9296
extern "C" {
9397
extern const char flibcpp_version[];

include/flc_algorithm.i

Lines changed: 53 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,24 @@ static RETURN_TYPE FUNCNAME##_cmp(ARGS, bool (*cmp)(T, T)) {
5555

5656
%enddef
5757

58-
/******************************
58+
/* -------------------------------------------------------------------------
5959
* Types
60-
******************************/
60+
* ------------------------------------------------------------------------- */
6161

62-
// >>> Index integer type
62+
// Alias the native C integer to an "indexing" integer returned by algorithm
63+
// functions.
6364
%inline %{
6465
typedef int index_int;
6566
%}
6667
%insert("fdecl") %{integer, parameter, public :: INDEX_INT = C_INT
6768
%}
6869

70+
// Give it a particularly named type in the Fortran proxy code.
6971
%apply int { index_int };
7072
%typemap(ftype, in={integer(INDEX_INT), intent(in)}) index_int
71-
%{integer(INDEX_INT)%}
73+
%{integer(INDEX_INT)%}
7274

73-
// >>> Array types
75+
// Apply array-to-C translation for numeric values
7476
%apply (SWIGTYPE *DATA, size_t SIZE) { (index_int *IDX, size_t IDXSIZE) };
7577

7678
%apply (const SWIGTYPE *DATA, size_t SIZE) {
@@ -82,22 +84,22 @@ typedef int index_int;
8284
(const double *DATA2, size_t DATASIZE2) };
8385

8486

85-
// >>> Function pointer types
87+
// Make function pointers available as generic types
8688
%typemap(fin) bool (*)(SWIGTYPE, SWIGTYPE)
8789
"$1 = c_funloc($input)"
8890
%typemap(findecl, match="fin") bool (*)(SWIGTYPE, SWIGTYPE) ""
8991
%typemap(fout) bool (*)(CTYPE, CTYPE)
9092
"call c_f_procpointer($1, $result)"
9193
%typemap(foutdecl, match="fout") bool (*)(SWIGTYPE, SWIGTYPE) ""
9294

93-
%flc_cmp_funptr(int32_t, integer(C_INT32_T))
94-
%flc_cmp_funptr(int64_t, integer(C_INT64_T))
95-
%flc_cmp_funptr(double, real(C_DOUBLE))
96-
%flc_cmp_funptr(index_int, integer(INDEX_INT))
95+
%flc_cmp_funptr(int32_t, integer(C_INT32_T))
96+
%flc_cmp_funptr(int64_t, integer(C_INT64_T))
97+
%flc_cmp_funptr(double, real(C_DOUBLE))
98+
%flc_cmp_funptr(index_int, integer(INDEX_INT))
9799

98-
/******************************
99-
* Sorting
100-
******************************/
100+
/* -------------------------------------------------------------------------
101+
* Sorting routines
102+
* ------------------------------------------------------------------------- */
101103

102104
%{
103105
template<class T, class Compare>
@@ -130,21 +132,25 @@ static void argsort_impl(const T *data, size_t size,
130132

131133
%}
132134

133-
%flc_cmp_algorithm(void, sort, %arg(T *DATA, size_t DATASIZE),
135+
%flc_cmp_algorithm(void, sort,
136+
%arg(T *DATA, size_t DATASIZE),
134137
%arg(DATA, DATASIZE))
135-
%flc_cmp_algorithm(bool, is_sorted, %arg(const T *DATA, size_t DATASIZE),
138+
%flc_cmp_algorithm(bool, is_sorted,
139+
%arg(const T *DATA, size_t DATASIZE),
136140
%arg(DATA, DATASIZE))
137-
%flc_cmp_algorithm(void, argsort, %arg(const T *DATA, size_t DATASIZE,
138-
index_int *IDX, size_t IDXSIZE),
141+
%flc_cmp_algorithm(void, argsort,
142+
%arg(const T *DATA, size_t DATASIZE,
143+
index_int *IDX, size_t IDXSIZE),
139144
%arg(DATA, DATASIZE, IDX, IDXSIZE))
140145

141-
/******************************
142-
* Searching
143-
******************************/
146+
/* -------------------------------------------------------------------------
147+
* Searching routines
148+
* ------------------------------------------------------------------------- */
144149

145150
%{
146151
template<class T, class Compare>
147-
static index_int binary_search_impl(const T *data, size_t size, T value, Compare cmp) {
152+
static index_int binary_search_impl(const T *data, size_t size, T value,
153+
Compare cmp) {
148154
const T *end = data + size;
149155
auto iter = std::lower_bound(data, end, value, cmp);
150156
if (iter == end || cmp(*iter, value) || cmp(value, *iter))
@@ -154,7 +160,9 @@ static index_int binary_search_impl(const T *data, size_t size, T value, Compare
154160
}
155161

156162
template<class T, class Compare>
157-
static void equal_range_impl(const T *data, size_t size, T value, index_int &first_index, index_int &last_index, Compare cmp) {
163+
static void equal_range_impl(const T *data, size_t size, T value,
164+
index_int &first_index, index_int &last_index,
165+
Compare cmp) {
158166
const T *end = data + size;
159167
auto range_pair = std::equal_range(data, end, value, cmp);
160168
// Index of the min/max items *IN FORTAN INDEXING*
@@ -163,7 +171,9 @@ static void equal_range_impl(const T *data, size_t size, T value, index_int &fir
163171
}
164172

165173
template<class T, class Compare>
166-
static void minmax_element_impl(const T *data, size_t size, index_int &min_index, index_int &max_index, Compare cmp) {
174+
static void minmax_element_impl(const T *data, size_t size,
175+
index_int &min_index, index_int &max_index,
176+
Compare cmp) {
167177
const T *end = data + size;
168178
auto mm_pair = std::minmax_element(data, end, cmp);
169179
// Index of the min/max items *IN FORTAN INDEXING*
@@ -172,37 +182,41 @@ static void minmax_element_impl(const T *data, size_t size, index_int &min_index
172182
}
173183
%}
174184

175-
%flc_cmp_algorithm(index_int, binary_search, %arg(const T *DATA, size_t DATASIZE,
176-
T value),
185+
%flc_cmp_algorithm(index_int, binary_search,
186+
%arg(const T *DATA, size_t DATASIZE, T value),
177187
%arg(DATA, DATASIZE, value))
178188

179-
%flc_cmp_algorithm(void, equal_range, %arg(const T *DATA, size_t DATASIZE,
180-
T value,
181-
index_int &first_index, index_int &last_index),
189+
%flc_cmp_algorithm(void, equal_range,
190+
%arg(const T *DATA, size_t DATASIZE, T value,
191+
index_int &first_index, index_int &last_index),
182192
%arg(DATA, DATASIZE, value, first_index, last_index))
183193

184-
%flc_cmp_algorithm(void, minmax_element, %arg(const T *DATA, size_t DATASIZE,
185-
index_int &min_index, index_int &max_index),
194+
%flc_cmp_algorithm(void, minmax_element,
195+
%arg(const T *DATA, size_t DATASIZE,
196+
index_int &min_index, index_int &max_index),
186197
%arg(DATA, DATASIZE, min_index, max_index))
187198

188-
/******************************
189-
* Set operations
190-
******************************/
199+
/* -------------------------------------------------------------------------
200+
* Set operation routines
201+
* ------------------------------------------------------------------------- */
191202

192203
%{
193204
template<class T, class Compare>
194-
static bool includes_impl(const T *data1, size_t size1, const T *data2, size_t size2, Compare cmp) {
205+
static bool includes_impl(const T *data1, size_t size1,
206+
const T *data2, size_t size2,
207+
Compare cmp) {
195208
return std::includes(data1, data1 + size1, data2, data2 + size2, cmp);
196209
}
197210
%}
198211

199-
%flc_cmp_algorithm(bool, includes, %arg(const T *DATA1, size_t DATASIZE1,
200-
const T *DATA2, size_t DATASIZE2),
212+
%flc_cmp_algorithm(bool, includes,
213+
%arg(const T *DATA1, size_t DATASIZE1,
214+
const T *DATA2, size_t DATASIZE2),
201215
%arg(DATA1, DATASIZE1, DATA2, DATASIZE2))
202216

203-
/******************************
204-
* Reordering
205-
******************************/
217+
/* -------------------------------------------------------------------------
218+
* Modifying routines
219+
* ------------------------------------------------------------------------- */
206220

207221
%{
208222
#include <random>

include/flc_chrono.i

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
%include "import_flc.i"
1010
%flc_add_header
1111

12+
/* -------------------------------------------------------------------------
13+
* Utility routines
14+
* ------------------------------------------------------------------------- */
15+
1216
%{
1317
#include <chrono>
1418
#include <thread>

include/flc_random.i

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
%include "import_flc.i"
1010
%flc_add_header
1111

12-
/******************************
13-
* Generator definition
14-
******************************/
12+
/* -------------------------------------------------------------------------
13+
* Generator class definition
14+
* ------------------------------------------------------------------------- */
1515

1616
%{
1717
#include <random>
@@ -42,13 +42,13 @@ class SWIG_MERSENNE_TWISTER
4242
};
4343
} // namespace std
4444

45-
/******************************
46-
* Distributions
45+
/* -------------------------------------------------------------------------
46+
* RNG distribution routines
4747
*
4848
* The generated subroutines will be called from Fortran like:
4949
*
5050
* call uniform_real_distribution(gen, -10, 10, fill_array)
51-
******************************/
51+
* ------------------------------------------------------------------------- */
5252

5353
%define %flc_random_distribution1(DISTNAME, TYPE, ARG1)
5454
%inline {

include/flc_string.i

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,21 @@
1616
// Include typemaps for integer offsets and native integer types
1717
%include <std_common.i>
1818

19-
// Typemap to convert positions from npos -> 0 and 1-offset otherwise
19+
/* -------------------------------------------------------------------------
20+
* Typemaps
21+
* ------------------------------------------------------------------------- */
22+
23+
// Typemap to convert positions from npos -> 0 and 1-offset otherwise. Similar
24+
// to
2025
%apply int FORTRAN_INT { std::size_t POSITION };
2126
%typemap(out, noblock=1) std::size_t POSITION {
2227
$result = ($1 == std::string::npos ? 0 : $1 + 1);
2328
}
2429

30+
/* -------------------------------------------------------------------------
31+
* String class definition
32+
* ------------------------------------------------------------------------- */
33+
2534
// Automatically free temporary strings as appropriate
2635
%fortran_autofree_rvalue(std::string);
2736

@@ -113,6 +122,10 @@ class string {
113122
}
114123
};
115124

125+
/* -------------------------------------------------------------------------
126+
* String conversion routines
127+
* ------------------------------------------------------------------------- */
128+
116129
%exception {
117130
SWIG_check_unhandled_exception();
118131
try {
@@ -126,7 +139,8 @@ class string {
126139
}
127140
}
128141

129-
%fragment("flc_has_junk", "header", fragment="<cctype>", fragment="<algorithm>") %{
142+
%fragment("flc_has_junk", "header",
143+
fragment="<cctype>", fragment="<algorithm>") %{
130144
SWIGINTERN bool flc_has_junk(const std::string& s, size_t pos) {
131145
return !std::all_of(s.begin() + pos, s.end(),
132146
[](unsigned char c) -> bool { return std::isspace(c); });
@@ -155,7 +169,7 @@ class string {
155169
%add_string_real_conversion(float, stof);
156170
%add_string_real_conversion(double, stod);
157171

158-
// Don't add exception code later
172+
// Don't add exception code for subsequent functions
159173
%exception;
160174

161175
} // namespace std

0 commit comments

Comments
 (0)