Skip to content

Commit 5e38dce

Browse files
committed
feat: Add macro with-gsl-check-success
Introduce a macro that checks the result of executing gsl functions. If the result is not $gsl-success it raises an error. This macro avoids duplication of code.
1 parent 22ac5c1 commit 5e38dce

7 files changed

Lines changed: 29 additions & 18 deletions

File tree

doc/source/gsl-common.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ The GSL-COMMON module
2222
:value vec: An instance of :const:`<vector-double-float>`.
2323

2424
.. macro:: with-c-double-array
25+
26+
.. macro:: with-gsl-check-success

src/lib/dylan-gsl.lid

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ Files: library.dylan
1515
ffi/vector-and-matrices/block.dylan
1616
ffi/vector-and-matrices/module-vector.dylan
1717
ffi/vector-and-matrices/vector.dylan
18-
gsl/common/module.dylan
19-
gsl/common/common.dylan
2018
gsl/error/module.dylan
2119
gsl/error/error.dylan
20+
gsl/common/module.dylan
21+
gsl/common/common.dylan
2222
gsl/math/module.dylan
2323
gsl/math/math.dylan
2424
gsl/vector-and-matrices/module-vector.dylan

src/lib/gsl/common/common.dylan

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,11 @@ define macro with-c-double-array
3535
?body
3636
end }
3737
end macro;
38+
39+
define macro with-gsl-check-success
40+
{ with-gsl-check-success () ?body:body end }
41+
=> { let status :: <integer> = ?body;
42+
if (status ~= $gsl-success)
43+
error(make(<gsl-error>, code: status))
44+
end }
45+
end macro;

src/lib/gsl/common/module.dylan

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ License: See LICENSE in this distribution for details.
66

77
define module gsl-common
88

9+
use gsl-error,
10+
export: { $gsl-success,
11+
<gsl-error> };
12+
913
create
1014
<double-float?>,
1115
<vector-double-float>,
@@ -14,15 +18,15 @@ define module gsl-common
1418
<numeric-sequence>;
1519

1620
create
17-
with-c-double-array;
21+
with-c-double-array,
22+
with-gsl-check-success;
1823

1924
end module;
2025

2126
define module gsl-common-impl
2227

2328
use common-dylan;
2429
use c-ffi;
25-
2630
use gsl-common;
2731

2832
end module;

src/lib/gsl/error/module.dylan

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ License: See License.txt in this distribution for details.
66

77
define module gsl-error
88

9+
use gsl-ffi-error,
10+
export: { $gsl-success };
11+
912
create
1013
<gsl-error>,
1114
gsl-error-code,
@@ -53,8 +56,7 @@ define module gsl-error-impl
5356

5457
use common-dylan;
5558
use c-ffi;
56-
use gsl-ffi-error,
57-
export: { $gsl-success };
59+
use gsl-ffi-error;
5860

5961
use gsl-error;
6062

src/lib/gsl/module-running-statistics.dylan

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ define module gsl-running-statistics-impl
4444

4545
use common-dylan;
4646
use c-ffi;
47-
use gsl-error;
48-
use gsl-error-impl,
49-
import: { $gsl-success };
47+
use gsl-common;
5048

5149
use gsl-ffi-running-statistics,
5250
prefix: "ffi/";

src/lib/gsl/running-statistics.dylan

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ end;
2828

2929
define function rstat-reset!
3030
(rstat :: <rstat>) => ()
31-
let status = ffi/gsl-rstat-reset(rstat.%rstat-workspace);
32-
if (status ~= $gsl-success)
33-
error(make(<gsl-error>, code: status))
31+
with-gsl-check-success ()
32+
ffi/gsl-rstat-reset(rstat.%rstat-workspace)
3433
end
3534
end;
3635

@@ -45,9 +44,8 @@ define generic rstat-add!
4544
define method rstat-add!
4645
(rstat :: <rstat>, x :: <double-float>)
4746
=> ()
48-
let status = ffi/gsl-rstat-add(rstat.%rstat-workspace, x);
49-
if (status ~= $gsl-success)
50-
error(make(<gsl-error>, code: status))
47+
with-gsl-check-success ()
48+
ffi/gsl-rstat-add(rstat.%rstat-workspace, x)
5149
end
5250
end;
5351

@@ -144,9 +142,8 @@ define generic quantile-rstat-add!
144142

145143
define method quantile-rstat-add!
146144
(quantile-rstat :: <quantile-rstat>, x :: <double-float>) => ()
147-
let status = ffi/gsl-rstat-quantile-add(x, quantile-rstat.%quantile-rstat-workspace);
148-
if (status ~= $gsl-success)
149-
error(make(<gsl-error>, code: status))
145+
with-gsl-check-success ()
146+
ffi/gsl-rstat-quantile-add(x, quantile-rstat.%quantile-rstat-workspace)
150147
end
151148
end;
152149

0 commit comments

Comments
 (0)