Skip to content

Commit 02b5392

Browse files
committed
Result type
1 parent 53b1910 commit 02b5392

4 files changed

Lines changed: 113 additions & 2 deletions

File tree

meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ if pyprojectwheelbuild_enabled
6969
'src/example_fgen_basic/get_wavelength.f90',
7070
'src/example_fgen_basic/kind_parameters.f90',
7171
'src/example_fgen_basic/result/result.f90',
72+
'src/example_fgen_basic/result/result0D_int.f90',
7273
'src/example_fgen_basic/result/result_int.f90',
7374
)
7475

src/example_fgen_basic/error_v/error_v.f90

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ module m_error_v
3535

3636
private
3737

38-
procedure, public :: build, finalise
38+
procedure, public :: build
39+
procedure, public :: finalise
40+
final :: clean_up
3941
! get_res sort of not needed (?)
4042
! get_err sort of not needed (?)
4143

@@ -93,4 +95,14 @@ subroutine finalise(self)
9395

9496
end subroutine finalise
9597

98+
subroutine clean_up(self)
99+
!! Finalise the instance (i.e. free/deallocate)
100+
101+
type(ErrorV), intent(inout) :: self
102+
! Hopefully can leave without docstring (like Python)
103+
104+
call self%finalise()
105+
106+
end subroutine clean_up
107+
96108
end module m_error_v

src/example_fgen_basic/result/result.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ subroutine clean_up(self)
5555
class(Result), intent(inout) :: self
5656
! Hopefully can leave without docstring (like Python)
5757

58-
if (allocated(self % error_v)) deallocate (self % error_v)
58+
deallocate (self % error_v)
5959

6060
end subroutine clean_up
6161

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
!> Result value for integers
2+
!>
3+
!> Inspired by the excellent, MIT licensed
4+
!> https://github.com/samharrison7/fortran-error-handler
5+
module m_result_int
6+
7+
use m_error_v, only: ErrorV
8+
use m_result, only: Result_base
9+
10+
implicit none (type, external)
11+
private
12+
13+
type, extends(Result_base), public :: ResultInteger0D
14+
!! Result type that holds integer values
15+
!!
16+
!! Holds either an integer value or an error.
17+
18+
integer, allocatable :: data_v
19+
!! Data i.e. the result (if no error occurs)
20+
21+
! class(ErrorV), allocatable :: error_v
22+
!! Error
23+
24+
contains
25+
26+
private
27+
28+
procedure, public :: build
29+
! `finalise` and `is_error` come from abstract base class
30+
final :: finalise
31+
32+
end type ResultInteger0D
33+
34+
interface ResultInteger0D
35+
!! Constructor interface - see build (TODO: figure out cross-ref syntax) for details
36+
module procedure :: constructor
37+
end interface ResultInteger0D
38+
39+
contains
40+
41+
function constructor(data_v, error_v) result(self)
42+
!! Build instance
43+
44+
type(ResultInteger0D) :: self
45+
! Hopefully can leave without docstring (like Python)
46+
47+
class(ErrorV), intent(inout), optional :: error_v
48+
!! Error message
49+
50+
integer, intent(in), optional :: data_v
51+
!! Data
52+
53+
call self % build(data_v_in=data_v, error_v_in=error_v)
54+
55+
end function constructor
56+
57+
function build(data_v_in, error_v_in) result(res)
58+
!! Build instance
59+
60+
class(Result_base), intent(out) :: res
61+
! Hopefully can leave without docstring (like Python)
62+
63+
integer, intent(in), optional :: data_v_in
64+
!! Data
65+
66+
class(ErrorV), intent(inout), optional :: error_v_in
67+
!! Error message
68+
69+
if (present(data_v_in) .and. present(error_v_in)) then
70+
allocate(Result_base :: res)
71+
res % error_v % message = "Both data and error were provided"
72+
else if (present(data_v_in)) then
73+
allocate (ResultInteger0D :: res)
74+
allocate (self % data_v, source=data_v_in)
75+
! No error - no need to call res % build
76+
else if (present(error_v_in)) then
77+
allocate(Result_base :: res)
78+
allocate (res % error_v, source=error_v_in)
79+
! No error - no need to call res % build
80+
else
81+
allocate(Result_base :: res)
82+
res % error_v % message = "Neither data nor error were provided"
83+
end if
84+
85+
end function build
86+
87+
subroutine finalise(self)
88+
!! Finalise instance
89+
90+
type(ResultInteger0D), intent(inout) :: self
91+
! Hopefully can leave without docstring (like Python)
92+
93+
if (allocated(self % data_v)) deallocate (self % data_v)
94+
call self % clean_up()
95+
96+
end subroutine finalise
97+
98+
end module m_result_int

0 commit comments

Comments
 (0)