-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.F90
1569 lines (1249 loc) · 51.9 KB
/
config.F90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
! Copyright (C) 2020 National Center for Atmospheric Research
! SPDX-License-Identifier: Apache-2.0
!
!> \file
!> The musica_config module
!> The config_t type and related functions
module musica_config
use iso_c_binding
use musica_constants, only : musica_ik, musica_rk, musica_dk
use musica_iterator, only : iterator_t
use musica_yaml_util
implicit none
private
public :: config_t
!> Model configuration data
!!
!! Instances of type \c config_t can be used to access model configuration
!! data in \c json format. If there is a need to use model configuration
!! in another format (e.g., XML) in the future, an abstract \c config_t
!! type could be set up, that this type and an XML-based type could extend.
!! The rest of the model code would be unaffected.
!!
!! It is assumed that most configuration datasets will be small enough that
!! returned subsets of configuration data can just be a copy of the original
!! data (instead of using a pointer to the start of the subset in the original
!! dataset, or something like this). This avoids ownership problems with
!! cleaning up the memory after a \c config_t object goes out of scope.
!!
!! Only use \c config_t objects during initialization. They are not designed
!! for efficiency.
!!
!! **IMPORTANT:** The order of elements is arbitrary. No user of a \c config_t
!! object can assume anything by the order of key-value pairs in the data.
!! This dataset:
!! \code{yaml}
!! foo: 1
!! bar: 2
!! foobar: 3
!! \endcode
!! ... is the same as:
!! \code{yaml}
!! bar: 2
!! foobar: 3
!! foo: 1
!! \endcode
!!
!! There is no guarantee that an iterator over the elements of a config_t
!! object will return them in the same order they exist in the original
!! file or string.
!!
!! Example of a config_t object generated from a file:
!! \code{f90}
!! use musica_config, only : config_t
!! use musica_constants, only : musica_dk, musica_ik
!! use musica_iterator, only : iterator_t
!! use musica_string, only : string_t
!!
!! character(len=*), parameter :: my_name = "config file example"
!! type(config_t) :: main_config, sub_config, sub_real_config
!! real(musica_dk) :: my_real
!! integer(musica_ik) :: my_int
!! type(string_t) :: my_string
!! class(iterator_t), pointer :: iter
!! logical :: found
!!
!! call main_config%from_file( 'data/config_example.yaml' )
!!
!! ! this would fail with an error if 'a string' is not found
!! call main_config%get( "a string", my_string, my_name )
!! write(*,*) "a string value: ", my_string
!!
!! ! add the found argument to avoid failure if the pair is not found
!! call main_config%get( "my int", my_int, my_name, found = found )
!! if( found ) then
!! write(*,*) "my int value: ", my_int
!! else
!! write(*,*) "'my int' was not found"
!! end if
!!
!! ! when you get a subset of the properties, a new config_t object is
!! ! created containing the subset data. The two config_t objects are
!! ! independent of one another after this point.
!! call main_config%get( "other props", sub_config, my_name )
!! call sub_config%get( "an int", my_int, my_name )
!! write(*,*) "other props->an int value: ", my_int
!!
!! ! you can iterate over a set of key-value pairs. but remember that
!! ! the order is always arbitrary. you also must provide the right type
!! ! of variable for the values.
!! call main_config%get( "real props", sub_real_config, my_name )
!! iter => sub_real_config%get_iterator( )
!! do while( iter%next( ) )
!! my_string = sub_real_config%key( iter )
!! call sub_real_config%get( iter, my_real, my_name )
!! write(*,*) my_string, " value: ", my_real
!! end do
!!
!! ! you can also get the number of child objects before iterating over
!! ! them, if you want to allocate an array or something first
!! write(*,*) "number of children: ", sub_real_config%number_of_children( )
!!
!! ! you can add key-value pairs with the add function
!! call main_config%add( "my new int", 43, my_name )
!! call main_config%get( "my new int", my_int, my_name )
!! write(*,*) "my new int value: ", my_int
!!
!! ! clean up memory
!! deallocate( iter )
!! \endcode
!!
!! `data/config_example.json`:
!! \code{yaml}
!! my int: 12
!! other props:
!! some time [min]: 12
!! a pressure [bar]: 103.4
!! an int: 45
!! real props:
!! foo: 14.2
!! bar: 64.2
!! foobar: 920.4
!! a string: foo
!! \endcode
!!
!! Output:
!! \code{bash}
!! a string value: foo
!! my int value: 12
!! other props->an int value: 45
!! other props->some time value: 720.00000000000000 s
!! other props->a pressure value: 10340000.000000000 Pa
!! foo value: 14.199999999999999
!! bar value: 64.200000000000003
!! foobar value: 920.39999999999998
!! number of children: 3
!! my new int value: 43
!! \endcode
!!
type :: config_t
private
!> Pointer to YAML node
type(c_ptr) :: node_ = c_null_ptr
contains
!> Empties the configuration
procedure :: empty
!> Loads a configuration with data from a file
procedure :: from_file => construct_from_file
!> Writes a configuration to a file
procedure :: to_file
!> Returns the number of child objects
procedure :: number_of_children
!> Gets an iterator for the configuration data
procedure :: get_iterator
!> Gets the key name for a key-value pair
procedure :: key
!> @name Gets some configuration data
!!
!! Each function includes optional \c found and \c default arguments. If
!! neither is included and the data are not found, execution is stopped
!! with an error message.
!!
!! If a \c default value is included and the data are not found, the
!! returned argument is set to this default value, otherwise it is set to
!! a standard default value.
!!
!! If the \c found argument is included and the data are found, \c found
!! is set to \c true, otherwise it is set to \c false.
!! @{
procedure, private :: get_config
procedure, private :: get_string_string_default
procedure, private :: get_string
procedure, private :: get_int
procedure, private :: get_float
procedure, private :: get_double
procedure, private :: get_logical
procedure, private :: get_string_array
procedure, private :: get_double_array
procedure, private :: get_config_array
procedure, private :: get_from_iterator
procedure, private :: get_array_from_iterator
generic :: get => get_config, get_string, get_string_string_default, &
get_int, get_float, get_double, &
get_logical, get_string_array, get_double_array, &
get_config_array, get_from_iterator, &
get_array_from_iterator
!> @}
!> @name Adds a named piece of configuration data
!! @{
procedure, private :: add_config
procedure, private :: add_char_array
procedure, private :: add_string
procedure, private :: add_int
procedure, private :: add_float
procedure, private :: add_double
procedure, private :: add_logical
procedure, private :: add_string_array
procedure, private :: add_double_array
procedure, private :: add_config_array
generic :: add => add_config, add_char_array, add_string, &
add_int, add_float, add_double, add_logical, &
add_string_array, add_double_array, add_config_array
!> @}
!> @name Assignment
!! @{
procedure, private :: config_assign_config
procedure, private :: config_assign_string
procedure, private :: config_assign_char
procedure, private, pass(config) :: string_assign_config
generic :: assignment(=) => config_assign_config, config_assign_string, &
config_assign_char, string_assign_config
!> @}
!> Merges another config_t object into the config_t object
procedure :: merge_in
!> Validates the format of the configuration file
procedure :: validate
!> Print the raw contents of the configuration
procedure :: print => do_print
!> Returns the number of bytes required to pack the object onto a buffer
procedure :: pack_size
!> Packs the object onto a character buffer
procedure :: mpi_pack
!> Unpacks an object from a character buffer
procedure :: mpi_unpack
!> Cleans up memory
final :: finalize, finalize_1D_array
!> Find a JSON key by prefix
procedure, private :: find_by_prefix
end type config_t
!> Configuration data iterator
type, extends(iterator_t) :: config_iterator_t
!> Pointer to the node to iterator over (owned by config_t)
type(c_ptr) :: node_ = c_null_ptr
!> Current iterator
type(c_ptr) :: curr_ = c_null_ptr
!> End pointer
type(c_ptr) :: end_ = c_null_ptr
contains
!> Advances to the next key-value pair
procedure :: next => iterator_next
!> Resets the iterator
procedure :: reset => iterator_reset
!> Clean up memory
final :: iterator_finalize
end type config_iterator_t
contains
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Empties the configuration
subroutine empty( this )
!> Configuration
class(config_t), intent(out) :: this
call initialize_config_t( this )
end subroutine empty
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Constructs a configuration from a file
subroutine construct_from_file( this, file_name )
use musica_assert, only : die
!> New configuration
class(config_t), intent(out) :: this
!> File name containing configuration data
character(len=*), intent(in) :: file_name
character(len=1, kind=c_char), allocatable :: c_file_name(:)
c_file_name = to_c_string( file_name )
select type( this )
type is( config_t )
call finalize( this )
class default
call die( 316253716 )
end select
this%node_ = yaml_create_from_file_c( c_file_name )
end subroutine construct_from_file
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Writes a configuration to a file
subroutine to_file( this, file_name )
!> Configuration
class(config_t), intent(inout) :: this
!> File name to save configuration with
character(len=*), intent(in) :: file_name
character(len=1, kind=c_char), allocatable :: c_file_name(:)
c_file_name = to_c_string( file_name )
call yaml_to_file_c( this%node_, c_file_name )
end subroutine to_file
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Returns the number of child objects
function number_of_children( this )
use musica_assert, only : assert
!> Number of child objects
integer(kind=musica_ik) :: number_of_children
!> Configuration
class(config_t), intent(inout) :: this
number_of_children = yaml_size_c( this%node_ )
end function number_of_children
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets an interator for the configuration data
function get_iterator( this )
use musica_assert, only : assert
!> Pointer to the iterator
class(iterator_t), pointer :: get_iterator
!> Configuration
class(config_t), intent(in), target :: this
call assert( 398295168, c_associated( this%node_ ) )
allocate( config_iterator_t :: get_iterator )
select type( iter => get_iterator )
type is( config_iterator_t )
iter%node_ = this%node_
iter%end_ = yaml_end_c( this%node_ )
iter%curr_ = c_null_ptr
end select
end function get_iterator
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets the key name using an iterator
function key( this, iterator )
use musica_assert, only : assert, die_msg
use musica_string, only : string_t
!> Key name
type(string_t) :: key
!> Configuration
class(config_t), intent(inout) :: this
!> Configuration iterator
class(iterator_t), intent(in) :: iterator
type(string_t_c) :: c_key
select type( iterator )
class is( config_iterator_t )
c_key = yaml_key_c( iterator%curr_ )
key = to_f_string( c_key )
call yaml_delete_string_c( c_key )
class default
call die_msg( 790805324, "Config iterator type mismatch" )
end select
end function key
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets a subset of the configuration data
subroutine get_config( this, key, value, caller, default, found )
use musica_assert, only : die_msg
use musica_string, only : string_t
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
class(config_t), intent(out) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
class(config_t), intent(in), optional :: default
!> Flag indicating whether key was found
logical, intent(out), optional :: found
logical(kind=c_bool) :: l_found
value%node_ = yaml_get_node_c( this%node_, to_c_string( key ), l_found )
if( .not. l_found .and. present( default ) ) value = default
if( present( found ) ) then
found = l_found
return
end if
if( .not. l_found .and. .not. present( default ) ) then
call die_msg( 859993455, "Key '"//trim( key )// &
"' requested by "//trim( caller )//" not found" )
end if
end subroutine get_config
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets a string from the configuration data
subroutine get_string_string_default( this, key, value, caller, default, &
found )
use musica_string, only : string_t
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
class(string_t), intent(out) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
class(string_t), intent(in) :: default
!> Flag indicating whether key was found
logical, intent(out), optional :: found
call get_string( this, key, value, caller, default = default%val_, &
found = found )
end subroutine get_string_string_default
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets a string from the configuration data
subroutine get_string( this, key, value, caller, default, found )
use musica_assert, only : die_msg
use musica_string, only : string_t
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
class(string_t), intent(out) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
character(len=*), intent(in), optional :: default
!> Flag indicating whether key was found
logical, intent(out), optional :: found
logical(kind=c_bool) :: l_found
type(string_t_c) :: c_value
c_value = yaml_get_string_c( this%node_, to_c_string( key ), l_found )
if( l_found ) then
value%val_ = to_f_string( c_value )
call yaml_delete_string_c( c_value )
end if
if( .not. l_found .and. present( default ) ) value = default
if( present( found ) ) then
found = l_found
return
end if
if( .not. l_found .and. .not. present( default ) ) then
call die_msg( 705088796, "Key '"//trim( key )// &
"' requested by "//trim( caller )//" not found" )
end if
end subroutine get_string
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets an integer from the configuration data
subroutine get_int( this, key, value, caller, default, found )
use musica_assert, only : die_msg
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
integer(kind=musica_ik), intent(out) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
integer(kind=musica_ik), intent(in), optional :: default
!> Flag indicating whether key was found
logical, intent(out), optional :: found
logical(kind=c_bool) :: l_found
value = yaml_get_int_c( this%node_, to_c_string( key ), l_found )
if( .not. l_found .and. present( default ) ) value = default
if( present( found ) ) then
found = l_found
return
end if
if( .not. l_found .and. .not. present( default ) ) then
call die_msg( 689949329, "Key '"//trim( key )// &
"' requested by "//trim( caller )//" not found" )
end if
end subroutine get_int
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets a single-precision real number from the configuration data
subroutine get_float( this, key, value, caller, default, found )
use musica_assert, only : die_msg
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
real(kind=musica_rk), intent(out) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
real(kind=musica_rk), intent(in), optional :: default
!> Flag indicating whether key was found
logical, intent(out), optional :: found
logical(kind=c_bool) :: l_found
value = yaml_get_float_c( this%node_, to_c_string( key ), l_found )
if( .not. l_found .and. present( default ) ) value = default
if( present( found ) ) then
found = l_found
return
end if
if( .not. l_found .and. .not. present( default ) ) then
call die_msg( 337653668, "Key '"//trim( key )// &
"' requested by "//trim( caller )//" not found" )
end if
end subroutine get_float
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets a double-precision real number from the configuration data
subroutine get_double( this, key, value, caller, default, found )
use musica_assert, only : die_msg
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
real(kind=musica_dk), intent(out) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
real(kind=musica_dk), intent(in), optional :: default
!> Flag indicating whether key was found
logical, intent(out), optional :: found
logical(kind=c_bool) :: l_found
value = yaml_get_double_c( this%node_, to_c_string( key ), l_found )
if( .not. l_found .and. present( default ) ) value = default
if( present( found ) ) then
found = l_found
return
end if
if( .not. l_found .and. .not. present( default ) ) then
call die_msg( 339559202, "Key '"//trim( key )// &
"' requested by "//trim( caller )//" not found" )
end if
end subroutine get_double
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets a boolean value from the configuration data
subroutine get_logical( this, key, value, caller, default, found )
use musica_assert, only : die_msg
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
logical, intent(out) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
logical, intent(in), optional :: default
!> Flag indicating whether key was found
logical, intent(out), optional :: found
logical(kind=c_bool) :: l_found
value = yaml_get_bool_c( this%node_, to_c_string( key ), l_found )
if( .not. l_found .and. present( default ) ) value = default
if( present( found ) ) then
found = l_found
return
end if
if( .not. l_found .and. .not. present( default ) ) then
call die_msg( 506357333, "Key '"//trim( key )// &
"' requested by "//trim( caller )//" not found" )
end if
end subroutine get_logical
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets an array of strings from the configuration data
subroutine get_string_array( this, key, value, caller, default, found )
use musica_assert, only : assert, assert_msg
use musica_string, only : string_t
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
type(string_t), allocatable, intent(out) :: value(:)
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
type(string_t), intent(in), optional :: default(:)
!> Flag indicating whether key was found
logical, intent(out), optional :: found
type(string_array_t_c) :: c_array
integer(c_int) :: size, i
type(string_t_c), pointer :: c_strings(:)
logical(kind=c_bool) :: l_found
c_array = yaml_get_string_array_c( this%node_, to_c_string( key ), &
l_found )
call assert_msg( 469804765, l_found .or. present( default ) .or. &
present( found ), "Key '"//trim( key )// &
"' requested by "//trim( caller )//" not found" )
if( present( found ) ) then
found = l_found
if( .not. l_found .and. .not. present( default ) ) return
end if
if( .not. l_found .and. present( default ) ) then
value = default
return
end if
call c_f_pointer( c_array%ptr_, c_strings, [ c_array%size_ ] )
allocate( value( c_array%size_ ) )
do i = 1, size( c_strings )
value(i) = to_f_string( c_strings( i ) )
end do
call yaml_delete_string_array_c( c_array )
end subroutine get_string_array
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets an array of doubles from the configuration data
subroutine get_double_array( this, key, value, caller, default, found )
use musica_assert, only : assert, assert_msg
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
real(musica_dk), allocatable, intent(out) :: value(:)
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
real(musica_dk), intent(in), optional :: default(:)
!> Flag indicating whether key was found
logical, intent(out), optional :: found
type(double_array_t_c) :: c_array
real(kind=c_double), pointer :: c_doubles(:)
integer :: i
logical(kind=c_bool) :: l_found
c_array = yaml_get_double_array_c( this%node_, to_c_string( key ), &
l_found )
call assert_msg( 507829003, l_found .or. present( default ) &
.or. present( found ), "Key '"//trim( key )// &
"' requested by "//trim( caller )//" not found" )
if( present( found ) ) then
found = l_found
if( .not. l_found .and. .not. present( default ) ) return
end if
if( .not. l_found .and. present( default ) ) then
value = default
return
end if
call c_f_pointer( c_array%ptr_, c_doubles, [ c_array%size_ ] )
allocate( value( c_array%size_ ) )
value(:) = c_doubles(:)
call yaml_delete_double_array_c( c_array )
end subroutine get_double_array
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets an array of config_t objects
subroutine get_config_array( this, key, value, caller, default, found )
use musica_assert, only : assert, assert_msg
!> Configuration
class(config_t), intent(inout) :: this
!> Key used to find value
character(len=*), intent(in) :: key
!> Returned value
type(config_t), allocatable, intent(out) :: value(:)
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
!> Default value if not found
type(config_t), intent(in), optional :: default(:)
!> Flag indicating whether key was found
logical, intent(out), optional :: found
type(node_array_t_c) :: c_array
type(c_ptr), pointer :: c_nodes(:)
integer :: i
logical(kind=c_bool) :: l_found
c_array = yaml_get_node_array_c( this%node_, to_c_string( key ), l_found )
call assert_msg( 737497064, l_found .or. present( default ) &
.or. present( found ), "Key '"//trim( key )// &
"' requested by "//trim( caller )//" not found" )
if( present( found ) ) then
found = l_found
if( .not. l_found .and. .not. present( default ) ) return
end if
if( .not. l_found .and. present( default ) ) then
value = default
return
end if
call c_f_pointer( c_array%ptr_, c_nodes, [ c_array%size_ ] )
allocate( value( c_array%size_ ) )
value(:)%node_ = c_nodes(:)
call yaml_delete_node_array_c( c_array )
end subroutine get_config_array
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets a value using an iterator
subroutine get_from_iterator( this, iterator, value, caller )
use musica_assert, only : assert, die_msg
use musica_string, only : string_t
!> Configuration
class(config_t), intent(inout) :: this
!> Iterator to use to find value
class(iterator_t), intent(in) :: iterator
!> Returned value
class(*), intent(out) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
type(string_t_c) :: str
select type( iterator )
class is( config_iterator_t )
select type( value )
type is( config_t )
value%node_ = yaml_get_node_from_iterator_c( iterator%curr_ )
type is( integer( musica_ik ) )
value = yaml_get_int_from_iterator_c( iterator%curr_ )
type is( real( musica_rk ) )
value = yaml_get_float_from_iterator_c( iterator%curr_ )
type is( real( musica_dk ) )
value = yaml_get_double_from_iterator_c( iterator%curr_ )
type is( logical )
value = yaml_get_bool_from_iterator_c( iterator%curr_ )
type is( string_t )
str = yaml_get_string_from_iterator_c( iterator%curr_ )
value = to_f_string( str )
call yaml_delete_string_c( str )
class default
call die_msg( 227296475, "Unknown type for get function." )
end select
class default
call die_msg( 446668858, "Iterator type mismatch. Expected "// &
"config_iterator_t" )
end select
end subroutine get_from_iterator
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Gets an array value using an iterator
subroutine get_array_from_iterator( this, iterator, value, caller )
use musica_assert, only : assert, die_msg
use musica_string, only : string_t
!> Configuration
class(config_t), intent(inout) :: this
!> Iterator to use to find value
class(iterator_t), intent(in) :: iterator
!> Returned value
type(string_t), allocatable, intent(out) :: value(:)
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
integer :: i
type(string_array_t_c) :: c_array
type(string_t_c), pointer :: c_strings(:)
select type( iterator )
class is( config_iterator_t )
c_array = yaml_get_string_array_from_iterator_c( iterator%curr_ )
call c_f_pointer( c_array%ptr_, c_strings, [ c_array%size_ ] )
allocate( value( c_array%size_ ) )
do i = 1, size( c_strings )
value(i) = to_f_string( c_strings( i ) )
end do
call yaml_delete_string_array_c( c_array )
class default
call die_msg( 217094588, "Iterator type mismatch. Expected "// &
"config_iterator_t" )
end select
end subroutine get_array_from_iterator
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Adds a subset of configuration data
subroutine add_config( this, key, value, caller )
use musica_assert, only : assert_msg
!> Configuration
class(config_t), intent(inout) :: this
!> Key in insert
character(len=*), intent(in) :: key
!> Value to set
type(config_t), intent(in) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
character(len=1, kind=c_char), allocatable :: c_key(:)
c_key = to_c_string( key )
call assert_msg( 644309796, c_associated( value%node_ ), &
"Trying to add uninitialized config_t object by "// &
caller )
call yaml_add_node_c( this%node_, c_key, value%node_ )
end subroutine add_config
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Adds a string to the configuration data
subroutine add_char_array( this, key, value, caller )
!> Configuration
class(config_t), intent(inout) :: this
!> Key in insert
character(len=*), intent(in) :: key
!> Value to set
character(len=*), intent(in) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
character(len=1, kind=c_char), allocatable :: c_key(:), c_value(:)
c_key = to_c_string( key )
c_value = to_c_string( value )
if( .not. c_associated( this%node_ ) ) call initialize_config_t( this )
call yaml_add_string_c( this%node_, c_key, c_value )
end subroutine add_char_array
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Adds a string to the configuration data
subroutine add_string( this, key, value, caller )
use musica_string, only : string_t
!> Configuration
class(config_t), intent(inout) :: this
!> Key in insert
character(len=*), intent(in) :: key
!> Value to set
type(string_t), intent(in) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
character(len=1, kind=c_char), allocatable :: c_key(:), c_value(:)
c_key = to_c_string( key )
c_value = to_c_string( value%val_ )
if( .not. c_associated( this%node_ ) ) call initialize_config_t( this )
call yaml_add_string_c( this%node_, c_key, c_value )
end subroutine add_string
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Adds an integer to the configuration data
subroutine add_int( this, key, value, caller )
!> Configuration
class(config_t), intent(inout) :: this
!> Key in insert
character(len=*), intent(in) :: key
!> Value to set
integer, intent(in) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
character(len=1, kind=c_char), allocatable :: c_key(:)
c_key = to_c_string( key )
if( .not. c_associated( this%node_ ) ) call initialize_config_t( this )
call yaml_add_int_c( this%node_, c_key, int( value, kind=c_int ) )
end subroutine add_int
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Adds a single-precision real number to the configuration data
subroutine add_float( this, key, value, caller )
!> Configuration
class(config_t), intent(inout) :: this
!> Key in insert
character(len=*), intent(in) :: key
!> Value to set
real(kind=musica_rk), intent(in) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
character(len=1, kind=c_char), allocatable :: c_key(:)
c_key = to_c_string( key )
if( .not. c_associated( this%node_ ) ) call initialize_config_t( this )
call yaml_add_float_c( this%node_, c_key, real( value, kind=c_float ) )
end subroutine add_float
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Adds a double-precision real number to the configuration data
subroutine add_double( this, key, value, caller )
!> Configuration
class(config_t), intent(inout) :: this
!> Key in insert
character(len=*), intent(in) :: key
!> Value to set
real(kind=musica_dk), intent(in) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
character(len=1, kind=c_char), allocatable :: c_key(:)
c_key = to_c_string( key )
if( .not. c_associated( this%node_ ) ) call initialize_config_t( this )
call yaml_add_double_c( this%node_, c_key, real( value, kind=c_double ) )
end subroutine add_double
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!> Adds a boolean to the configuration data
subroutine add_logical( this, key, value, caller )
!> Configuration
class(config_t), intent(inout) :: this
!> Key in insert
character(len=*), intent(in) :: key
!> Value to set
logical, intent(in) :: value
!> Name of the calling function (only for use in error messages)
character(len=*), intent(in) :: caller
character(len=1, kind=c_char), allocatable :: c_key(:)
c_key = to_c_string( key )
if( .not. c_associated( this%node_ ) ) call initialize_config_t( this )
call yaml_add_bool_c( this%node_, c_key, logical( value, kind=c_bool ) )
end subroutine add_logical