Line data Source code
1 : /* Macros for extracting config values out of a pod */
2 :
3 : #define CFG_POP( type, cfg_path ) \
4 3156 : do { \
5 3156 : char const * key = #cfg_path; \
6 3156 : fd_pod_info_t info[1]; \
7 3156 : if( fd_pod_query( pod, key, info ) ) break; \
8 3156 : if( FD_UNLIKELY( !fdctl_cfg_get_##type( &config->cfg_path, sizeof(config->cfg_path), \
9 1311 : info, key ) ) ) \
10 1311 : return NULL; \
11 1311 : fd_pod_remove( pod, key ); \
12 1311 : } while(0)
13 :
14 : #define CFG_POP1( type, toml_path, cfg_path ) \
15 75 : do { \
16 75 : char const * key = #toml_path; \
17 75 : fd_pod_info_t info[1]; \
18 75 : if( fd_pod_query( pod, key, info ) ) break; \
19 75 : if( FD_UNLIKELY( !fdctl_cfg_get_##type( &config->cfg_path, sizeof(config->cfg_path), \
20 30 : info, key ) ) ) \
21 30 : return NULL; \
22 30 : fd_pod_remove( pod, key ); \
23 30 : } while(0)
24 :
25 : #define CFG_POP_ARRAY( type, cfg_path ) \
26 150 : do { \
27 150 : char const * key = #cfg_path; \
28 150 : fd_pod_info_t info[1]; \
29 150 : if( fd_pod_query( pod, key, info ) ) break; \
30 150 : if( FD_UNLIKELY( info->val_type!=FD_POD_VAL_TYPE_SUBPOD ) ) { \
31 0 : FD_LOG_WARNING(( "`%s`: expected array", key )); \
32 0 : return NULL; \
33 0 : } \
34 66 : ulong arr_len = sizeof( config->cfg_path ) / sizeof( config->cfg_path[ 0 ] ); \
35 66 : ulong j = 0UL; \
36 72 : for( fd_pod_iter_t iter = fd_pod_iter_init( info->val ); !fd_pod_iter_done( iter ); iter = fd_pod_iter_next( iter ) ) { \
37 6 : if( FD_UNLIKELY( j>=arr_len ) ) { \
38 0 : FD_LOG_WARNING(( "`%s`: too many values (max %lu)", key, arr_len )); \
39 0 : return NULL; \
40 0 : } \
41 6 : fd_pod_info_t sub_info = fd_pod_iter_info( iter ); \
42 6 : fdctl_cfg_get_##type( &config->cfg_path[j], sizeof(config->cfg_path[j]), &sub_info, key ); \
43 6 : j++; \
44 6 : } \
45 66 : config->cfg_path ## _cnt = j; \
46 66 : fd_pod_remove( pod, key ); \
47 66 : } while(0)
48 :
49 : #define CFG_POP1_ARRAY( type, toml_path, cfg_path ) \
50 15 : do { \
51 15 : char const * key = #toml_path; \
52 15 : fd_pod_info_t info[1]; \
53 15 : if( fd_pod_query( pod, key, info ) ) break; \
54 15 : if( FD_UNLIKELY( info->val_type!=FD_POD_VAL_TYPE_SUBPOD ) ) { \
55 0 : FD_LOG_WARNING(( "`%s`: expected array", key )); \
56 0 : return NULL; \
57 0 : } \
58 6 : ulong arr_len = sizeof( config->cfg_path ) / sizeof( config->cfg_path[ 0 ] ); \
59 6 : ulong j = 0UL; \
60 6 : for( fd_pod_iter_t iter = fd_pod_iter_init( info->val ); !fd_pod_iter_done( iter ); iter = fd_pod_iter_next( iter ) ) { \
61 0 : if( FD_UNLIKELY( j>=arr_len ) ) { \
62 0 : FD_LOG_WARNING(( "`%s`: too many values (max %lu)", key, arr_len )); \
63 0 : return NULL; \
64 0 : } \
65 0 : fd_pod_info_t sub_info = fd_pod_iter_info( iter ); \
66 0 : fdctl_cfg_get_##type( &config->cfg_path[j], sizeof(config->cfg_path[j]), &sub_info, key ); \
67 0 : j++; \
68 0 : } \
69 6 : config->cfg_path ## _cnt = j; \
70 6 : fd_pod_remove( pod, key ); \
71 6 : } while(0)
72 :
73 : #define CFG_POP_TABLE( type, toml_path, cfg_path, cfg_field, field_idx ) \
74 6 : do { \
75 6 : char const * key = #toml_path; \
76 6 : fd_pod_info_t info[1]; \
77 6 : if( fd_pod_query( pod, key, info ) ) break; \
78 6 : if( FD_UNLIKELY( info->val_type!=FD_POD_VAL_TYPE_SUBPOD ) ) { \
79 0 : FD_LOG_WARNING(( "`%s`: expected table", key )); \
80 0 : return NULL; \
81 0 : } \
82 6 : ulong table_len = fd_pod_cnt( info->val ); \
83 6 : ulong j = 0UL; \
84 12 : for( fd_pod_iter_t iter = fd_pod_iter_init( info->val ); !fd_pod_iter_done( iter ); iter = fd_pod_iter_next( iter ) ) { \
85 6 : if( FD_UNLIKELY( j>=table_len ) ) { \
86 0 : FD_LOG_WARNING(( "`%s`: too many values (max %lu)", key, table_len )); \
87 0 : return NULL; \
88 0 : } \
89 6 : fd_pod_info_t sub_info = fd_pod_iter_info( iter ); \
90 6 : if( FD_UNLIKELY( sub_info.val_type!=FD_POD_VAL_TYPE_SUBPOD ) ) continue; \
91 6 : fd_pod_info_t list[ 256UL ]; \
92 6 : ulong fields_cnt = fd_pod_cnt( sub_info.val ); \
93 6 : if( FD_UNLIKELY( fields_cnt>256UL ) ) { \
94 0 : FD_LOG_WARNING(( "`%s`: Too many subpods (%lu) in table", sub_info.key, fields_cnt )); \
95 0 : return NULL; \
96 0 : } \
97 6 : fd_pod_info_t * fields = fd_pod_list( sub_info.val, list ); \
98 6 : FD_TEST( field_idx<fields_cnt ); \
99 6 : fd_pod_info_t field_info = fields[ field_idx ]; \
100 6 : char table_toml_path[ PATH_MAX ]; \
101 6 : char const * cfg_field_str = #cfg_field; \
102 6 : FD_TEST( fd_cstr_printf_check( table_toml_path, PATH_MAX, NULL, "%s.%lu.%s", key, j, cfg_field_str ) ); \
103 6 : fdctl_cfg_get_##type( &config->cfg_path[j].cfg_field, sizeof(config->cfg_path[j].cfg_field), &field_info, table_toml_path ); \
104 6 : j++; \
105 6 : } \
106 6 : config->cfg_path ## _cnt = j; \
107 6 : } while(0)
108 :
109 : #define CFG_POP_TABLE_FINI( toml_path ) \
110 3 : do { \
111 3 : fd_pod_remove( pod, #toml_path ); \
112 3 : } while(0)
|