Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 1 | #ifndef OBJECT_INTERFACES_H |
| 2 | #define OBJECT_INTERFACES_H |
| 3 | |
| 4 | #include "qom/object.h" |
Kevin Wolf | f375026 | 2021-02-17 12:06:20 +0100 | [diff] [blame] | 5 | #include "qapi/qapi-types-qom.h" |
Daniel P. Berrange | 90998d5 | 2016-02-10 18:40:59 +0000 | [diff] [blame] | 6 | #include "qapi/visitor.h" |
Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 7 | |
| 8 | #define TYPE_USER_CREATABLE "user-creatable" |
| 9 | |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 10 | typedef struct UserCreatableClass UserCreatableClass; |
Eduardo Habkost | 8110fa1 | 2020-08-31 17:07:33 -0400 | [diff] [blame] | 11 | DECLARE_CLASS_CHECKERS(UserCreatableClass, USER_CREATABLE, |
| 12 | TYPE_USER_CREATABLE) |
Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 13 | #define USER_CREATABLE(obj) \ |
| 14 | INTERFACE_CHECK(UserCreatable, (obj), \ |
| 15 | TYPE_USER_CREATABLE) |
| 16 | |
Marc-André Lureau | aa1b35b | 2018-12-04 18:20:06 +0400 | [diff] [blame] | 17 | typedef struct UserCreatable UserCreatable; |
Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 18 | |
| 19 | /** |
| 20 | * UserCreatableClass: |
| 21 | * @parent_class: the base class |
| 22 | * @complete: callback to be called after @obj's properties are set. |
Lin Ma | d6edb15 | 2015-03-30 16:36:28 +0800 | [diff] [blame] | 23 | * @can_be_deleted: callback to be called before an object is removed |
| 24 | * to check if @obj can be removed safely. |
Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 25 | * |
| 26 | * Interface is designed to work with -object/object-add/object_add |
| 27 | * commands. |
| 28 | * Interface is mandatory for objects that are designed to be user |
| 29 | * creatable (i.e. -object/object-add/object_add, will accept only |
| 30 | * objects that inherit this interface). |
| 31 | * |
| 32 | * Interface also provides an optional ability to do the second |
| 33 | * stage * initialization of the object after its properties were |
| 34 | * set. |
| 35 | * |
| 36 | * For objects created without using -object/object-add/object_add, |
| 37 | * @user_creatable_complete() wrapper should be called manually if |
| 38 | * object's type implements USER_CREATABLE interface and needs |
| 39 | * complete() callback to be called. |
| 40 | */ |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 41 | struct UserCreatableClass { |
Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 42 | /* <private> */ |
| 43 | InterfaceClass parent_class; |
| 44 | |
| 45 | /* <public> */ |
| 46 | void (*complete)(UserCreatable *uc, Error **errp); |
Eduardo Habkost | 3beacfb | 2017-08-29 19:03:37 -0300 | [diff] [blame] | 47 | bool (*can_be_deleted)(UserCreatable *uc); |
Eduardo Habkost | db1015e | 2020-09-03 16:43:22 -0400 | [diff] [blame] | 48 | }; |
Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 49 | |
| 50 | /** |
| 51 | * user_creatable_complete: |
Marc-André Lureau | 3650b2d | 2018-12-04 18:20:07 +0400 | [diff] [blame] | 52 | * @uc: the user-creatable object whose complete() method is called if defined |
Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 53 | * @errp: if an error occurs, a pointer to an area to store the error |
| 54 | * |
| 55 | * Wrapper to call complete() method if one of types it's inherited |
| 56 | * from implements USER_CREATABLE interface, otherwise the call does |
| 57 | * nothing. |
Markus Armbruster | 6fd5bef | 2020-07-07 18:05:55 +0200 | [diff] [blame] | 58 | * |
| 59 | * Returns: %true on success, %false on failure. |
Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 60 | */ |
Markus Armbruster | 6fd5bef | 2020-07-07 18:05:55 +0200 | [diff] [blame] | 61 | bool user_creatable_complete(UserCreatable *uc, Error **errp); |
Lin Ma | d6edb15 | 2015-03-30 16:36:28 +0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * user_creatable_can_be_deleted: |
| 65 | * @uc: the object whose can_be_deleted() method is called if implemented |
Lin Ma | d6edb15 | 2015-03-30 16:36:28 +0800 | [diff] [blame] | 66 | * |
| 67 | * Wrapper to call can_be_deleted() method if one of types it's inherited |
| 68 | * from implements USER_CREATABLE interface. |
| 69 | */ |
Eduardo Habkost | 3beacfb | 2017-08-29 19:03:37 -0300 | [diff] [blame] | 70 | bool user_creatable_can_be_deleted(UserCreatable *uc); |
Daniel P. Berrange | 90998d5 | 2016-02-10 18:40:59 +0000 | [diff] [blame] | 71 | |
| 72 | /** |
Daniel P. Berrange | 90998d5 | 2016-02-10 18:40:59 +0000 | [diff] [blame] | 73 | * user_creatable_add_type: |
| 74 | * @type: the object type name |
| 75 | * @id: the unique ID for the object |
| 76 | * @qdict: the object properties |
| 77 | * @v: the visitor |
| 78 | * @errp: if an error occurs, a pointer to an area to store the error |
| 79 | * |
| 80 | * Create an instance of the user creatable object @type, placing |
| 81 | * it in the object composition tree with name @id, initializing |
| 82 | * it with properties from @qdict |
| 83 | * |
| 84 | * Returns: the newly created object or NULL on error |
| 85 | */ |
| 86 | Object *user_creatable_add_type(const char *type, const char *id, |
| 87 | const QDict *qdict, |
| 88 | Visitor *v, Error **errp); |
| 89 | |
| 90 | /** |
Kevin Wolf | f375026 | 2021-02-17 12:06:20 +0100 | [diff] [blame] | 91 | * user_creatable_add_qapi: |
| 92 | * @options: the object definition |
| 93 | * @errp: if an error occurs, a pointer to an area to store the error |
| 94 | * |
| 95 | * Create an instance of the user creatable object according to the |
| 96 | * options passed in @opts as described in the QAPI schema documentation. |
Kevin Wolf | f375026 | 2021-02-17 12:06:20 +0100 | [diff] [blame] | 97 | */ |
| 98 | void user_creatable_add_qapi(ObjectOptions *options, Error **errp); |
| 99 | |
| 100 | /** |
Kevin Wolf | ddf6dae | 2021-02-19 18:14:01 +0100 | [diff] [blame] | 101 | * user_creatable_parse_str: |
| 102 | * @optarg: the object definition string as passed on the command line |
| 103 | * @errp: if an error occurs, a pointer to an area to store the error |
| 104 | * |
| 105 | * Parses the option for the user creatable object with a keyval parser and |
| 106 | * implicit key 'qom-type', converting the result to ObjectOptions. |
| 107 | * |
| 108 | * If a help option is given, print help instead. |
| 109 | * |
| 110 | * Returns: ObjectOptions on success, NULL when an error occurred (*errp is set |
| 111 | * then) or help was printed (*errp is not set). |
| 112 | */ |
| 113 | ObjectOptions *user_creatable_parse_str(const char *optarg, Error **errp); |
| 114 | |
| 115 | /** |
Kevin Wolf | ffd58ef | 2021-02-17 14:59:06 +0100 | [diff] [blame] | 116 | * user_creatable_add_from_str: |
| 117 | * @optarg: the object definition string as passed on the command line |
| 118 | * @errp: if an error occurs, a pointer to an area to store the error |
| 119 | * |
| 120 | * Create an instance of the user creatable object by parsing optarg |
| 121 | * with a keyval parser and implicit key 'qom-type', converting the |
| 122 | * result to ObjectOptions and calling into qmp_object_add(). |
| 123 | * |
| 124 | * If a help option is given, print help instead. |
| 125 | * |
| 126 | * Returns: true when an object was successfully created, false when an error |
| 127 | * occurred (*errp is set then) or help was printed (*errp is not set). |
| 128 | */ |
| 129 | bool user_creatable_add_from_str(const char *optarg, Error **errp); |
| 130 | |
| 131 | /** |
Kevin Wolf | f375026 | 2021-02-17 12:06:20 +0100 | [diff] [blame] | 132 | * user_creatable_process_cmdline: |
| 133 | * @optarg: the object definition string as passed on the command line |
| 134 | * |
| 135 | * Create an instance of the user creatable object by parsing optarg |
| 136 | * with a keyval parser and implicit key 'qom-type', converting the |
| 137 | * result to ObjectOptions and calling into qmp_object_add(). |
| 138 | * |
| 139 | * If a help option is given, print help instead and exit. |
| 140 | * |
| 141 | * This function is only meant to be called during command line parsing. |
| 142 | * It exits the process on failure or after printing help. |
| 143 | */ |
| 144 | void user_creatable_process_cmdline(const char *optarg); |
| 145 | |
| 146 | /** |
Kevin Wolf | 3e9297f | 2019-10-11 19:20:12 +0200 | [diff] [blame] | 147 | * user_creatable_print_help: |
| 148 | * @type: the QOM type to be added |
| 149 | * @opts: options to create |
| 150 | * |
Kevin Wolf | c9ac145 | 2020-10-07 18:49:02 +0200 | [diff] [blame] | 151 | * Prints help if requested in @type or @opts. Note that if @type is neither |
| 152 | * "help"/"?" nor a valid user creatable type, no help will be printed |
| 153 | * regardless of @opts. |
Kevin Wolf | 3e9297f | 2019-10-11 19:20:12 +0200 | [diff] [blame] | 154 | * |
Kevin Wolf | c9ac145 | 2020-10-07 18:49:02 +0200 | [diff] [blame] | 155 | * Returns: true if a help option was found and help was printed, false |
| 156 | * otherwise. |
Kevin Wolf | 3e9297f | 2019-10-11 19:20:12 +0200 | [diff] [blame] | 157 | */ |
| 158 | bool user_creatable_print_help(const char *type, QemuOpts *opts); |
| 159 | |
| 160 | /** |
Daniel P. Berrange | 90998d5 | 2016-02-10 18:40:59 +0000 | [diff] [blame] | 161 | * user_creatable_del: |
| 162 | * @id: the unique ID for the object |
| 163 | * @errp: if an error occurs, a pointer to an area to store the error |
| 164 | * |
| 165 | * Delete an instance of the user creatable object identified |
| 166 | * by @id. |
Markus Armbruster | 6fd5bef | 2020-07-07 18:05:55 +0200 | [diff] [blame] | 167 | * |
| 168 | * Returns: %true on success, %false on failure. |
Daniel P. Berrange | 90998d5 | 2016-02-10 18:40:59 +0000 | [diff] [blame] | 169 | */ |
Markus Armbruster | 6fd5bef | 2020-07-07 18:05:55 +0200 | [diff] [blame] | 170 | bool user_creatable_del(const char *id, Error **errp); |
Daniel P. Berrange | 90998d5 | 2016-02-10 18:40:59 +0000 | [diff] [blame] | 171 | |
Eduardo Habkost | 9d5139e | 2017-08-24 16:23:13 -0300 | [diff] [blame] | 172 | /** |
| 173 | * user_creatable_cleanup: |
| 174 | * |
| 175 | * Delete all user-creatable objects and the user-creatable |
| 176 | * objects container. |
| 177 | */ |
| 178 | void user_creatable_cleanup(void); |
| 179 | |
Igor Mammedov | 269e09f | 2014-01-16 17:34:38 +0100 | [diff] [blame] | 180 | #endif |