Debian Codegraph @

src/atlas_f/util/atlas_Config_module.F90

raw
1 ! (C) Copyright 2013 ECMWF.
2 !
3 ! This software is licensed under the terms of the Apache Licence Version 2.0
4 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5 ! In applying this licence, ECMWF does not waive the privileges and immunities
6 ! granted to it by virtue of its status as an intergovernmental organisation nor
7 ! does it submit to any jurisdiction.
8
9 #include "atlas/atlas_f.h"
10
11 module atlas_config_module
12
13 use fckit_shared_object_module, only : fckit_shared_object, fckit_c_deleter, fckit_c_nodeleter
14
15 implicit none
16
17 public :: atlas_Config
18
19 private
20
21 TYPE, extends(fckit_shared_object) :: atlas_Config
22
23 ! Purpose :
24 ! -------
25 ! *Config* : Container of Config, parameters or attributes
26 ! The Config are seted as key, value pairs
27
28 ! Methods :
29 ! -------
30 ! set : set a new property with given key and value
31 ! set : Modify a property with given key and value
32 ! get : Return a property value for given key
33
34 ! Author :
35 ! ------
36 ! June-2015 Willem Deconinck *ECMWF*
37
38 !------------------------------------------------------------------------------
39 contains
40 procedure, private :: set_config => atlas_Config__set_config
41 procedure, private :: set_config_list => atlas_Config__set_config_list
42 procedure, private :: set_logical => atlas_Config__set_logical
43 procedure, private :: set_int32 => atlas_Config__set_int32
44 procedure, private :: set_int64 => atlas_Config__set_int64
45 procedure, private :: set_real32 => atlas_Config__set_real32
46 procedure, private :: set_real64 => atlas_Config__set_real64
47 procedure, private :: set_string => atlas_Config__set_string
48 procedure, private :: set_array_int32 => atlas_Config__set_array_int32
49 procedure, private :: set_array_int64 => atlas_Config__set_array_int64
50 procedure, private :: set_array_real32 => atlas_Config__set_array_real32
51 procedure, private :: set_array_real64 => atlas_Config__set_array_real64
52 procedure :: has => atlas_Config__has
53 generic :: set => set_config, set_config_list, set_logical, set_int32, set_int64, set_real32, set_real64, &
54 set_string, set_array_int32, set_array_int64, set_array_real32, set_array_real64
55 procedure, private :: get_config => atlas_Config__get_config
56 procedure, private :: get_config_list => atlas_Config__get_config_list
57 procedure, private :: get_int32 => atlas_Config__get_int32
58 procedure, private :: get_logical => atlas_Config__get_logical
59 procedure, private :: get_real32 => atlas_Config__get_real32
60 procedure, private :: get_real64 => atlas_Config__get_real64
61 procedure, private :: get_string => atlas_Config__get_string
62 procedure, private :: get_array_int32 => atlas_Config__get_array_int32
63 procedure, private :: get_array_int64 => atlas_Config__get_array_int64
64 procedure, private :: get_array_real32 => atlas_Config__get_array_real32
65 procedure, private :: get_array_real64 => atlas_Config__get_array_real64
66 generic :: get => get_config, get_config_list, get_int32, get_logical, get_real32, get_real64, &
67 get_string, get_array_int32, get_array_int64, get_array_real32, get_array_real64
68 procedure :: json => atlas_Config__json
69
70 #if FCKIT_FINAL_NOT_INHERITING
71 final :: atlas_Config__final_auto
72 #endif
73
74 END TYPE atlas_Config
75
76 !------------------------------------------------------------------------------
77
78 interface atlas_Config
79 module procedure atlas_Config__ctor
80 module procedure atlas_Config__ctor_from_cptr
81 module procedure atlas_Config__ctor_from_file
82 module procedure atlas_Config__ctor_from_json
83 end interface
84
85 !------------------------------------------------------------------------------
86
87 private :: fckit_shared_object
88 private :: fckit_c_deleter
89 private :: fckit_c_nodeleter
90
91 !========================================================
92 contains
93 !========================================================
94
95 ! -----------------------------------------------------------------------------
96 ! Config routines
97
98 #if FCKIT_FINAL_NOT_INHERITING
99 ATLAS_FINAL subroutine atlas_Config__final_auto(this)
100 type(atlas_Config), intent(inout) :: this
101 #if FCKIT_FINAL_DEBUGGING
102 write(0,*) "atlas_Config__final_auto"
103 #endif
104 #if FCKIT_FINAL_NOT_PROPAGATING
105 call this%final()
106 #endif
107 FCKIT_SUPPRESS_UNUSED( this )
108 end subroutine
109 #endif
110
111 function atlas_Config__ctor() result(this)
112 use atlas_Config_c_binding
113 type(atlas_Config) :: this
114 call this%reset_c_ptr( atlas__Config__new(), &
115 & fckit_c_deleter(atlas__Config__delete) )
116 call this%return()
117 end function atlas_Config__ctor
118
119 function atlas_Config__ctor_from_cptr(cptr,delete) result(this)
120 use, intrinsic :: iso_c_binding, only : c_ptr
121 use atlas_Config_c_binding
122 type(c_ptr), value :: cptr
123 type(atlas_Config) :: this
124 logical, optional :: delete
125 logical :: opt_delete
126 opt_delete = .true.
127 if( present(delete) ) opt_delete = delete
128 if( opt_delete ) then
129 call this%reset_c_ptr( cptr, fckit_c_deleter(atlas__Config__delete) )
130 else
131 call this%reset_c_ptr( cptr, fckit_c_nodeleter() )
132 endif
133 call this%return()
134 end function
135
136 function atlas_Config__ctor_from_json(json) result(this)
137 use fckit_c_interop_module, only : c_str
138 use atlas_Config_c_binding
139 use atlas_JSON_module
140 type(atlas_Config) :: this
141 class(atlas_JSON) :: json
142 call this%reset_c_ptr( atlas__Config__new_from_json(c_str(json%str())), &
143 & fckit_c_deleter(atlas__Config__delete) )
144 call this%return()
145 end function atlas_Config__ctor_from_json
146
147 function atlas_Config__ctor_from_file(path) result(this)
148 use fckit_c_interop_module, only : c_str
149 use atlas_Config_c_binding
150 use atlas_JSON_module
151 type(atlas_Config) :: this
152 class(atlas_PathName), intent(in) :: path
153 call this%reset_c_ptr( atlas__Config__new_from_file(c_str(path%str())), &
154 & fckit_c_deleter(atlas__Config__delete) )
155 call this%return()
156 end function atlas_Config__ctor_from_file
157
158 function atlas_Config__has(this, name) result(value)
159 use fckit_c_interop_module, only : c_str
160 use atlas_Config_c_binding
161 class(atlas_Config), intent(inout) :: this
162 character(len=*), intent(in) :: name
163 logical :: value
164 integer :: value_int
165 value_int = atlas__Config__has(this%CPTR_PGIBUG_B, c_str(name) )
166 if( value_int == 1 ) then
167 value = .True.
168 else
169 value = .False.
170 end if
171 end function atlas_Config__has
172
173 subroutine atlas_Config__set_config(this, name, value)
174 use fckit_c_interop_module, only : c_str
175 use atlas_Config_c_binding
176 class(atlas_Config), intent(inout) :: this
177 character(len=*), intent(in) :: name
178 class(atlas_Config), intent(in) :: value
179 call atlas__Config__set_config(this%CPTR_PGIBUG_B, c_str(name), value%CPTR_PGIBUG_B )
180 end subroutine atlas_Config__set_config
181
182 subroutine atlas_Config__set_config_list(this, name, value)
183 use, intrinsic :: iso_c_binding, only : c_ptr, c_loc
184 use fckit_c_interop_module, only : c_str
185 use atlas_Config_c_binding
186 class(atlas_Config), intent(inout) :: this
187 character(len=*), intent(in) :: name
188 type(atlas_Config), intent(in) :: value(:) !PGI (17.7) compiler bug when "type" replaced with "class"
189 type(c_ptr), target :: value_cptrs(size(value))
190 integer :: j
191 if( size(value) > 0 ) then
192 do j=1,size(value)
193 value_cptrs(j) = value(j)%CPTR_PGIBUG_B
194 enddo
195 call atlas__Config__set_config_list(this%CPTR_PGIBUG_B, c_str(name), c_loc(value_cptrs(1)), size(value_cptrs) )
196 endif
197 end subroutine atlas_Config__set_config_list
198
199
200 subroutine atlas_Config__set_logical(this, name, value)
201 use fckit_c_interop_module, only : c_str
202 use atlas_Config_c_binding
203 class(atlas_Config), intent(inout) :: this
204 character(len=*), intent(in) :: name
205 logical, intent(in) :: value
206 integer :: value_int
207 if( value ) then
208 value_int = 1
209 else
210 value_int = 0
211 end if
212 call atlas__Config__set_int(this%CPTR_PGIBUG_B, c_str(name), value_int )
213 end subroutine atlas_Config__set_logical
214
215 subroutine atlas_Config__set_int32(this, name, value)
216 use, intrinsic :: iso_c_binding, only : c_int
217 use fckit_c_interop_module, only : c_str
218 use atlas_Config_c_binding
219 class(atlas_Config), intent(inout) :: this
220 character(len=*), intent(in) :: name
221 integer(c_int), intent(in) :: value
222 call atlas__Config__set_int(this%CPTR_PGIBUG_B, c_str(name), value)
223 end subroutine atlas_Config__set_int32
224
225 subroutine atlas_Config__set_int64(this, name, value)
226 use, intrinsic :: iso_c_binding, only : c_long
227 use fckit_c_interop_module, only : c_str
228 use atlas_Config_c_binding
229 class(atlas_Config), intent(inout) :: this
230 character(len=*), intent(in) :: name
231 integer(c_long), intent(in) :: value
232 call atlas__Config__set_long(this%CPTR_PGIBUG_B, c_str(name), value)
233 end subroutine atlas_Config__set_int64
234
235 subroutine atlas_Config__set_real32(this, name, value)
236 use, intrinsic :: iso_c_binding, only : c_float
237 use fckit_c_interop_module, only : c_str
238 use atlas_Config_c_binding
239 class(atlas_Config), intent(inout) :: this
240 character(len=*), intent(in) :: name
241 real(c_float), intent(in) :: value
242 call atlas__Config__set_float(this%CPTR_PGIBUG_B, c_str(name) ,value)
243 end subroutine atlas_Config__set_real32
244
245 subroutine atlas_Config__set_real64(this, name, value)
246 use, intrinsic :: iso_c_binding, only : c_double
247 use fckit_c_interop_module, only : c_str
248 use atlas_Config_c_binding
249 class(atlas_Config), intent(inout) :: this
250 character(len=*), intent(in) :: name
251 real(c_double), intent(in) :: value
252 call atlas__Config__set_double(this%CPTR_PGIBUG_B, c_str(name) ,value)
253 end subroutine atlas_Config__set_real64
254
255 subroutine atlas_Config__set_string(this, name, value)
256 use fckit_c_interop_module, only : c_str
257 use atlas_Config_c_binding
258 class(atlas_Config), intent(inout) :: this
259 character(len=*), intent(in) :: name
260 character(len=*), intent(in) :: value
261 call atlas__Config__set_string(this%CPTR_PGIBUG_B, c_str(name) , c_str(value) )
262 end subroutine atlas_Config__set_string
263
264 function atlas_Config__get_config(this, name, value) result(found)
265 use fckit_c_interop_module, only : c_str
266 use atlas_Config_c_binding
267 logical :: found
268 class(atlas_Config), intent(in) :: this
269 character(len=*), intent(in) :: name
270 class(atlas_Config), intent(inout) :: value
271 integer :: found_int
272 value = atlas_Config()
273 found_int = atlas__Config__get_config( this%CPTR_PGIBUG_B, c_str(name), value%CPTR_PGIBUG_B )
274 found = .False.
275 if (found_int == 1) found = .True.
276 end function atlas_Config__get_config
277
278 function atlas_Config__get_config_list(this, name, value) result(found)
279 use, intrinsic :: iso_c_binding, only : c_ptr, c_f_pointer, c_null_ptr
280 use fckit_c_interop_module, only : c_str, c_ptr_free
281 use atlas_Config_c_binding
282 logical :: found
283 class(atlas_Config), intent(in) :: this
284 character(len=*), intent(in) :: name
285 type(atlas_Config), allocatable, intent(inout) :: value(:)
286 type(c_ptr) :: value_list_cptr
287 type(c_ptr), pointer :: value_cptrs(:)
288 integer :: value_list_allocated
289 integer :: value_list_size
290 integer :: found_int
291 integer :: j
292 value_list_cptr = c_null_ptr
293 found_int = atlas__Config__get_config_list(this%CPTR_PGIBUG_B, c_str(name), &
294 & value_list_cptr, value_list_size, value_list_allocated )
295 if( found_int == 1 ) then
296 call c_f_pointer(value_list_cptr,value_cptrs,(/value_list_size/))
297 if( allocated(value) ) deallocate(value)
298 allocate(value(value_list_size))
299 do j=1,value_list_size
300 value(j) = atlas_Config( value_cptrs(j) )
301 enddo
302 if( value_list_allocated == 1 ) call c_ptr_free(value_list_cptr)
303 endif
304 found = .False.
305 if (found_int == 1) found = .True.
306 end function atlas_Config__get_config_list
307
308 function atlas_Config__get_logical(this, name, value) result(found)
309 use fckit_c_interop_module, only : c_str
310 use atlas_Config_c_binding
311 logical :: found
312 class(atlas_Config), intent(in) :: this
313 character(len=*), intent(in) :: name
314 logical, intent(inout) :: value
315 integer :: value_int
316 integer :: found_int
317 found_int = atlas__Config__get_int(this%CPTR_PGIBUG_B,c_str(name), value_int )
318 found = .False.
319 if (found_int == 1) found = .True.
320 if (found) then
321 if (value_int > 0) then
322 value = .True.
323 else
324 value = .False.
325 end if
326 endif
327 end function atlas_Config__get_logical
328
329 function atlas_Config__get_int32(this, name, value) result(found)
330 use fckit_c_interop_module, only : c_str
331 use atlas_Config_c_binding
332 logical :: found
333 class(atlas_Config), intent(in) :: this
334 character(len=*), intent(in) :: name
335 integer, intent(inout) :: value
336 integer :: found_int
337 found_int = atlas__Config__get_int(this%CPTR_PGIBUG_B, c_str(name), value )
338 found = .False.
339 if (found_int == 1) found = .True.
340 end function atlas_Config__get_int32
341
342 function atlas_Config__get_real32(this, name, value) result(found)
343 use, intrinsic :: iso_c_binding, only : c_float
344 use fckit_c_interop_module, only : c_str
345 use atlas_Config_c_binding
346 logical :: found
347 class(atlas_Config), intent(in) :: this
348 character(len=*), intent(in) :: name
349 real(c_float), intent(inout) :: value
350 integer :: found_int
351 found_int = atlas__Config__get_float(this%CPTR_PGIBUG_B, c_str(name), value )
352 found = .False.
353 if (found_int == 1) found = .True.
354 end function atlas_Config__get_real32
355
356 function atlas_Config__get_real64(this, name, value) result(found)
357 use, intrinsic :: iso_c_binding, only : c_double
358 use fckit_c_interop_module, only : c_str
359 use atlas_Config_c_binding
360 logical :: found
361 class(atlas_Config), intent(in) :: this
362 character(len=*), intent(in) :: name
363 real(c_double), intent(inout) :: value
364 integer :: found_int
365 found_int = atlas__Config__get_double(this%CPTR_PGIBUG_B, c_str(name), value )
366 found = .False.
367 if (found_int == 1) found = .True.
368 end function atlas_Config__get_real64
369
370 function atlas_Config__get_string(this, name, value) result(found)
371 use, intrinsic :: iso_c_binding, only : c_ptr, c_int
372 use fckit_c_interop_module, only : c_str, c_ptr_to_string, c_ptr_free
373 use atlas_Config_c_binding
374 logical :: found
375 class(atlas_Config), intent(in) :: this
376 character(len=*), intent(in) :: name
377 character(len=:), allocatable, intent(inout) :: value
378 type(c_ptr) :: value_cptr
379 integer :: found_int
380 integer(c_int) :: value_size
381 integer(c_int) :: value_allocated
382 found_int = atlas__Config__get_string(this%CPTR_PGIBUG_B,c_str(name),value_cptr,value_size,value_allocated)
383 if( found_int == 1 ) then
384 if( allocated(value) ) deallocate(value)
385 allocate(character(len=value_size) :: value )
386 value = c_ptr_to_string(value_cptr)
387 if( value_allocated == 1 ) call c_ptr_free(value_cptr)
388 endif
389 found = .False.
390 if (found_int == 1) found = .True.
391 end function atlas_Config__get_string
392
393 subroutine atlas_Config__set_array_int32(this, name, value)
394 use, intrinsic :: iso_c_binding, only : c_int
395 use fckit_c_interop_module, only : c_str
396 use atlas_Config_c_binding
397 class(atlas_Config), intent(in) :: this
398 character(len=*), intent(in) :: name
399 integer(c_int), intent(in) :: value(:)
400 call atlas__Config__set_array_int(this%CPTR_PGIBUG_B, c_str(name), &
401 & value, size(value) )
402 end subroutine atlas_Config__set_array_int32
403
404 subroutine atlas_Config__set_array_int64(this, name, value)
405 use, intrinsic :: iso_c_binding, only : c_long
406 use fckit_c_interop_module, only : c_str
407 use atlas_Config_c_binding
408 class(atlas_Config), intent(in) :: this
409 character(len=*), intent(in) :: name
410 integer(c_long), intent(in) :: value(:)
411 call atlas__Config__set_array_long(this%CPTR_PGIBUG_B, c_str(name), &
412 & value, size(value) )
413 end subroutine atlas_Config__set_array_int64
414
415 subroutine atlas_Config__set_array_real32(this, name, value)
416 use, intrinsic :: iso_c_binding, only : c_float
417 use fckit_c_interop_module, only : c_str
418 use atlas_Config_c_binding
419 class(atlas_Config), intent(in) :: this
420 character(len=*), intent(in) :: name
421 real(c_float), intent(in) :: value(:)
422 call atlas__Config__set_array_float(this%CPTR_PGIBUG_B, c_str(name), &
423 & value, size(value) )
424 end subroutine atlas_Config__set_array_real32
425
426 subroutine atlas_Config__set_array_real64(this, name, value)
427 use, intrinsic :: iso_c_binding, only : c_double
428 use fckit_c_interop_module, only : c_str
429 use atlas_Config_c_binding
430 class(atlas_Config), intent(in) :: this
431 character(len=*), intent(in) :: name
432 real(c_double), intent(in) :: value(:)
433 call atlas__Config__set_array_double(this%CPTR_PGIBUG_B, c_str(name), &
434 & value, size(value) )
435 end subroutine atlas_Config__set_array_real64
436
437 function atlas_Config__get_array_int32(this, name, value) result(found)
438 use, intrinsic :: iso_c_binding, only : c_ptr, c_int, c_f_pointer
439 use fckit_c_interop_module, only : c_str, c_ptr_free
440 use atlas_Config_c_binding
441 logical :: found
442 class(atlas_Config), intent(in) :: this
443 character(len=*), intent(in) :: name
444 integer(c_int), allocatable, intent(inout) :: value(:)
445 type(c_ptr) :: value_cptr
446 integer(c_int), pointer :: value_fptr(:)
447 integer :: value_size
448 integer :: value_allocated
449 integer :: found_int
450 found_int = atlas__Config__get_array_int(this%CPTR_PGIBUG_B, c_str(name), &
451 & value_cptr, value_size, value_allocated )
452 if (found_int ==1 ) then
453 call c_f_pointer(value_cptr,value_fptr,(/value_size/))
454 if( allocated(value) ) deallocate(value)
455 allocate(value(value_size))
456 value(:) = value_fptr(:)
457 if( value_allocated == 1 ) call c_ptr_free(value_cptr)
458 endif
459 found = .False.
460 if (found_int == 1) found = .True.
461 end function atlas_Config__get_array_int32
462
463 function atlas_Config__get_array_int64(this, name, value) result(found)
464 use, intrinsic :: iso_c_binding, only : c_long, c_ptr, c_f_pointer
465 use fckit_c_interop_module, only : c_str, c_ptr_free
466 use atlas_Config_c_binding
467 logical :: found
468 class(atlas_Config), intent(in) :: this
469 character(len=*), intent(in) :: name
470 integer(c_long), allocatable, intent(inout) :: value(:)
471 type(c_ptr) :: value_cptr
472 integer(c_long), pointer :: value_fptr(:)
473 integer :: value_size
474 integer :: value_allocated
475 integer :: found_int
476 found_int = atlas__Config__get_array_long(this%CPTR_PGIBUG_B, c_str(name), &
477 & value_cptr, value_size, value_allocated )
478 if (found_int == 1) then
479 call c_f_pointer(value_cptr,value_fptr,(/value_size/))
480 if( allocated(value) ) deallocate(value)
481 allocate(value(value_size))
482 value(:) = value_fptr(:)
483 if( value_allocated == 1 ) call c_ptr_free(value_cptr)
484 endif
485 found = .False.
486 if (found_int == 1) found = .True.
487 end function atlas_Config__get_array_int64
488
489 function atlas_Config__get_array_real32(this, name, value) result(found)
490 use, intrinsic :: iso_c_binding, only : c_float, c_ptr, c_f_pointer
491 use fckit_c_interop_module, only : c_str, c_ptr_free
492 use atlas_Config_c_binding
493 logical :: found
494 class(atlas_Config), intent(in) :: this
495 character(len=*), intent(in) :: name
496 real(c_float), allocatable, intent(inout) :: value(:)
497 type(c_ptr) :: value_cptr
498 real(c_float), pointer :: value_fptr(:)
499 integer :: value_size
500 integer :: value_allocated
501 integer :: found_int
502 found_int = atlas__Config__get_array_float(this%CPTR_PGIBUG_B, c_str(name), &
503 & value_cptr, value_size, value_allocated )
504 if (found_int == 1 ) then
505 call c_f_pointer(value_cptr,value_fptr,(/value_size/))
506 if( allocated(value) ) deallocate(value)
507 allocate(value(value_size))
508 value(:) = value_fptr(:)
509 if( value_allocated == 1 ) call c_ptr_free(value_cptr)
510 endif
511 found = .False.
512 if (found_int == 1) found = .True.
513 end function atlas_Config__get_array_real32
514
515 function atlas_Config__get_array_real64(this, name, value) result(found)
516 use, intrinsic :: iso_c_binding, only : c_double, c_ptr, c_f_pointer
517 use fckit_c_interop_module, only : c_str, c_ptr_free
518 use atlas_Config_c_binding
519 logical :: found
520 class(atlas_Config), intent(in) :: this
521 character(len=*), intent(in) :: name
522 real(c_double), allocatable, intent(inout) :: value(:)
523 type(c_ptr) :: value_cptr
524 real(c_double), pointer :: value_fptr(:)
525 integer :: value_size
526 integer :: value_allocated
527 integer :: found_int
528 found_int = atlas__Config__get_array_double(this%CPTR_PGIBUG_B, c_str(name), &
529 & value_cptr, value_size, value_allocated )
530 if (found_int == 1) then
531 call c_f_pointer(value_cptr,value_fptr,(/value_size/))
532 if( allocated(value) ) deallocate(value)
533 allocate(value(value_size))
534 value(:) = value_fptr(:)
535 if( value_allocated == 1 ) call c_ptr_free(value_cptr)
536 endif
537 found = .False.
538 if (found_int == 1) found = .True.
539 end function atlas_Config__get_array_real64
540
541 function atlas_Config__json(this) result(json)
542 use, intrinsic :: iso_c_binding, only : c_ptr, c_int
543 use fckit_c_interop_module, only : c_str, c_ptr_to_string, c_ptr_free
544 use atlas_Config_c_binding
545 character(len=:), allocatable :: json
546 class(atlas_Config), intent(in) :: this
547 type(c_ptr) :: json_cptr
548 integer(c_int) :: json_size
549 integer(c_int) :: json_allocated
550 call atlas__Config__json(this%CPTR_PGIBUG_B,json_cptr,json_size,json_allocated)
551 allocate(character(len=json_size) :: json )
552 json = c_ptr_to_string(json_cptr)
553 if( json_allocated == 1 ) call c_ptr_free(json_cptr)
554 end function atlas_Config__json
555
556 end module atlas_config_module
557