-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_int.f90
More file actions
128 lines (86 loc) · 3.53 KB
/
result_int.f90
File metadata and controls
128 lines (86 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
!> Result type for integers
!>
!> Inspired by the excellent, MIT licensed
!> https://github.com/samharrison7/fortran-error-handler
module m_result_int
use kind_parameters, only: i8
use m_error_v, only: ErrorV
use m_result, only: ResultBase
use m_result_none, only: ResultNone
implicit none (type, external)
private
type, extends(ResultBase), public :: ResultInt
!! Result type that holds integer values
integer(kind=i8), allocatable :: data_v
!! Data i.e. the result (if no error occurs)
! Note: the error_v attribute comes from ResultBase
contains
private
procedure, public :: build
procedure, public :: finalise
final :: finalise_auto
end type ResultInt
interface ResultInt
!! Constructor interface - see build [TODO: x-ref] for details
module procedure :: constructor
end interface ResultInt
contains
function constructor(data_v, error_v) result(self)
!! Build instance
type(ResultInt) :: self
! Hopefully can leave without docstring (like Python)
integer(kind=i8), intent(in), optional :: data_v
!! Data
class(ErrorV), intent(in), optional :: error_v
!! Error
type(ResultNone) :: build_res
build_res = self % build(data_v_in=data_v, error_v_in=error_v)
if (build_res % is_error()) then
! This interface has to return the initialised object,
! it cannot return a Result type,
! so we have no choice but to raise a fatal error here.
print *, build_res % error_v % message
error stop build_res % error_v % code
! else
! Assume no error occurred and initialisation was fine
end if
end function constructor
function build(self, data_v_in, error_v_in) result(res)
!! Build instance
class(ResultInt), intent(out) :: self
! Hopefully can leave without docstring (like Python)
integer(kind=i8), intent(in), optional :: data_v_in
!! Data
class(ErrorV), intent(in), optional :: error_v_in
!! Error message
type(ResultNone) :: res
!! Result
if (present(data_v_in) .and. present(error_v_in)) then
res % error_v % message = "Both data and error were provided"
else if (present(data_v_in)) then
allocate (self % data_v, source=data_v_in)
! No error - no need to call res % build
else if (present(error_v_in)) then
allocate (self % error_v, source=error_v_in)
! No error - no need to call res % build
else
res % error_v % message = "Neither data nor error were provided"
end if
end function build
subroutine finalise(self)
!! Finalise the instance (i.e. free/deallocate)
class(ResultInt), intent(inout) :: self
! Hopefully can leave without docstring (like Python)
if (allocated(self % data_v)) deallocate (self % data_v)
if (allocated(self % error_v)) deallocate(self % error_v)
end subroutine finalise
subroutine finalise_auto(self)
!! Finalise the instance (i.e. free/deallocate)
!!
!! This method is expected to be called automatically
!! by clever clean up, which is why it differs from [TODO x-ref] `finalise`
type(ResultInt), intent(inout) :: self
! Hopefully can leave without docstring (like Python)
call self % finalise()
end subroutine finalise_auto
end module m_result_int