@@ -4,6 +4,7 @@ module m_result_dp_manager
44 use kind_parameters, only: dp
55 use m_error_v, only: ErrorV
66 use m_result_dp, only: ResultDP
7+ use m_result_int, only: ResultInt
78 use m_result_none, only: ResultNone
89
910 implicit none (type, external )
@@ -18,7 +19,7 @@ module m_result_dp_manager
1819
1920contains
2021
21- function build_instance (data_v_in , error_v_in ) result(instance_index )
22+ function build_instance (data_v_in , error_v_in ) result(res_available_instance_index )
2223 ! ! Build an instance
2324
2425 real (kind= dp), intent (in ), optional :: data_v_in
@@ -27,14 +28,18 @@ function build_instance(data_v_in, error_v_in) result(instance_index)
2728 class(ErrorV), intent (in ), optional :: error_v_in
2829 ! ! Error message
2930
30- integer :: instance_index
31+ type (ResultInt) :: res_available_instance_index
3132 ! ! Index of the built instance
3233
3334 type (ResultNone) :: res_build
3435
3536 call ensure_instance_array_size_is_at_least(1 )
36- call get_available_instance_index(instance_index)
37- res_build = instance_array(instance_index) % build(data_v_in= data_v_in, error_v_in= error_v_in)
37+ 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)
3843
3944 ! TODO: check build has no error
4045
@@ -46,44 +51,46 @@ subroutine finalise_instance(instance_index)
4651 integer , intent (in ) :: instance_index
4752 ! ! Index of the instance to finalise
4853
49- call check_index_claimed(instance_index)
54+ type (ResultNone) :: res_check_index_claimed
55+
56+ res_check_index_claimed = check_index_claimed(instance_index)
57+ if (res_check_index_claimed% is_error()) return
5058
5159 call instance_array(instance_index) % finalise()
5260 instance_available(instance_index) = .true.
5361
5462 end subroutine finalise_instance
5563
56- subroutine get_available_instance_index (available_instance_index )
64+ subroutine get_available_instance_index (res_available_instance_index )
5765 ! ! Get a free instance index
5866
5967 ! TODO: think through whether race conditions are possible
6068 ! e.g. while returning a free index number to one Python call
6169 ! a different one can be looking up a free instance index at the same time
6270 ! and something goes wrong (maybe we need a lock)
6371
64- integer , intent (out ) :: available_instance_index
72+ type (ResultInt), intent (out ) :: res_available_instance_index
73+ ! integer, intent(out) :: available_instance_index
6574 ! ! Available instance index
66-
6775 integer :: i
6876
6977 do i = 1 , size (instance_array)
7078
7179 if (instance_available(i)) then
7280
7381 instance_available(i) = .false.
74- available_instance_index = i
82+ ! available_instance_index = i
7583 ! TODO: switch to returning a Result type
76- ! res = ResultInt(data =i)
84+ res_available_instance_index = ResultInt(data_v = i)
7785 return
7886
7987 end if
8088
8189 end do
8290
8391 ! TODO: switch to returning a Result type with an error set
84- ! res = ResultInt(ResultDP(code=1, message="No available instances"))
85- error stop 1
86-
92+ res_available_instance_index = ResultInt(error_v= ErrorV(code= 1 , message= " No available instances" ))
93+ ! error stop 1
8794 end subroutine get_available_instance_index
8895
8996 ! Change to pure function when we update check_index_claimed to be pure
@@ -95,8 +102,14 @@ function get_instance(instance_index) result(inst)
95102 type (ResultDP) :: inst
96103 ! ! Instance at `instance_array(instance_index)`
97104
98- call check_index_claimed(instance_index)
99- inst = instance_array(instance_index)
105+ type (ResultNone) :: res_check_index_claimed
106+
107+ res_check_index_claimed = check_index_claimed(instance_index)
108+ if (res_check_index_claimed% is_error()) then
109+ inst = ResultDP(error_v= res_check_index_claimed% error_v)
110+ else
111+ inst = instance_array(instance_index)
112+ end if
100113
101114 end function get_instance
102115
@@ -106,19 +119,22 @@ subroutine set_instance_index_to(instance_index, val)
106119 ! ! Index in `instance_array` of which to set the value equal to `val`
107120
108121 type (ResultDP), intent (in ) :: val
122+ type (ResultNone) :: res_check_index_claimed
109123
110- call check_index_claimed(instance_index)
111- instance_array(instance_index) = val
124+ res_check_index_claimed = check_index_claimed(instance_index)
125+ if (res_check_index_claimed % is_error()) instance_array(instance_index) = val
112126
113127 end subroutine set_instance_index_to
114128
115- subroutine check_index_claimed (instance_index )
129+ function check_index_claimed (instance_index ) result(res_check_index_claimed )
116130 ! ! Check that an index has already been claimed
117131 ! !
118132 ! ! Stops execution if the index has not been claimed.
119133
120134 integer , intent (in ) :: instance_index
121135 ! ! Instance index to check
136+ type (ResultNone) :: res_check_index_claimed
137+ character (len= :), allocatable :: msg
122138
123139 if (instance_available(instance_index)) then
124140 ! TODO: Switch to using Result here
@@ -128,8 +144,12 @@ subroutine check_index_claimed(instance_index)
128144 ! if it fails, the result_dp attribute will be set).
129145 ! So the code would be something like
130146 ! res = ResultNone(ResultDP(code=1, message="Index ", instance_index, " has not been claimed"))
131- print * , " Index " , instance_index, " has not been claimed"
132- error stop 1
147+ ! print *, "Index ", instance_index, " has not been claimed"
148+ ! error stop 1
149+ ! MZ Weird thing allocatable message
150+ msg = " "
151+ write (msg,fmt= " (A, I0, A)" ) " Index " , instance_index," has not been claimed"
152+ res_check_index_claimed = ResultNone(error_v= ErrorV(code= 1 , message= msg))
133153 end if
134154
135155 if (instance_index < 1 ) then
@@ -140,8 +160,10 @@ subroutine check_index_claimed(instance_index)
140160 ! if it fails, the result_dp attribute will be set).
141161 ! So the code would be something like
142162 ! res = ResultNone(ResultDP(code=2, message="Requested index is ", instance_index, " which is less than 1"))
143- print * , " Requested index is " , instance_index, " which is less than 1"
144- error stop 1
163+ ! print *, "Requested index is ", instance_index, " which is less than 1"
164+ ! error stop 1
165+ write (msg,fmt= " (A, I0, A)" ) " Requested index is " , instance_index, " which is less than 1"
166+ res_check_index_claimed = ResultNone(error_v= ErrorV(code= 2 , message= msg))
145167 end if
146168
147169 ! ! Here, result becomes
@@ -151,8 +173,9 @@ subroutine check_index_claimed(instance_index)
151173 ! ! We will no longer have subroutines that return nothing
152174 ! ! (like this one currently does).
153175 ! res = ResultNone()
176+ res_check_index_claimed = ResultNone()
154177
155- end subroutine check_index_claimed
178+ end function check_index_claimed
156179
157180 subroutine ensure_instance_array_size_is_at_least (n )
158181 ! ! Ensure that `instance_array` and `instance_available` have at least `n` slots
0 commit comments