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 {} }
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 bt_param_validation_value_descr
makeArray(
57 uint64_t min_length
, uint64_t max_length
,
58 const bt_param_validation_value_descr
&element_type
)
60 bt_param_validation_value_descr descr
{BT_VALUE_TYPE_ARRAY
};
62 descr
.array
.min_length
= min_length
;
63 descr
.array
.max_length
= max_length
;
64 descr
.array
.element_type
= &element_type
;
69 static bt_param_validation_value_descr
makeString(
70 const char **choices
= nullptr)
72 bt_param_validation_value_descr descr
{BT_VALUE_TYPE_STRING
};
74 descr
.string
.choices
= choices
;
79 static bt_param_validation_value_descr
makeSignedInteger()
81 return bt_param_validation_value_descr
{BT_VALUE_TYPE_SIGNED_INTEGER
};
84 static bt_param_validation_value_descr
makeBool()
86 return bt_param_validation_value_descr
{BT_VALUE_TYPE_BOOL
};
90 bt_param_validation_value_descr(bt_value_type type_
)
99 /* Additional checks dependent on the type. */
101 struct bt_param_validation_array_value_descr array
;
102 struct bt_param_validation_map_value_descr map
;
103 struct bt_param_validation_string_value_descr string
;
107 * If set, call this function, which is responsible of validating the
108 * value. The other fields are ignored.
110 * If validation fails, this function must call
111 * `bt_param_validation_error` with the provided context
112 * to set the error string.
114 bt_param_validation_func
*validation_func
115 #if defined(__cplusplus)
121 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL true
122 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY false
124 struct bt_param_validation_map_value_entry_descr
{
125 #if defined(__cplusplus)
127 * Construct an "end" descriptor, used to denote the end of a descriptor
130 bt_param_validation_map_value_entry_descr()
132 /* These values are not important. */
133 is_optional
{false}, value_descr(bt_param_validation_value_descr::makeBool())
136 bt_param_validation_map_value_entry_descr(const char *key_
, bool is_optional_
,
137 bt_param_validation_value_descr value_descr_
)
138 : key(key_
), is_optional(is_optional_
), value_descr(value_descr_
)
142 /* If NULL/nullptr, this entry represents the end of the list. */
145 const struct bt_param_validation_value_descr value_descr
;
149 enum bt_param_validation_status
bt_param_validation_validate(
150 const bt_value
*params
,
151 const struct bt_param_validation_map_value_entry_descr
*entries
,
154 BT_EXTERN_C
__BT_ATTR_FORMAT_PRINTF(2, 3)
155 enum bt_param_validation_status
bt_param_validation_error(
156 struct bt_param_validation_context
*ctx
,
157 const char *format
, ...);
159 #endif /* BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H */