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