2 * SPDX-License-Identifier: MIT
4 * Copyright 2019 EfficiOS Inc.
7 #ifndef BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H
8 #define BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H
10 #include <babeltrace2/babeltrace.h>
14 #include <stdio.h> /* For __MINGW_PRINTF_FORMAT. */
16 #include <common/macros.h>
18 struct bt_param_validation_context
;
19 struct bt_param_validation_value_descr
;
21 #if defined(__cplusplus)
22 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END \
23 { bt_param_validation_map_value_entry_descr {} }
25 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END { NULL, 0, {} }
28 struct bt_param_validation_map_value_descr
{
29 const struct bt_param_validation_map_value_entry_descr
*entries
;
32 #define BT_PARAM_VALIDATION_INFINITE UINT64_MAX
34 struct bt_param_validation_array_value_descr
{
36 uint64_t max_length
; /* Use BT_PARAM_VALIDATION_INFINITE if there's no max. */
37 const struct bt_param_validation_value_descr
*element_type
;
40 struct bt_param_validation_string_value_descr
{
41 /* NULL-terminated array of choices. Unused if NULL. */
45 enum bt_param_validation_status
{
46 BT_PARAM_VALIDATION_STATUS_OK
= 0,
47 BT_PARAM_VALIDATION_STATUS_MEMORY_ERROR
= -1,
48 BT_PARAM_VALIDATION_STATUS_VALIDATION_ERROR
= -2,
51 typedef enum bt_param_validation_status
52 (bt_param_validation_func
)(const bt_value
*value
,
53 struct bt_param_validation_context
*);
55 struct bt_param_validation_value_descr
{
56 #if defined(__cplusplus)
57 static bt_param_validation_value_descr
makeArray(
58 uint64_t min_length
, uint64_t max_length
,
59 const bt_param_validation_value_descr
&element_type
)
61 bt_param_validation_value_descr descr
{BT_VALUE_TYPE_ARRAY
};
63 descr
.array
.min_length
= min_length
;
64 descr
.array
.max_length
= max_length
;
65 descr
.array
.element_type
= &element_type
;
70 static bt_param_validation_value_descr
makeString(
71 const char **choices
= nullptr)
73 bt_param_validation_value_descr descr
{BT_VALUE_TYPE_STRING
};
75 descr
.string
.choices
= choices
;
80 static bt_param_validation_value_descr
makeSignedInteger()
82 return bt_param_validation_value_descr
{BT_VALUE_TYPE_SIGNED_INTEGER
};
85 static bt_param_validation_value_descr
makeBool()
87 return bt_param_validation_value_descr
{BT_VALUE_TYPE_BOOL
};
91 bt_param_validation_value_descr(bt_value_type type_
)
100 /* Additional checks dependent on the type. */
102 struct bt_param_validation_array_value_descr array
;
103 struct bt_param_validation_map_value_descr map
;
104 struct bt_param_validation_string_value_descr string
;
108 * If set, call this function, which is responsible of validating the
109 * value. The other fields are ignored.
111 * If validation fails, this function must call
112 * `bt_param_validation_error` with the provided context
113 * to set the error string.
115 bt_param_validation_func
*validation_func
116 #if defined(__cplusplus)
122 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL true
123 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY false
125 struct bt_param_validation_map_value_entry_descr
{
126 #if defined(__cplusplus)
128 * Construct an "end" descriptor, used to denote the end of a descriptor
131 bt_param_validation_map_value_entry_descr()
133 /* These values are not important. */
134 is_optional
{false}, value_descr(bt_param_validation_value_descr::makeBool())
137 bt_param_validation_map_value_entry_descr(const char *key_
, bool is_optional_
,
138 bt_param_validation_value_descr value_descr_
)
139 : key(key_
), is_optional(is_optional_
), value_descr(value_descr_
)
143 /* If NULL/nullptr, this entry represents the end of the list. */
146 const struct bt_param_validation_value_descr value_descr
;
150 enum bt_param_validation_status
bt_param_validation_validate(
151 const bt_value
*params
,
152 const struct bt_param_validation_map_value_entry_descr
*entries
,
155 BT_EXTERN_C
__BT_ATTR_FORMAT_PRINTF(2, 3)
156 enum bt_param_validation_status
bt_param_validation_error(
157 struct bt_param_validation_context
*ctx
,
158 const char *format
, ...);
160 #endif /* BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H */