|
| 1 | +!> Result value |
| 2 | +!> |
| 3 | +!> Inspired by the excellent, MIT licensed |
| 4 | +!> https://github.com/samharrison7/fortran-error-handler |
| 5 | +module m_result |
| 6 | + |
| 7 | + implicit none |
| 8 | + private |
| 9 | + |
| 10 | + type, abstract, public :: Result |
| 11 | + !! Result type |
| 12 | + !! |
| 13 | + !! Holds either the result or an error. |
| 14 | + |
| 15 | + class(*), allocatable :: data(..) |
| 16 | + !! Data i.e. the result (if no error occurs) |
| 17 | + !! |
| 18 | + ! Assumed rank array |
| 19 | + ! (https://fortran-lang.discourse.group/t/assumed-rank-arrays/1049) |
| 20 | + ! Technically a Fortran 2018 feature, |
| 21 | + ! so maybe we need to update our file extensions. |
| 22 | + ! If we can't use this, just comment this out |
| 23 | + ! and leave each subclass of Result to set its data type |
| 24 | + ! (e.g. ResultInteger will have `integer :: data`, |
| 25 | + ! ResultDP1D will have `real(dp), dimension(:), allocatable :: data`) |
| 26 | + |
| 27 | + class(ErrorV), allocatable :: error_v |
| 28 | + !! Error |
| 29 | + |
| 30 | + contains |
| 31 | + |
| 32 | + private |
| 33 | + |
| 34 | + procedure, public:: build, finalise, is_error |
| 35 | + ! TODO: Think about whether build should be on the abstract class |
| 36 | + ! or just on each concrete implementation |
| 37 | + |
| 38 | + end type Result |
| 39 | + |
| 40 | + interface Result |
| 41 | + !! Constructor interface - see build (TODO: figure out cross-ref syntax) for details |
| 42 | + module procedure :: constructor |
| 43 | + end interface Result |
| 44 | + |
| 45 | +contains |
| 46 | + |
| 47 | + ! See above about whether we include this here or not |
| 48 | + ! Build should return a Result with an error if we try to set/allocate both |
| 49 | + ! data and error |
| 50 | + ! subroutine build(self, code, message) |
| 51 | + ! !! Build instance |
| 52 | + ! |
| 53 | + ! class(ErrorV), intent(inout) :: self |
| 54 | + ! ! Hopefully can leave without docstring (like Python) |
| 55 | + ! |
| 56 | + ! integer, intent(in) :: code |
| 57 | + ! !! Error code |
| 58 | + ! !! |
| 59 | + ! !! Use [TODO: figure out xref] `NO_ERROR_CODE` if there is no error |
| 60 | + ! |
| 61 | + ! character(len=*), optional, intent(in) :: message |
| 62 | + ! !! Error message |
| 63 | + ! |
| 64 | + ! self % code = code |
| 65 | + ! if (present(message)) then |
| 66 | + ! self % message = message |
| 67 | + ! end if |
| 68 | + ! |
| 69 | + ! end subroutine build |
| 70 | + |
| 71 | + subroutine finalise(self) |
| 72 | + !! Finalise the instance (i.e. free/deallocate) |
| 73 | + |
| 74 | + class(Result), intent(inout) :: self |
| 75 | + ! Hopefully can leave without docstring (like Python) |
| 76 | + |
| 77 | + deallocate(self % data) |
| 78 | + deallocate(self % error) |
| 79 | + |
| 80 | + end subroutine finalise |
| 81 | + |
| 82 | + pure function is_error(self) result(is_err) |
| 83 | + !! Determine whether `self` contains an error or not |
| 84 | + |
| 85 | + class(Result), intent(in) :: self |
| 86 | + ! Hopefully can leave without docstring (like Python) |
| 87 | + |
| 88 | + logical :: is_err |
| 89 | + ! Whether `self` is an error or not |
| 90 | + |
| 91 | + is_err = self % error_v % is_error() |
| 92 | + ! TODO: implement is_error on `error_v` |
| 93 | + |
| 94 | + end function is_error |
| 95 | + |
| 96 | +end module m_result |
0 commit comments