1- ! > Manager of `ResultDP ` (TODO : xref) across the Fortran-Python interface
1+ ! > manager of `resultdp ` (todo : xref) across the fortran-python interface
22module m_result_dp_manager
33
44 use kind_parameters, only: dp
5- use m_error_v, only: ErrorV
6- use m_result_dp, only: ResultDP
7- use m_result_int, only: ResultInt
8- use m_result_none, only: ResultNone
5+ use m_error_v, only: errorv
6+ use m_result_dp, only: resultdp
7+ use m_result_int, only: resultint
8+ use m_result_none, only: resultnone
99
1010 implicit none (type, external )
1111 private
1212
13- type (ResultDP ), dimension (:), allocatable :: instance_array
13+ type (resultdp ), dimension (:), allocatable :: instance_array
1414 logical , dimension (:), allocatable :: instance_available
1515
16- ! TODO : think about ordering here, alphabetical probably easiest
16+ ! todo : think about ordering here, alphabetical probably easiest
1717 public :: build_instance, finalise_instance, get_available_instance_index, get_instance, set_instance_index_to, &
1818 ensure_instance_array_size_is_at_least
1919
2020contains
2121
22- function build_instance (data_v_in , error_v_in ) result( res_available_instance_index)
22+ subroutine build_instance (data_v_in , error_v_in , res_available_instance_index )
2323 ! ! Build an instance
2424
2525 real (kind= dp), intent (in ), optional :: data_v_in
@@ -28,22 +28,28 @@ function build_instance(data_v_in, error_v_in) result(res_available_instance_ind
2828 class(ErrorV), intent (in ), optional :: error_v_in
2929 ! ! Error message
3030
31- type (ResultInt) :: res_available_instance_index
31+ type (ResultInt) , intent ( out ) :: res_available_instance_index
3232 ! ! Index of the built instance
3333
3434 type (ResultNone) :: res_build
3535
3636 call ensure_instance_array_size_is_at_least(1 )
3737 call get_available_instance_index(res_available_instance_index)
38- ! MZ check for errors ?
39- ! MZ function with side effect: good idea??
40- ! MZ why res_build is ResultNone??
41- res_build = instance_array(res_available_instance_index% data_v) % &
42- build(data_v_in= data_v_in, error_v_in= error_v_in)
4338
44- ! TODO: check build has no error
39+ if (res_available_instance_index % is_error()) return
4540
46- end function build_instance
41+ call instance_array(res_available_instance_index% data_v) % &
42+ build(data_v_in= data_v_in, error_v_in= error_v_in, res= res_build)
43+
44+ ! Check if build failed
45+ if (res_build % is_error()) then
46+ ! free slot again
47+ instance_available(res_available_instance_index% data_v) = .true.
48+ ! bubble the error up as ResultInt
49+ res_available_instance_index = ResultInt(error_v= res_build% error_v)
50+ end if
51+
52+ end subroutine build_instance
4753
4854 subroutine finalise_instance (instance_index )
4955 ! ! Finalise an instance
@@ -89,8 +95,9 @@ subroutine get_available_instance_index(res_available_instance_index)
8995 end do
9096
9197 ! TODO: switch to returning a Result type with an error set
92- res_available_instance_index = ResultInt(error_v= ErrorV(code= 1 , message= " No available instances" ))
9398 ! error stop 1
99+ res_available_instance_index = ResultInt(error_v= ErrorV(code= 1 , message= " No available instances" ))
100+
94101 end subroutine get_available_instance_index
95102
96103 ! Change to pure function when we update check_index_claimed to be pure
@@ -135,6 +142,8 @@ function check_index_claimed(instance_index) result(res_check_index_claimed)
135142 ! ! Instance index to check
136143 type (ResultNone) :: res_check_index_claimed
137144 character (len= :), allocatable :: msg
145+ ! msg initialisation to avoid compiler warning
146+ msg = " "
138147
139148 if (instance_available(instance_index)) then
140149 ! TODO: Switch to using Result here
@@ -146,8 +155,6 @@ function check_index_claimed(instance_index) result(res_check_index_claimed)
146155 ! res = ResultNone(ResultDP(code=1, message="Index ", instance_index, " has not been claimed"))
147156 ! print *, "Index ", instance_index, " has not been claimed"
148157 ! error stop 1
149- ! MZ Weird thing allocatable message
150- msg = " "
151158 write (msg,fmt= " (A, I0, A)" ) " Index " , instance_index," has not been claimed"
152159 res_check_index_claimed = ResultNone(error_v= ErrorV(code= 1 , message= msg))
153160 end if
0 commit comments