ctf: compile plugin as C++
[babeltrace.git] / src / plugins / common / param-validation / param-validation.h
CommitLineData
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
17struct bt_param_validation_context;
18struct bt_param_validation_value_descr;
19
087cd0f5
SM
20#if defined(__cplusplus)
21#define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END \
22 { bt_param_validation_map_value_entry_descr::end }
23#else
d0d4e0ed 24#define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_END { NULL, 0, {} }
087cd0f5 25#endif
d0d4e0ed
SM
26
27struct 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
33struct 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
39struct bt_param_validation_string_value_descr {
40 /* NULL-terminated array of choices. Unused if NULL. */
41 const char **choices;
42};
43
44enum 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
50typedef enum bt_param_validation_status
51 (bt_param_validation_func)(const bt_value *value,
52 struct bt_param_validation_context *);
53
54struct bt_param_validation_value_descr {
087cd0f5
SM
55#if defined(__cplusplus)
56 static struct {} array_t;
57 static struct {} string_t;
58 static struct {} signed_integer_t;
59 static struct {} bool_t;
60
61 bt_param_validation_value_descr(decltype(array_t),
62 uint64_t min_length, uint64_t max_length,
63 const bt_param_validation_value_descr &element_type)
64 : type{BT_VALUE_TYPE_ARRAY}, array{min_length, max_length, &element_type}
65 {}
66
67 bt_param_validation_value_descr(decltype(string_t),
68 const char **choices = nullptr)
69 : type{BT_VALUE_TYPE_STRING}, string{choices}
70 {}
71
72 bt_param_validation_value_descr(decltype(signed_integer_t))
73 : type{BT_VALUE_TYPE_SIGNED_INTEGER}
74 {}
75
76 bt_param_validation_value_descr(decltype(bool_t))
77 : type{BT_VALUE_TYPE_BOOL}
78 {}
79#endif
80
d0d4e0ed
SM
81 bt_value_type type;
82
83 /* Additional checks dependent on the type. */
84 union {
85 struct bt_param_validation_array_value_descr array;
86 struct bt_param_validation_map_value_descr map;
87 struct bt_param_validation_string_value_descr string;
88 };
89
90 /*
91 * If set, call this function, which is responsible of validating the
92 * value. The other fields are ignored.
93 *
94 * If validation fails, this function must call
95 * `bt_param_validation_error` with the provided context
96 * to set the error string.
97 */
087cd0f5
SM
98 bt_param_validation_func *validation_func
99#if defined(__cplusplus)
100 = nullptr
101#endif
102 ;
d0d4e0ed
SM
103};
104
105#define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_OPTIONAL true
106#define BT_PARAM_VALIDATION_MAP_VALUE_ENTRY_MANDATORY false
107
108struct bt_param_validation_map_value_entry_descr {
087cd0f5
SM
109#if defined(__cplusplus)
110 static struct {} end;
111
112 bt_param_validation_map_value_entry_descr(decltype(end))
113 : key{nullptr},
114 /* These values are not important. */
115 is_optional{false}, value_descr(bt_param_validation_value_descr::bool_t)
116 {}
117
118 bt_param_validation_map_value_entry_descr(const char *key_, bool is_optional_,
119 bt_param_validation_value_descr value_descr_)
120 : key(key_), is_optional(is_optional_), value_descr(value_descr_)
121 {}
122
123#endif
124 /* If NULL/nullptr, this entry represents the end of the list. */
d0d4e0ed
SM
125 const char *key;
126 bool is_optional;
d0d4e0ed
SM
127 const struct bt_param_validation_value_descr value_descr;
128};
129
087cd0f5 130BT_EXTERN_C BT_HIDDEN
d0d4e0ed
SM
131enum bt_param_validation_status bt_param_validation_validate(
132 const bt_value *params,
133 const struct bt_param_validation_map_value_entry_descr *entries,
134 gchar **error);
135
087cd0f5 136BT_EXTERN_C BT_HIDDEN __BT_ATTR_FORMAT_PRINTF(2, 3)
d0d4e0ed
SM
137enum bt_param_validation_status bt_param_validation_error(
138 struct bt_param_validation_context *ctx,
139 const char *format, ...);
140
141#endif /* BABELTRACE_PLUGINS_COMMON_PARAM_VALIDATION_PARAM_VALIDATION_H */
This page took 0.047598 seconds and 4 git commands to generate.