blob: dd53d242a5cffe6810cf6b315ee5f6efa56d3316 [file] [log] [blame]
Anthony Liguori2f28d2f2011-12-03 17:10:08 -06001/*
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 Bonzini14cccb62012-12-17 18:19:50 +010013#include "qom/object.h"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060014#include "qemu-common.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010015#include "qapi/visitor.h"
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +010016#include "qapi/string-input-visitor.h"
17#include "qapi/string-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010018#include "qapi/qmp/qerror.h"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060019
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010020/* TODO: replace QObject with a simpler visitor to avoid a dependency
21 * of the QOM core on QObject? */
Paolo Bonzini14cccb62012-12-17 18:19:50 +010022#include "qom/qom-qobject.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010023#include "qapi/qmp/qobject.h"
24#include "qapi/qmp/qbool.h"
25#include "qapi/qmp/qint.h"
26#include "qapi/qmp/qstring.h"
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010027
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060028#define MAX_INTERFACES 32
29
30typedef struct InterfaceImpl InterfaceImpl;
31typedef struct TypeImpl TypeImpl;
32
33struct InterfaceImpl
34{
Anthony Liguori33e95c62012-08-10 13:16:10 +100035 const char *typename;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060036};
37
38struct TypeImpl
39{
40 const char *name;
41
42 size_t class_size;
43
44 size_t instance_size;
45
46 void (*class_init)(ObjectClass *klass, void *data);
Paolo Bonzini3b50e312012-05-02 13:30:55 +020047 void (*class_base_init)(ObjectClass *klass, void *data);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060048 void (*class_finalize)(ObjectClass *klass, void *data);
49
50 void *class_data;
51
52 void (*instance_init)(Object *obj);
53 void (*instance_finalize)(Object *obj);
54
55 bool abstract;
56
57 const char *parent;
58 TypeImpl *parent_type;
59
60 ObjectClass *class;
61
62 int num_interfaces;
63 InterfaceImpl interfaces[MAX_INTERFACES];
64};
65
Paolo Bonzini9970bd82012-02-03 11:51:39 +010066static Type type_interface;
67
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060068static GHashTable *type_table_get(void)
69{
70 static GHashTable *type_table;
71
72 if (type_table == NULL) {
73 type_table = g_hash_table_new(g_str_hash, g_str_equal);
74 }
75
76 return type_table;
77}
78
79static void type_table_add(TypeImpl *ti)
80{
81 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
82}
83
84static TypeImpl *type_table_lookup(const char *name)
85{
86 return g_hash_table_lookup(type_table_get(), name);
87}
88
Paolo Bonzini049cb3c2012-04-04 15:58:40 +020089static TypeImpl *type_register_internal(const TypeInfo *info)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060090{
91 TypeImpl *ti = g_malloc0(sizeof(*ti));
Anthony Liguori33e95c62012-08-10 13:16:10 +100092 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060093
94 g_assert(info->name != NULL);
95
Anthony Liguori73093352012-01-25 13:37:36 -060096 if (type_table_lookup(info->name) != NULL) {
97 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
98 abort();
99 }
100
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600101 ti->name = g_strdup(info->name);
102 ti->parent = g_strdup(info->parent);
103
104 ti->class_size = info->class_size;
105 ti->instance_size = info->instance_size;
106
107 ti->class_init = info->class_init;
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200108 ti->class_base_init = info->class_base_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600109 ti->class_finalize = info->class_finalize;
110 ti->class_data = info->class_data;
111
112 ti->instance_init = info->instance_init;
113 ti->instance_finalize = info->instance_finalize;
114
115 ti->abstract = info->abstract;
116
Anthony Liguori33e95c62012-08-10 13:16:10 +1000117 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
118 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600119 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000120 ti->num_interfaces = i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600121
122 type_table_add(ti);
123
124 return ti;
125}
126
Paolo Bonzini049cb3c2012-04-04 15:58:40 +0200127TypeImpl *type_register(const TypeInfo *info)
128{
129 assert(info->parent);
130 return type_register_internal(info);
131}
132
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600133TypeImpl *type_register_static(const TypeInfo *info)
134{
135 return type_register(info);
136}
137
138static TypeImpl *type_get_by_name(const char *name)
139{
140 if (name == NULL) {
141 return NULL;
142 }
143
144 return type_table_lookup(name);
145}
146
147static TypeImpl *type_get_parent(TypeImpl *type)
148{
149 if (!type->parent_type && type->parent) {
150 type->parent_type = type_get_by_name(type->parent);
151 g_assert(type->parent_type != NULL);
152 }
153
154 return type->parent_type;
155}
156
157static bool type_has_parent(TypeImpl *type)
158{
159 return (type->parent != NULL);
160}
161
162static size_t type_class_get_size(TypeImpl *ti)
163{
164 if (ti->class_size) {
165 return ti->class_size;
166 }
167
168 if (type_has_parent(ti)) {
169 return type_class_get_size(type_get_parent(ti));
170 }
171
172 return sizeof(ObjectClass);
173}
174
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400175static size_t type_object_get_size(TypeImpl *ti)
176{
177 if (ti->instance_size) {
178 return ti->instance_size;
179 }
180
181 if (type_has_parent(ti)) {
182 return type_object_get_size(type_get_parent(ti));
183 }
184
185 return 0;
186}
187
Anthony Liguori33e95c62012-08-10 13:16:10 +1000188static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600189{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000190 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600191
Anthony Liguori33e95c62012-08-10 13:16:10 +1000192 /* Check if typename is a direct ancestor of type */
193 while (type) {
194 if (type == target_type) {
195 return true;
196 }
197
198 type = type_get_parent(type);
199 }
200
201 return false;
202}
203
204static void type_initialize(TypeImpl *ti);
205
206static void type_initialize_interface(TypeImpl *ti, const char *parent)
207{
208 InterfaceClass *new_iface;
209 TypeInfo info = { };
210 TypeImpl *iface_impl;
211
212 info.parent = parent;
213 info.name = g_strdup_printf("%s::%s", ti->name, info.parent);
214 info.abstract = true;
215
216 iface_impl = type_register(&info);
217 type_initialize(iface_impl);
218 g_free((char *)info.name);
219
220 new_iface = (InterfaceClass *)iface_impl->class;
221 new_iface->concrete_class = ti->class;
222
223 ti->class->interfaces = g_slist_append(ti->class->interfaces,
224 iface_impl->class);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600225}
226
Igor Mitsyankoac451032012-02-28 15:57:11 +0400227static void type_initialize(TypeImpl *ti)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600228{
Paolo Bonzini745549c2012-03-31 16:45:54 +0200229 TypeImpl *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600230
231 if (ti->class) {
232 return;
233 }
234
235 ti->class_size = type_class_get_size(ti);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400236 ti->instance_size = type_object_get_size(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600237
238 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600239
Paolo Bonzini745549c2012-03-31 16:45:54 +0200240 parent = type_get_parent(ti);
241 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400242 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000243 GSList *e;
244 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600245
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600246 g_assert(parent->class_size <= ti->class_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200247 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000248 ti->class->interfaces = NULL;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000249
250 for (e = parent->class->interfaces; e; e = e->next) {
251 ObjectClass *iface = e->data;
252 type_initialize_interface(ti, object_class_get_name(iface));
253 }
254
255 for (i = 0; i < ti->num_interfaces; i++) {
256 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
257 for (e = ti->class->interfaces; e; e = e->next) {
258 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
259
260 if (type_is_ancestor(target_type, t)) {
261 break;
262 }
263 }
264
265 if (e) {
266 continue;
267 }
268
269 type_initialize_interface(ti, ti->interfaces[i].typename);
270 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600271 }
272
Paolo Bonzini745549c2012-03-31 16:45:54 +0200273 ti->class->type = ti;
274
275 while (parent) {
276 if (parent->class_base_init) {
277 parent->class_base_init(ti->class, ti->class_data);
278 }
279 parent = type_get_parent(parent);
280 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600281
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600282 if (ti->class_init) {
283 ti->class_init(ti->class, ti->class_data);
284 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600285
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600286
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600287}
288
289static void object_init_with_type(Object *obj, TypeImpl *ti)
290{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600291 if (type_has_parent(ti)) {
292 object_init_with_type(obj, type_get_parent(ti));
293 }
294
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600295 if (ti->instance_init) {
296 ti->instance_init(obj);
297 }
298}
299
300void object_initialize_with_type(void *data, TypeImpl *type)
301{
302 Object *obj = data;
303
304 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400305 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400306
307 g_assert(type->instance_size >= sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600308 g_assert(type->abstract == false);
309
310 memset(obj, 0, type->instance_size);
311 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100312 object_ref(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600313 QTAILQ_INIT(&obj->properties);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600314 object_init_with_type(obj, type);
315}
316
317void object_initialize(void *data, const char *typename)
318{
319 TypeImpl *type = type_get_by_name(typename);
320
321 object_initialize_with_type(data, type);
322}
323
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200324static inline bool object_property_is_child(ObjectProperty *prop)
325{
326 return strstart(prop->type, "child<", NULL);
327}
328
329static inline bool object_property_is_link(ObjectProperty *prop)
330{
331 return strstart(prop->type, "link<", NULL);
332}
333
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600334static void object_property_del_all(Object *obj)
335{
336 while (!QTAILQ_EMPTY(&obj->properties)) {
337 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
338
339 QTAILQ_REMOVE(&obj->properties, prop, node);
340
341 if (prop->release) {
342 prop->release(obj, prop->name, prop->opaque);
343 }
344
345 g_free(prop->name);
346 g_free(prop->type);
347 g_free(prop);
348 }
349}
350
351static void object_property_del_child(Object *obj, Object *child, Error **errp)
352{
353 ObjectProperty *prop;
354
355 QTAILQ_FOREACH(prop, &obj->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200356 if (object_property_is_child(prop) && prop->opaque == child) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600357 object_property_del(obj, prop->name, errp);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100358 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600359 }
360 }
361}
362
363void object_unparent(Object *obj)
364{
Paolo Bonzinie0a83fc2013-04-02 15:50:00 +0200365 if (!obj->parent) {
366 return;
367 }
368
Paolo Bonzini52e636c2013-01-25 14:12:30 +0100369 object_ref(obj);
Paolo Bonzini667d22d2012-11-23 09:47:13 +0100370 if (obj->class->unparent) {
371 (obj->class->unparent)(obj);
372 }
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200373 if (obj->parent) {
374 object_property_del_child(obj->parent, obj, NULL);
375 }
Paolo Bonzini52e636c2013-01-25 14:12:30 +0100376 object_unref(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600377}
378
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600379static void object_deinit(Object *obj, TypeImpl *type)
380{
381 if (type->instance_finalize) {
382 type->instance_finalize(obj);
383 }
384
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600385 if (type_has_parent(type)) {
386 object_deinit(obj, type_get_parent(type));
387 }
388}
389
Paolo Bonzini339c2702012-11-23 09:47:16 +0100390static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600391{
392 Object *obj = data;
393 TypeImpl *ti = obj->class->type;
394
395 object_deinit(obj, ti);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600396 object_property_del_all(obj);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600397
398 g_assert(obj->ref == 0);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100399 if (obj->free) {
400 obj->free(obj);
401 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600402}
403
404Object *object_new_with_type(Type type)
405{
406 Object *obj;
407
408 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400409 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600410
411 obj = g_malloc(type->instance_size);
412 object_initialize_with_type(obj, type);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100413 obj->free = g_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600414
415 return obj;
416}
417
418Object *object_new(const char *typename)
419{
420 TypeImpl *ti = type_get_by_name(typename);
421
422 return object_new_with_type(ti);
423}
424
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600425Object *object_dynamic_cast(Object *obj, const char *typename)
426{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100427 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100428 return obj;
429 }
430
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600431 return NULL;
432}
433
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600434Object *object_dynamic_cast_assert(Object *obj, const char *typename)
435{
436 Object *inst;
437
438 inst = object_dynamic_cast(obj, typename);
439
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100440 if (!inst && obj) {
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600441 fprintf(stderr, "Object %p is not an instance of type %s\n",
442 obj, typename);
443 abort();
444 }
445
446 return inst;
447}
448
449ObjectClass *object_class_dynamic_cast(ObjectClass *class,
450 const char *typename)
451{
452 TypeImpl *target_type = type_get_by_name(typename);
453 TypeImpl *type = class->type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000454 ObjectClass *ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600455
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000456 if (type->class->interfaces &&
457 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000458 int found = 0;
459 GSList *i;
460
461 for (i = class->interfaces; i; i = i->next) {
462 ObjectClass *target_class = i->data;
463
464 if (type_is_ancestor(target_class->type, target_type)) {
465 ret = target_class;
466 found++;
467 }
468 }
469
470 /* The match was ambiguous, don't allow a cast */
471 if (found > 1) {
472 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600473 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000474 } else if (type_is_ancestor(type, target_type)) {
475 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600476 }
477
Anthony Liguori33e95c62012-08-10 13:16:10 +1000478 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600479}
480
481ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
482 const char *typename)
483{
484 ObjectClass *ret = object_class_dynamic_cast(class, typename);
485
486 if (!ret) {
487 fprintf(stderr, "Object %p is not an instance of type %s\n",
488 class, typename);
489 abort();
490 }
491
492 return ret;
493}
494
495const char *object_get_typename(Object *obj)
496{
497 return obj->class->type->name;
498}
499
500ObjectClass *object_get_class(Object *obj)
501{
502 return obj->class;
503}
504
Andreas Färber17862372013-01-23 12:20:18 +0100505bool object_class_is_abstract(ObjectClass *klass)
506{
507 return klass->type->abstract;
508}
509
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600510const char *object_class_get_name(ObjectClass *klass)
511{
512 return klass->type->name;
513}
514
515ObjectClass *object_class_by_name(const char *typename)
516{
517 TypeImpl *type = type_get_by_name(typename);
518
519 if (!type) {
520 return NULL;
521 }
522
Igor Mitsyankoac451032012-02-28 15:57:11 +0400523 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600524
525 return type->class;
526}
527
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200528ObjectClass *object_class_get_parent(ObjectClass *class)
529{
530 TypeImpl *type = type_get_parent(class->type);
531
532 if (!type) {
533 return NULL;
534 }
535
536 type_initialize(type);
537
538 return type->class;
539}
540
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600541typedef struct OCFData
542{
543 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600544 const char *implements_type;
545 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600546 void *opaque;
547} OCFData;
548
549static void object_class_foreach_tramp(gpointer key, gpointer value,
550 gpointer opaque)
551{
552 OCFData *data = opaque;
553 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600554 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600555
Igor Mitsyankoac451032012-02-28 15:57:11 +0400556 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600557 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600558
Anthony Liguori93c511a2011-12-22 14:11:53 -0600559 if (!data->include_abstract && type->abstract) {
560 return;
561 }
562
563 if (data->implements_type &&
564 !object_class_dynamic_cast(k, data->implements_type)) {
565 return;
566 }
567
568 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600569}
570
571void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600572 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600573 void *opaque)
574{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600575 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600576
577 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
578}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600579
Paolo Bonzini32efc532012-04-11 23:30:20 +0200580int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
581 void *opaque)
582{
583 ObjectProperty *prop;
584 int ret = 0;
585
586 QTAILQ_FOREACH(prop, &obj->properties, node) {
587 if (object_property_is_child(prop)) {
588 ret = fn(prop->opaque, opaque);
589 if (ret != 0) {
590 break;
591 }
592 }
593 }
594 return ret;
595}
596
Andreas Färber418ba9e2012-02-25 23:07:34 +0100597static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
598{
599 GSList **list = opaque;
600
601 *list = g_slist_prepend(*list, klass);
602}
603
604GSList *object_class_get_list(const char *implements_type,
605 bool include_abstract)
606{
607 GSList *list = NULL;
608
609 object_class_foreach(object_class_get_list_tramp,
610 implements_type, include_abstract, &list);
611 return list;
612}
613
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600614void object_ref(Object *obj)
615{
616 obj->ref++;
617}
618
619void object_unref(Object *obj)
620{
621 g_assert(obj->ref > 0);
622 obj->ref--;
623
624 /* parent always holds a reference to its children */
625 if (obj->ref == 0) {
626 object_finalize(obj);
627 }
628}
629
630void object_property_add(Object *obj, const char *name, const char *type,
631 ObjectPropertyAccessor *get,
632 ObjectPropertyAccessor *set,
633 ObjectPropertyRelease *release,
634 void *opaque, Error **errp)
635{
Peter Maydell54852b02013-03-25 13:15:13 +0000636 ObjectProperty *prop;
637
638 QTAILQ_FOREACH(prop, &obj->properties, node) {
639 if (strcmp(prop->name, name) == 0) {
640 error_setg(errp, "attempt to add duplicate property '%s'"
641 " to object (type '%s')", name,
642 object_get_typename(obj));
643 return;
644 }
645 }
646
647 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600648
649 prop->name = g_strdup(name);
650 prop->type = g_strdup(type);
651
652 prop->get = get;
653 prop->set = set;
654 prop->release = release;
655 prop->opaque = opaque;
656
657 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
658}
659
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200660ObjectProperty *object_property_find(Object *obj, const char *name,
661 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600662{
663 ObjectProperty *prop;
664
665 QTAILQ_FOREACH(prop, &obj->properties, node) {
666 if (strcmp(prop->name, name) == 0) {
667 return prop;
668 }
669 }
670
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200671 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600672 return NULL;
673}
674
675void object_property_del(Object *obj, const char *name, Error **errp)
676{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200677 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600678 if (prop == NULL) {
Anthony Liguori0866aca2011-12-23 15:34:39 -0600679 return;
680 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600681
Anthony Liguori0866aca2011-12-23 15:34:39 -0600682 if (prop->release) {
683 prop->release(obj, name, prop->opaque);
684 }
685
686 QTAILQ_REMOVE(&obj->properties, prop, node);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600687
688 g_free(prop->name);
689 g_free(prop->type);
690 g_free(prop);
691}
692
693void object_property_get(Object *obj, Visitor *v, const char *name,
694 Error **errp)
695{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200696 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600697 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600698 return;
699 }
700
701 if (!prop->get) {
702 error_set(errp, QERR_PERMISSION_DENIED);
703 } else {
704 prop->get(obj, v, prop->opaque, name, errp);
705 }
706}
707
708void object_property_set(Object *obj, Visitor *v, const char *name,
709 Error **errp)
710{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200711 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600712 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600713 return;
714 }
715
716 if (!prop->set) {
717 error_set(errp, QERR_PERMISSION_DENIED);
718 } else {
719 prop->set(obj, v, prop->opaque, name, errp);
720 }
721}
722
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100723void object_property_set_str(Object *obj, const char *value,
724 const char *name, Error **errp)
725{
726 QString *qstr = qstring_from_str(value);
727 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
728
729 QDECREF(qstr);
730}
731
732char *object_property_get_str(Object *obj, const char *name,
733 Error **errp)
734{
735 QObject *ret = object_property_get_qobject(obj, name, errp);
736 QString *qstring;
737 char *retval;
738
739 if (!ret) {
740 return NULL;
741 }
742 qstring = qobject_to_qstring(ret);
743 if (!qstring) {
744 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
745 retval = NULL;
746 } else {
747 retval = g_strdup(qstring_get_str(qstring));
748 }
749
750 QDECREF(qstring);
751 return retval;
752}
753
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100754void object_property_set_link(Object *obj, Object *value,
755 const char *name, Error **errp)
756{
757 object_property_set_str(obj, object_get_canonical_path(value),
758 name, errp);
759}
760
761Object *object_property_get_link(Object *obj, const char *name,
762 Error **errp)
763{
764 char *str = object_property_get_str(obj, name, errp);
765 Object *target = NULL;
766
767 if (str && *str) {
768 target = object_resolve_path(str, NULL);
769 if (!target) {
770 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
771 }
772 }
773
774 g_free(str);
775 return target;
776}
777
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100778void object_property_set_bool(Object *obj, bool value,
779 const char *name, Error **errp)
780{
781 QBool *qbool = qbool_from_int(value);
782 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
783
784 QDECREF(qbool);
785}
786
787bool object_property_get_bool(Object *obj, const char *name,
788 Error **errp)
789{
790 QObject *ret = object_property_get_qobject(obj, name, errp);
791 QBool *qbool;
792 bool retval;
793
794 if (!ret) {
795 return false;
796 }
797 qbool = qobject_to_qbool(ret);
798 if (!qbool) {
799 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
800 retval = false;
801 } else {
802 retval = qbool_get_int(qbool);
803 }
804
805 QDECREF(qbool);
806 return retval;
807}
808
809void object_property_set_int(Object *obj, int64_t value,
810 const char *name, Error **errp)
811{
812 QInt *qint = qint_from_int(value);
813 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
814
815 QDECREF(qint);
816}
817
818int64_t object_property_get_int(Object *obj, const char *name,
819 Error **errp)
820{
821 QObject *ret = object_property_get_qobject(obj, name, errp);
822 QInt *qint;
823 int64_t retval;
824
825 if (!ret) {
826 return -1;
827 }
828 qint = qobject_to_qint(ret);
829 if (!qint) {
830 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
831 retval = -1;
832 } else {
833 retval = qint_get_int(qint);
834 }
835
836 QDECREF(qint);
837 return retval;
838}
839
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100840void object_property_parse(Object *obj, const char *string,
841 const char *name, Error **errp)
842{
843 StringInputVisitor *mi;
844 mi = string_input_visitor_new(string);
845 object_property_set(obj, string_input_get_visitor(mi), name, errp);
846
847 string_input_visitor_cleanup(mi);
848}
849
850char *object_property_print(Object *obj, const char *name,
851 Error **errp)
852{
853 StringOutputVisitor *mo;
854 char *string;
855
856 mo = string_output_visitor_new();
Paolo Bonzini8185bfc2012-05-02 13:31:00 +0200857 object_property_get(obj, string_output_get_visitor(mo), name, errp);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100858 string = string_output_get_string(mo);
859 string_output_visitor_cleanup(mo);
860 return string;
861}
862
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600863const char *object_property_get_type(Object *obj, const char *name, Error **errp)
864{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200865 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600866 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600867 return NULL;
868 }
869
870 return prop->type;
871}
872
873Object *object_get_root(void)
874{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600875 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600876
Anthony Liguori8b45d442011-12-23 09:08:05 -0600877 if (!root) {
878 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600879 }
880
Anthony Liguori8b45d442011-12-23 09:08:05 -0600881 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600882}
883
884static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
885 const char *name, Error **errp)
886{
887 Object *child = opaque;
888 gchar *path;
889
890 path = object_get_canonical_path(child);
891 visit_type_str(v, &path, name, errp);
892 g_free(path);
893}
894
Anthony Liguoridb85b572011-12-23 08:47:39 -0600895static void object_finalize_child_property(Object *obj, const char *name,
896 void *opaque)
897{
898 Object *child = opaque;
899
900 object_unref(child);
901}
902
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600903void object_property_add_child(Object *obj, const char *name,
904 Object *child, Error **errp)
905{
906 gchar *type;
907
908 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
909
910 object_property_add(obj, name, type, object_get_child_property,
Anthony Liguoridb85b572011-12-23 08:47:39 -0600911 NULL, object_finalize_child_property, child, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600912
913 object_ref(child);
914 g_assert(child->parent == NULL);
915 child->parent = obj;
916
917 g_free(type);
918}
919
920static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
921 const char *name, Error **errp)
922{
923 Object **child = opaque;
924 gchar *path;
925
926 if (*child) {
927 path = object_get_canonical_path(*child);
928 visit_type_str(v, &path, name, errp);
929 g_free(path);
930 } else {
931 path = (gchar *)"";
932 visit_type_str(v, &path, name, errp);
933 }
934}
935
936static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
937 const char *name, Error **errp)
938{
939 Object **child = opaque;
Alexander Barabashf0cdc962012-02-22 19:22:26 +0200940 Object *old_target;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600941 bool ambiguous = false;
942 const char *type;
943 char *path;
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100944 gchar *target_type;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600945
946 type = object_property_get_type(obj, name, NULL);
947
948 visit_type_str(v, &path, name, errp);
949
Alexander Barabashf0cdc962012-02-22 19:22:26 +0200950 old_target = *child;
951 *child = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600952
953 if (strcmp(path, "") != 0) {
954 Object *target;
955
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100956 /* Go from link<FOO> to FOO. */
957 target_type = g_strndup(&type[5], strlen(type) - 6);
958 target = object_resolve_path_type(path, target_type, &ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600959
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100960 if (ambiguous) {
961 error_set(errp, QERR_AMBIGUOUS_PATH, path);
962 } else if (target) {
963 object_ref(target);
964 *child = target;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600965 } else {
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100966 target = object_resolve_path(path, &ambiguous);
967 if (target || ambiguous) {
968 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
969 } else {
970 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
971 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600972 }
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100973 g_free(target_type);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600974 }
975
976 g_free(path);
Alexander Barabashf0cdc962012-02-22 19:22:26 +0200977
978 if (old_target != NULL) {
979 object_unref(old_target);
980 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600981}
982
983void object_property_add_link(Object *obj, const char *name,
984 const char *type, Object **child,
985 Error **errp)
986{
987 gchar *full_type;
988
989 full_type = g_strdup_printf("link<%s>", type);
990
991 object_property_add(obj, name, full_type,
992 object_get_link_property,
993 object_set_link_property,
994 NULL, child, errp);
995
996 g_free(full_type);
997}
998
999gchar *object_get_canonical_path(Object *obj)
1000{
1001 Object *root = object_get_root();
1002 char *newpath = NULL, *path = NULL;
1003
1004 while (obj != root) {
1005 ObjectProperty *prop = NULL;
1006
1007 g_assert(obj->parent != NULL);
1008
1009 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001010 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001011 continue;
1012 }
1013
1014 if (prop->opaque == obj) {
1015 if (path) {
1016 newpath = g_strdup_printf("%s/%s", prop->name, path);
1017 g_free(path);
1018 path = newpath;
1019 } else {
1020 path = g_strdup(prop->name);
1021 }
1022 break;
1023 }
1024 }
1025
1026 g_assert(prop != NULL);
1027
1028 obj = obj->parent;
1029 }
1030
1031 newpath = g_strdup_printf("/%s", path);
1032 g_free(path);
1033
1034 return newpath;
1035}
1036
Andreas Färber3e84b482013-01-15 02:55:10 +01001037Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001038{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001039 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001040 if (prop == NULL) {
1041 return NULL;
1042 }
1043
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001044 if (object_property_is_link(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001045 return *(Object **)prop->opaque;
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001046 } else if (object_property_is_child(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001047 return prop->opaque;
1048 } else {
1049 return NULL;
1050 }
1051}
1052
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001053static Object *object_resolve_abs_path(Object *parent,
1054 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001055 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001056 int index)
1057{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001058 Object *child;
1059
1060 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001061 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001062 }
1063
1064 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001065 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001066 }
1067
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001068 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001069 if (!child) {
1070 return NULL;
1071 }
1072
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001073 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001074}
1075
1076static Object *object_resolve_partial_path(Object *parent,
1077 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001078 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001079 bool *ambiguous)
1080{
1081 Object *obj;
1082 ObjectProperty *prop;
1083
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001084 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001085
1086 QTAILQ_FOREACH(prop, &parent->properties, node) {
1087 Object *found;
1088
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001089 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001090 continue;
1091 }
1092
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001093 found = object_resolve_partial_path(prop->opaque, parts,
1094 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001095 if (found) {
1096 if (obj) {
1097 if (ambiguous) {
1098 *ambiguous = true;
1099 }
1100 return NULL;
1101 }
1102 obj = found;
1103 }
1104
1105 if (ambiguous && *ambiguous) {
1106 return NULL;
1107 }
1108 }
1109
1110 return obj;
1111}
1112
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001113Object *object_resolve_path_type(const char *path, const char *typename,
1114 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001115{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001116 Object *obj;
1117 gchar **parts;
1118
1119 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001120 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001121
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001122 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001123 if (ambiguous) {
1124 *ambiguous = false;
1125 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001126 obj = object_resolve_partial_path(object_get_root(), parts,
1127 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001128 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001129 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001130 }
1131
1132 g_strfreev(parts);
1133
1134 return obj;
1135}
1136
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001137Object *object_resolve_path(const char *path, bool *ambiguous)
1138{
1139 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1140}
1141
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001142typedef struct StringProperty
1143{
1144 char *(*get)(Object *, Error **);
1145 void (*set)(Object *, const char *, Error **);
1146} StringProperty;
1147
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001148static void property_get_str(Object *obj, Visitor *v, void *opaque,
1149 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001150{
1151 StringProperty *prop = opaque;
1152 char *value;
1153
1154 value = prop->get(obj, errp);
1155 if (value) {
1156 visit_type_str(v, &value, name, errp);
1157 g_free(value);
1158 }
1159}
1160
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001161static void property_set_str(Object *obj, Visitor *v, void *opaque,
1162 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001163{
1164 StringProperty *prop = opaque;
1165 char *value;
1166 Error *local_err = NULL;
1167
1168 visit_type_str(v, &value, name, &local_err);
1169 if (local_err) {
1170 error_propagate(errp, local_err);
1171 return;
1172 }
1173
1174 prop->set(obj, value, errp);
1175 g_free(value);
1176}
1177
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001178static void property_release_str(Object *obj, const char *name,
1179 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001180{
1181 StringProperty *prop = opaque;
1182 g_free(prop);
1183}
1184
1185void object_property_add_str(Object *obj, const char *name,
1186 char *(*get)(Object *, Error **),
1187 void (*set)(Object *, const char *, Error **),
1188 Error **errp)
1189{
1190 StringProperty *prop = g_malloc0(sizeof(*prop));
1191
1192 prop->get = get;
1193 prop->set = set;
1194
1195 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001196 get ? property_get_str : NULL,
1197 set ? property_set_str : NULL,
1198 property_release_str,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001199 prop, errp);
1200}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001201
Anthony Liguori0e558842012-06-25 10:32:46 -05001202typedef struct BoolProperty
1203{
1204 bool (*get)(Object *, Error **);
1205 void (*set)(Object *, bool, Error **);
1206} BoolProperty;
1207
1208static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1209 const char *name, Error **errp)
1210{
1211 BoolProperty *prop = opaque;
1212 bool value;
1213
1214 value = prop->get(obj, errp);
1215 visit_type_bool(v, &value, name, errp);
1216}
1217
1218static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1219 const char *name, Error **errp)
1220{
1221 BoolProperty *prop = opaque;
1222 bool value;
1223 Error *local_err = NULL;
1224
1225 visit_type_bool(v, &value, name, &local_err);
1226 if (local_err) {
1227 error_propagate(errp, local_err);
1228 return;
1229 }
1230
1231 prop->set(obj, value, errp);
1232}
1233
1234static void property_release_bool(Object *obj, const char *name,
1235 void *opaque)
1236{
1237 BoolProperty *prop = opaque;
1238 g_free(prop);
1239}
1240
1241void object_property_add_bool(Object *obj, const char *name,
1242 bool (*get)(Object *, Error **),
1243 void (*set)(Object *, bool, Error **),
1244 Error **errp)
1245{
1246 BoolProperty *prop = g_malloc0(sizeof(*prop));
1247
1248 prop->get = get;
1249 prop->set = set;
1250
1251 object_property_add(obj, name, "bool",
1252 get ? property_get_bool : NULL,
1253 set ? property_set_bool : NULL,
1254 property_release_bool,
1255 prop, errp);
1256}
1257
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001258static char *qdev_get_type(Object *obj, Error **errp)
1259{
1260 return g_strdup(object_get_typename(obj));
1261}
1262
1263static void object_instance_init(Object *obj)
1264{
1265 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1266}
1267
Paolo Bonzini745549c2012-03-31 16:45:54 +02001268static void register_types(void)
1269{
1270 static TypeInfo interface_info = {
1271 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10001272 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02001273 .abstract = true,
1274 };
1275
1276 static TypeInfo object_info = {
1277 .name = TYPE_OBJECT,
1278 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001279 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02001280 .abstract = true,
1281 };
1282
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02001283 type_interface = type_register_internal(&interface_info);
1284 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02001285}
1286
1287type_init(register_types)