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 | |
| 13 | #include "qemu/object.h" |
| 14 | #include "qemu-common.h" |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 15 | #include "qapi/qapi-visit-core.h" |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 16 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 17 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
| 18 | * of the QOM core on QObject? */ |
| 19 | #include "qemu/qom-qobject.h" |
| 20 | #include "qobject.h" |
| 21 | #include "qbool.h" |
| 22 | #include "qint.h" |
| 23 | #include "qstring.h" |
| 24 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 25 | #define MAX_INTERFACES 32 |
| 26 | |
| 27 | typedef struct InterfaceImpl InterfaceImpl; |
| 28 | typedef struct TypeImpl TypeImpl; |
| 29 | |
| 30 | struct InterfaceImpl |
| 31 | { |
| 32 | const char *parent; |
| 33 | void (*interface_initfn)(ObjectClass *class, void *data); |
| 34 | TypeImpl *type; |
| 35 | }; |
| 36 | |
| 37 | struct TypeImpl |
| 38 | { |
| 39 | const char *name; |
| 40 | |
| 41 | size_t class_size; |
| 42 | |
| 43 | size_t instance_size; |
| 44 | |
| 45 | void (*class_init)(ObjectClass *klass, void *data); |
| 46 | void (*class_finalize)(ObjectClass *klass, void *data); |
| 47 | |
| 48 | void *class_data; |
| 49 | |
| 50 | void (*instance_init)(Object *obj); |
| 51 | void (*instance_finalize)(Object *obj); |
| 52 | |
| 53 | bool abstract; |
| 54 | |
| 55 | const char *parent; |
| 56 | TypeImpl *parent_type; |
| 57 | |
| 58 | ObjectClass *class; |
| 59 | |
| 60 | int num_interfaces; |
| 61 | InterfaceImpl interfaces[MAX_INTERFACES]; |
| 62 | }; |
| 63 | |
| 64 | typedef struct Interface |
| 65 | { |
| 66 | Object parent; |
| 67 | Object *obj; |
| 68 | } Interface; |
| 69 | |
| 70 | #define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE) |
| 71 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 72 | static Type type_interface; |
| 73 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 74 | static GHashTable *type_table_get(void) |
| 75 | { |
| 76 | static GHashTable *type_table; |
| 77 | |
| 78 | if (type_table == NULL) { |
| 79 | type_table = g_hash_table_new(g_str_hash, g_str_equal); |
| 80 | } |
| 81 | |
| 82 | return type_table; |
| 83 | } |
| 84 | |
| 85 | static void type_table_add(TypeImpl *ti) |
| 86 | { |
| 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 | |
| 95 | TypeImpl *type_register(const TypeInfo *info) |
| 96 | { |
| 97 | TypeImpl *ti = g_malloc0(sizeof(*ti)); |
| 98 | |
| 99 | g_assert(info->name != NULL); |
| 100 | |
Anthony Liguori | 7309335 | 2012-01-25 13:37:36 -0600 | [diff] [blame] | 101 | if (type_table_lookup(info->name) != NULL) { |
| 102 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); |
| 103 | abort(); |
| 104 | } |
| 105 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 106 | ti->name = g_strdup(info->name); |
| 107 | ti->parent = g_strdup(info->parent); |
| 108 | |
| 109 | ti->class_size = info->class_size; |
| 110 | ti->instance_size = info->instance_size; |
| 111 | |
| 112 | ti->class_init = info->class_init; |
| 113 | ti->class_finalize = info->class_finalize; |
| 114 | ti->class_data = info->class_data; |
| 115 | |
| 116 | ti->instance_init = info->instance_init; |
| 117 | ti->instance_finalize = info->instance_finalize; |
| 118 | |
| 119 | ti->abstract = info->abstract; |
| 120 | |
| 121 | if (info->interfaces) { |
| 122 | int i; |
| 123 | |
| 124 | for (i = 0; info->interfaces[i].type; i++) { |
| 125 | ti->interfaces[i].parent = info->interfaces[i].type; |
| 126 | ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn; |
| 127 | ti->num_interfaces++; |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | type_table_add(ti); |
| 132 | |
| 133 | return ti; |
| 134 | } |
| 135 | |
| 136 | TypeImpl *type_register_static(const TypeInfo *info) |
| 137 | { |
| 138 | return type_register(info); |
| 139 | } |
| 140 | |
| 141 | static TypeImpl *type_get_by_name(const char *name) |
| 142 | { |
| 143 | if (name == NULL) { |
| 144 | return NULL; |
| 145 | } |
| 146 | |
| 147 | return type_table_lookup(name); |
| 148 | } |
| 149 | |
| 150 | static TypeImpl *type_get_parent(TypeImpl *type) |
| 151 | { |
| 152 | if (!type->parent_type && type->parent) { |
| 153 | type->parent_type = type_get_by_name(type->parent); |
| 154 | g_assert(type->parent_type != NULL); |
| 155 | } |
| 156 | |
| 157 | return type->parent_type; |
| 158 | } |
| 159 | |
| 160 | static bool type_has_parent(TypeImpl *type) |
| 161 | { |
| 162 | return (type->parent != NULL); |
| 163 | } |
| 164 | |
| 165 | static size_t type_class_get_size(TypeImpl *ti) |
| 166 | { |
| 167 | if (ti->class_size) { |
| 168 | return ti->class_size; |
| 169 | } |
| 170 | |
| 171 | if (type_has_parent(ti)) { |
| 172 | return type_class_get_size(type_get_parent(ti)); |
| 173 | } |
| 174 | |
| 175 | return sizeof(ObjectClass); |
| 176 | } |
| 177 | |
| 178 | static void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface) |
| 179 | { |
| 180 | TypeInfo info = { |
| 181 | .instance_size = sizeof(Interface), |
| 182 | .parent = iface->parent, |
| 183 | .class_size = sizeof(InterfaceClass), |
| 184 | .class_init = iface->interface_initfn, |
| 185 | .abstract = true, |
| 186 | }; |
| 187 | char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent); |
| 188 | |
| 189 | info.name = name; |
| 190 | iface->type = type_register(&info); |
| 191 | g_free(name); |
| 192 | } |
| 193 | |
| 194 | static void type_class_init(TypeImpl *ti) |
| 195 | { |
| 196 | size_t class_size = sizeof(ObjectClass); |
| 197 | int i; |
| 198 | |
| 199 | if (ti->class) { |
| 200 | return; |
| 201 | } |
| 202 | |
| 203 | ti->class_size = type_class_get_size(ti); |
| 204 | |
| 205 | ti->class = g_malloc0(ti->class_size); |
| 206 | ti->class->type = ti; |
| 207 | |
| 208 | if (type_has_parent(ti)) { |
| 209 | TypeImpl *parent = type_get_parent(ti); |
| 210 | |
| 211 | type_class_init(parent); |
| 212 | |
| 213 | class_size = parent->class_size; |
| 214 | g_assert(parent->class_size <= ti->class_size); |
| 215 | |
| 216 | memcpy((void *)ti->class + sizeof(ObjectClass), |
| 217 | (void *)parent->class + sizeof(ObjectClass), |
| 218 | parent->class_size - sizeof(ObjectClass)); |
| 219 | } |
| 220 | |
| 221 | memset((void *)ti->class + class_size, 0, ti->class_size - class_size); |
| 222 | |
| 223 | for (i = 0; i < ti->num_interfaces; i++) { |
| 224 | type_class_interface_init(ti, &ti->interfaces[i]); |
| 225 | } |
| 226 | |
| 227 | if (ti->class_init) { |
| 228 | ti->class_init(ti->class, ti->class_data); |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | static void object_interface_init(Object *obj, InterfaceImpl *iface) |
| 233 | { |
| 234 | TypeImpl *ti = iface->type; |
| 235 | Interface *iface_obj; |
| 236 | |
| 237 | iface_obj = INTERFACE(object_new(ti->name)); |
| 238 | iface_obj->obj = obj; |
| 239 | |
| 240 | obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj); |
| 241 | } |
| 242 | |
| 243 | static void object_init_with_type(Object *obj, TypeImpl *ti) |
| 244 | { |
| 245 | int i; |
| 246 | |
| 247 | if (type_has_parent(ti)) { |
| 248 | object_init_with_type(obj, type_get_parent(ti)); |
| 249 | } |
| 250 | |
| 251 | for (i = 0; i < ti->num_interfaces; i++) { |
| 252 | object_interface_init(obj, &ti->interfaces[i]); |
| 253 | } |
| 254 | |
| 255 | if (ti->instance_init) { |
| 256 | ti->instance_init(obj); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void object_initialize_with_type(void *data, TypeImpl *type) |
| 261 | { |
| 262 | Object *obj = data; |
| 263 | |
| 264 | g_assert(type != NULL); |
| 265 | g_assert(type->instance_size >= sizeof(ObjectClass)); |
| 266 | |
| 267 | type_class_init(type); |
| 268 | g_assert(type->abstract == false); |
| 269 | |
| 270 | memset(obj, 0, type->instance_size); |
| 271 | obj->class = type->class; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 272 | QTAILQ_INIT(&obj->properties); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 273 | object_init_with_type(obj, type); |
| 274 | } |
| 275 | |
| 276 | void object_initialize(void *data, const char *typename) |
| 277 | { |
| 278 | TypeImpl *type = type_get_by_name(typename); |
| 279 | |
| 280 | object_initialize_with_type(data, type); |
| 281 | } |
| 282 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 283 | static void object_property_del_all(Object *obj) |
| 284 | { |
| 285 | while (!QTAILQ_EMPTY(&obj->properties)) { |
| 286 | ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); |
| 287 | |
| 288 | QTAILQ_REMOVE(&obj->properties, prop, node); |
| 289 | |
| 290 | if (prop->release) { |
| 291 | prop->release(obj, prop->name, prop->opaque); |
| 292 | } |
| 293 | |
| 294 | g_free(prop->name); |
| 295 | g_free(prop->type); |
| 296 | g_free(prop); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | static void object_property_del_child(Object *obj, Object *child, Error **errp) |
| 301 | { |
| 302 | ObjectProperty *prop; |
| 303 | |
| 304 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 305 | if (!strstart(prop->type, "child<", NULL)) { |
| 306 | continue; |
| 307 | } |
| 308 | |
| 309 | if (prop->opaque == child) { |
| 310 | object_property_del(obj, prop->name, errp); |
| 311 | } |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | void object_unparent(Object *obj) |
| 316 | { |
| 317 | if (obj->parent) { |
| 318 | object_property_del_child(obj->parent, obj, NULL); |
| 319 | } |
| 320 | } |
| 321 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 322 | static void object_deinit(Object *obj, TypeImpl *type) |
| 323 | { |
| 324 | if (type->instance_finalize) { |
| 325 | type->instance_finalize(obj); |
| 326 | } |
| 327 | |
| 328 | while (obj->interfaces) { |
| 329 | Interface *iface_obj = obj->interfaces->data; |
| 330 | obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces); |
| 331 | object_delete(OBJECT(iface_obj)); |
| 332 | } |
| 333 | |
| 334 | if (type_has_parent(type)) { |
| 335 | object_deinit(obj, type_get_parent(type)); |
| 336 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 337 | |
| 338 | object_unparent(obj); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | void object_finalize(void *data) |
| 342 | { |
| 343 | Object *obj = data; |
| 344 | TypeImpl *ti = obj->class->type; |
| 345 | |
| 346 | object_deinit(obj, ti); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 347 | object_property_del_all(obj); |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 348 | |
| 349 | g_assert(obj->ref == 0); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | Object *object_new_with_type(Type type) |
| 353 | { |
| 354 | Object *obj; |
| 355 | |
| 356 | g_assert(type != NULL); |
| 357 | |
| 358 | obj = g_malloc(type->instance_size); |
| 359 | object_initialize_with_type(obj, type); |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 360 | object_ref(obj); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 361 | |
| 362 | return obj; |
| 363 | } |
| 364 | |
| 365 | Object *object_new(const char *typename) |
| 366 | { |
| 367 | TypeImpl *ti = type_get_by_name(typename); |
| 368 | |
| 369 | return object_new_with_type(ti); |
| 370 | } |
| 371 | |
| 372 | void object_delete(Object *obj) |
| 373 | { |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 374 | object_unref(obj); |
| 375 | g_assert(obj->ref == 0); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 376 | g_free(obj); |
| 377 | } |
| 378 | |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 379 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 380 | { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 381 | assert(target_type); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 382 | |
| 383 | /* Check if typename is a direct ancestor of type */ |
| 384 | while (type) { |
| 385 | if (type == target_type) { |
| 386 | return true; |
| 387 | } |
| 388 | |
| 389 | type = type_get_parent(type); |
| 390 | } |
| 391 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 392 | return false; |
| 393 | } |
| 394 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 395 | static bool object_is_type(Object *obj, TypeImpl *target_type) |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 396 | { |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 397 | return !target_type || type_is_ancestor(obj->class->type, target_type); |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 398 | } |
| 399 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 400 | Object *object_dynamic_cast(Object *obj, const char *typename) |
| 401 | { |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 402 | TypeImpl *target_type = type_get_by_name(typename); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 403 | GSList *i; |
| 404 | |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 405 | /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT, |
| 406 | * we want to go back from interfaces to the parent. |
| 407 | */ |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 408 | if (target_type && object_is_type(obj, target_type)) { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 409 | return obj; |
| 410 | } |
| 411 | |
| 412 | /* Check if obj is an interface and its containing object is a direct |
| 413 | * ancestor of typename. In principle we could do this test at the very |
| 414 | * beginning of object_dynamic_cast, avoiding a second call to |
| 415 | * object_is_type. However, casting between interfaces is relatively |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 416 | * rare, and object_is_type(obj, type_interface) would fail almost always. |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 417 | * |
| 418 | * Perhaps we could add a magic value to the object header for increased |
| 419 | * (run-time) type safety and to speed up tests like this one. If we ever |
| 420 | * do that we can revisit the order here. |
| 421 | */ |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 422 | if (object_is_type(obj, type_interface)) { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 423 | assert(!obj->interfaces); |
| 424 | obj = INTERFACE(obj)->obj; |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 425 | if (object_is_type(obj, target_type)) { |
Paolo Bonzini | acc4af3 | 2012-02-03 11:57:23 +0100 | [diff] [blame] | 426 | return obj; |
| 427 | } |
| 428 | } |
| 429 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 430 | if (!target_type) { |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 431 | return obj; |
| 432 | } |
| 433 | |
| 434 | /* Check if obj has an interface of typename */ |
| 435 | for (i = obj->interfaces; i; i = i->next) { |
| 436 | Interface *iface = i->data; |
| 437 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 438 | if (object_is_type(OBJECT(iface), target_type)) { |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 439 | return OBJECT(iface); |
| 440 | } |
| 441 | } |
| 442 | |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 443 | return NULL; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | static void register_interface(void) |
| 448 | { |
| 449 | static TypeInfo interface_info = { |
| 450 | .name = TYPE_INTERFACE, |
| 451 | .instance_size = sizeof(Interface), |
| 452 | .abstract = true, |
| 453 | }; |
| 454 | |
Paolo Bonzini | 9970bd8 | 2012-02-03 11:51:39 +0100 | [diff] [blame] | 455 | type_interface = type_register_static(&interface_info); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | device_init(register_interface); |
| 459 | |
| 460 | Object *object_dynamic_cast_assert(Object *obj, const char *typename) |
| 461 | { |
| 462 | Object *inst; |
| 463 | |
| 464 | inst = object_dynamic_cast(obj, typename); |
| 465 | |
| 466 | if (!inst) { |
| 467 | fprintf(stderr, "Object %p is not an instance of type %s\n", |
| 468 | obj, typename); |
| 469 | abort(); |
| 470 | } |
| 471 | |
| 472 | return inst; |
| 473 | } |
| 474 | |
| 475 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, |
| 476 | const char *typename) |
| 477 | { |
| 478 | TypeImpl *target_type = type_get_by_name(typename); |
| 479 | TypeImpl *type = class->type; |
| 480 | |
| 481 | while (type) { |
| 482 | if (type == target_type) { |
| 483 | return class; |
| 484 | } |
| 485 | |
| 486 | type = type_get_parent(type); |
| 487 | } |
| 488 | |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, |
| 493 | const char *typename) |
| 494 | { |
| 495 | ObjectClass *ret = object_class_dynamic_cast(class, typename); |
| 496 | |
| 497 | if (!ret) { |
| 498 | fprintf(stderr, "Object %p is not an instance of type %s\n", |
| 499 | class, typename); |
| 500 | abort(); |
| 501 | } |
| 502 | |
| 503 | return ret; |
| 504 | } |
| 505 | |
| 506 | const char *object_get_typename(Object *obj) |
| 507 | { |
| 508 | return obj->class->type->name; |
| 509 | } |
| 510 | |
| 511 | ObjectClass *object_get_class(Object *obj) |
| 512 | { |
| 513 | return obj->class; |
| 514 | } |
| 515 | |
| 516 | const char *object_class_get_name(ObjectClass *klass) |
| 517 | { |
| 518 | return klass->type->name; |
| 519 | } |
| 520 | |
| 521 | ObjectClass *object_class_by_name(const char *typename) |
| 522 | { |
| 523 | TypeImpl *type = type_get_by_name(typename); |
| 524 | |
| 525 | if (!type) { |
| 526 | return NULL; |
| 527 | } |
| 528 | |
| 529 | type_class_init(type); |
| 530 | |
| 531 | return type->class; |
| 532 | } |
| 533 | |
| 534 | typedef struct OCFData |
| 535 | { |
| 536 | void (*fn)(ObjectClass *klass, void *opaque); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 537 | const char *implements_type; |
| 538 | bool include_abstract; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 539 | void *opaque; |
| 540 | } OCFData; |
| 541 | |
| 542 | static void object_class_foreach_tramp(gpointer key, gpointer value, |
| 543 | gpointer opaque) |
| 544 | { |
| 545 | OCFData *data = opaque; |
| 546 | TypeImpl *type = value; |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 547 | ObjectClass *k; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 548 | |
| 549 | type_class_init(type); |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 550 | k = type->class; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 551 | |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 552 | if (!data->include_abstract && type->abstract) { |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | if (data->implements_type && |
| 557 | !object_class_dynamic_cast(k, data->implements_type)) { |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | data->fn(k, data->opaque); |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 565 | const char *implements_type, bool include_abstract, |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 566 | void *opaque) |
| 567 | { |
Anthony Liguori | 93c511a | 2011-12-22 14:11:53 -0600 | [diff] [blame] | 568 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
Anthony Liguori | 2f28d2f | 2011-12-03 17:10:08 -0600 | [diff] [blame] | 569 | |
| 570 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
| 571 | } |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 572 | |
| 573 | void object_ref(Object *obj) |
| 574 | { |
| 575 | obj->ref++; |
| 576 | } |
| 577 | |
| 578 | void object_unref(Object *obj) |
| 579 | { |
| 580 | g_assert(obj->ref > 0); |
| 581 | obj->ref--; |
| 582 | |
| 583 | /* parent always holds a reference to its children */ |
| 584 | if (obj->ref == 0) { |
| 585 | object_finalize(obj); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | void object_property_add(Object *obj, const char *name, const char *type, |
| 590 | ObjectPropertyAccessor *get, |
| 591 | ObjectPropertyAccessor *set, |
| 592 | ObjectPropertyRelease *release, |
| 593 | void *opaque, Error **errp) |
| 594 | { |
| 595 | ObjectProperty *prop = g_malloc0(sizeof(*prop)); |
| 596 | |
| 597 | prop->name = g_strdup(name); |
| 598 | prop->type = g_strdup(type); |
| 599 | |
| 600 | prop->get = get; |
| 601 | prop->set = set; |
| 602 | prop->release = release; |
| 603 | prop->opaque = opaque; |
| 604 | |
| 605 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); |
| 606 | } |
| 607 | |
| 608 | static ObjectProperty *object_property_find(Object *obj, const char *name) |
| 609 | { |
| 610 | ObjectProperty *prop; |
| 611 | |
| 612 | QTAILQ_FOREACH(prop, &obj->properties, node) { |
| 613 | if (strcmp(prop->name, name) == 0) { |
| 614 | return prop; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | return NULL; |
| 619 | } |
| 620 | |
| 621 | void object_property_del(Object *obj, const char *name, Error **errp) |
| 622 | { |
| 623 | ObjectProperty *prop = object_property_find(obj, name); |
| 624 | |
| 625 | QTAILQ_REMOVE(&obj->properties, prop, node); |
| 626 | |
| 627 | prop->release(obj, prop->name, prop->opaque); |
| 628 | |
| 629 | g_free(prop->name); |
| 630 | g_free(prop->type); |
| 631 | g_free(prop); |
| 632 | } |
| 633 | |
| 634 | void object_property_get(Object *obj, Visitor *v, const char *name, |
| 635 | Error **errp) |
| 636 | { |
| 637 | ObjectProperty *prop = object_property_find(obj, name); |
| 638 | |
| 639 | if (prop == NULL) { |
| 640 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); |
| 641 | return; |
| 642 | } |
| 643 | |
| 644 | if (!prop->get) { |
| 645 | error_set(errp, QERR_PERMISSION_DENIED); |
| 646 | } else { |
| 647 | prop->get(obj, v, prop->opaque, name, errp); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | void object_property_set(Object *obj, Visitor *v, const char *name, |
| 652 | Error **errp) |
| 653 | { |
| 654 | ObjectProperty *prop = object_property_find(obj, name); |
| 655 | |
| 656 | if (prop == NULL) { |
| 657 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); |
| 658 | return; |
| 659 | } |
| 660 | |
| 661 | if (!prop->set) { |
| 662 | error_set(errp, QERR_PERMISSION_DENIED); |
| 663 | } else { |
| 664 | prop->set(obj, v, prop->opaque, name, errp); |
| 665 | } |
| 666 | } |
| 667 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 668 | void object_property_set_str(Object *obj, const char *value, |
| 669 | const char *name, Error **errp) |
| 670 | { |
| 671 | QString *qstr = qstring_from_str(value); |
| 672 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); |
| 673 | |
| 674 | QDECREF(qstr); |
| 675 | } |
| 676 | |
| 677 | char *object_property_get_str(Object *obj, const char *name, |
| 678 | Error **errp) |
| 679 | { |
| 680 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 681 | QString *qstring; |
| 682 | char *retval; |
| 683 | |
| 684 | if (!ret) { |
| 685 | return NULL; |
| 686 | } |
| 687 | qstring = qobject_to_qstring(ret); |
| 688 | if (!qstring) { |
| 689 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); |
| 690 | retval = NULL; |
| 691 | } else { |
| 692 | retval = g_strdup(qstring_get_str(qstring)); |
| 693 | } |
| 694 | |
| 695 | QDECREF(qstring); |
| 696 | return retval; |
| 697 | } |
| 698 | |
| 699 | void object_property_set_bool(Object *obj, bool value, |
| 700 | const char *name, Error **errp) |
| 701 | { |
| 702 | QBool *qbool = qbool_from_int(value); |
| 703 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); |
| 704 | |
| 705 | QDECREF(qbool); |
| 706 | } |
| 707 | |
| 708 | bool object_property_get_bool(Object *obj, const char *name, |
| 709 | Error **errp) |
| 710 | { |
| 711 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 712 | QBool *qbool; |
| 713 | bool retval; |
| 714 | |
| 715 | if (!ret) { |
| 716 | return false; |
| 717 | } |
| 718 | qbool = qobject_to_qbool(ret); |
| 719 | if (!qbool) { |
| 720 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); |
| 721 | retval = false; |
| 722 | } else { |
| 723 | retval = qbool_get_int(qbool); |
| 724 | } |
| 725 | |
| 726 | QDECREF(qbool); |
| 727 | return retval; |
| 728 | } |
| 729 | |
| 730 | void object_property_set_int(Object *obj, int64_t value, |
| 731 | const char *name, Error **errp) |
| 732 | { |
| 733 | QInt *qint = qint_from_int(value); |
| 734 | object_property_set_qobject(obj, QOBJECT(qint), name, errp); |
| 735 | |
| 736 | QDECREF(qint); |
| 737 | } |
| 738 | |
| 739 | int64_t object_property_get_int(Object *obj, const char *name, |
| 740 | Error **errp) |
| 741 | { |
| 742 | QObject *ret = object_property_get_qobject(obj, name, errp); |
| 743 | QInt *qint; |
| 744 | int64_t retval; |
| 745 | |
| 746 | if (!ret) { |
| 747 | return -1; |
| 748 | } |
| 749 | qint = qobject_to_qint(ret); |
| 750 | if (!qint) { |
| 751 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); |
| 752 | retval = -1; |
| 753 | } else { |
| 754 | retval = qint_get_int(qint); |
| 755 | } |
| 756 | |
| 757 | QDECREF(qint); |
| 758 | return retval; |
| 759 | } |
| 760 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 761 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
| 762 | { |
| 763 | ObjectProperty *prop = object_property_find(obj, name); |
| 764 | |
| 765 | if (prop == NULL) { |
| 766 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); |
| 767 | return NULL; |
| 768 | } |
| 769 | |
| 770 | return prop->type; |
| 771 | } |
| 772 | |
| 773 | Object *object_get_root(void) |
| 774 | { |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 775 | static Object *root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 776 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 777 | if (!root) { |
| 778 | root = object_new("container"); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 779 | } |
| 780 | |
Anthony Liguori | 8b45d44 | 2011-12-23 09:08:05 -0600 | [diff] [blame] | 781 | return root; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, |
| 785 | const char *name, Error **errp) |
| 786 | { |
| 787 | Object *child = opaque; |
| 788 | gchar *path; |
| 789 | |
| 790 | path = object_get_canonical_path(child); |
| 791 | visit_type_str(v, &path, name, errp); |
| 792 | g_free(path); |
| 793 | } |
| 794 | |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 795 | static void object_finalize_child_property(Object *obj, const char *name, |
| 796 | void *opaque) |
| 797 | { |
| 798 | Object *child = opaque; |
| 799 | |
| 800 | object_unref(child); |
| 801 | } |
| 802 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 803 | void object_property_add_child(Object *obj, const char *name, |
| 804 | Object *child, Error **errp) |
| 805 | { |
| 806 | gchar *type; |
| 807 | |
| 808 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
| 809 | |
| 810 | object_property_add(obj, name, type, object_get_child_property, |
Anthony Liguori | db85b57 | 2011-12-23 08:47:39 -0600 | [diff] [blame] | 811 | NULL, object_finalize_child_property, child, errp); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 812 | |
| 813 | object_ref(child); |
| 814 | g_assert(child->parent == NULL); |
| 815 | child->parent = obj; |
| 816 | |
| 817 | g_free(type); |
| 818 | } |
| 819 | |
| 820 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, |
| 821 | const char *name, Error **errp) |
| 822 | { |
| 823 | Object **child = opaque; |
| 824 | gchar *path; |
| 825 | |
| 826 | if (*child) { |
| 827 | path = object_get_canonical_path(*child); |
| 828 | visit_type_str(v, &path, name, errp); |
| 829 | g_free(path); |
| 830 | } else { |
| 831 | path = (gchar *)""; |
| 832 | visit_type_str(v, &path, name, errp); |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, |
| 837 | const char *name, Error **errp) |
| 838 | { |
| 839 | Object **child = opaque; |
| 840 | bool ambiguous = false; |
| 841 | const char *type; |
| 842 | char *path; |
| 843 | |
| 844 | type = object_property_get_type(obj, name, NULL); |
| 845 | |
| 846 | visit_type_str(v, &path, name, errp); |
| 847 | |
| 848 | if (*child) { |
| 849 | object_unref(*child); |
| 850 | } |
| 851 | |
| 852 | if (strcmp(path, "") != 0) { |
| 853 | Object *target; |
| 854 | |
| 855 | target = object_resolve_path(path, &ambiguous); |
| 856 | if (target) { |
Paolo Bonzini | 8f770d3 | 2012-02-03 15:41:13 +0100 | [diff] [blame] | 857 | /* Go from link<FOO> to FOO. */ |
| 858 | gchar *target_type = g_strndup(&type[5], strlen(type) - 6); |
Anthony Liguori | fe40e62 | 2011-12-23 08:35:43 -0600 | [diff] [blame] | 859 | if (object_dynamic_cast(target, target_type)) { |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 860 | object_ref(target); |
Anthony Liguori | fe40e62 | 2011-12-23 08:35:43 -0600 | [diff] [blame] | 861 | *child = target; |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 862 | } else { |
| 863 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, type); |
| 864 | } |
| 865 | |
| 866 | g_free(target_type); |
| 867 | } else { |
| 868 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); |
| 869 | } |
| 870 | } else { |
| 871 | *child = NULL; |
| 872 | } |
| 873 | |
| 874 | g_free(path); |
| 875 | } |
| 876 | |
| 877 | void object_property_add_link(Object *obj, const char *name, |
| 878 | const char *type, Object **child, |
| 879 | Error **errp) |
| 880 | { |
| 881 | gchar *full_type; |
| 882 | |
| 883 | full_type = g_strdup_printf("link<%s>", type); |
| 884 | |
| 885 | object_property_add(obj, name, full_type, |
| 886 | object_get_link_property, |
| 887 | object_set_link_property, |
| 888 | NULL, child, errp); |
| 889 | |
| 890 | g_free(full_type); |
| 891 | } |
| 892 | |
| 893 | gchar *object_get_canonical_path(Object *obj) |
| 894 | { |
| 895 | Object *root = object_get_root(); |
| 896 | char *newpath = NULL, *path = NULL; |
| 897 | |
| 898 | while (obj != root) { |
| 899 | ObjectProperty *prop = NULL; |
| 900 | |
| 901 | g_assert(obj->parent != NULL); |
| 902 | |
| 903 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { |
| 904 | if (!strstart(prop->type, "child<", NULL)) { |
| 905 | continue; |
| 906 | } |
| 907 | |
| 908 | if (prop->opaque == obj) { |
| 909 | if (path) { |
| 910 | newpath = g_strdup_printf("%s/%s", prop->name, path); |
| 911 | g_free(path); |
| 912 | path = newpath; |
| 913 | } else { |
| 914 | path = g_strdup(prop->name); |
| 915 | } |
| 916 | break; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | g_assert(prop != NULL); |
| 921 | |
| 922 | obj = obj->parent; |
| 923 | } |
| 924 | |
| 925 | newpath = g_strdup_printf("/%s", path); |
| 926 | g_free(path); |
| 927 | |
| 928 | return newpath; |
| 929 | } |
| 930 | |
| 931 | static Object *object_resolve_abs_path(Object *parent, |
| 932 | gchar **parts, |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 933 | const char *typename, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 934 | int index) |
| 935 | { |
| 936 | ObjectProperty *prop; |
| 937 | Object *child; |
| 938 | |
| 939 | if (parts[index] == NULL) { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 940 | return object_dynamic_cast(parent, typename); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | if (strcmp(parts[index], "") == 0) { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 944 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | prop = object_property_find(parent, parts[index]); |
| 948 | if (prop == NULL) { |
| 949 | return NULL; |
| 950 | } |
| 951 | |
| 952 | child = NULL; |
| 953 | if (strstart(prop->type, "link<", NULL)) { |
| 954 | Object **pchild = prop->opaque; |
| 955 | if (*pchild) { |
| 956 | child = *pchild; |
| 957 | } |
| 958 | } else if (strstart(prop->type, "child<", NULL)) { |
| 959 | child = prop->opaque; |
| 960 | } |
| 961 | |
| 962 | if (!child) { |
| 963 | return NULL; |
| 964 | } |
| 965 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 966 | return object_resolve_abs_path(child, parts, typename, index + 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 967 | } |
| 968 | |
| 969 | static Object *object_resolve_partial_path(Object *parent, |
| 970 | gchar **parts, |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 971 | const char *typename, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 972 | bool *ambiguous) |
| 973 | { |
| 974 | Object *obj; |
| 975 | ObjectProperty *prop; |
| 976 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 977 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 978 | |
| 979 | QTAILQ_FOREACH(prop, &parent->properties, node) { |
| 980 | Object *found; |
| 981 | |
| 982 | if (!strstart(prop->type, "child<", NULL)) { |
| 983 | continue; |
| 984 | } |
| 985 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 986 | found = object_resolve_partial_path(prop->opaque, parts, |
| 987 | typename, ambiguous); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 988 | if (found) { |
| 989 | if (obj) { |
| 990 | if (ambiguous) { |
| 991 | *ambiguous = true; |
| 992 | } |
| 993 | return NULL; |
| 994 | } |
| 995 | obj = found; |
| 996 | } |
| 997 | |
| 998 | if (ambiguous && *ambiguous) { |
| 999 | return NULL; |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | return obj; |
| 1004 | } |
| 1005 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 1006 | Object *object_resolve_path_type(const char *path, const char *typename, |
| 1007 | bool *ambiguous) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1008 | { |
| 1009 | bool partial_path = true; |
| 1010 | Object *obj; |
| 1011 | gchar **parts; |
| 1012 | |
| 1013 | parts = g_strsplit(path, "/", 0); |
| 1014 | if (parts == NULL || parts[0] == NULL) { |
| 1015 | g_strfreev(parts); |
| 1016 | return object_get_root(); |
| 1017 | } |
| 1018 | |
| 1019 | if (strcmp(parts[0], "") == 0) { |
| 1020 | partial_path = false; |
| 1021 | } |
| 1022 | |
| 1023 | if (partial_path) { |
| 1024 | if (ambiguous) { |
| 1025 | *ambiguous = false; |
| 1026 | } |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 1027 | obj = object_resolve_partial_path(object_get_root(), parts, |
| 1028 | typename, ambiguous); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1029 | } else { |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 1030 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | g_strfreev(parts); |
| 1034 | |
| 1035 | return obj; |
| 1036 | } |
| 1037 | |
Paolo Bonzini | 02fe2db | 2012-02-03 11:21:01 +0100 | [diff] [blame^] | 1038 | Object *object_resolve_path(const char *path, bool *ambiguous) |
| 1039 | { |
| 1040 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); |
| 1041 | } |
| 1042 | |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1043 | typedef struct StringProperty |
| 1044 | { |
| 1045 | char *(*get)(Object *, Error **); |
| 1046 | void (*set)(Object *, const char *, Error **); |
| 1047 | } StringProperty; |
| 1048 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1049 | static void property_get_str(Object *obj, Visitor *v, void *opaque, |
| 1050 | const char *name, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1051 | { |
| 1052 | StringProperty *prop = opaque; |
| 1053 | char *value; |
| 1054 | |
| 1055 | value = prop->get(obj, errp); |
| 1056 | if (value) { |
| 1057 | visit_type_str(v, &value, name, errp); |
| 1058 | g_free(value); |
| 1059 | } |
| 1060 | } |
| 1061 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1062 | static void property_set_str(Object *obj, Visitor *v, void *opaque, |
| 1063 | const char *name, Error **errp) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1064 | { |
| 1065 | StringProperty *prop = opaque; |
| 1066 | char *value; |
| 1067 | Error *local_err = NULL; |
| 1068 | |
| 1069 | visit_type_str(v, &value, name, &local_err); |
| 1070 | if (local_err) { |
| 1071 | error_propagate(errp, local_err); |
| 1072 | return; |
| 1073 | } |
| 1074 | |
| 1075 | prop->set(obj, value, errp); |
| 1076 | g_free(value); |
| 1077 | } |
| 1078 | |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1079 | static void property_release_str(Object *obj, const char *name, |
| 1080 | void *opaque) |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1081 | { |
| 1082 | StringProperty *prop = opaque; |
| 1083 | g_free(prop); |
| 1084 | } |
| 1085 | |
| 1086 | void object_property_add_str(Object *obj, const char *name, |
| 1087 | char *(*get)(Object *, Error **), |
| 1088 | void (*set)(Object *, const char *, Error **), |
| 1089 | Error **errp) |
| 1090 | { |
| 1091 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
| 1092 | |
| 1093 | prop->get = get; |
| 1094 | prop->set = set; |
| 1095 | |
| 1096 | object_property_add(obj, name, "string", |
Paolo Bonzini | 7b7b7d1 | 2012-02-01 17:16:22 +0100 | [diff] [blame] | 1097 | get ? property_get_str : NULL, |
| 1098 | set ? property_set_str : NULL, |
| 1099 | property_release_str, |
Anthony Liguori | 57c9faf | 2012-01-30 08:55:55 -0600 | [diff] [blame] | 1100 | prop, errp); |
| 1101 | } |