]>
Commit | Line | Data |
---|---|---|
2345c77c MR |
1 | /* |
2 | * Core Definitions for QAPI Visitor Classes | |
3 | * | |
08f9541d | 4 | * Copyright (C) 2012-2016 Red Hat, Inc. |
2345c77c MR |
5 | * Copyright IBM, Corp. 2011 |
6 | * | |
7 | * Authors: | |
8 | * Anthony Liguori <[email protected]> | |
9 | * | |
10 | * This work is licensed under the terms of the GNU LGPL, version 2.1 or later. | |
11 | * See the COPYING.LIB file in the top-level directory. | |
12 | * | |
13 | */ | |
121d0712 MA |
14 | |
15 | #ifndef QAPI_VISITOR_H | |
16 | #define QAPI_VISITOR_H | |
2345c77c | 17 | |
eb815e24 | 18 | #include "qapi/qapi-builtin-types.h" |
ed29bb28 | 19 | #include "qapi/qapi-types-compat.h" |
2345c77c | 20 | |
adfb264c EB |
21 | /* |
22 | * The QAPI schema defines both a set of C data types, and a QMP wire | |
23 | * format. QAPI objects can contain references to other QAPI objects, | |
24 | * resulting in a directed acyclic graph. QAPI also generates visitor | |
25 | * functions to walk these graphs. This file represents the interface | |
26 | * for doing work at each node of a QAPI graph; it can also be used | |
27 | * for a virtual walk, where there is no actual QAPI C struct. | |
28 | * | |
554d6586 MA |
29 | * There are four kinds of visitors: input visitors (QObject, string, |
30 | * and QemuOpts) parse an external representation and build the | |
31 | * corresponding QAPI object, output visitors (QObject and string) | |
32 | * take a QAPI object and generate an external representation, the | |
33 | * dealloc visitor takes a QAPI object (possibly partially | |
34 | * constructed) and recursively frees it, and the clone visitor | |
35 | * performs a deep clone of a QAPI object. | |
36 | * | |
37 | * While the dealloc and QObject input/output visitors are general, | |
38 | * the string, QemuOpts, and clone visitors have some implementation | |
39 | * limitations; see the documentation for each visitor for more | |
40 | * details on what it supports. Also, see visitor-impl.h for the | |
41 | * callback contracts implemented by each visitor, and | |
42 | * docs/devel/qapi-code-gen.txt for more about the QAPI code | |
43 | * generator. | |
adfb264c | 44 | * |
2c0ef9f4 EB |
45 | * All of the visitors are created via: |
46 | * | |
3b098d56 | 47 | * Visitor *subtype_visitor_new(parameters...); |
2c0ef9f4 EB |
48 | * |
49 | * A visitor should be used for exactly one top-level visit_type_FOO() | |
3b098d56 | 50 | * or virtual walk; if that is successful, the caller can optionally |
554d6586 MA |
51 | * call visit_complete() (useful only for output visits, but safe to |
52 | * call on all visits). Then, regardless of success or failure, the | |
53 | * user should call visit_free() to clean up resources. It is okay to | |
54 | * free the visitor without completing the visit, if some other error | |
55 | * is detected in the meantime. | |
56 | * | |
57 | * The clone and dealloc visitor should not be used directly outside | |
58 | * of QAPI code. Use the qapi_free_FOO() and QAPI_CLONE() instead, | |
59 | * described below. | |
2c0ef9f4 | 60 | * |
adfb264c EB |
61 | * All QAPI types have a corresponding function with a signature |
62 | * roughly compatible with this: | |
63 | * | |
012d4c96 | 64 | * bool visit_type_FOO(Visitor *v, const char *name, T obj, Error **errp); |
adfb264c EB |
65 | * |
66 | * where T is FOO for scalar types, and FOO * otherwise. The scalar | |
67 | * visitors are declared here; the remaining visitors are generated in | |
3777d36e | 68 | * qapi-visit-MODULE.h. |
adfb264c EB |
69 | * |
70 | * The @name parameter of visit_type_FOO() describes the relation | |
71 | * between this QAPI value and its parent container. When visiting | |
72 | * the root of a tree, @name is ignored; when visiting a member of an | |
ed0ba0f4 MA |
73 | * object, @name is the key associated with the value; when visiting a |
74 | * member of a list, @name is NULL; and when visiting the member of an | |
75 | * alternate, @name should equal the name used for visiting the | |
76 | * alternate. | |
adfb264c | 77 | * |
554d6586 MA |
78 | * The visit_type_FOO() functions take a non-null @obj argument; they |
79 | * allocate *@obj during input visits, leave it unchanged during | |
80 | * output and clone visits, and free it (recursively) during a dealloc | |
81 | * visit. | |
82 | * | |
83 | * Each function also takes the customary @errp argument (see | |
adfb264c EB |
84 | * qapi/error.h for details), for reporting any errors (such as if a |
85 | * member @name is not present, or is present but not the specified | |
1f584248 | 86 | * type). Only input visitors can fail. |
adfb264c | 87 | * |
68ab47e4 | 88 | * If an error is detected during visit_type_FOO() with an input |
554d6586 MA |
89 | * visitor, then *@obj will be set to NULL for pointer types, and left |
90 | * unchanged for scalar types. | |
91 | * | |
92 | * Using an output or clone visitor with an incomplete object has | |
93 | * undefined behavior (other than a special case for visit_type_str() | |
94 | * treating NULL like ""), while the dealloc visitor safely handles | |
95 | * incomplete objects. Since input visitors never produce an | |
96 | * incomplete object, such an object is possible only by manual | |
97 | * construction. | |
adfb264c | 98 | * |
012d4c96 MA |
99 | * visit_type_FOO() returns true on success, false on error. |
100 | * | |
adfb264c | 101 | * For the QAPI object types (structs, unions, and alternates), there |
3777d36e MA |
102 | * is an additional generated function in qapi-visit-MODULE.h |
103 | * compatible with: | |
adfb264c | 104 | * |
012d4c96 | 105 | * bool visit_type_FOO_members(Visitor *v, FOO *obj, Error **errp); |
adfb264c EB |
106 | * |
107 | * for visiting the members of a type without also allocating the QAPI | |
012d4c96 | 108 | * struct. It also returns true on success, false on error. |
adfb264c | 109 | * |
3777d36e MA |
110 | * Additionally, QAPI pointer types (structs, unions, alternates, and |
111 | * lists) have a generated function in qapi-types-MODULE.h compatible | |
adfb264c EB |
112 | * with: |
113 | * | |
114 | * void qapi_free_FOO(FOO *obj); | |
115 | * | |
554d6586 MA |
116 | * Does nothing when @obj is NULL. |
117 | * | |
118 | * Such objects may also be used with macro | |
a15fcc3c EB |
119 | * |
120 | * Type *QAPI_CLONE(Type, src); | |
121 | * | |
554d6586 | 122 | * in order to perform a deep clone of @src. |
a15fcc3c | 123 | * |
554d6586 MA |
124 | * For QAPI types can that inherit from a base type, a function is |
125 | * generated for going from the derived type to the base type: | |
adfb264c EB |
126 | * |
127 | * BASE *qapi_CHILD_base(CHILD *obj); | |
128 | * | |
554d6586 | 129 | * Typical input visitor usage involves: |
adfb264c EB |
130 | * |
131 | * <example> | |
132 | * Foo *f; | |
133 | * Error *err = NULL; | |
134 | * Visitor *v; | |
135 | * | |
3b098d56 | 136 | * v = FOO_visitor_new(...); |
012d4c96 | 137 | * if (!visit_type_Foo(v, NULL, &f, &err)) { |
adfb264c EB |
138 | * ...handle error... |
139 | * } else { | |
140 | * ...use f... | |
141 | * } | |
3b098d56 | 142 | * visit_free(v); |
adfb264c EB |
143 | * qapi_free_Foo(f); |
144 | * </example> | |
145 | * | |
146 | * For a list, it is: | |
147 | * <example> | |
148 | * FooList *l; | |
149 | * Error *err = NULL; | |
150 | * Visitor *v; | |
151 | * | |
3b098d56 | 152 | * v = FOO_visitor_new(...); |
012d4c96 | 153 | * if (!visit_type_FooList(v, NULL, &l, &err)) { |
adfb264c EB |
154 | * ...handle error... |
155 | * } else { | |
156 | * for ( ; l; l = l->next) { | |
157 | * ...use l->value... | |
158 | * } | |
159 | * } | |
3b098d56 | 160 | * visit_free(v); |
adfb264c EB |
161 | * qapi_free_FooList(l); |
162 | * </example> | |
163 | * | |
554d6586 | 164 | * Typical output visitor usage: |
adfb264c EB |
165 | * |
166 | * <example> | |
167 | * Foo *f = ...obtain populated object... | |
adfb264c | 168 | * Visitor *v; |
3b098d56 | 169 | * Type *result; |
adfb264c | 170 | * |
3b098d56 | 171 | * v = FOO_visitor_new(..., &result); |
1f584248 MA |
172 | * visit_type_Foo(v, NULL, &f, &error_abort); |
173 | * visit_complete(v, &result); | |
3b098d56 | 174 | * visit_free(v); |
1f584248 | 175 | * ...use result... |
adfb264c EB |
176 | * </example> |
177 | * | |
adfb264c | 178 | * It is also possible to use the visitors to do a virtual walk, where |
554d6586 | 179 | * no actual QAPI object is present. In this situation, decisions |
adfb264c EB |
180 | * about what needs to be walked are made by the calling code, and |
181 | * structured visits are split between pairs of start and end methods | |
182 | * (where the end method must be called if the start function | |
183 | * succeeded, even if an intermediate visit encounters an error). | |
184 | * Thus, a virtual walk corresponding to '{ "list": [1, 2] }' looks | |
185 | * like: | |
186 | * | |
187 | * <example> | |
188 | * Visitor *v; | |
189 | * Error *err = NULL; | |
012d4c96 | 190 | * bool ok = false; |
adfb264c EB |
191 | * int value; |
192 | * | |
3b098d56 | 193 | * v = FOO_visitor_new(...); |
012d4c96 | 194 | * if (!visit_start_struct(v, NULL, NULL, 0, &err)) { |
adfb264c EB |
195 | * goto out; |
196 | * } | |
012d4c96 | 197 | * if (!visit_start_list(v, "list", NULL, 0, &err)) { |
adfb264c EB |
198 | * goto outobj; |
199 | * } | |
200 | * value = 1; | |
012d4c96 | 201 | * if (!visit_type_int(v, NULL, &value, &err)) { |
adfb264c EB |
202 | * goto outlist; |
203 | * } | |
204 | * value = 2; | |
012d4c96 | 205 | * if (!visit_type_int(v, NULL, &value, &err)) { |
adfb264c EB |
206 | * goto outlist; |
207 | * } | |
012d4c96 | 208 | * ok = true; |
adfb264c | 209 | * outlist: |
012d4c96 MA |
210 | * if (ok) { |
211 | * ok = visit_check_list(v, &err); | |
294c9066 | 212 | * } |
1158bb2a | 213 | * visit_end_list(v, NULL); |
012d4c96 MA |
214 | * if (ok) { |
215 | * ok = visit_check_struct(v, &err); | |
15c2f669 | 216 | * } |
adfb264c | 217 | * outobj: |
1158bb2a | 218 | * visit_end_struct(v, NULL); |
adfb264c | 219 | * out: |
3b098d56 | 220 | * visit_free(v); |
adfb264c | 221 | * </example> |
554d6586 MA |
222 | * |
223 | * This file provides helpers for use by the generated | |
224 | * visit_type_FOO(): visit_optional() for the 'has_member' field | |
225 | * associated with optional 'member' in the C struct, | |
226 | * visit_next_list() for advancing through a FooList linked list, and | |
227 | * visit_is_input() for cleaning up on failure. | |
adfb264c EB |
228 | */ |
229 | ||
230 | /*** Useful types ***/ | |
231 | ||
e65d89bf | 232 | /* This struct is layout-compatible with all other *List structs |
adfb264c | 233 | * created by the QAPI generator. It is used as a typical |
e65d89bf EB |
234 | * singly-linked list. */ |
235 | typedef struct GenericList { | |
2345c77c | 236 | struct GenericList *next; |
e65d89bf | 237 | char padding[]; |
2345c77c MR |
238 | } GenericList; |
239 | ||
dbf11922 | 240 | /* This struct is layout-compatible with all Alternate types |
adfb264c | 241 | * created by the QAPI generator. */ |
dbf11922 EB |
242 | typedef struct GenericAlternate { |
243 | QType type; | |
244 | char padding[]; | |
245 | } GenericAlternate; | |
246 | ||
2c0ef9f4 EB |
247 | /*** Visitor cleanup ***/ |
248 | ||
3b098d56 EB |
249 | /* |
250 | * Complete the visit, collecting any output. | |
251 | * | |
252 | * May only be called only once after a successful top-level | |
253 | * visit_type_FOO() or visit_end_ITEM(), and marks the end of the | |
254 | * visit. The @opaque pointer should match the output parameter | |
255 | * passed to the subtype_visitor_new() used to create an output | |
256 | * visitor, or NULL for any other visitor. Needed for output | |
257 | * visitors, but may also be called with other visitors. | |
258 | */ | |
259 | void visit_complete(Visitor *v, void *opaque); | |
260 | ||
2c0ef9f4 EB |
261 | /* |
262 | * Free @v and any resources it has tied up. | |
263 | * | |
264 | * May be called whether or not the visit has been successfully | |
265 | * completed, but should not be called until a top-level | |
266 | * visit_type_FOO() or visit_start_ITEM() has been performed on the | |
267 | * visitor. Safe if @v is NULL. | |
268 | */ | |
269 | void visit_free(Visitor *v); | |
270 | ||
271 | ||
adfb264c EB |
272 | /*** Visiting structures ***/ |
273 | ||
274 | /* | |
275 | * Start visiting an object @obj (struct or union). | |
276 | * | |
277 | * @name expresses the relationship of this object to its parent | |
278 | * container; see the general description of @name above. | |
279 | * | |
280 | * @obj must be non-NULL for a real walk, in which case @size | |
a15fcc3c EB |
281 | * determines how much memory an input or clone visitor will allocate |
282 | * into *@obj. @obj may also be NULL for a virtual walk, in which | |
283 | * case @size is ignored. | |
adfb264c | 284 | * |
c5460d5e | 285 | * On failure, set *@obj to NULL and store an error through @errp. |
1f584248 | 286 | * Can happen only when @v is an input visitor. |
adfb264c | 287 | * |
012d4c96 MA |
288 | * Return true on success, false on failure. |
289 | * | |
adfb264c EB |
290 | * After visit_start_struct() succeeds, the caller may visit its |
291 | * members one after the other, passing the member's name and address | |
292 | * within the struct. Finally, visit_end_struct() needs to be called | |
1158bb2a EB |
293 | * with the same @obj to clean up, even if intermediate visits fail. |
294 | * See the examples above. | |
adfb264c EB |
295 | * |
296 | * FIXME Should this be named visit_start_object, since it is also | |
297 | * used for QAPI unions, and maps to JSON objects? | |
298 | */ | |
012d4c96 | 299 | bool visit_start_struct(Visitor *v, const char *name, void **obj, |
337283df | 300 | size_t size, Error **errp); |
adfb264c EB |
301 | |
302 | /* | |
15c2f669 | 303 | * Prepare for completing an object visit. |
adfb264c | 304 | * |
1f584248 MA |
305 | * On failure, store an error through @errp. Can happen only when @v |
306 | * is an input visitor. | |
adfb264c | 307 | * |
012d4c96 MA |
308 | * Return true on success, false on failure. |
309 | * | |
15c2f669 EB |
310 | * Should be called prior to visit_end_struct() if all other |
311 | * intermediate visit steps were successful, to allow the visitor one | |
312 | * last chance to report errors. May be skipped on a cleanup path, | |
313 | * where there is no need to check for further errors. | |
314 | */ | |
012d4c96 | 315 | bool visit_check_struct(Visitor *v, Error **errp); |
15c2f669 EB |
316 | |
317 | /* | |
318 | * Complete an object visit started earlier. | |
319 | * | |
1158bb2a EB |
320 | * @obj must match what was passed to the paired visit_start_struct(). |
321 | * | |
adfb264c EB |
322 | * Must be called after any successful use of visit_start_struct(), |
323 | * even if intermediate processing was skipped due to errors, to allow | |
324 | * the backend to release any resources. Destroying the visitor early | |
2c0ef9f4 | 325 | * with visit_free() behaves as if this was implicitly called. |
adfb264c | 326 | */ |
1158bb2a | 327 | void visit_end_struct(Visitor *v, void **obj); |
08f9541d | 328 | |
adfb264c EB |
329 | |
330 | /*** Visiting lists ***/ | |
331 | ||
332 | /* | |
333 | * Start visiting a list. | |
334 | * | |
335 | * @name expresses the relationship of this list to its parent | |
336 | * container; see the general description of @name above. | |
337 | * | |
d9f62dde | 338 | * @list must be non-NULL for a real walk, in which case @size |
a15fcc3c EB |
339 | * determines how much memory an input or clone visitor will allocate |
340 | * into *@list (at least sizeof(GenericList)). Some visitors also | |
341 | * allow @list to be NULL for a virtual walk, in which case @size is | |
d9f62dde EB |
342 | * ignored. |
343 | * | |
c5460d5e | 344 | * On failure, set *@list to NULL and store an error through @errp. |
1f584248 | 345 | * Can happen only when @v is an input visitor. |
adfb264c | 346 | * |
012d4c96 MA |
347 | * Return true on success, false on failure. |
348 | * | |
adfb264c | 349 | * After visit_start_list() succeeds, the caller may visit its members |
782586c7 | 350 | * one after the other. A real visit (where @list is non-NULL) uses |
d9f62dde | 351 | * visit_next_list() for traversing the linked list, while a virtual |
782586c7 | 352 | * visit (where @list is NULL) uses other means. For each list |
d9f62dde EB |
353 | * element, call the appropriate visit_type_FOO() with name set to |
354 | * NULL and obj set to the address of the value member of the list | |
1158bb2a EB |
355 | * element. Finally, visit_end_list() needs to be called with the |
356 | * same @list to clean up, even if intermediate visits fail. See the | |
357 | * examples above. | |
adfb264c | 358 | */ |
012d4c96 | 359 | bool visit_start_list(Visitor *v, const char *name, GenericList **list, |
d9f62dde | 360 | size_t size, Error **errp); |
adfb264c EB |
361 | |
362 | /* | |
363 | * Iterate over a GenericList during a non-virtual list visit. | |
364 | * | |
365 | * @size represents the size of a linked list node (at least | |
366 | * sizeof(GenericList)). | |
367 | * | |
d9f62dde EB |
368 | * @tail must not be NULL; on the first call, @tail is the value of |
369 | * *list after visit_start_list(), and on subsequent calls @tail must | |
370 | * be the previously returned value. Should be called in a loop until | |
81b49004 MA |
371 | * a NULL return; for each non-NULL return, the caller then calls the |
372 | * appropriate visit_type_*() for the element type of the list, with | |
373 | * that function's name parameter set to NULL and obj set to the | |
374 | * address of @tail->value. | |
adfb264c | 375 | */ |
d9f62dde | 376 | GenericList *visit_next_list(Visitor *v, GenericList *tail, size_t size); |
adfb264c | 377 | |
a4a1c70d MA |
378 | /* |
379 | * Prepare for completing a list visit. | |
380 | * | |
1f584248 MA |
381 | * On failure, store an error through @errp. Can happen only when @v |
382 | * is an input visitor. | |
a4a1c70d | 383 | * |
012d4c96 MA |
384 | * Return true on success, false on failure. |
385 | * | |
a4a1c70d MA |
386 | * Should be called prior to visit_end_list() if all other |
387 | * intermediate visit steps were successful, to allow the visitor one | |
388 | * last chance to report errors. May be skipped on a cleanup path, | |
389 | * where there is no need to check for further errors. | |
390 | */ | |
012d4c96 | 391 | bool visit_check_list(Visitor *v, Error **errp); |
a4a1c70d | 392 | |
adfb264c EB |
393 | /* |
394 | * Complete a list visit started earlier. | |
395 | * | |
1158bb2a EB |
396 | * @list must match what was passed to the paired visit_start_list(). |
397 | * | |
adfb264c EB |
398 | * Must be called after any successful use of visit_start_list(), even |
399 | * if intermediate processing was skipped due to errors, to allow the | |
400 | * backend to release any resources. Destroying the visitor early | |
2c0ef9f4 | 401 | * with visit_free() behaves as if this was implicitly called. |
adfb264c | 402 | */ |
1158bb2a | 403 | void visit_end_list(Visitor *v, void **list); |
5cdc8831 | 404 | |
adfb264c EB |
405 | |
406 | /*** Visiting alternates ***/ | |
407 | ||
dbf11922 | 408 | /* |
adfb264c | 409 | * Start the visit of an alternate @obj. |
dbf11922 | 410 | * |
adfb264c EB |
411 | * @name expresses the relationship of this alternate to its parent |
412 | * container; see the general description of @name above. | |
dbf11922 | 413 | * |
a15fcc3c EB |
414 | * @obj must not be NULL. Input and clone visitors use @size to |
415 | * determine how much memory to allocate into *@obj, then determine | |
c5460d5e MA |
416 | * the qtype of the next thing to be visited, and store it in |
417 | * (*@obj)->type. Other visitors leave @obj unchanged. | |
418 | * | |
419 | * On failure, set *@obj to NULL and store an error through @errp. | |
1f584248 | 420 | * Can happen only when @v is an input visitor. |
dbf11922 | 421 | * |
012d4c96 MA |
422 | * Return true on success, false on failure. |
423 | * | |
1158bb2a EB |
424 | * If successful, this must be paired with visit_end_alternate() with |
425 | * the same @obj to clean up, even if visiting the contents of the | |
426 | * alternate fails. | |
dbf11922 | 427 | */ |
012d4c96 | 428 | bool visit_start_alternate(Visitor *v, const char *name, |
dbf11922 | 429 | GenericAlternate **obj, size_t size, |
60390d2d | 430 | Error **errp); |
dbf11922 EB |
431 | |
432 | /* | |
433 | * Finish visiting an alternate type. | |
434 | * | |
1158bb2a EB |
435 | * @obj must match what was passed to the paired visit_start_alternate(). |
436 | * | |
adfb264c EB |
437 | * Must be called after any successful use of visit_start_alternate(), |
438 | * even if intermediate processing was skipped due to errors, to allow | |
439 | * the backend to release any resources. Destroying the visitor early | |
2c0ef9f4 | 440 | * with visit_free() behaves as if this was implicitly called. |
dbf11922 | 441 | * |
dbf11922 | 442 | */ |
1158bb2a | 443 | void visit_end_alternate(Visitor *v, void **obj); |
dbf11922 | 444 | |
adfb264c EB |
445 | |
446 | /*** Other helpers ***/ | |
447 | ||
448 | /* | |
449 | * Does optional struct member @name need visiting? | |
450 | * | |
451 | * @name must not be NULL. This function is only useful between | |
452 | * visit_start_struct() and visit_end_struct(), since only objects | |
453 | * have optional keys. | |
454 | * | |
455 | * @present points to the address of the optional member's has_ flag. | |
456 | * | |
457 | * Input visitors set *@present according to input; other visitors | |
458 | * leave it unchanged. In either case, return *@present for | |
459 | * convenience. | |
5cdc8831 | 460 | */ |
51e72bc1 | 461 | bool visit_optional(Visitor *v, const char *name, bool *present); |
0426d53c | 462 | |
db291641 | 463 | /* |
a1307285 MA |
464 | * Should we reject member @name due to policy? |
465 | * | |
466 | * @special_features is the member's special features encoded as a | |
467 | * bitset of QapiSpecialFeature. | |
db291641 MA |
468 | * |
469 | * @name must not be NULL. This function is only useful between | |
470 | * visit_start_struct() and visit_end_struct(), since only objects | |
471 | * have deprecated members. | |
472 | */ | |
a1307285 MA |
473 | bool visit_policy_reject(Visitor *v, const char *name, |
474 | unsigned special_features, Error **errp); | |
db291641 | 475 | |
91fa93e5 | 476 | /* |
a1307285 MA |
477 | * |
478 | * Should we skip member @name due to policy? | |
479 | * | |
480 | * @special_features is the member's special features encoded as a | |
481 | * bitset of QapiSpecialFeature. | |
91fa93e5 MA |
482 | * |
483 | * @name must not be NULL. This function is only useful between | |
484 | * visit_start_struct() and visit_end_struct(), since only objects | |
485 | * have deprecated members. | |
486 | */ | |
a1307285 MA |
487 | bool visit_policy_skip(Visitor *v, const char *name, |
488 | unsigned special_features); | |
91fa93e5 | 489 | |
ed29bb28 MA |
490 | /* |
491 | * Set policy for handling deprecated management interfaces. | |
492 | * | |
493 | * Intended use: call visit_set_policy(v, &compat_policy) when | |
494 | * visiting management interface input or output. | |
495 | */ | |
496 | void visit_set_policy(Visitor *v, CompatPolicy *policy); | |
497 | ||
983f52d4 EB |
498 | /* |
499 | * Visit an enum value. | |
500 | * | |
adfb264c EB |
501 | * @name expresses the relationship of this enum to its parent |
502 | * container; see the general description of @name above. | |
503 | * | |
504 | * @obj must be non-NULL. Input visitors parse input and set *@obj to | |
505 | * the enumeration value, leaving @obj unchanged on error; other | |
506 | * visitors use *@obj but leave it unchanged. | |
507 | * | |
508 | * Currently, all input visitors parse text input, and all output | |
509 | * visitors produce text output. The mapping between enumeration | |
c5460d5e MA |
510 | * values and strings is done by the visitor core, using @lookup. |
511 | * | |
1f584248 MA |
512 | * On failure, store an error through @errp. Can happen only when @v |
513 | * is an input visitor. | |
983f52d4 | 514 | * |
012d4c96 MA |
515 | * Return true on success, false on failure. |
516 | * | |
983f52d4 EB |
517 | * May call visit_type_str() under the hood, and the enum visit may |
518 | * fail even if the corresponding string visit succeeded; this implies | |
1f584248 MA |
519 | * that an input visitor's visit_type_str() must have no unwelcome |
520 | * side effects. | |
983f52d4 | 521 | */ |
012d4c96 | 522 | bool visit_type_enum(Visitor *v, const char *name, int *obj, |
f7abe0ec | 523 | const QEnumLookup *lookup, Error **errp); |
983f52d4 | 524 | |
68ab47e4 EB |
525 | /* |
526 | * Check if visitor is an input visitor. | |
527 | */ | |
528 | bool visit_is_input(Visitor *v); | |
529 | ||
8e08bf4e MA |
530 | /* |
531 | * Check if visitor is a dealloc visitor. | |
532 | */ | |
533 | bool visit_is_dealloc(Visitor *v); | |
534 | ||
adfb264c EB |
535 | /*** Visiting built-in types ***/ |
536 | ||
537 | /* | |
538 | * Visit an integer value. | |
539 | * | |
540 | * @name expresses the relationship of this integer to its parent | |
541 | * container; see the general description of @name above. | |
542 | * | |
543 | * @obj must be non-NULL. Input visitors set *@obj to the value; | |
544 | * other visitors will leave *@obj unchanged. | |
c5460d5e | 545 | * |
1f584248 MA |
546 | * On failure, store an error through @errp. Can happen only when @v |
547 | * is an input visitor. | |
012d4c96 MA |
548 | * |
549 | * Return true on success, false on failure. | |
adfb264c | 550 | */ |
012d4c96 | 551 | bool visit_type_int(Visitor *v, const char *name, int64_t *obj, Error **errp); |
adfb264c EB |
552 | |
553 | /* | |
554 | * Visit a uint8_t value. | |
555 | * Like visit_type_int(), except clamps the value to uint8_t range. | |
556 | */ | |
012d4c96 | 557 | bool visit_type_uint8(Visitor *v, const char *name, uint8_t *obj, |
51e72bc1 | 558 | Error **errp); |
adfb264c EB |
559 | |
560 | /* | |
561 | * Visit a uint16_t value. | |
562 | * Like visit_type_int(), except clamps the value to uint16_t range. | |
563 | */ | |
012d4c96 | 564 | bool visit_type_uint16(Visitor *v, const char *name, uint16_t *obj, |
51e72bc1 | 565 | Error **errp); |
adfb264c EB |
566 | |
567 | /* | |
568 | * Visit a uint32_t value. | |
569 | * Like visit_type_int(), except clamps the value to uint32_t range. | |
570 | */ | |
012d4c96 | 571 | bool visit_type_uint32(Visitor *v, const char *name, uint32_t *obj, |
51e72bc1 | 572 | Error **errp); |
adfb264c EB |
573 | |
574 | /* | |
575 | * Visit a uint64_t value. | |
576 | * Like visit_type_int(), except clamps the value to uint64_t range, | |
577 | * that is, ensures it is unsigned. | |
578 | */ | |
012d4c96 | 579 | bool visit_type_uint64(Visitor *v, const char *name, uint64_t *obj, |
51e72bc1 | 580 | Error **errp); |
adfb264c EB |
581 | |
582 | /* | |
583 | * Visit an int8_t value. | |
584 | * Like visit_type_int(), except clamps the value to int8_t range. | |
585 | */ | |
012d4c96 | 586 | bool visit_type_int8(Visitor *v, const char *name, int8_t *obj, Error **errp); |
adfb264c EB |
587 | |
588 | /* | |
589 | * Visit an int16_t value. | |
590 | * Like visit_type_int(), except clamps the value to int16_t range. | |
591 | */ | |
012d4c96 | 592 | bool visit_type_int16(Visitor *v, const char *name, int16_t *obj, |
51e72bc1 | 593 | Error **errp); |
adfb264c EB |
594 | |
595 | /* | |
596 | * Visit an int32_t value. | |
597 | * Like visit_type_int(), except clamps the value to int32_t range. | |
598 | */ | |
012d4c96 | 599 | bool visit_type_int32(Visitor *v, const char *name, int32_t *obj, |
51e72bc1 | 600 | Error **errp); |
adfb264c EB |
601 | |
602 | /* | |
603 | * Visit an int64_t value. | |
604 | * Identical to visit_type_int(). | |
605 | */ | |
012d4c96 | 606 | bool visit_type_int64(Visitor *v, const char *name, int64_t *obj, |
51e72bc1 | 607 | Error **errp); |
adfb264c EB |
608 | |
609 | /* | |
610 | * Visit a uint64_t value. | |
611 | * Like visit_type_uint64(), except that some visitors may choose to | |
612 | * recognize additional syntax, such as suffixes for easily scaling | |
613 | * values. | |
614 | */ | |
012d4c96 | 615 | bool visit_type_size(Visitor *v, const char *name, uint64_t *obj, |
51e72bc1 | 616 | Error **errp); |
adfb264c EB |
617 | |
618 | /* | |
619 | * Visit a boolean value. | |
620 | * | |
621 | * @name expresses the relationship of this boolean to its parent | |
622 | * container; see the general description of @name above. | |
623 | * | |
624 | * @obj must be non-NULL. Input visitors set *@obj to the value; | |
625 | * other visitors will leave *@obj unchanged. | |
c5460d5e | 626 | * |
1f584248 MA |
627 | * On failure, store an error through @errp. Can happen only when @v |
628 | * is an input visitor. | |
012d4c96 MA |
629 | * |
630 | * Return true on success, false on failure. | |
adfb264c | 631 | */ |
012d4c96 | 632 | bool visit_type_bool(Visitor *v, const char *name, bool *obj, Error **errp); |
adfb264c EB |
633 | |
634 | /* | |
635 | * Visit a string value. | |
636 | * | |
637 | * @name expresses the relationship of this string to its parent | |
638 | * container; see the general description of @name above. | |
639 | * | |
a15fcc3c EB |
640 | * @obj must be non-NULL. Input and clone visitors set *@obj to the |
641 | * value (always using "" rather than NULL for an empty string). | |
642 | * Other visitors leave *@obj unchanged, and commonly treat NULL like | |
643 | * "". | |
adfb264c EB |
644 | * |
645 | * It is safe to cast away const when preparing a (const char *) value | |
646 | * into @obj for use by an output visitor. | |
647 | * | |
c5460d5e | 648 | * On failure, set *@obj to NULL and store an error through @errp. |
1f584248 | 649 | * Can happen only when @v is an input visitor. |
c5460d5e | 650 | * |
012d4c96 MA |
651 | * Return true on success, false on failure. |
652 | * | |
adfb264c EB |
653 | * FIXME: Callers that try to output NULL *obj should not be allowed. |
654 | */ | |
012d4c96 | 655 | bool visit_type_str(Visitor *v, const char *name, char **obj, Error **errp); |
adfb264c EB |
656 | |
657 | /* | |
658 | * Visit a number (i.e. double) value. | |
659 | * | |
660 | * @name expresses the relationship of this number to its parent | |
661 | * container; see the general description of @name above. | |
662 | * | |
663 | * @obj must be non-NULL. Input visitors set *@obj to the value; | |
664 | * other visitors will leave *@obj unchanged. Visitors should | |
665 | * document if infinity or NaN are not permitted. | |
c5460d5e | 666 | * |
1f584248 MA |
667 | * On failure, store an error through @errp. Can happen only when @v |
668 | * is an input visitor. | |
012d4c96 MA |
669 | * |
670 | * Return true on success, false on failure. | |
adfb264c | 671 | */ |
012d4c96 | 672 | bool visit_type_number(Visitor *v, const char *name, double *obj, |
51e72bc1 | 673 | Error **errp); |
adfb264c EB |
674 | |
675 | /* | |
676 | * Visit an arbitrary value. | |
677 | * | |
678 | * @name expresses the relationship of this value to its parent | |
679 | * container; see the general description of @name above. | |
680 | * | |
681 | * @obj must be non-NULL. Input visitors set *@obj to the value; | |
682 | * other visitors will leave *@obj unchanged. *@obj must be non-NULL | |
683 | * for output visitors. | |
8339fa26 | 684 | * |
c5460d5e | 685 | * On failure, set *@obj to NULL and store an error through @errp. |
1f584248 | 686 | * Can happen only when @v is an input visitor. |
c5460d5e | 687 | * |
012d4c96 MA |
688 | * Return true on success, false on failure. |
689 | * | |
8339fa26 MA |
690 | * Note that some kinds of input can't express arbitrary QObject. |
691 | * E.g. the visitor returned by qobject_input_visitor_new_keyval() | |
692 | * can't create numbers or booleans, only strings. | |
adfb264c | 693 | */ |
012d4c96 | 694 | bool visit_type_any(Visitor *v, const char *name, QObject **obj, Error **errp); |
2345c77c | 695 | |
3bc97fd5 EB |
696 | /* |
697 | * Visit a JSON null value. | |
698 | * | |
699 | * @name expresses the relationship of the null value to its parent | |
700 | * container; see the general description of @name above. | |
701 | * | |
d2f95f4d MA |
702 | * @obj must be non-NULL. Input visitors set *@obj to the value; |
703 | * other visitors ignore *@obj. | |
c5460d5e MA |
704 | * |
705 | * On failure, set *@obj to NULL and store an error through @errp. | |
1f584248 | 706 | * Can happen only when @v is an input visitor. |
012d4c96 MA |
707 | * |
708 | * Return true on success, false on failure. | |
3bc97fd5 | 709 | */ |
012d4c96 | 710 | bool visit_type_null(Visitor *v, const char *name, QNull **obj, |
d2f95f4d | 711 | Error **errp); |
3bc97fd5 | 712 | |
2345c77c | 713 | #endif |