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