2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 EfficiOS Inc.
6 #ifndef BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H
7 #define BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H
9 #include <babeltrace2/babeltrace.h>
13 #include <stdio.h> /* For __MINGW_PRINTF_FORMAT. */
15 #include <common/macros.h>
17 struct bt_param_validation_context
;
18 struct bt_param_validation_value_descr
;
20 #if defined(__cplusplus)
21 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END \
22 { bt_param_validation_map_value_entry_descr::end }
24 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END { NULL, 0, {} }
27 struct bt_param_validation_map_value_descr
{
28 const struct bt_param_validation_map_value_entry_descr
*entries
;
31 #define BT_PARAM_VALIDATION_INFINITE UINT64_MAX
33 struct bt_param_validation_array_value_descr
{
35 uint64_t max_length
; /* Use BT_PARAM_VALIDATION_INFINITE if there's no max. */
36 const struct bt_param_validation_value_descr
*element_type
;
39 struct bt_param_validation_string_value_descr
{
40 /* NULL-terminated array of choices. Unused if NULL. */
44 enum bt_param_validation_status
{
45 BT_PARAM_VALIDATION_STATUS_OK
= 0,
46 BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
= -1,
47 BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
= -2,
50 typedef enum bt_param_validation_status
51 (bt_param_validation_func
)(const bt_value
*value
,
52 struct bt_param_validation_context
*);
54 struct bt_param_validation_value_descr
{
55 #if defined(__cplusplus)
56 static struct {} array_t
;
57 static struct {} string_t
;
58 static struct {} signed_integer_t
;
59 static struct {} bool_t
;
61 bt_param_validation_value_descr(decltype(array_t
),
62 uint64_t min_length
, uint64_t max_length
,
63 const bt_param_validation_value_descr
&element_type
)
64 : type
{BT_VALUE_TYPE_ARRAY
}, array
{min_length
, max_length
, &element_type
}
67 bt_param_validation_value_descr(decltype(string_t
),
68 const char **choices
= nullptr)
69 : type
{BT_VALUE_TYPE_STRING
}, string
{choices
}
72 bt_param_validation_value_descr(decltype(signed_integer_t
))
73 : type
{BT_VALUE_TYPE_SIGNED_INTEGER
}
76 bt_param_validation_value_descr(decltype(bool_t
))
77 : type
{BT_VALUE_TYPE_BOOL
}
83 /* Additional checks dependent on the type. */
85 struct bt_param_validation_array_value_descr array
;
86 struct bt_param_validation_map_value_descr map
;
87 struct bt_param_validation_string_value_descr string
;
91 * If set, call this function, which is responsible of validating the
92 * value. The other fields are ignored.
94 * If validation fails, this function must call
95 * `bt_param_validation_error` with the provided context
96 * to set the error string.
98 bt_param_validation_func
*validation_func
99 #if defined(__cplusplus)
105 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL true
106 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY false
108 struct bt_param_validation_map_value_entry_descr
{
109 #if defined(__cplusplus)
110 static struct {} end
;
112 bt_param_validation_map_value_entry_descr(decltype(end
))
114 /* These values are not important. */
115 is_optional
{false}, value_descr(bt_param_validation_value_descr::bool_t
)
118 bt_param_validation_map_value_entry_descr(const char *key_
, bool is_optional_
,
119 bt_param_validation_value_descr value_descr_
)
120 : key(key_
), is_optional(is_optional_
), value_descr(value_descr_
)
124 /* If NULL/nullptr, this entry represents the end of the list. */
127 const struct bt_param_validation_value_descr value_descr
;
130 BT_EXTERN_C BT_HIDDEN
131 enum bt_param_validation_status
bt_param_validation_validate(
132 const bt_value
*params
,
133 const struct bt_param_validation_map_value_entry_descr
*entries
,
136 BT_EXTERN_C BT_HIDDEN
__BT_ATTR_FORMAT_PRINTF(2, 3)
137 enum bt_param_validation_status
bt_param_validation_error(
138 struct bt_param_validation_context
*ctx
,
139 const char *format
, ...);
141 #endif /* BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H */