translate-all: Avoid -Werror=switch-bool
[qemu/ar7.git] / qapi / qapi-clone-visitor.c
blob34086cbfc00aff1bbb51b726ff9a30969a234890
1 /*
2 * Copy one QAPI object to another
4 * Copyright (C) 2016 Red Hat, Inc.
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
9 */
11 #include "qemu/osdep.h"
12 #include "qapi/clone-visitor.h"
13 #include "qapi/visitor-impl.h"
14 #include "qapi/error.h"
16 struct QapiCloneVisitor {
17 Visitor visitor;
18 size_t depth;
21 static QapiCloneVisitor *to_qcv(Visitor *v)
23 return container_of(v, QapiCloneVisitor, visitor);
26 static void qapi_clone_start_struct(Visitor *v, const char *name, void **obj,
27 size_t size, Error **errp)
29 QapiCloneVisitor *qcv = to_qcv(v);
31 if (!obj) {
32 assert(qcv->depth);
33 /* Only possible when visiting an alternate's object
34 * branch. Nothing further to do here, since the earlier
35 * visit_start_alternate() already copied memory. */
36 return;
39 *obj = g_memdup(*obj, size);
40 qcv->depth++;
43 static void qapi_clone_end(Visitor *v, void **obj)
45 QapiCloneVisitor *qcv = to_qcv(v);
47 assert(qcv->depth);
48 if (obj) {
49 qcv->depth--;
53 static void qapi_clone_start_list(Visitor *v, const char *name,
54 GenericList **listp, size_t size,
55 Error **errp)
57 qapi_clone_start_struct(v, name, (void **)listp, size, errp);
60 static GenericList *qapi_clone_next_list(Visitor *v, GenericList *tail,
61 size_t size)
63 QapiCloneVisitor *qcv = to_qcv(v);
65 assert(qcv->depth);
66 /* Unshare the tail of the list cloned by g_memdup() */
67 tail->next = g_memdup(tail->next, size);
68 return tail->next;
71 static void qapi_clone_start_alternate(Visitor *v, const char *name,
72 GenericAlternate **obj, size_t size,
73 bool promote_int, Error **errp)
75 qapi_clone_start_struct(v, name, (void **)obj, size, errp);
78 static void qapi_clone_type_int64(Visitor *v, const char *name, int64_t *obj,
79 Error **errp)
81 QapiCloneVisitor *qcv = to_qcv(v);
83 assert(qcv->depth);
84 /* Value was already cloned by g_memdup() */
87 static void qapi_clone_type_uint64(Visitor *v, const char *name,
88 uint64_t *obj, Error **errp)
90 QapiCloneVisitor *qcv = to_qcv(v);
92 assert(qcv->depth);
93 /* Value was already cloned by g_memdup() */
96 static void qapi_clone_type_bool(Visitor *v, const char *name, bool *obj,
97 Error **errp)
99 QapiCloneVisitor *qcv = to_qcv(v);
101 assert(qcv->depth);
102 /* Value was already cloned by g_memdup() */
105 static void qapi_clone_type_str(Visitor *v, const char *name, char **obj,
106 Error **errp)
108 QapiCloneVisitor *qcv = to_qcv(v);
110 assert(qcv->depth);
112 * Pointer was already cloned by g_memdup; create fresh copy.
113 * Note that as long as qobject-output-visitor accepts NULL instead of
114 * "", then we must do likewise. However, we want to obey the
115 * input visitor semantics of never producing NULL when the empty
116 * string is intended.
118 *obj = g_strdup(*obj ?: "");
121 static void qapi_clone_type_number(Visitor *v, const char *name, double *obj,
122 Error **errp)
124 QapiCloneVisitor *qcv = to_qcv(v);
126 assert(qcv->depth);
127 /* Value was already cloned by g_memdup() */
130 static void qapi_clone_type_null(Visitor *v, const char *name, Error **errp)
132 QapiCloneVisitor *qcv = to_qcv(v);
134 assert(qcv->depth);
135 /* Nothing to do */
138 static void qapi_clone_free(Visitor *v)
140 g_free(v);
143 static Visitor *qapi_clone_visitor_new(void)
145 QapiCloneVisitor *v;
147 v = g_malloc0(sizeof(*v));
149 v->visitor.type = VISITOR_CLONE;
150 v->visitor.start_struct = qapi_clone_start_struct;
151 v->visitor.end_struct = qapi_clone_end;
152 v->visitor.start_list = qapi_clone_start_list;
153 v->visitor.next_list = qapi_clone_next_list;
154 v->visitor.end_list = qapi_clone_end;
155 v->visitor.start_alternate = qapi_clone_start_alternate;
156 v->visitor.end_alternate = qapi_clone_end;
157 v->visitor.type_int64 = qapi_clone_type_int64;
158 v->visitor.type_uint64 = qapi_clone_type_uint64;
159 v->visitor.type_bool = qapi_clone_type_bool;
160 v->visitor.type_str = qapi_clone_type_str;
161 v->visitor.type_number = qapi_clone_type_number;
162 v->visitor.type_null = qapi_clone_type_null;
163 v->visitor.free = qapi_clone_free;
165 return &v->visitor;
168 void *qapi_clone(const void *src, void (*visit_type)(Visitor *, const char *,
169 void **, Error **))
171 Visitor *v;
172 void *dst = (void *) src; /* Cast away const */
174 if (!src) {
175 return NULL;
178 v = qapi_clone_visitor_new();
179 visit_type(v, NULL, &dst, &error_abort);
180 visit_free(v);
181 return dst;