xref: /openbmc/qemu/include/qapi/clone-visitor.h (revision cdba3b90)
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 "qapi/error.h"
15 #include "qapi/visitor.h"
16 
17 /*
18  * The clone visitor is for direct use only by the QAPI_CLONE() macro;
19  * it requires that the root visit occur on an object, list, or
20  * alternate, and is not usable directly on built-in QAPI types.
21  */
22 typedef struct QapiCloneVisitor QapiCloneVisitor;
23 
24 Visitor *qapi_clone_visitor_new(void);
25 Visitor *qapi_clone_members_visitor_new(void);
26 
27 /*
28  * Deep-clone QAPI object @src of the given @type, and return the result.
29  *
30  * Not usable on QAPI scalars (integers, strings, enums), nor on a
31  * QAPI object that references the 'any' type.  Safe when @src is NULL.
32  */
33 #define QAPI_CLONE(type, src)                                   \
34     ({                                                          \
35         Visitor *v_;                                            \
36         type *dst_ = (type *) (src); /* Cast away const */      \
37                                                                 \
38         if (dst_) {                                             \
39             v_ = qapi_clone_visitor_new();                      \
40             visit_type_ ## type(v_, NULL, &dst_, &error_abort); \
41             visit_free(v_);                                     \
42         }                                                       \
43         dst_;                                                   \
44     })
45 
46 /*
47  * Copy deep clones of @type members from @src to @dst.
48  *
49  * Not usable on QAPI scalars (integers, strings, enums), nor on a
50  * QAPI object that references the 'any' type.
51  */
52 #define QAPI_CLONE_MEMBERS(type, dst, src)                                \
53     ({                                                                    \
54         Visitor *v_;                                                      \
55                                                                           \
56         v_ = qapi_clone_members_visitor_new();                            \
57         *(type *)(dst) = *(src);                                          \
58         visit_type_ ## type ## _members(v_, (type *)(dst), &error_abort); \
59         visit_free(v_);                                                   \
60     })
61 
62 #endif
63