qapi: Simplify visits of optional fields
None of the visitor callbacks would set an error when testing if an optional field was present; make this part of the interface contract by eliminating the errp argument. The resulting generated code has a nice diff: |- visit_optional(v, &has_fdset_id, "fdset-id", &err); |- if (err) { |- goto out; |- } |+ visit_optional(v, &has_fdset_id, "fdset-id"); | if (has_fdset_id) { | visit_type_int(v, &fdset_id, "fdset-id", &err); | if (err) { | goto out; | } | } Signed-off-by: Eric Blake <eblake@redhat.com> Message-Id: <1449033659-25497-9-git-send-email-eblake@redhat.com> Signed-off-by: Markus Armbruster <armbru@redhat.com>
This commit is contained in:
parent
d00341af38
commit
5cdc8831a7
@ -44,9 +44,8 @@ struct Visitor
|
|||||||
void (*type_any)(Visitor *v, QObject **obj, const char *name,
|
void (*type_any)(Visitor *v, QObject **obj, const char *name,
|
||||||
Error **errp);
|
Error **errp);
|
||||||
|
|
||||||
/* May be NULL */
|
/* May be NULL; most useful for input visitors. */
|
||||||
void (*optional)(Visitor *v, bool *present, const char *name,
|
void (*optional)(Visitor *v, bool *present, const char *name);
|
||||||
Error **errp);
|
|
||||||
|
|
||||||
void (*type_uint8)(Visitor *v, uint8_t *obj, const char *name, Error **errp);
|
void (*type_uint8)(Visitor *v, uint8_t *obj, const char *name, Error **errp);
|
||||||
void (*type_uint16)(Visitor *v, uint16_t *obj, const char *name, Error **errp);
|
void (*type_uint16)(Visitor *v, uint16_t *obj, const char *name, Error **errp);
|
||||||
|
@ -36,8 +36,14 @@ void visit_end_implicit_struct(Visitor *v, Error **errp);
|
|||||||
void visit_start_list(Visitor *v, const char *name, Error **errp);
|
void visit_start_list(Visitor *v, const char *name, Error **errp);
|
||||||
GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp);
|
GenericList *visit_next_list(Visitor *v, GenericList **list, Error **errp);
|
||||||
void visit_end_list(Visitor *v, Error **errp);
|
void visit_end_list(Visitor *v, Error **errp);
|
||||||
void visit_optional(Visitor *v, bool *present, const char *name,
|
|
||||||
Error **errp);
|
/**
|
||||||
|
* Check if an optional member @name of an object needs visiting.
|
||||||
|
* For input visitors, set *@present according to whether the
|
||||||
|
* corresponding visit_type_*() needs calling; for other visitors,
|
||||||
|
* leave *@present unchanged.
|
||||||
|
*/
|
||||||
|
void visit_optional(Visitor *v, bool *present, const char *name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine the qtype of the item @name in the current object visit.
|
* Determine the qtype of the item @name in the current object visit.
|
||||||
|
@ -488,7 +488,7 @@ opts_type_size(Visitor *v, uint64_t *obj, const char *name, Error **errp)
|
|||||||
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
opts_optional(Visitor *v, bool *present, const char *name, Error **errp)
|
opts_optional(Visitor *v, bool *present, const char *name)
|
||||||
{
|
{
|
||||||
OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
|
OptsVisitor *ov = DO_UPCAST(OptsVisitor, visitor, v);
|
||||||
|
|
||||||
|
@ -73,11 +73,10 @@ void visit_end_union(Visitor *v, bool data_present, Error **errp)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void visit_optional(Visitor *v, bool *present, const char *name,
|
void visit_optional(Visitor *v, bool *present, const char *name)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
if (v->optional) {
|
if (v->optional) {
|
||||||
v->optional(v, present, name, errp);
|
v->optional(v, present, name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,8 +303,7 @@ static void qmp_input_type_any(Visitor *v, QObject **obj, const char *name,
|
|||||||
*obj = qobj;
|
*obj = qobj;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void qmp_input_optional(Visitor *v, bool *present, const char *name,
|
static void qmp_input_optional(Visitor *v, bool *present, const char *name)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
QmpInputVisitor *qiv = to_qiv(v);
|
QmpInputVisitor *qiv = to_qiv(v);
|
||||||
QObject *qobj = qmp_input_get_object(qiv, name, true);
|
QObject *qobj = qmp_input_get_object(qiv, name, true);
|
||||||
|
@ -299,8 +299,7 @@ static void parse_type_number(Visitor *v, double *obj, const char *name,
|
|||||||
*obj = val;
|
*obj = val;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void parse_optional(Visitor *v, bool *present, const char *name,
|
static void parse_optional(Visitor *v, bool *present, const char *name)
|
||||||
Error **errp)
|
|
||||||
{
|
{
|
||||||
StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
|
StringInputVisitor *siv = DO_UPCAST(StringInputVisitor, visitor, v);
|
||||||
|
|
||||||
|
@ -1656,15 +1656,11 @@ def gen_visit_fields(members, prefix='', need_cast=False, skiperr=False):
|
|||||||
for memb in members:
|
for memb in members:
|
||||||
if memb.optional:
|
if memb.optional:
|
||||||
ret += mcgen('''
|
ret += mcgen('''
|
||||||
visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s", %(errp)s);
|
visit_optional(v, &%(prefix)shas_%(c_name)s, "%(name)s");
|
||||||
|
if (%(prefix)shas_%(c_name)s) {
|
||||||
''',
|
''',
|
||||||
prefix=prefix, c_name=c_name(memb.name),
|
prefix=prefix, c_name=c_name(memb.name),
|
||||||
name=memb.name, errp=errparg)
|
name=memb.name, errp=errparg)
|
||||||
ret += gen_err_check(skiperr=skiperr)
|
|
||||||
ret += mcgen('''
|
|
||||||
if (%(prefix)shas_%(c_name)s) {
|
|
||||||
''',
|
|
||||||
prefix=prefix, c_name=c_name(memb.name))
|
|
||||||
push_indent()
|
push_indent()
|
||||||
|
|
||||||
# Ugly: sometimes we need to cast away const
|
# Ugly: sometimes we need to cast away const
|
||||||
|
Loading…
Reference in New Issue
Block a user