Re-format with clang-format 16
[babeltrace.git] / src / plugins / common / param-validation / param-validation.h
1 /*
2 * SPDX-License-Identifier: MIT
3 *
4 * Copyright 2019 EfficiOS Inc.
5 */
6
7 #ifndef BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H
8 #define BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H
9
10 #include <babeltrace2/babeltrace.h>
11 #include <glib.h>
12 #include <stdbool.h>
13
14 #include <stdio.h> /* For __MINGW_PRINTF_FORMAT. */
15
16 #include <common/macros.h>
17
18 struct bt_param_validation_context;
19 struct bt_param_validation_value_descr;
20
21 #if defined(__cplusplus)
22 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END \
23 { bt_param_validation_map_value_entry_descr {} }
24 #else
25 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END { NULL, 0, {} }
26 #endif
27
28 struct bt_param_validation_map_value_descr {
29 const struct bt_param_validation_map_value_entry_descr *entries;
30 };
31
32 #define BT_PARAM_VALIDATION_INFINITE UINT64_MAX
33
34 struct bt_param_validation_array_value_descr {
35 uint64_t min_length;
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;
38 };
39
40 struct bt_param_validation_string_value_descr {
41 /* NULL-terminated array of choices. Unused if NULL. */
42 const char **choices;
43 };
44
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,
49 };
50
51 typedef enum bt_param_validation_status
52 (bt_param_validation_func)(const bt_value *value,
53 struct bt_param_validation_context *);
54
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)
60 {
61 bt_param_validation_value_descr descr {BT_VALUE_TYPE_ARRAY};
62
63 descr.array.min_length = min_length;
64 descr.array.max_length = max_length;
65 descr.array.element_type = &element_type;
66
67 return descr;
68 }
69
70 static bt_param_validation_value_descr makeString(
71 const char **choices = nullptr)
72 {
73 bt_param_validation_value_descr descr {BT_VALUE_TYPE_STRING};
74
75 descr.string.choices = choices;
76
77 return descr;
78 }
79
80 static bt_param_validation_value_descr makeSignedInteger()
81 {
82 return bt_param_validation_value_descr {BT_VALUE_TYPE_SIGNED_INTEGER};
83 }
84
85 static bt_param_validation_value_descr makeBool()
86 {
87 return bt_param_validation_value_descr {BT_VALUE_TYPE_BOOL};
88 }
89
90 private:
91 bt_param_validation_value_descr(bt_value_type type_)
92 : type {type_}
93 {}
94
95 public:
96 #endif
97
98 bt_value_type type;
99
100 /* Additional checks dependent on the type. */
101 union {
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;
105 };
106
107 /*
108 * If set, call this function, which is responsible of validating the
109 * value. The other fields are ignored.
110 *
111 * If validation fails, this function must call
112 * `bt_param_validation_error` with the provided context
113 * to set the error string.
114 */
115 bt_param_validation_func *validation_func
116 #if defined(__cplusplus)
117 = nullptr
118 #endif
119 ;
120 };
121
122 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL true
123 #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY false
124
125 struct bt_param_validation_map_value_entry_descr {
126 #if defined(__cplusplus)
127 /*
128 * Construct an "end" descriptor, used to denote the end of a descriptor
129 * list.
130 */
131 bt_param_validation_map_value_entry_descr()
132 : key{nullptr},
133 /* These values are not important. */
134 is_optional{false}, value_descr(bt_param_validation_value_descr::makeBool())
135 {}
136
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_)
140 {}
141
142 #endif
143 /* If NULL/nullptr, this entry represents the end of the list. */
144 const char *key;
145 bool is_optional;
146 const struct bt_param_validation_value_descr value_descr;
147 };
148
149 BT_EXTERN_C
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,
153 gchar **error);
154
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, ...);
159
160 #endif /* BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H */
This page took 0.034745 seconds and 5 git commands to generate.