-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_int.f90
More file actions
94 lines (66 loc) · 2.63 KB
/
result_int.f90
File metadata and controls
94 lines (66 loc) · 2.63 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
!> Result value for integers
!>
!> Inspired by the excellent, MIT licensed
!> https://github.com/samharrison7/fortran-error-handler
module m_result_int
use m_error_v, only: ErrorV
use m_result, only: Result
implicit none (type, external)
private
type, extends(Result), public :: ResultInteger1D
!! Result type that holds integer values
!!
!! Holds either an integer value or an error.
integer, allocatable :: data_v(:)
!! Data i.e. the result (if no error occurs)
! class(ErrorV), allocatable :: error_v
!! Error
contains
private
procedure, public :: build
! `finalise` and `is_error` come from abstract base class
final :: finalise
end type ResultInteger1D
interface ResultInteger1D
!! Constructor interface - see build (TODO: figure out cross-ref syntax) for details
module procedure :: constructor
end interface ResultInteger1D
contains
function constructor(data_v, error_v) result(self)
!! Build instance
type(ResultInteger1D) :: self
! Hopefully can leave without docstring (like Python)
class(ErrorV), intent(inout), optional :: error_v
!! Error message
integer, allocatable, intent(in), optional :: data_v(:)
!! Data
call self % build(data_v_in=data_v, error_v_in=error_v)
end function constructor
subroutine build(self, data_v_in, error_v_in)
!! Build instance
class(ResultInteger1D), intent(inout) :: self
! Hopefully can leave without docstring (like Python)
integer, intent(in), optional :: data_v_in(:)
!! Data
class(ErrorV), intent(inout), optional :: error_v_in
!! Error message
if (present(data_v_in) .and. present(error_v_in)) then
error_v_in % 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
error_v_in % message = "Neither data nor error were provided"
end if
end subroutine build
subroutine finalise(self)
!! Finalise instance
type(ResultInteger1D), intent(inout) :: self
! Hopefully can leave without docstring (like Python)
if (allocated(self % data_v)) deallocate (self % data_v)
if (allocated(self % error_v)) call self % clean_up()
end subroutine finalise
end module m_result_int