xref: /qemu/include/qapi/clone-visitor.h (revision 01b2ffce)
1 /*
2  * Clone Visitor
3  *
4  * Copyright (C) 2016 Red Hat, Inc.
5  *
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.
8  *
9  */
10 
11 #ifndef QAPI_CLONE_VISITOR_H
12 #define QAPI_CLONE_VISITOR_H
13 
14 #include "qemu/typedefs.h"
15 #include "qapi/visitor.h"
16 #include "qapi-visit.h"
17 
18 /*
19  * The clone visitor is for direct use only by the QAPI_CLONE() macro;
20  * it requires that the root visit occur on an object, list, or
21  * alternate, and is not usable directly on built-in QAPI types.
22  */
23 typedef struct QapiCloneVisitor QapiCloneVisitor;
24 
25 void *qapi_clone(const void *src, void (*visit_type)(Visitor *, const char *,
26                                                      void **, Error **));
27 void qapi_clone_members(void *dst, const void *src, size_t sz,
28                         void (*visit_type_members)(Visitor *, void *,
29                                                    Error **));
30 
31 /*
32  * Deep-clone QAPI object @src of the given @type, and return the result.
33  *
34  * Not usable on QAPI scalars (integers, strings, enums), nor on a
35  * QAPI object that references the 'any' type.  Safe when @src is NULL.
36  */
37 #define QAPI_CLONE(type, src)                                           \
38     ((type *)qapi_clone(src,                                            \
39                         (void (*)(Visitor *, const char *, void**,      \
40                                   Error **))visit_type_ ## type))
41 
42 /*
43  * Copy deep clones of @type members from @src to @dst.
44  *
45  * Not usable on QAPI scalars (integers, strings, enums), nor on a
46  * QAPI object that references the 'any' type.
47  */
48 #define QAPI_CLONE_MEMBERS(type, dst, src)                              \
49     qapi_clone_members(dst, src, sizeof(type),                          \
50                        (void (*)(Visitor *, void *,                     \
51                                  Error **))visit_type_ ## type ## _members)
52 
53 #endif
54