Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 1 | /* |
| 2 | * QEMU Object Model |
| 3 | * |
| 4 | * Copyright IBM, Corp. 2011 |
| 5 | * |
| 6 | * Authors: |
| 7 | * Anthony Liguori <aliguori@us.ibm.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
Paolo Bonzini | 14cccb6 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 13 | #include "qom/object.h" |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 14 | #include "qemu-common.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 15 | #include "qapi/visitor.h" |
Hu Tao | 1f21772 | 2014-05-14 17:43:33 +0800 | [diff] [blame] | 16 | #include "qapi-visit.h" |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 17 | #include "qapi/string-input-visitor.h" |
| 18 | #include "qapi/string-output-visitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 19 | #include "qapi/qmp/qerror.h" |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 20 | #include "trace.h" |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 21 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 22 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
| 23 | * of the QOM core on QObject? */ |
Paolo Bonzini | 14cccb6 | 2012-12-17 18:19:50 +0100 | [diff] [blame] | 24 | #include "qom/qom-qobject.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 25 | #include "qapi/qmp/qobject.h" |
| 26 | #include "qapi/qmp/qbool.h" |
| 27 | #include "qapi/qmp/qint.h" |
| 28 | #include "qapi/qmp/qstring.h" |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 29 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 30 | #define MAX_INTERFACES 32 |
| 31 | |
| 32 | typedef struct InterfaceImpl InterfaceImpl; |
| 33 | typedef struct TypeImpl TypeImpl; |
| 34 | |
| 35 | struct InterfaceImpl |
| 36 | { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 37 | const char *typename; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | struct TypeImpl |
| 41 | { |
| 42 | const char *name; |
| 43 | |
| 44 | size_t class_size; |
| 45 | |
| 46 | size_t instance_size; |
| 47 | |
| 48 | void (*class_init)(ObjectClass *klass, void *data); |
Paolo Bonzini | 3b50e31 | 2012-05-02 13:30:55 +0200 | [diff] [blame] | 49 | void (*class_base_init)(ObjectClass *klass, void *data); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 50 | void (*class_finalize)(ObjectClass *klass, void *data); |
| 51 | |
| 52 | void *class_data; |
| 53 | |
| 54 | void (*instance_init)(Object *obj); |
Eduardo Habkost | 8231c2d | 2013-07-10 17:08:41 -0300 | [diff] [blame] | 55 | void (*instance_post_init)(Object *obj); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 56 | void (*instance_finalize)(Object *obj); |
| 57 | |
| 58 | bool abstract; |
| 59 | |
| 60 | const char *parent; |
| 61 | TypeImpl *parent_type; |
| 62 | |
| 63 | ObjectClass *class; |
| 64 | |
| 65 | int num_interfaces; |
| 66 | InterfaceImpl interfaces[MAX_INTERFACES]; |
| 67 | }; |
| 68 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 69 | static Type type_interface; |
| 70 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 71 | static GHashTable *type_table_get(void) |
| 72 | { |
| 73 | static GHashTable *type_table; |
| 74 | |
| 75 | if (type_table == NULL) { |
| 76 | type_table = g_hash_table_new(g_str_hash, g_str_equal); |
| 77 | } |
| 78 | |
| 79 | return type_table; |
| 80 | } |
| 81 | |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 82 | static bool enumerating_types; |
| 83 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 84 | static void type_table_add(TypeImpl *ti) |
| 85 | { |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 86 | assert(!enumerating_types); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 87 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); |
| 88 | } |
| 89 | |
| 90 | static TypeImpl *type_table_lookup(const char *name) |
| 91 | { |
| 92 | return g_hash_table_lookup(type_table_get(), name); |
| 93 | } |
| 94 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 95 | static TypeImpl *type_new(const TypeInfo *info) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 96 | { |
| 97 | TypeImpl *ti = g_malloc0(sizeof(*ti)); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 98 | int i; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 99 | |
| 100 | g_assert(info->name != NULL); |
| 101 | |
Anthony Liguori | 7309335 | 2012-01-25 13:37:36 -0600 | [diff] [blame] | 102 | if (type_table_lookup(info->name) != NULL) { |
| 103 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); |
| 104 | abort(); |
| 105 | } |
| 106 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 107 | ti->name = g_strdup(info->name); |
| 108 | ti->parent = g_strdup(info->parent); |
| 109 | |
| 110 | ti->class_size = info->class_size; |
| 111 | ti->instance_size = info->instance_size; |
| 112 | |
| 113 | ti->class_init = info->class_init; |
Paolo Bonzini | 3b50e31 | 2012-05-02 13:30:55 +0200 | [diff] [blame] | 114 | ti->class_base_init = info->class_base_init; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 115 | ti->class_finalize = info->class_finalize; |
| 116 | ti->class_data = info->class_data; |
| 117 | |
| 118 | ti->instance_init = info->instance_init; |
Eduardo Habkost | 8231c2d | 2013-07-10 17:08:41 -0300 | [diff] [blame] | 119 | ti->instance_post_init = info->instance_post_init; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 120 | ti->instance_finalize = info->instance_finalize; |
| 121 | |
| 122 | ti->abstract = info->abstract; |
| 123 | |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 124 | for (i = 0; info->interfaces && info->interfaces[i].type; i++) { |
| 125 | ti->interfaces[i].typename = g_strdup(info->interfaces[i].type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 126 | } |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 127 | ti->num_interfaces = i; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 128 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 129 | return ti; |
| 130 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 131 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 132 | static TypeImpl *type_register_internal(const TypeInfo *info) |
| 133 | { |
| 134 | TypeImpl *ti; |
| 135 | ti = type_new(info); |
| 136 | |
| 137 | type_table_add(ti); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 138 | return ti; |
| 139 | } |
| 140 | |
Paolo Bonzini | 049cb3c | 2012-04-04 15:58:40 +0200 | [diff] [blame] | 141 | TypeImpl *type_register(const TypeInfo *info) |
| 142 | { |
| 143 | assert(info->parent); |
| 144 | return type_register_internal(info); |
| 145 | } |
| 146 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 147 | TypeImpl *type_register_static(const TypeInfo *info) |
| 148 | { |
| 149 | return type_register(info); |
| 150 | } |
| 151 | |
| 152 | static TypeImpl *type_get_by_name(const char *name) |
| 153 | { |
| 154 | if (name == NULL) { |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | return type_table_lookup(name); |
| 159 | } |
| 160 | |
| 161 | static TypeImpl *type_get_parent(TypeImpl *type) |
| 162 | { |
| 163 | if (!type->parent_type && type->parent) { |
| 164 | type->parent_type = type_get_by_name(type->parent); |
| 165 | g_assert(type->parent_type != NULL); |
| 166 | } |
| 167 | |
| 168 | return type->parent_type; |
| 169 | } |
| 170 | |
| 171 | static bool type_has_parent(TypeImpl *type) |
| 172 | { |
| 173 | return (type->parent != NULL); |
| 174 | } |
| 175 | |
| 176 | static size_t type_class_get_size(TypeImpl *ti) |
| 177 | { |
| 178 | if (ti->class_size) { |
| 179 | return ti->class_size; |
| 180 | } |
| 181 | |
| 182 | if (type_has_parent(ti)) { |
| 183 | return type_class_get_size(type_get_parent(ti)); |
| 184 | } |
| 185 | |
| 186 | return sizeof(ObjectClass); |
| 187 | } |
| 188 | |
Igor Mitsyanko | aca59af | 2012-02-28 15:57:10 +0400 | [diff] [blame] | 189 | static size_t type_object_get_size(TypeImpl *ti) |
| 190 | { |
| 191 | if (ti->instance_size) { |
| 192 | return ti->instance_size; |
| 193 | } |
| 194 | |
| 195 | if (type_has_parent(ti)) { |
| 196 | return type_object_get_size(type_get_parent(ti)); |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 202 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 203 | { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 204 | assert(target_type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 205 | |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 206 | /* Check if typename is a direct ancestor of type */ |
| 207 | while (type) { |
| 208 | if (type == target_type) { |
| 209 | return true; |
| 210 | } |
| 211 | |
| 212 | type = type_get_parent(type); |
| 213 | } |
| 214 | |
| 215 | return false; |
| 216 | } |
| 217 | |
| 218 | static void type_initialize(TypeImpl *ti); |
| 219 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 220 | static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type, |
| 221 | TypeImpl *parent_type) |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 222 | { |
| 223 | InterfaceClass *new_iface; |
| 224 | TypeInfo info = { }; |
| 225 | TypeImpl *iface_impl; |
| 226 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 227 | info.parent = parent_type->name; |
| 228 | info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 229 | info.abstract = true; |
| 230 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 231 | iface_impl = type_new(&info); |
| 232 | iface_impl->parent_type = parent_type; |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 233 | type_initialize(iface_impl); |
| 234 | g_free((char *)info.name); |
| 235 | |
| 236 | new_iface = (InterfaceClass *)iface_impl->class; |
| 237 | new_iface->concrete_class = ti->class; |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 238 | new_iface->interface_type = interface_type; |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 239 | |
| 240 | ti->class->interfaces = g_slist_append(ti->class->interfaces, |
| 241 | iface_impl->class); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 242 | } |
| 243 | |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 244 | static void type_initialize(TypeImpl *ti) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 245 | { |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 246 | TypeImpl *parent; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 247 | |
| 248 | if (ti->class) { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | ti->class_size = type_class_get_size(ti); |
Igor Mitsyanko | aca59af | 2012-02-28 15:57:10 +0400 | [diff] [blame] | 253 | ti->instance_size = type_object_get_size(ti); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 254 | |
| 255 | ti->class = g_malloc0(ti->class_size); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 256 | |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 257 | parent = type_get_parent(ti); |
| 258 | if (parent) { |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 259 | type_initialize(parent); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 260 | GSList *e; |
| 261 | int i; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 262 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 263 | g_assert(parent->class_size <= ti->class_size); |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 264 | memcpy(ti->class, parent->class, parent->class_size); |
Peter Crosthwaite | 3e407de | 2013-02-19 14:02:09 +1000 | [diff] [blame] | 265 | ti->class->interfaces = NULL; |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 266 | |
| 267 | for (e = parent->class->interfaces; e; e = e->next) { |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 268 | InterfaceClass *iface = e->data; |
| 269 | ObjectClass *klass = OBJECT_CLASS(iface); |
| 270 | |
| 271 | type_initialize_interface(ti, iface->interface_type, klass->type); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | for (i = 0; i < ti->num_interfaces; i++) { |
| 275 | TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); |
| 276 | for (e = ti->class->interfaces; e; e = e->next) { |
| 277 | TypeImpl *target_type = OBJECT_CLASS(e->data)->type; |
| 278 | |
| 279 | if (type_is_ancestor(target_type, t)) { |
| 280 | break; |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (e) { |
| 285 | continue; |
| 286 | } |
| 287 | |
Paolo Bonzini | b061dc4 | 2013-12-03 16:41:59 +0100 | [diff] [blame] | 288 | type_initialize_interface(ti, t, t); |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 289 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 290 | } |
| 291 | |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 292 | ti->class->type = ti; |
| 293 | |
| 294 | while (parent) { |
| 295 | if (parent->class_base_init) { |
| 296 | parent->class_base_init(ti->class, ti->class_data); |
| 297 | } |
| 298 | parent = type_get_parent(parent); |
| 299 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 300 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 301 | if (ti->class_init) { |
| 302 | ti->class_init(ti->class, ti->class_data); |
| 303 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static void object_init_with_type(Object *obj, TypeImpl *ti) |
| 307 | { |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 308 | if (type_has_parent(ti)) { |
| 309 | object_init_with_type(obj, type_get_parent(ti)); |
| 310 | } |
| 311 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 312 | if (ti->instance_init) { |
| 313 | ti->instance_init(obj); |
| 314 | } |
| 315 | } |
| 316 | |
Eduardo Habkost | 8231c2d | 2013-07-10 17:08:41 -0300 | [diff] [blame] | 317 | static void object_post_init_with_type(Object *obj, TypeImpl *ti) |
| 318 | { |
| 319 | if (ti->instance_post_init) { |
| 320 | ti->instance_post_init(obj); |
| 321 | } |
| 322 | |
| 323 | if (type_has_parent(ti)) { |
| 324 | object_post_init_with_type(obj, type_get_parent(ti)); |
| 325 | } |
| 326 | } |
| 327 | |
Andreas Färber | 5b9237f | 2013-08-30 18:28:37 +0200 | [diff] [blame] | 328 | void object_initialize_with_type(void *data, size_t size, TypeImpl *type) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 329 | { |
| 330 | Object *obj = data; |
| 331 | |
| 332 | g_assert(type != NULL); |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 333 | type_initialize(type); |
Igor Mitsyanko | aca59af | 2012-02-28 15:57:10 +0400 | [diff] [blame] | 334 | |
| 335 | g_assert(type->instance_size >= sizeof(Object)); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 336 | g_assert(type->abstract == false); |
Andreas Färber | 5b9237f | 2013-08-30 18:28:37 +0200 | [diff] [blame] | 337 | g_assert(size >= type->instance_size); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 338 | |
| 339 | memset(obj, 0, type->instance_size); |
| 340 | obj->class = type->class; |
Paolo Bonzini | 764b631 | 2012-11-23 09:47:12 +0100 | [diff] [blame] | 341 | object_ref(obj); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 342 | QTAILQ_INIT(&obj->properties); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 343 | object_init_with_type(obj, type); |
Eduardo Habkost | 8231c2d | 2013-07-10 17:08:41 -0300 | [diff] [blame] | 344 | object_post_init_with_type(obj, type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 345 | } |
| 346 | |
Andreas Färber | 213f0c4 | 2013-08-23 19:37:12 +0200 | [diff] [blame] | 347 | void object_initialize(void *data, size_t size, const char *typename) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 348 | { |
| 349 | TypeImpl *type = type_get_by_name(typename); |
| 350 | |
Andreas Färber | 5b9237f | 2013-08-30 18:28:37 +0200 | [diff] [blame] | 351 | object_initialize_with_type(data, size, type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 352 | } |
| 353 | |
Andreas Färber | 5d9d3f4 | 2012-05-27 00:32:40 +0200 | [diff] [blame] | 354 | static inline bool object_property_is_child(ObjectProperty *prop) |
| 355 | { |
| 356 | return strstart(prop->type, "child<", NULL); |
| 357 | } |
| 358 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 359 | static void object_property_del_all(Object *obj) |
| 360 | { |
| 361 | while (!QTAILQ_EMPTY(&obj->properties)) { |
| 362 | ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); |
| 363 | |
| 364 | QTAILQ_REMOVE(&obj->properties, prop, node); |
| 365 | |
| 366 | if (prop->release) { |
| 367 | prop->release(obj, prop->name, prop->opaque); |
| 368 | } |
| 369 | |
| 370 | g_free(prop->name); |
| 371 | g_free(prop->type); |
| 372 | g_free(prop); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | static void object_property_del_child(Object *obj, Object *child, Error **errp) |
| 377 | { |
| 378 | ObjectProperty *prop; |
| 379 | |
| 380 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
Andreas Färber | 5d9d3f4 | 2012-05-27 00:32:40 +0200 | [diff] [blame] | 381 | if (object_property_is_child(prop) && prop->opaque == child) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 382 | object_property_del(obj, prop->name, errp); |
Paolo Bonzini | 6c1fdcf | 2012-02-28 09:54:15 +0100 | [diff] [blame] | 383 | break; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | void object_unparent(Object *obj) |
| 389 | { |
Paolo Bonzini | e0a83fc | 2013-04-02 15:50:00 +0200 | [diff] [blame] | 390 | if (!obj->parent) { |
| 391 | return; |
| 392 | } |
| 393 | |
Paolo Bonzini | 52e636c | 2013-01-25 14:12:30 +0100 | [diff] [blame] | 394 | object_ref(obj); |
Paolo Bonzini | 667d22d | 2012-11-23 09:47:13 +0100 | [diff] [blame] | 395 | if (obj->class->unparent) { |
| 396 | (obj->class->unparent)(obj); |
| 397 | } |
Michael S. Tsirkin | e998fa8 | 2013-03-18 21:01:37 +0200 | [diff] [blame] | 398 | if (obj->parent) { |
| 399 | object_property_del_child(obj->parent, obj, NULL); |
Peter Crosthwaite | c28322d | 2014-05-26 17:39:51 -0700 | [diff] [blame] | 400 | obj->parent = NULL; |
Michael S. Tsirkin | e998fa8 | 2013-03-18 21:01:37 +0200 | [diff] [blame] | 401 | } |
Paolo Bonzini | 52e636c | 2013-01-25 14:12:30 +0100 | [diff] [blame] | 402 | object_unref(obj); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 403 | } |
| 404 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 405 | static void object_deinit(Object *obj, TypeImpl *type) |
| 406 | { |
| 407 | if (type->instance_finalize) { |
| 408 | type->instance_finalize(obj); |
| 409 | } |
| 410 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 411 | if (type_has_parent(type)) { |
| 412 | object_deinit(obj, type_get_parent(type)); |
| 413 | } |
| 414 | } |
| 415 | |
Paolo Bonzini | 339c270 | 2012-11-23 09:47:16 +0100 | [diff] [blame] | 416 | static void object_finalize(void *data) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 417 | { |
| 418 | Object *obj = data; |
| 419 | TypeImpl *ti = obj->class->type; |
| 420 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 421 | object_property_del_all(obj); |
Paolo Bonzini | 76a6e1c | 2014-06-11 11:58:30 +0200 | [diff] [blame^] | 422 | object_deinit(obj, ti); |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 423 | |
| 424 | g_assert(obj->ref == 0); |
Paolo Bonzini | fde9bf4 | 2012-11-23 09:47:14 +0100 | [diff] [blame] | 425 | if (obj->free) { |
| 426 | obj->free(obj); |
| 427 | } |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | Object *object_new_with_type(Type type) |
| 431 | { |
| 432 | Object *obj; |
| 433 | |
| 434 | g_assert(type != NULL); |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 435 | type_initialize(type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 436 | |
| 437 | obj = g_malloc(type->instance_size); |
Andreas Färber | 5b9237f | 2013-08-30 18:28:37 +0200 | [diff] [blame] | 438 | object_initialize_with_type(obj, type->instance_size, type); |
Paolo Bonzini | fde9bf4 | 2012-11-23 09:47:14 +0100 | [diff] [blame] | 439 | obj->free = g_free; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 440 | |
| 441 | return obj; |
| 442 | } |
| 443 | |
| 444 | Object *object_new(const char *typename) |
| 445 | { |
| 446 | TypeImpl *ti = type_get_by_name(typename); |
| 447 | |
| 448 | return object_new_with_type(ti); |
| 449 | } |
| 450 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 451 | Object *object_dynamic_cast(Object *obj, const char *typename) |
| 452 | { |
Paolo Bonzini | b7f43fe | 2012-11-23 16:56:17 +0100 | [diff] [blame] | 453 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 454 | return obj; |
| 455 | } |
| 456 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 457 | return NULL; |
| 458 | } |
| 459 | |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 460 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
| 461 | const char *file, int line, const char *func) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 462 | { |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 463 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
| 464 | typename, file, line, func); |
| 465 | |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 466 | #ifdef CONFIG_QOM_CAST_DEBUG |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 467 | int i; |
| 468 | Object *inst; |
| 469 | |
Peter Crosthwaite | 95916ab | 2013-05-22 11:19:16 +1000 | [diff] [blame] | 470 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 471 | if (obj->class->object_cast_cache[i] == typename) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 472 | goto out; |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | inst = object_dynamic_cast(obj, typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 477 | |
Paolo Bonzini | b7f43fe | 2012-11-23 16:56:17 +0100 | [diff] [blame] | 478 | if (!inst && obj) { |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 479 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
| 480 | file, line, func, obj, typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 481 | abort(); |
| 482 | } |
| 483 | |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 484 | assert(obj == inst); |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 485 | |
Peter Crosthwaite | 95916ab | 2013-05-22 11:19:16 +1000 | [diff] [blame] | 486 | if (obj && obj == inst) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 487 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 488 | obj->class->object_cast_cache[i - 1] = |
| 489 | obj->class->object_cast_cache[i]; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 490 | } |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 491 | obj->class->object_cast_cache[i - 1] = typename; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | out: |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 495 | #endif |
| 496 | return obj; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, |
| 500 | const char *typename) |
| 501 | { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 502 | ObjectClass *ret = NULL; |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 503 | TypeImpl *target_type; |
| 504 | TypeImpl *type; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 505 | |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 506 | if (!class) { |
| 507 | return NULL; |
| 508 | } |
| 509 | |
Paolo Bonzini | 793c96b | 2013-05-10 14:16:37 +0200 | [diff] [blame] | 510 | /* A simple fast path that can trigger a lot for leaf classes. */ |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 511 | type = class->type; |
Paolo Bonzini | 793c96b | 2013-05-10 14:16:37 +0200 | [diff] [blame] | 512 | if (type->name == typename) { |
| 513 | return class; |
| 514 | } |
| 515 | |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 516 | target_type = type_get_by_name(typename); |
Alexander Graf | 9ab880b | 2013-04-30 15:02:16 +0200 | [diff] [blame] | 517 | if (!target_type) { |
| 518 | /* target class type unknown, so fail the cast */ |
| 519 | return NULL; |
| 520 | } |
| 521 | |
Peter Crosthwaite | 00e2cea | 2013-02-19 14:02:10 +1000 | [diff] [blame] | 522 | if (type->class->interfaces && |
| 523 | type_is_ancestor(target_type, type_interface)) { |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 524 | int found = 0; |
| 525 | GSList *i; |
| 526 | |
| 527 | for (i = class->interfaces; i; i = i->next) { |
| 528 | ObjectClass *target_class = i->data; |
| 529 | |
| 530 | if (type_is_ancestor(target_class->type, target_type)) { |
| 531 | ret = target_class; |
| 532 | found++; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | /* The match was ambiguous, don't allow a cast */ |
| 537 | if (found > 1) { |
| 538 | ret = NULL; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 539 | } |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 540 | } else if (type_is_ancestor(type, target_type)) { |
| 541 | ret = class; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 542 | } |
| 543 | |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 544 | return ret; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 548 | const char *typename, |
| 549 | const char *file, int line, |
| 550 | const char *func) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 551 | { |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 552 | ObjectClass *ret; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 553 | |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 554 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", |
| 555 | typename, file, line, func); |
| 556 | |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 557 | #ifdef CONFIG_QOM_CAST_DEBUG |
| 558 | int i; |
| 559 | |
Peter Crosthwaite | 9d6a3d5 | 2013-06-18 19:18:59 +1000 | [diff] [blame] | 560 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 561 | if (class->class_cast_cache[i] == typename) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 562 | ret = class; |
| 563 | goto out; |
| 564 | } |
| 565 | } |
| 566 | #else |
Peter Crosthwaite | 9d6a3d5 | 2013-06-18 19:18:59 +1000 | [diff] [blame] | 567 | if (!class || !class->interfaces) { |
Paolo Bonzini | 3556c23 | 2013-05-10 14:16:40 +0200 | [diff] [blame] | 568 | return class; |
| 569 | } |
| 570 | #endif |
| 571 | |
Paolo Bonzini | fa131d9 | 2013-05-10 14:16:39 +0200 | [diff] [blame] | 572 | ret = object_class_dynamic_cast(class, typename); |
Paolo Bonzini | bf0fda3 | 2013-05-10 14:16:36 +0200 | [diff] [blame] | 573 | if (!ret && class) { |
Paolo Bonzini | be17f18 | 2013-05-10 14:16:38 +0200 | [diff] [blame] | 574 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
| 575 | file, line, func, class, typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 576 | abort(); |
| 577 | } |
| 578 | |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 579 | #ifdef CONFIG_QOM_CAST_DEBUG |
Peter Crosthwaite | 9d6a3d5 | 2013-06-18 19:18:59 +1000 | [diff] [blame] | 580 | if (class && ret == class) { |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 581 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 582 | class->class_cast_cache[i - 1] = class->class_cast_cache[i]; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 583 | } |
Peter Crosthwaite | 0ab4c94 | 2013-11-27 20:27:33 -0800 | [diff] [blame] | 584 | class->class_cast_cache[i - 1] = typename; |
Anthony Liguori | 0358732 | 2013-05-13 15:22:24 -0500 | [diff] [blame] | 585 | } |
| 586 | out: |
| 587 | #endif |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | const char *object_get_typename(Object *obj) |
| 592 | { |
| 593 | return obj->class->type->name; |
| 594 | } |
| 595 | |
| 596 | ObjectClass *object_get_class(Object *obj) |
| 597 | { |
| 598 | return obj->class; |
| 599 | } |
| 600 | |
Andreas Färber | 1786237 | 2013-01-23 12:20:18 +0100 | [diff] [blame] | 601 | bool object_class_is_abstract(ObjectClass *klass) |
| 602 | { |
| 603 | return klass->type->abstract; |
| 604 | } |
| 605 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 606 | const char *object_class_get_name(ObjectClass *klass) |
| 607 | { |
| 608 | return klass->type->name; |
| 609 | } |
| 610 | |
| 611 | ObjectClass *object_class_by_name(const char *typename) |
| 612 | { |
| 613 | TypeImpl *type = type_get_by_name(typename); |
| 614 | |
| 615 | if (!type) { |
| 616 | return NULL; |
| 617 | } |
| 618 | |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 619 | type_initialize(type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 620 | |
| 621 | return type->class; |
| 622 | } |
| 623 | |
Paolo Bonzini | e7cce67 | 2012-05-02 13:30:54 +0200 | [diff] [blame] | 624 | ObjectClass *object_class_get_parent(ObjectClass *class) |
| 625 | { |
| 626 | TypeImpl *type = type_get_parent(class->type); |
| 627 | |
| 628 | if (!type) { |
| 629 | return NULL; |
| 630 | } |
| 631 | |
| 632 | type_initialize(type); |
| 633 | |
| 634 | return type->class; |
| 635 | } |
| 636 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 637 | typedef struct OCFData |
| 638 | { |
| 639 | void (*fn)(ObjectClass *klass, void *opaque); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 640 | const char *implements_type; |
| 641 | bool include_abstract; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 642 | void *opaque; |
| 643 | } OCFData; |
| 644 | |
| 645 | static void object_class_foreach_tramp(gpointer key, gpointer value, |
| 646 | gpointer opaque) |
| 647 | { |
| 648 | OCFData *data = opaque; |
| 649 | TypeImpl *type = value; |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 650 | ObjectClass *k; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 651 | |
Igor Mitsyanko | ac45103 | 2012-02-28 15:57:11 +0400 | [diff] [blame] | 652 | type_initialize(type); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 653 | k = type->class; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 654 | |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 655 | if (!data->include_abstract && type->abstract) { |
| 656 | return; |
| 657 | } |
| 658 | |
| 659 | if (data->implements_type && |
| 660 | !object_class_dynamic_cast(k, data->implements_type)) { |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | data->fn(k, data->opaque); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 668 | const char *implements_type, bool include_abstract, |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 669 | void *opaque) |
| 670 | { |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 671 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 672 | |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 673 | enumerating_types = true; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 674 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
Hervé Poussineau | f54c19c | 2013-12-03 16:42:00 +0100 | [diff] [blame] | 675 | enumerating_types = false; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 676 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 677 | |
Paolo Bonzini | 32efc53 | 2012-04-11 23:30:20 +0200 | [diff] [blame] | 678 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
| 679 | void *opaque) |
| 680 | { |
| 681 | ObjectProperty *prop; |
| 682 | int ret = 0; |
| 683 | |
| 684 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 685 | if (object_property_is_child(prop)) { |
| 686 | ret = fn(prop->opaque, opaque); |
| 687 | if (ret != 0) { |
| 688 | break; |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | return ret; |
| 693 | } |
| 694 | |
Andreas Färber | 418ba9e | 2012-02-25 23:07:34 +0100 | [diff] [blame] | 695 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
| 696 | { |
| 697 | GSList **list = opaque; |
| 698 | |
| 699 | *list = g_slist_prepend(*list, klass); |
| 700 | } |
| 701 | |
| 702 | GSList *object_class_get_list(const char *implements_type, |
| 703 | bool include_abstract) |
| 704 | { |
| 705 | GSList *list = NULL; |
| 706 | |
| 707 | object_class_foreach(object_class_get_list_tramp, |
| 708 | implements_type, include_abstract, &list); |
| 709 | return list; |
| 710 | } |
| 711 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 712 | void object_ref(Object *obj) |
| 713 | { |
Peter Crosthwaite | 8ffad85 | 2014-06-05 23:13:36 -0700 | [diff] [blame] | 714 | if (!obj) { |
| 715 | return; |
| 716 | } |
Jan Kiszka | f08c03f | 2013-07-02 11:36:39 +0200 | [diff] [blame] | 717 | atomic_inc(&obj->ref); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | void object_unref(Object *obj) |
| 721 | { |
Peter Crosthwaite | 8ffad85 | 2014-06-05 23:13:36 -0700 | [diff] [blame] | 722 | if (!obj) { |
| 723 | return; |
| 724 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 725 | g_assert(obj->ref > 0); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 726 | |
| 727 | /* parent always holds a reference to its children */ |
Jan Kiszka | f08c03f | 2013-07-02 11:36:39 +0200 | [diff] [blame] | 728 | if (atomic_fetch_dec(&obj->ref) == 1) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 729 | object_finalize(obj); |
| 730 | } |
| 731 | } |
| 732 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 733 | ObjectProperty * |
| 734 | object_property_add(Object *obj, const char *name, const char *type, |
| 735 | ObjectPropertyAccessor *get, |
| 736 | ObjectPropertyAccessor *set, |
| 737 | ObjectPropertyRelease *release, |
| 738 | void *opaque, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 739 | { |
Peter Maydell | 54852b0 | 2013-03-25 13:15:13 +0000 | [diff] [blame] | 740 | ObjectProperty *prop; |
| 741 | |
| 742 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 743 | if (strcmp(prop->name, name) == 0) { |
| 744 | error_setg(errp, "attempt to add duplicate property '%s'" |
| 745 | " to object (type '%s')", name, |
| 746 | object_get_typename(obj)); |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 747 | return NULL; |
Peter Maydell | 54852b0 | 2013-03-25 13:15:13 +0000 | [diff] [blame] | 748 | } |
| 749 | } |
| 750 | |
| 751 | prop = g_malloc0(sizeof(*prop)); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 752 | |
| 753 | prop->name = g_strdup(name); |
| 754 | prop->type = g_strdup(type); |
| 755 | |
| 756 | prop->get = get; |
| 757 | prop->set = set; |
| 758 | prop->release = release; |
| 759 | prop->opaque = opaque; |
| 760 | |
| 761 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 762 | return prop; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 763 | } |
| 764 | |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 765 | ObjectProperty *object_property_find(Object *obj, const char *name, |
| 766 | Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 767 | { |
| 768 | ObjectProperty *prop; |
| 769 | |
| 770 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 771 | if (strcmp(prop->name, name) == 0) { |
| 772 | return prop; |
| 773 | } |
| 774 | } |
| 775 | |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 776 | error_setg(errp, "Property '.%s' not found", name); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 777 | return NULL; |
| 778 | } |
| 779 | |
| 780 | void object_property_del(Object *obj, const char *name, Error **errp) |
| 781 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 782 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 783 | if (prop == NULL) { |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 784 | return; |
| 785 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 786 | |
Anthony Liguori | 0866aca | 2011-12-23 15:34:39 -0600 | [diff] [blame] | 787 | if (prop->release) { |
| 788 | prop->release(obj, name, prop->opaque); |
| 789 | } |
| 790 | |
| 791 | QTAILQ_REMOVE(&obj->properties, prop, node); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 792 | |
| 793 | g_free(prop->name); |
| 794 | g_free(prop->type); |
| 795 | g_free(prop); |
| 796 | } |
| 797 | |
| 798 | void object_property_get(Object *obj, Visitor *v, const char *name, |
| 799 | Error **errp) |
| 800 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 801 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 802 | if (prop == NULL) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 803 | return; |
| 804 | } |
| 805 | |
| 806 | if (!prop->get) { |
| 807 | error_set(errp, QERR_PERMISSION_DENIED); |
| 808 | } else { |
| 809 | prop->get(obj, v, prop->opaque, name, errp); |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | void object_property_set(Object *obj, Visitor *v, const char *name, |
| 814 | Error **errp) |
| 815 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 816 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 817 | if (prop == NULL) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 818 | return; |
| 819 | } |
| 820 | |
| 821 | if (!prop->set) { |
| 822 | error_set(errp, QERR_PERMISSION_DENIED); |
| 823 | } else { |
| 824 | prop->set(obj, v, prop->opaque, name, errp); |
| 825 | } |
| 826 | } |
| 827 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 828 | void object_property_set_str(Object *obj, const char *value, |
| 829 | const char *name, Error **errp) |
| 830 | { |
| 831 | QString *qstr = qstring_from_str(value); |
| 832 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); |
| 833 | |
| 834 | QDECREF(qstr); |
| 835 | } |
| 836 | |
| 837 | char *object_property_get_str(Object *obj, const char *name, |
| 838 | Error **errp) |
| 839 | { |
| 840 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 841 | QString *qstring; |
| 842 | char *retval; |
| 843 | |
| 844 | if (!ret) { |
| 845 | return NULL; |
| 846 | } |
| 847 | qstring = qobject_to_qstring(ret); |
| 848 | if (!qstring) { |
| 849 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); |
| 850 | retval = NULL; |
| 851 | } else { |
| 852 | retval = g_strdup(qstring_get_str(qstring)); |
| 853 | } |
| 854 | |
| 855 | QDECREF(qstring); |
| 856 | return retval; |
| 857 | } |
| 858 | |
Paolo Bonzini | 1d9c5a1 | 2012-02-02 10:51:57 +0100 | [diff] [blame] | 859 | void object_property_set_link(Object *obj, Object *value, |
| 860 | const char *name, Error **errp) |
| 861 | { |
Vlad Yasevich | 2d3aa28 | 2013-11-15 12:09:47 -0500 | [diff] [blame] | 862 | gchar *path = object_get_canonical_path(value); |
| 863 | object_property_set_str(obj, path, name, errp); |
| 864 | g_free(path); |
Paolo Bonzini | 1d9c5a1 | 2012-02-02 10:51:57 +0100 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | Object *object_property_get_link(Object *obj, const char *name, |
| 868 | Error **errp) |
| 869 | { |
| 870 | char *str = object_property_get_str(obj, name, errp); |
| 871 | Object *target = NULL; |
| 872 | |
| 873 | if (str && *str) { |
| 874 | target = object_resolve_path(str, NULL); |
| 875 | if (!target) { |
| 876 | error_set(errp, QERR_DEVICE_NOT_FOUND, str); |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | g_free(str); |
| 881 | return target; |
| 882 | } |
| 883 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 884 | void object_property_set_bool(Object *obj, bool value, |
| 885 | const char *name, Error **errp) |
| 886 | { |
| 887 | QBool *qbool = qbool_from_int(value); |
| 888 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); |
| 889 | |
| 890 | QDECREF(qbool); |
| 891 | } |
| 892 | |
| 893 | bool object_property_get_bool(Object *obj, const char *name, |
| 894 | Error **errp) |
| 895 | { |
| 896 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 897 | QBool *qbool; |
| 898 | bool retval; |
| 899 | |
| 900 | if (!ret) { |
| 901 | return false; |
| 902 | } |
| 903 | qbool = qobject_to_qbool(ret); |
| 904 | if (!qbool) { |
| 905 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); |
| 906 | retval = false; |
| 907 | } else { |
| 908 | retval = qbool_get_int(qbool); |
| 909 | } |
| 910 | |
| 911 | QDECREF(qbool); |
| 912 | return retval; |
| 913 | } |
| 914 | |
| 915 | void object_property_set_int(Object *obj, int64_t value, |
| 916 | const char *name, Error **errp) |
| 917 | { |
| 918 | QInt *qint = qint_from_int(value); |
| 919 | object_property_set_qobject(obj, QOBJECT(qint), name, errp); |
| 920 | |
| 921 | QDECREF(qint); |
| 922 | } |
| 923 | |
| 924 | int64_t object_property_get_int(Object *obj, const char *name, |
| 925 | Error **errp) |
| 926 | { |
| 927 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 928 | QInt *qint; |
| 929 | int64_t retval; |
| 930 | |
| 931 | if (!ret) { |
| 932 | return -1; |
| 933 | } |
| 934 | qint = qobject_to_qint(ret); |
| 935 | if (!qint) { |
| 936 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); |
| 937 | retval = -1; |
| 938 | } else { |
| 939 | retval = qint_get_int(qint); |
| 940 | } |
| 941 | |
| 942 | QDECREF(qint); |
| 943 | return retval; |
| 944 | } |
| 945 | |
Hu Tao | 1f21772 | 2014-05-14 17:43:33 +0800 | [diff] [blame] | 946 | int object_property_get_enum(Object *obj, const char *name, |
| 947 | const char *strings[], Error **errp) |
| 948 | { |
| 949 | StringOutputVisitor *sov; |
| 950 | StringInputVisitor *siv; |
| 951 | int ret; |
| 952 | |
| 953 | sov = string_output_visitor_new(false); |
| 954 | object_property_get(obj, string_output_get_visitor(sov), name, errp); |
| 955 | siv = string_input_visitor_new(string_output_get_string(sov)); |
| 956 | string_output_visitor_cleanup(sov); |
| 957 | visit_type_enum(string_input_get_visitor(siv), |
| 958 | &ret, strings, NULL, name, errp); |
| 959 | string_input_visitor_cleanup(siv); |
| 960 | |
| 961 | return ret; |
| 962 | } |
| 963 | |
| 964 | void object_property_get_uint16List(Object *obj, const char *name, |
| 965 | uint16List **list, Error **errp) |
| 966 | { |
| 967 | StringOutputVisitor *ov; |
| 968 | StringInputVisitor *iv; |
| 969 | |
| 970 | ov = string_output_visitor_new(false); |
| 971 | object_property_get(obj, string_output_get_visitor(ov), |
| 972 | name, errp); |
| 973 | iv = string_input_visitor_new(string_output_get_string(ov)); |
| 974 | visit_type_uint16List(string_input_get_visitor(iv), |
| 975 | list, NULL, errp); |
| 976 | string_output_visitor_cleanup(ov); |
| 977 | string_input_visitor_cleanup(iv); |
| 978 | } |
| 979 | |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 980 | void object_property_parse(Object *obj, const char *string, |
| 981 | const char *name, Error **errp) |
| 982 | { |
| 983 | StringInputVisitor *mi; |
| 984 | mi = string_input_visitor_new(string); |
| 985 | object_property_set(obj, string_input_get_visitor(mi), name, errp); |
| 986 | |
| 987 | string_input_visitor_cleanup(mi); |
| 988 | } |
| 989 | |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 990 | char *object_property_print(Object *obj, const char *name, bool human, |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 991 | Error **errp) |
| 992 | { |
| 993 | StringOutputVisitor *mo; |
| 994 | char *string; |
| 995 | |
Paolo Bonzini | 0b7593e | 2014-02-08 11:01:50 +0100 | [diff] [blame] | 996 | mo = string_output_visitor_new(human); |
Paolo Bonzini | 8185bfc | 2012-05-02 13:31:00 +0200 | [diff] [blame] | 997 | object_property_get(obj, string_output_get_visitor(mo), name, errp); |
Paolo Bonzini | b2cd7de | 2012-02-09 09:52:59 +0100 | [diff] [blame] | 998 | string = string_output_get_string(mo); |
| 999 | string_output_visitor_cleanup(mo); |
| 1000 | return string; |
| 1001 | } |
| 1002 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1003 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
| 1004 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 1005 | ObjectProperty *prop = object_property_find(obj, name, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1006 | if (prop == NULL) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1007 | return NULL; |
| 1008 | } |
| 1009 | |
| 1010 | return prop->type; |
| 1011 | } |
| 1012 | |
| 1013 | Object *object_get_root(void) |
| 1014 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 1015 | static Object *root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1016 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 1017 | if (!root) { |
| 1018 | root = object_new("container"); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1019 | } |
| 1020 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 1021 | return root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, |
| 1025 | const char *name, Error **errp) |
| 1026 | { |
| 1027 | Object *child = opaque; |
| 1028 | gchar *path; |
| 1029 | |
| 1030 | path = object_get_canonical_path(child); |
| 1031 | visit_type_str(v, &path, name, errp); |
| 1032 | g_free(path); |
| 1033 | } |
| 1034 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1035 | static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part) |
| 1036 | { |
| 1037 | return opaque; |
| 1038 | } |
| 1039 | |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 1040 | static void object_finalize_child_property(Object *obj, const char *name, |
| 1041 | void *opaque) |
| 1042 | { |
| 1043 | Object *child = opaque; |
| 1044 | |
| 1045 | object_unref(child); |
| 1046 | } |
| 1047 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1048 | void object_property_add_child(Object *obj, const char *name, |
| 1049 | Object *child, Error **errp) |
| 1050 | { |
Paolo Bonzini | b0ed5e9 | 2013-12-20 23:21:08 +0100 | [diff] [blame] | 1051 | Error *local_err = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1052 | gchar *type; |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1053 | ObjectProperty *op; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1054 | |
| 1055 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
| 1056 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1057 | op = object_property_add(obj, name, type, object_get_child_property, NULL, |
| 1058 | object_finalize_child_property, child, &local_err); |
Paolo Bonzini | b0ed5e9 | 2013-12-20 23:21:08 +0100 | [diff] [blame] | 1059 | if (local_err) { |
| 1060 | error_propagate(errp, local_err); |
| 1061 | goto out; |
| 1062 | } |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1063 | |
| 1064 | op->resolve = object_resolve_child_property; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1065 | object_ref(child); |
| 1066 | g_assert(child->parent == NULL); |
| 1067 | child->parent = obj; |
| 1068 | |
Paolo Bonzini | b0ed5e9 | 2013-12-20 23:21:08 +0100 | [diff] [blame] | 1069 | out: |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1070 | g_free(type); |
| 1071 | } |
| 1072 | |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1073 | void object_property_allow_set_link(Object *obj, const char *name, |
| 1074 | Object *val, Error **errp) |
| 1075 | { |
| 1076 | /* Allow the link to be set, always */ |
| 1077 | } |
| 1078 | |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1079 | typedef struct { |
| 1080 | Object **child; |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1081 | void (*check)(Object *, const char *, Object *, Error **); |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1082 | ObjectPropertyLinkFlags flags; |
| 1083 | } LinkProperty; |
| 1084 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1085 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, |
| 1086 | const char *name, Error **errp) |
| 1087 | { |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1088 | LinkProperty *lprop = opaque; |
| 1089 | Object **child = lprop->child; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1090 | gchar *path; |
| 1091 | |
| 1092 | if (*child) { |
| 1093 | path = object_get_canonical_path(*child); |
| 1094 | visit_type_str(v, &path, name, errp); |
| 1095 | g_free(path); |
| 1096 | } else { |
| 1097 | path = (gchar *)""; |
| 1098 | visit_type_str(v, &path, name, errp); |
| 1099 | } |
| 1100 | } |
| 1101 | |
Stefan Hajnoczi | f5ec670 | 2014-03-19 08:58:53 +0100 | [diff] [blame] | 1102 | /* |
| 1103 | * object_resolve_link: |
| 1104 | * |
| 1105 | * Lookup an object and ensure its type matches the link property type. This |
| 1106 | * is similar to object_resolve_path() except type verification against the |
| 1107 | * link property is performed. |
| 1108 | * |
| 1109 | * Returns: The matched object or NULL on path lookup failures. |
| 1110 | */ |
| 1111 | static Object *object_resolve_link(Object *obj, const char *name, |
| 1112 | const char *path, Error **errp) |
| 1113 | { |
| 1114 | const char *type; |
| 1115 | gchar *target_type; |
| 1116 | bool ambiguous = false; |
| 1117 | Object *target; |
| 1118 | |
| 1119 | /* Go from link<FOO> to FOO. */ |
| 1120 | type = object_property_get_type(obj, name, NULL); |
| 1121 | target_type = g_strndup(&type[5], strlen(type) - 6); |
| 1122 | target = object_resolve_path_type(path, target_type, &ambiguous); |
| 1123 | |
| 1124 | if (ambiguous) { |
Cole Robinson | f231b88 | 2014-03-21 19:42:26 -0400 | [diff] [blame] | 1125 | error_set(errp, ERROR_CLASS_GENERIC_ERROR, |
| 1126 | "Path '%s' does not uniquely identify an object", path); |
Stefan Hajnoczi | f5ec670 | 2014-03-19 08:58:53 +0100 | [diff] [blame] | 1127 | } else if (!target) { |
| 1128 | target = object_resolve_path(path, &ambiguous); |
| 1129 | if (target || ambiguous) { |
| 1130 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); |
| 1131 | } else { |
| 1132 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); |
| 1133 | } |
| 1134 | target = NULL; |
| 1135 | } |
| 1136 | g_free(target_type); |
| 1137 | |
| 1138 | return target; |
| 1139 | } |
| 1140 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1141 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, |
| 1142 | const char *name, Error **errp) |
| 1143 | { |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1144 | Error *local_err = NULL; |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1145 | LinkProperty *prop = opaque; |
| 1146 | Object **child = prop->child; |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1147 | Object *old_target = *child; |
| 1148 | Object *new_target = NULL; |
| 1149 | char *path = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1150 | |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1151 | visit_type_str(v, &path, name, &local_err); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1152 | |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1153 | if (!local_err && strcmp(path, "") != 0) { |
| 1154 | new_target = object_resolve_link(obj, name, path, &local_err); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | g_free(path); |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1158 | if (local_err) { |
| 1159 | error_propagate(errp, local_err); |
| 1160 | return; |
| 1161 | } |
Alexander Barabash | f0cdc96 | 2012-02-22 19:22:26 +0200 | [diff] [blame] | 1162 | |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1163 | prop->check(obj, name, new_target, &local_err); |
| 1164 | if (local_err) { |
| 1165 | error_propagate(errp, local_err); |
| 1166 | return; |
| 1167 | } |
| 1168 | |
Peter Crosthwaite | 8ffad85 | 2014-06-05 23:13:36 -0700 | [diff] [blame] | 1169 | object_ref(new_target); |
Stefan Hajnoczi | c6aed98 | 2014-03-19 08:58:54 +0100 | [diff] [blame] | 1170 | *child = new_target; |
Peter Crosthwaite | 8ffad85 | 2014-06-05 23:13:36 -0700 | [diff] [blame] | 1171 | object_unref(old_target); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1172 | } |
| 1173 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1174 | static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part) |
| 1175 | { |
| 1176 | LinkProperty *lprop = opaque; |
| 1177 | |
| 1178 | return *lprop->child; |
| 1179 | } |
| 1180 | |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1181 | static void object_release_link_property(Object *obj, const char *name, |
| 1182 | void *opaque) |
| 1183 | { |
| 1184 | LinkProperty *prop = opaque; |
| 1185 | |
| 1186 | if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { |
| 1187 | object_unref(*prop->child); |
| 1188 | } |
| 1189 | g_free(prop); |
| 1190 | } |
| 1191 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1192 | void object_property_add_link(Object *obj, const char *name, |
| 1193 | const char *type, Object **child, |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1194 | void (*check)(Object *, const char *, |
| 1195 | Object *, Error **), |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1196 | ObjectPropertyLinkFlags flags, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1197 | Error **errp) |
| 1198 | { |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1199 | Error *local_err = NULL; |
| 1200 | LinkProperty *prop = g_malloc(sizeof(*prop)); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1201 | gchar *full_type; |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1202 | ObjectProperty *op; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1203 | |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1204 | prop->child = child; |
Stefan Hajnoczi | 39f72ef | 2014-03-19 08:58:56 +0100 | [diff] [blame] | 1205 | prop->check = check; |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1206 | prop->flags = flags; |
| 1207 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1208 | full_type = g_strdup_printf("link<%s>", type); |
| 1209 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1210 | op = object_property_add(obj, name, full_type, |
| 1211 | object_get_link_property, |
| 1212 | check ? object_set_link_property : NULL, |
| 1213 | object_release_link_property, |
| 1214 | prop, |
| 1215 | &local_err); |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1216 | if (local_err) { |
| 1217 | error_propagate(errp, local_err); |
| 1218 | g_free(prop); |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1219 | goto out; |
Stefan Hajnoczi | 9561fda | 2014-03-19 08:58:55 +0100 | [diff] [blame] | 1220 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1221 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1222 | op->resolve = object_resolve_link_property; |
| 1223 | |
| 1224 | out: |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1225 | g_free(full_type); |
| 1226 | } |
| 1227 | |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1228 | gchar *object_get_canonical_path_component(Object *obj) |
| 1229 | { |
| 1230 | ObjectProperty *prop = NULL; |
| 1231 | |
| 1232 | g_assert(obj); |
| 1233 | g_assert(obj->parent != NULL); |
| 1234 | |
| 1235 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { |
| 1236 | if (!object_property_is_child(prop)) { |
| 1237 | continue; |
| 1238 | } |
| 1239 | |
| 1240 | if (prop->opaque == obj) { |
| 1241 | return g_strdup(prop->name); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | /* obj had a parent but was not a child, should never happen */ |
| 1246 | g_assert_not_reached(); |
| 1247 | return NULL; |
| 1248 | } |
| 1249 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1250 | gchar *object_get_canonical_path(Object *obj) |
| 1251 | { |
| 1252 | Object *root = object_get_root(); |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1253 | char *newpath, *path = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1254 | |
| 1255 | while (obj != root) { |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1256 | char *component = object_get_canonical_path_component(obj); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1257 | |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1258 | if (path) { |
| 1259 | newpath = g_strdup_printf("%s/%s", component, path); |
| 1260 | g_free(component); |
| 1261 | g_free(path); |
| 1262 | path = newpath; |
| 1263 | } else { |
| 1264 | path = component; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1265 | } |
| 1266 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1267 | obj = obj->parent; |
| 1268 | } |
| 1269 | |
Stefan Hajnoczi | 11f590b | 2014-03-03 11:30:02 +0100 | [diff] [blame] | 1270 | newpath = g_strdup_printf("/%s", path ? path : ""); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1271 | g_free(path); |
| 1272 | |
| 1273 | return newpath; |
| 1274 | } |
| 1275 | |
Andreas Färber | 3e84b48 | 2013-01-15 02:55:10 +0100 | [diff] [blame] | 1276 | Object *object_resolve_path_component(Object *parent, const gchar *part) |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1277 | { |
Paolo Bonzini | 89bfe00 | 2012-04-12 18:00:18 +0200 | [diff] [blame] | 1278 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1279 | if (prop == NULL) { |
| 1280 | return NULL; |
| 1281 | } |
| 1282 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1283 | if (prop->resolve) { |
| 1284 | return prop->resolve(parent, prop->opaque, part); |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1285 | } else { |
| 1286 | return NULL; |
| 1287 | } |
| 1288 | } |
| 1289 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1290 | static Object *object_resolve_abs_path(Object *parent, |
| 1291 | gchar **parts, |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1292 | const char *typename, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1293 | int index) |
| 1294 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1295 | Object *child; |
| 1296 | |
| 1297 | if (parts[index] == NULL) { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1298 | return object_dynamic_cast(parent, typename); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1299 | } |
| 1300 | |
| 1301 | if (strcmp(parts[index], "") == 0) { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1302 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1303 | } |
| 1304 | |
Paolo Bonzini | a612b2a | 2012-03-27 18:38:45 +0200 | [diff] [blame] | 1305 | child = object_resolve_path_component(parent, parts[index]); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1306 | if (!child) { |
| 1307 | return NULL; |
| 1308 | } |
| 1309 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1310 | return object_resolve_abs_path(child, parts, typename, index + 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | static Object *object_resolve_partial_path(Object *parent, |
| 1314 | gchar **parts, |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1315 | const char *typename, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1316 | bool *ambiguous) |
| 1317 | { |
| 1318 | Object *obj; |
| 1319 | ObjectProperty *prop; |
| 1320 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1321 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1322 | |
| 1323 | QTAILQ_FOREACH(prop, &parent->properties, node) { |
| 1324 | Object *found; |
| 1325 | |
Andreas Färber | 5d9d3f4 | 2012-05-27 00:32:40 +0200 | [diff] [blame] | 1326 | if (!object_property_is_child(prop)) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1327 | continue; |
| 1328 | } |
| 1329 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1330 | found = object_resolve_partial_path(prop->opaque, parts, |
| 1331 | typename, ambiguous); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1332 | if (found) { |
| 1333 | if (obj) { |
| 1334 | if (ambiguous) { |
| 1335 | *ambiguous = true; |
| 1336 | } |
| 1337 | return NULL; |
| 1338 | } |
| 1339 | obj = found; |
| 1340 | } |
| 1341 | |
| 1342 | if (ambiguous && *ambiguous) { |
| 1343 | return NULL; |
| 1344 | } |
| 1345 | } |
| 1346 | |
| 1347 | return obj; |
| 1348 | } |
| 1349 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1350 | Object *object_resolve_path_type(const char *path, const char *typename, |
| 1351 | bool *ambiguous) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1352 | { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1353 | Object *obj; |
| 1354 | gchar **parts; |
| 1355 | |
| 1356 | parts = g_strsplit(path, "/", 0); |
Paolo Bonzini | 2e1103f | 2013-04-18 18:44:02 +0200 | [diff] [blame] | 1357 | assert(parts); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1358 | |
Paolo Bonzini | 2e1103f | 2013-04-18 18:44:02 +0200 | [diff] [blame] | 1359 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1360 | if (ambiguous) { |
| 1361 | *ambiguous = false; |
| 1362 | } |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1363 | obj = object_resolve_partial_path(object_get_root(), parts, |
| 1364 | typename, ambiguous); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1365 | } else { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1366 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1367 | } |
| 1368 | |
| 1369 | g_strfreev(parts); |
| 1370 | |
| 1371 | return obj; |
| 1372 | } |
| 1373 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame] | 1374 | Object *object_resolve_path(const char *path, bool *ambiguous) |
| 1375 | { |
| 1376 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); |
| 1377 | } |
| 1378 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1379 | typedef struct StringProperty |
| 1380 | { |
| 1381 | char *(*get)(Object *, Error **); |
| 1382 | void (*set)(Object *, const char *, Error **); |
| 1383 | } StringProperty; |
| 1384 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1385 | static void property_get_str(Object *obj, Visitor *v, void *opaque, |
| 1386 | const char *name, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1387 | { |
| 1388 | StringProperty *prop = opaque; |
| 1389 | char *value; |
| 1390 | |
| 1391 | value = prop->get(obj, errp); |
| 1392 | if (value) { |
| 1393 | visit_type_str(v, &value, name, errp); |
| 1394 | g_free(value); |
| 1395 | } |
| 1396 | } |
| 1397 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1398 | static void property_set_str(Object *obj, Visitor *v, void *opaque, |
| 1399 | const char *name, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1400 | { |
| 1401 | StringProperty *prop = opaque; |
| 1402 | char *value; |
| 1403 | Error *local_err = NULL; |
| 1404 | |
| 1405 | visit_type_str(v, &value, name, &local_err); |
| 1406 | if (local_err) { |
| 1407 | error_propagate(errp, local_err); |
| 1408 | return; |
| 1409 | } |
| 1410 | |
| 1411 | prop->set(obj, value, errp); |
| 1412 | g_free(value); |
| 1413 | } |
| 1414 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1415 | static void property_release_str(Object *obj, const char *name, |
| 1416 | void *opaque) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1417 | { |
| 1418 | StringProperty *prop = opaque; |
| 1419 | g_free(prop); |
| 1420 | } |
| 1421 | |
| 1422 | void object_property_add_str(Object *obj, const char *name, |
| 1423 | char *(*get)(Object *, Error **), |
| 1424 | void (*set)(Object *, const char *, Error **), |
| 1425 | Error **errp) |
| 1426 | { |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1427 | Error *local_err = NULL; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1428 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
| 1429 | |
| 1430 | prop->get = get; |
| 1431 | prop->set = set; |
| 1432 | |
| 1433 | object_property_add(obj, name, "string", |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1434 | get ? property_get_str : NULL, |
| 1435 | set ? property_set_str : NULL, |
| 1436 | property_release_str, |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1437 | prop, &local_err); |
| 1438 | if (local_err) { |
| 1439 | error_propagate(errp, local_err); |
| 1440 | g_free(prop); |
| 1441 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1442 | } |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1443 | |
Anthony Liguori | 0e55884 | 2012-06-25 10:32:46 -0500 | [diff] [blame] | 1444 | typedef struct BoolProperty |
| 1445 | { |
| 1446 | bool (*get)(Object *, Error **); |
| 1447 | void (*set)(Object *, bool, Error **); |
| 1448 | } BoolProperty; |
| 1449 | |
| 1450 | static void property_get_bool(Object *obj, Visitor *v, void *opaque, |
| 1451 | const char *name, Error **errp) |
| 1452 | { |
| 1453 | BoolProperty *prop = opaque; |
| 1454 | bool value; |
| 1455 | |
| 1456 | value = prop->get(obj, errp); |
| 1457 | visit_type_bool(v, &value, name, errp); |
| 1458 | } |
| 1459 | |
| 1460 | static void property_set_bool(Object *obj, Visitor *v, void *opaque, |
| 1461 | const char *name, Error **errp) |
| 1462 | { |
| 1463 | BoolProperty *prop = opaque; |
| 1464 | bool value; |
| 1465 | Error *local_err = NULL; |
| 1466 | |
| 1467 | visit_type_bool(v, &value, name, &local_err); |
| 1468 | if (local_err) { |
| 1469 | error_propagate(errp, local_err); |
| 1470 | return; |
| 1471 | } |
| 1472 | |
| 1473 | prop->set(obj, value, errp); |
| 1474 | } |
| 1475 | |
| 1476 | static void property_release_bool(Object *obj, const char *name, |
| 1477 | void *opaque) |
| 1478 | { |
| 1479 | BoolProperty *prop = opaque; |
| 1480 | g_free(prop); |
| 1481 | } |
| 1482 | |
| 1483 | void object_property_add_bool(Object *obj, const char *name, |
| 1484 | bool (*get)(Object *, Error **), |
| 1485 | void (*set)(Object *, bool, Error **), |
| 1486 | Error **errp) |
| 1487 | { |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1488 | Error *local_err = NULL; |
Anthony Liguori | 0e55884 | 2012-06-25 10:32:46 -0500 | [diff] [blame] | 1489 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
| 1490 | |
| 1491 | prop->get = get; |
| 1492 | prop->set = set; |
| 1493 | |
| 1494 | object_property_add(obj, name, "bool", |
| 1495 | get ? property_get_bool : NULL, |
| 1496 | set ? property_set_bool : NULL, |
| 1497 | property_release_bool, |
Stefan Hajnoczi | a01aedc | 2014-03-04 15:28:18 +0100 | [diff] [blame] | 1498 | prop, &local_err); |
| 1499 | if (local_err) { |
| 1500 | error_propagate(errp, local_err); |
| 1501 | g_free(prop); |
| 1502 | } |
Anthony Liguori | 0e55884 | 2012-06-25 10:32:46 -0500 | [diff] [blame] | 1503 | } |
| 1504 | |
Paolo Bonzini | 2f262e0 | 2012-04-02 17:33:51 +0200 | [diff] [blame] | 1505 | static char *qdev_get_type(Object *obj, Error **errp) |
| 1506 | { |
| 1507 | return g_strdup(object_get_typename(obj)); |
| 1508 | } |
| 1509 | |
Michael S. Tsirkin | e732ea6 | 2013-09-22 10:10:17 +0300 | [diff] [blame] | 1510 | static void property_get_uint8_ptr(Object *obj, Visitor *v, |
| 1511 | void *opaque, const char *name, |
| 1512 | Error **errp) |
| 1513 | { |
| 1514 | uint8_t value = *(uint8_t *)opaque; |
| 1515 | visit_type_uint8(v, &value, name, errp); |
| 1516 | } |
| 1517 | |
| 1518 | static void property_get_uint16_ptr(Object *obj, Visitor *v, |
| 1519 | void *opaque, const char *name, |
| 1520 | Error **errp) |
| 1521 | { |
| 1522 | uint16_t value = *(uint16_t *)opaque; |
| 1523 | visit_type_uint16(v, &value, name, errp); |
| 1524 | } |
| 1525 | |
| 1526 | static void property_get_uint32_ptr(Object *obj, Visitor *v, |
| 1527 | void *opaque, const char *name, |
| 1528 | Error **errp) |
| 1529 | { |
| 1530 | uint32_t value = *(uint32_t *)opaque; |
| 1531 | visit_type_uint32(v, &value, name, errp); |
| 1532 | } |
| 1533 | |
| 1534 | static void property_get_uint64_ptr(Object *obj, Visitor *v, |
| 1535 | void *opaque, const char *name, |
| 1536 | Error **errp) |
| 1537 | { |
| 1538 | uint64_t value = *(uint64_t *)opaque; |
| 1539 | visit_type_uint64(v, &value, name, errp); |
| 1540 | } |
| 1541 | |
| 1542 | void object_property_add_uint8_ptr(Object *obj, const char *name, |
| 1543 | const uint8_t *v, Error **errp) |
| 1544 | { |
| 1545 | object_property_add(obj, name, "uint8", property_get_uint8_ptr, |
| 1546 | NULL, NULL, (void *)v, errp); |
| 1547 | } |
| 1548 | |
| 1549 | void object_property_add_uint16_ptr(Object *obj, const char *name, |
| 1550 | const uint16_t *v, Error **errp) |
| 1551 | { |
| 1552 | object_property_add(obj, name, "uint16", property_get_uint16_ptr, |
| 1553 | NULL, NULL, (void *)v, errp); |
| 1554 | } |
| 1555 | |
| 1556 | void object_property_add_uint32_ptr(Object *obj, const char *name, |
| 1557 | const uint32_t *v, Error **errp) |
| 1558 | { |
| 1559 | object_property_add(obj, name, "uint32", property_get_uint32_ptr, |
| 1560 | NULL, NULL, (void *)v, errp); |
| 1561 | } |
| 1562 | |
| 1563 | void object_property_add_uint64_ptr(Object *obj, const char *name, |
| 1564 | const uint64_t *v, Error **errp) |
| 1565 | { |
| 1566 | object_property_add(obj, name, "uint64", property_get_uint64_ptr, |
| 1567 | NULL, NULL, (void *)v, errp); |
| 1568 | } |
| 1569 | |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1570 | typedef struct { |
| 1571 | Object *target_obj; |
| 1572 | const char *target_name; |
| 1573 | } AliasProperty; |
| 1574 | |
| 1575 | static void property_get_alias(Object *obj, struct Visitor *v, void *opaque, |
| 1576 | const char *name, Error **errp) |
| 1577 | { |
| 1578 | AliasProperty *prop = opaque; |
| 1579 | |
| 1580 | object_property_get(prop->target_obj, v, prop->target_name, errp); |
| 1581 | } |
| 1582 | |
| 1583 | static void property_set_alias(Object *obj, struct Visitor *v, void *opaque, |
| 1584 | const char *name, Error **errp) |
| 1585 | { |
| 1586 | AliasProperty *prop = opaque; |
| 1587 | |
| 1588 | object_property_set(prop->target_obj, v, prop->target_name, errp); |
| 1589 | } |
| 1590 | |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1591 | static Object *property_resolve_alias(Object *obj, void *opaque, |
| 1592 | const gchar *part) |
| 1593 | { |
| 1594 | AliasProperty *prop = opaque; |
| 1595 | |
| 1596 | return object_resolve_path_component(prop->target_obj, prop->target_name); |
| 1597 | } |
| 1598 | |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1599 | static void property_release_alias(Object *obj, const char *name, void *opaque) |
| 1600 | { |
| 1601 | AliasProperty *prop = opaque; |
| 1602 | |
| 1603 | g_free(prop); |
| 1604 | } |
| 1605 | |
| 1606 | void object_property_add_alias(Object *obj, const char *name, |
| 1607 | Object *target_obj, const char *target_name, |
| 1608 | Error **errp) |
| 1609 | { |
| 1610 | AliasProperty *prop; |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1611 | ObjectProperty *op; |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1612 | ObjectProperty *target_prop; |
Paolo Bonzini | d190698 | 2014-06-10 11:17:35 +0200 | [diff] [blame] | 1613 | gchar *prop_type; |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1614 | |
| 1615 | target_prop = object_property_find(target_obj, target_name, errp); |
| 1616 | if (!target_prop) { |
| 1617 | return; |
| 1618 | } |
| 1619 | |
Paolo Bonzini | d190698 | 2014-06-10 11:17:35 +0200 | [diff] [blame] | 1620 | if (object_property_is_child(target_prop)) { |
| 1621 | prop_type = g_strdup_printf("link%s", |
| 1622 | target_prop->type + strlen("child")); |
| 1623 | } else { |
| 1624 | prop_type = g_strdup(target_prop->type); |
| 1625 | } |
| 1626 | |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1627 | prop = g_malloc(sizeof(*prop)); |
| 1628 | prop->target_obj = target_obj; |
| 1629 | prop->target_name = target_name; |
| 1630 | |
Paolo Bonzini | d190698 | 2014-06-10 11:17:35 +0200 | [diff] [blame] | 1631 | op = object_property_add(obj, name, prop_type, |
Paolo Bonzini | 64607d0 | 2014-06-05 13:11:51 +0200 | [diff] [blame] | 1632 | property_get_alias, |
| 1633 | property_set_alias, |
| 1634 | property_release_alias, |
| 1635 | prop, errp); |
| 1636 | op->resolve = property_resolve_alias; |
Paolo Bonzini | d190698 | 2014-06-10 11:17:35 +0200 | [diff] [blame] | 1637 | |
| 1638 | g_free(prop_type); |
Stefan Hajnoczi | ef7c7ff | 2014-06-18 17:58:28 +0800 | [diff] [blame] | 1639 | } |
| 1640 | |
Paolo Bonzini | 2f262e0 | 2012-04-02 17:33:51 +0200 | [diff] [blame] | 1641 | static void object_instance_init(Object *obj) |
| 1642 | { |
| 1643 | object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); |
| 1644 | } |
| 1645 | |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1646 | static void register_types(void) |
| 1647 | { |
| 1648 | static TypeInfo interface_info = { |
| 1649 | .name = TYPE_INTERFACE, |
Anthony Liguori | 33e95c6 | 2012-08-10 13:16:10 +1000 | [diff] [blame] | 1650 | .class_size = sizeof(InterfaceClass), |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1651 | .abstract = true, |
| 1652 | }; |
| 1653 | |
| 1654 | static TypeInfo object_info = { |
| 1655 | .name = TYPE_OBJECT, |
| 1656 | .instance_size = sizeof(Object), |
Paolo Bonzini | 2f262e0 | 2012-04-02 17:33:51 +0200 | [diff] [blame] | 1657 | .instance_init = object_instance_init, |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1658 | .abstract = true, |
| 1659 | }; |
| 1660 | |
Paolo Bonzini | 049cb3c | 2012-04-04 15:58:40 +0200 | [diff] [blame] | 1661 | type_interface = type_register_internal(&interface_info); |
| 1662 | type_register_internal(&object_info); |
Paolo Bonzini | 745549c | 2012-03-31 16:45:54 +0200 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | type_init(register_types) |