xref: /openbmc/qemu/qapi/qapi-visit-core.c (revision 2ae2bce7)
1 /*
2  * Core Definitions for QAPI Visitor Classes
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10  * See the COPYING.LIB file in the top-level directory.
11  *
12  */
13 
14 #include "qapi/qapi-visit-core.h"
15 #include "qapi/qapi-visit-impl.h"
16 
17 void visit_start_handle(Visitor *v, void **obj, const char *kind,
18                         const char *name, Error **errp)
19 {
20     if (!error_is_set(errp) && v->start_handle) {
21         v->start_handle(v, obj, kind, name, errp);
22     }
23 }
24 
25 void visit_end_handle(Visitor *v, Error **errp)
26 {
27     if (!error_is_set(errp) && v->end_handle) {
28         v->end_handle(v, errp);
29     }
30 }
31 
32 void visit_start_struct(Visitor *v, void **obj, const char *kind,
33                         const char *name, size_t size, Error **errp)
34 {
35     if (!error_is_set(errp)) {
36         v->start_struct(v, obj, kind, name, size, errp);
37     }
38 }
39 
40 void visit_end_struct(Visitor *v, Error **errp)
41 {
42     if (!error_is_set(errp)) {
43         v->end_struct(v, errp);
44     }
45 }
46 
47 void visit_start_list(Visitor *v, const char *name, Error **errp)
48 {
49     if (!error_is_set(errp)) {
50         v->start_list(v, name, errp);
51     }
52 }
53 
54 GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp)
55 {
56     if (!error_is_set(errp)) {
57         return v->next_list(v, list, errp);
58     }
59 
60     return 0;
61 }
62 
63 void visit_end_list(Visitor *v, Error **errp)
64 {
65     if (!error_is_set(errp)) {
66         v->end_list(v, errp);
67     }
68 }
69 
70 void visit_start_optional(Visitor *v, bool *present, const char *name,
71                           Error **errp)
72 {
73     if (!error_is_set(errp) && v->start_optional) {
74         v->start_optional(v, present, name, errp);
75     }
76 }
77 
78 void visit_end_optional(Visitor *v, Error **errp)
79 {
80     if (!error_is_set(errp) && v->end_optional) {
81         v->end_optional(v, errp);
82     }
83 }
84 
85 void visit_type_enum(Visitor *v, int *obj, const char *strings[],
86                      const char *kind, const char *name, Error **errp)
87 {
88     if (!error_is_set(errp)) {
89         v->type_enum(v, obj, strings, kind, name, errp);
90     }
91 }
92 
93 void visit_type_int(Visitor *v, int64_t *obj, const char *name, Error **errp)
94 {
95     if (!error_is_set(errp)) {
96         v->type_int(v, obj, name, errp);
97     }
98 }
99 
100 void visit_type_bool(Visitor *v, bool *obj, const char *name, Error **errp)
101 {
102     if (!error_is_set(errp)) {
103         v->type_bool(v, obj, name, errp);
104     }
105 }
106 
107 void visit_type_str(Visitor *v, char **obj, const char *name, Error **errp)
108 {
109     if (!error_is_set(errp)) {
110         v->type_str(v, obj, name, errp);
111     }
112 }
113 
114 void visit_type_number(Visitor *v, double *obj, const char *name, Error **errp)
115 {
116     if (!error_is_set(errp)) {
117         v->type_number(v, obj, name, errp);
118     }
119 }
120 
121 void output_type_enum(Visitor *v, int *obj, const char *strings[],
122                       const char *kind, const char *name,
123                       Error **errp)
124 {
125     int i = 0;
126     int value = *obj;
127     char *enum_str;
128 
129     assert(strings);
130     while (strings[i++] != NULL);
131     if (value < 0 || value >= i - 1) {
132         error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
133         return;
134     }
135 
136     enum_str = (char *)strings[value];
137     visit_type_str(v, &enum_str, name, errp);
138 }
139 
140 void input_type_enum(Visitor *v, int *obj, const char *strings[],
141                      const char *kind, const char *name,
142                      Error **errp)
143 {
144     int64_t value = 0;
145     char *enum_str;
146 
147     assert(strings);
148 
149     visit_type_str(v, &enum_str, name, errp);
150     if (error_is_set(errp)) {
151         return;
152     }
153 
154     while (strings[value] != NULL) {
155         if (strcmp(strings[value], enum_str) == 0) {
156             break;
157         }
158         value++;
159     }
160 
161     if (strings[value] == NULL) {
162         error_set(errp, QERR_INVALID_PARAMETER, name ? name : "null");
163         g_free(enum_str);
164         return;
165     }
166 
167     g_free(enum_str);
168     *obj = value;
169 }
170