Commit | Line | Data |
---|---|---|
d0d4e0ed | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: MIT |
d0d4e0ed | 3 | * |
0235b0db | 4 | * Copyright 2019 EfficiOS Inc. |
d0d4e0ed | 5 | */ |
ae2be88d | 6 | |
0235b0db MJ |
7 | #ifndef BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H |
8 | #define BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H | |
d0d4e0ed SM |
9 | |
10 | #include <babeltrace2/babeltrace.h> | |
11 | #include <glib.h> | |
4b3b8e4a | 12 | #include <stdbool.h> |
d0d4e0ed SM |
13 | |
14 | #include <stdio.h> /* For __MINGW_PRINTF_FORMAT. */ | |
15 | ||
40f17955 SM |
16 | #include <common/macros.h> |
17 | ||
d0d4e0ed SM |
18 | struct bt_param_validation_context; |
19 | struct bt_param_validation_value_descr; | |
20 | ||
087cd0f5 SM |
21 | #if defined(__cplusplus) |
22 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END \ | |
88730e42 | 23 | { bt_param_validation_map_value_entry_descr {} } |
087cd0f5 | 24 | #else |
d0d4e0ed | 25 | #define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END { NULL, 0, {} } |
087cd0f5 | 26 | #endif |
d0d4e0ed SM |
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 { | |
087cd0f5 | 56 | #if defined(__cplusplus) |
88730e42 | 57 | static bt_param_validation_value_descr makeArray( |
087cd0f5 SM |
58 | uint64_t min_length, uint64_t max_length, |
59 | const bt_param_validation_value_descr &element_type) | |
88730e42 SM |
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; | |
087cd0f5 | 66 | |
88730e42 SM |
67 | return descr; |
68 | } | |
69 | ||
70 | static bt_param_validation_value_descr makeString( | |
087cd0f5 | 71 | const char **choices = nullptr) |
88730e42 SM |
72 | { |
73 | bt_param_validation_value_descr descr {BT_VALUE_TYPE_STRING}; | |
087cd0f5 | 74 | |
88730e42 SM |
75 | descr.string.choices = choices; |
76 | ||
77 | return descr; | |
78 | } | |
087cd0f5 | 79 | |
88730e42 SM |
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_} | |
087cd0f5 | 93 | {} |
88730e42 SM |
94 | |
95 | public: | |
087cd0f5 SM |
96 | #endif |
97 | ||
d0d4e0ed SM |
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 | */ | |
087cd0f5 SM |
115 | bt_param_validation_func *validation_func |
116 | #if defined(__cplusplus) | |
117 | = nullptr | |
118 | #endif | |
119 | ; | |
d0d4e0ed SM |
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 { | |
087cd0f5 | 126 | #if defined(__cplusplus) |
88730e42 SM |
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() | |
087cd0f5 SM |
132 | : key{nullptr}, |
133 | /* These values are not important. */ | |
88730e42 | 134 | is_optional{false}, value_descr(bt_param_validation_value_descr::makeBool()) |
087cd0f5 SM |
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. */ | |
d0d4e0ed SM |
144 | const char *key; |
145 | bool is_optional; | |
d0d4e0ed SM |
146 | const struct bt_param_validation_value_descr value_descr; |
147 | }; | |
148 | ||
1353b066 | 149 | BT_EXTERN_C |
d0d4e0ed SM |
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 | ||
1353b066 | 155 | BT_EXTERN_C __BT_ATTR_FORMAT_PRINTF(2, 3) |
d0d4e0ed SM |
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 */ |