|
| 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