-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult.f90
More file actions
75 lines (54 loc) · 2.15 KB
/
result.f90
File metadata and controls
75 lines (54 loc) · 2.15 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
!> Result value
!>
!> Inspired by the excellent, MIT licensed
!> https://github.com/samharrison7/fortran-error-handler
module m_result
use m_error_v, only: ErrorV, NO_ERROR_CODE
implicit none (type, external)
private
type, abstract, public :: Result
!! Result type
!!
!! Holds either the result or an error.
! class(*), allocatable :: data_v(..)
! MZ: assumed rank can only be dummy argument NOT type/class argument
! Data i.e. the result (if no error occurs)
!
! Assumed rank array
! (https://fortran-lang.discourse.group/t/assumed-rank-arrays/1049)
! Technically a Fortran 2018 feature,
! so maybe we need to update our file extensions.
! If we can't use this, just comment this out
! and leave each subclass of Result to set its data type
! (e.g. ResultInteger will have `integer :: data`,
! ResultDP1D will have `real(dp), dimension(:), allocatable :: data`)
class(ErrorV), allocatable :: error_v
!! Error
contains
private
! procedure, public:: build
! TODO: Think about whether build should be on the abstract class
! or just on each concrete implementation
procedure, public :: is_error
procedure, public :: clean_up
end type Result
! interface Result
!! Constructor interface - see build (TODO: figure out cross-ref syntax) for details
! module procedure :: constructor
! end interface Result
contains
subroutine clean_up(self)
!! Finalise the instance (i.e. free/deallocate)
class(Result), intent(inout) :: self
! Hopefully can leave without docstring (like Python)
if (allocated(self % error_v)) deallocate (self % error_v)
end subroutine clean_up
pure function is_error(self) result(is_err)
!! Determine whether `self` contains an error or not
class(Result), intent(in) :: self
! Hopefully can leave without docstring (like Python)
logical :: is_err
! Whether `self` is an error or not
is_err = allocated(self % error_v)
end function is_error
end module m_result