blob: 35f4694987d2850d7bd6b402ad0f2779339bb5c6 [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{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000452 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200453 TypeImpl *target_type;
454 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600455
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200456 if (!class) {
457 return NULL;
458 }
459
460 type = class->type;
461 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200462 if (!target_type) {
463 /* target class type unknown, so fail the cast */
464 return NULL;
465 }
466
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000467 if (type->class->interfaces &&
468 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000469 int found = 0;
470 GSList *i;
471
472 for (i = class->interfaces; i; i = i->next) {
473 ObjectClass *target_class = i->data;
474
475 if (type_is_ancestor(target_class->type, target_type)) {
476 ret = target_class;
477 found++;
478 }
479 }
480
481 /* The match was ambiguous, don't allow a cast */
482 if (found > 1) {
483 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600484 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000485 } else if (type_is_ancestor(type, target_type)) {
486 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600487 }
488
Anthony Liguori33e95c62012-08-10 13:16:10 +1000489 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600490}
491
492ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
493 const char *typename)
494{
495 ObjectClass *ret = object_class_dynamic_cast(class, typename);
496
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200497 if (!ret && class) {
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600498 fprintf(stderr, "Object %p is not an instance of type %s\n",
499 class, typename);
500 abort();
501 }
502
503 return ret;
504}
505
506const char *object_get_typename(Object *obj)
507{
508 return obj->class->type->name;
509}
510
511ObjectClass *object_get_class(Object *obj)
512{
513 return obj->class;
514}
515
Andreas Färber17862372013-01-23 12:20:18 +0100516bool object_class_is_abstract(ObjectClass *klass)
517{
518 return klass->type->abstract;
519}
520
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600521const char *object_class_get_name(ObjectClass *klass)
522{
523 return klass->type->name;
524}
525
526ObjectClass *object_class_by_name(const char *typename)
527{
528 TypeImpl *type = type_get_by_name(typename);
529
530 if (!type) {
531 return NULL;
532 }
533
Igor Mitsyankoac451032012-02-28 15:57:11 +0400534 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600535
536 return type->class;
537}
538
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200539ObjectClass *object_class_get_parent(ObjectClass *class)
540{
541 TypeImpl *type = type_get_parent(class->type);
542
543 if (!type) {
544 return NULL;
545 }
546
547 type_initialize(type);
548
549 return type->class;
550}
551
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600552typedef struct OCFData
553{
554 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600555 const char *implements_type;
556 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600557 void *opaque;
558} OCFData;
559
560static void object_class_foreach_tramp(gpointer key, gpointer value,
561 gpointer opaque)
562{
563 OCFData *data = opaque;
564 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600565 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600566
Igor Mitsyankoac451032012-02-28 15:57:11 +0400567 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600568 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600569
Anthony Liguori93c511a2011-12-22 14:11:53 -0600570 if (!data->include_abstract && type->abstract) {
571 return;
572 }
573
574 if (data->implements_type &&
575 !object_class_dynamic_cast(k, data->implements_type)) {
576 return;
577 }
578
579 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600580}
581
582void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600583 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600584 void *opaque)
585{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600586 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600587
588 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
589}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600590
Paolo Bonzini32efc532012-04-11 23:30:20 +0200591int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
592 void *opaque)
593{
594 ObjectProperty *prop;
595 int ret = 0;
596
597 QTAILQ_FOREACH(prop, &obj->properties, node) {
598 if (object_property_is_child(prop)) {
599 ret = fn(prop->opaque, opaque);
600 if (ret != 0) {
601 break;
602 }
603 }
604 }
605 return ret;
606}
607
Andreas Färber418ba9e2012-02-25 23:07:34 +0100608static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
609{
610 GSList **list = opaque;
611
612 *list = g_slist_prepend(*list, klass);
613}
614
615GSList *object_class_get_list(const char *implements_type,
616 bool include_abstract)
617{
618 GSList *list = NULL;
619
620 object_class_foreach(object_class_get_list_tramp,
621 implements_type, include_abstract, &list);
622 return list;
623}
624
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600625void object_ref(Object *obj)
626{
627 obj->ref++;
628}
629
630void object_unref(Object *obj)
631{
632 g_assert(obj->ref > 0);
633 obj->ref--;
634
635 /* parent always holds a reference to its children */
636 if (obj->ref == 0) {
637 object_finalize(obj);
638 }
639}
640
641void object_property_add(Object *obj, const char *name, const char *type,
642 ObjectPropertyAccessor *get,
643 ObjectPropertyAccessor *set,
644 ObjectPropertyRelease *release,
645 void *opaque, Error **errp)
646{
Peter Maydell54852b02013-03-25 13:15:13 +0000647 ObjectProperty *prop;
648
649 QTAILQ_FOREACH(prop, &obj->properties, node) {
650 if (strcmp(prop->name, name) == 0) {
651 error_setg(errp, "attempt to add duplicate property '%s'"
652 " to object (type '%s')", name,
653 object_get_typename(obj));
654 return;
655 }
656 }
657
658 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600659
660 prop->name = g_strdup(name);
661 prop->type = g_strdup(type);
662
663 prop->get = get;
664 prop->set = set;
665 prop->release = release;
666 prop->opaque = opaque;
667
668 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
669}
670
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200671ObjectProperty *object_property_find(Object *obj, const char *name,
672 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600673{
674 ObjectProperty *prop;
675
676 QTAILQ_FOREACH(prop, &obj->properties, node) {
677 if (strcmp(prop->name, name) == 0) {
678 return prop;
679 }
680 }
681
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200682 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600683 return NULL;
684}
685
686void object_property_del(Object *obj, const char *name, Error **errp)
687{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200688 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600689 if (prop == NULL) {
Anthony Liguori0866aca2011-12-23 15:34:39 -0600690 return;
691 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600692
Anthony Liguori0866aca2011-12-23 15:34:39 -0600693 if (prop->release) {
694 prop->release(obj, name, prop->opaque);
695 }
696
697 QTAILQ_REMOVE(&obj->properties, prop, node);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600698
699 g_free(prop->name);
700 g_free(prop->type);
701 g_free(prop);
702}
703
704void object_property_get(Object *obj, Visitor *v, const char *name,
705 Error **errp)
706{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200707 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600708 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600709 return;
710 }
711
712 if (!prop->get) {
713 error_set(errp, QERR_PERMISSION_DENIED);
714 } else {
715 prop->get(obj, v, prop->opaque, name, errp);
716 }
717}
718
719void object_property_set(Object *obj, Visitor *v, const char *name,
720 Error **errp)
721{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200722 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600723 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600724 return;
725 }
726
727 if (!prop->set) {
728 error_set(errp, QERR_PERMISSION_DENIED);
729 } else {
730 prop->set(obj, v, prop->opaque, name, errp);
731 }
732}
733
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100734void object_property_set_str(Object *obj, const char *value,
735 const char *name, Error **errp)
736{
737 QString *qstr = qstring_from_str(value);
738 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
739
740 QDECREF(qstr);
741}
742
743char *object_property_get_str(Object *obj, const char *name,
744 Error **errp)
745{
746 QObject *ret = object_property_get_qobject(obj, name, errp);
747 QString *qstring;
748 char *retval;
749
750 if (!ret) {
751 return NULL;
752 }
753 qstring = qobject_to_qstring(ret);
754 if (!qstring) {
755 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
756 retval = NULL;
757 } else {
758 retval = g_strdup(qstring_get_str(qstring));
759 }
760
761 QDECREF(qstring);
762 return retval;
763}
764
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100765void object_property_set_link(Object *obj, Object *value,
766 const char *name, Error **errp)
767{
768 object_property_set_str(obj, object_get_canonical_path(value),
769 name, errp);
770}
771
772Object *object_property_get_link(Object *obj, const char *name,
773 Error **errp)
774{
775 char *str = object_property_get_str(obj, name, errp);
776 Object *target = NULL;
777
778 if (str && *str) {
779 target = object_resolve_path(str, NULL);
780 if (!target) {
781 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
782 }
783 }
784
785 g_free(str);
786 return target;
787}
788
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100789void object_property_set_bool(Object *obj, bool value,
790 const char *name, Error **errp)
791{
792 QBool *qbool = qbool_from_int(value);
793 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
794
795 QDECREF(qbool);
796}
797
798bool object_property_get_bool(Object *obj, const char *name,
799 Error **errp)
800{
801 QObject *ret = object_property_get_qobject(obj, name, errp);
802 QBool *qbool;
803 bool retval;
804
805 if (!ret) {
806 return false;
807 }
808 qbool = qobject_to_qbool(ret);
809 if (!qbool) {
810 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
811 retval = false;
812 } else {
813 retval = qbool_get_int(qbool);
814 }
815
816 QDECREF(qbool);
817 return retval;
818}
819
820void object_property_set_int(Object *obj, int64_t value,
821 const char *name, Error **errp)
822{
823 QInt *qint = qint_from_int(value);
824 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
825
826 QDECREF(qint);
827}
828
829int64_t object_property_get_int(Object *obj, const char *name,
830 Error **errp)
831{
832 QObject *ret = object_property_get_qobject(obj, name, errp);
833 QInt *qint;
834 int64_t retval;
835
836 if (!ret) {
837 return -1;
838 }
839 qint = qobject_to_qint(ret);
840 if (!qint) {
841 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
842 retval = -1;
843 } else {
844 retval = qint_get_int(qint);
845 }
846
847 QDECREF(qint);
848 return retval;
849}
850
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100851void object_property_parse(Object *obj, const char *string,
852 const char *name, Error **errp)
853{
854 StringInputVisitor *mi;
855 mi = string_input_visitor_new(string);
856 object_property_set(obj, string_input_get_visitor(mi), name, errp);
857
858 string_input_visitor_cleanup(mi);
859}
860
861char *object_property_print(Object *obj, const char *name,
862 Error **errp)
863{
864 StringOutputVisitor *mo;
865 char *string;
866
867 mo = string_output_visitor_new();
Paolo Bonzini8185bfc2012-05-02 13:31:00 +0200868 object_property_get(obj, string_output_get_visitor(mo), name, errp);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100869 string = string_output_get_string(mo);
870 string_output_visitor_cleanup(mo);
871 return string;
872}
873
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600874const char *object_property_get_type(Object *obj, const char *name, Error **errp)
875{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200876 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600877 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600878 return NULL;
879 }
880
881 return prop->type;
882}
883
884Object *object_get_root(void)
885{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600886 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600887
Anthony Liguori8b45d442011-12-23 09:08:05 -0600888 if (!root) {
889 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600890 }
891
Anthony Liguori8b45d442011-12-23 09:08:05 -0600892 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600893}
894
895static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
896 const char *name, Error **errp)
897{
898 Object *child = opaque;
899 gchar *path;
900
901 path = object_get_canonical_path(child);
902 visit_type_str(v, &path, name, errp);
903 g_free(path);
904}
905
Anthony Liguoridb85b572011-12-23 08:47:39 -0600906static void object_finalize_child_property(Object *obj, const char *name,
907 void *opaque)
908{
909 Object *child = opaque;
910
911 object_unref(child);
912}
913
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600914void object_property_add_child(Object *obj, const char *name,
915 Object *child, Error **errp)
916{
917 gchar *type;
918
919 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
920
921 object_property_add(obj, name, type, object_get_child_property,
Anthony Liguoridb85b572011-12-23 08:47:39 -0600922 NULL, object_finalize_child_property, child, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600923
924 object_ref(child);
925 g_assert(child->parent == NULL);
926 child->parent = obj;
927
928 g_free(type);
929}
930
931static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
932 const char *name, Error **errp)
933{
934 Object **child = opaque;
935 gchar *path;
936
937 if (*child) {
938 path = object_get_canonical_path(*child);
939 visit_type_str(v, &path, name, errp);
940 g_free(path);
941 } else {
942 path = (gchar *)"";
943 visit_type_str(v, &path, name, errp);
944 }
945}
946
947static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
948 const char *name, Error **errp)
949{
950 Object **child = opaque;
Alexander Barabashf0cdc962012-02-22 19:22:26 +0200951 Object *old_target;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600952 bool ambiguous = false;
953 const char *type;
954 char *path;
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100955 gchar *target_type;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600956
957 type = object_property_get_type(obj, name, NULL);
958
959 visit_type_str(v, &path, name, errp);
960
Alexander Barabashf0cdc962012-02-22 19:22:26 +0200961 old_target = *child;
962 *child = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600963
964 if (strcmp(path, "") != 0) {
965 Object *target;
966
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100967 /* Go from link<FOO> to FOO. */
968 target_type = g_strndup(&type[5], strlen(type) - 6);
969 target = object_resolve_path_type(path, target_type, &ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600970
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100971 if (ambiguous) {
972 error_set(errp, QERR_AMBIGUOUS_PATH, path);
973 } else if (target) {
974 object_ref(target);
975 *child = target;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600976 } else {
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100977 target = object_resolve_path(path, &ambiguous);
978 if (target || ambiguous) {
979 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
980 } else {
981 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
982 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600983 }
Paolo Bonzini11e35bf2012-02-02 12:37:53 +0100984 g_free(target_type);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600985 }
986
987 g_free(path);
Alexander Barabashf0cdc962012-02-22 19:22:26 +0200988
989 if (old_target != NULL) {
990 object_unref(old_target);
991 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600992}
993
994void object_property_add_link(Object *obj, const char *name,
995 const char *type, Object **child,
996 Error **errp)
997{
998 gchar *full_type;
999
1000 full_type = g_strdup_printf("link<%s>", type);
1001
1002 object_property_add(obj, name, full_type,
1003 object_get_link_property,
1004 object_set_link_property,
1005 NULL, child, errp);
1006
1007 g_free(full_type);
1008}
1009
1010gchar *object_get_canonical_path(Object *obj)
1011{
1012 Object *root = object_get_root();
1013 char *newpath = NULL, *path = NULL;
1014
1015 while (obj != root) {
1016 ObjectProperty *prop = NULL;
1017
1018 g_assert(obj->parent != NULL);
1019
1020 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001021 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001022 continue;
1023 }
1024
1025 if (prop->opaque == obj) {
1026 if (path) {
1027 newpath = g_strdup_printf("%s/%s", prop->name, path);
1028 g_free(path);
1029 path = newpath;
1030 } else {
1031 path = g_strdup(prop->name);
1032 }
1033 break;
1034 }
1035 }
1036
1037 g_assert(prop != NULL);
1038
1039 obj = obj->parent;
1040 }
1041
1042 newpath = g_strdup_printf("/%s", path);
1043 g_free(path);
1044
1045 return newpath;
1046}
1047
Andreas Färber3e84b482013-01-15 02:55:10 +01001048Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001049{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001050 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001051 if (prop == NULL) {
1052 return NULL;
1053 }
1054
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001055 if (object_property_is_link(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001056 return *(Object **)prop->opaque;
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001057 } else if (object_property_is_child(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001058 return prop->opaque;
1059 } else {
1060 return NULL;
1061 }
1062}
1063
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001064static Object *object_resolve_abs_path(Object *parent,
1065 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001066 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001067 int index)
1068{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001069 Object *child;
1070
1071 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001072 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001073 }
1074
1075 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001076 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001077 }
1078
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001079 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001080 if (!child) {
1081 return NULL;
1082 }
1083
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001084 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001085}
1086
1087static Object *object_resolve_partial_path(Object *parent,
1088 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001089 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001090 bool *ambiguous)
1091{
1092 Object *obj;
1093 ObjectProperty *prop;
1094
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001095 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001096
1097 QTAILQ_FOREACH(prop, &parent->properties, node) {
1098 Object *found;
1099
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001100 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001101 continue;
1102 }
1103
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001104 found = object_resolve_partial_path(prop->opaque, parts,
1105 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001106 if (found) {
1107 if (obj) {
1108 if (ambiguous) {
1109 *ambiguous = true;
1110 }
1111 return NULL;
1112 }
1113 obj = found;
1114 }
1115
1116 if (ambiguous && *ambiguous) {
1117 return NULL;
1118 }
1119 }
1120
1121 return obj;
1122}
1123
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001124Object *object_resolve_path_type(const char *path, const char *typename,
1125 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001126{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001127 Object *obj;
1128 gchar **parts;
1129
1130 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001131 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001132
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001133 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001134 if (ambiguous) {
1135 *ambiguous = false;
1136 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001137 obj = object_resolve_partial_path(object_get_root(), parts,
1138 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001139 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001140 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001141 }
1142
1143 g_strfreev(parts);
1144
1145 return obj;
1146}
1147
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001148Object *object_resolve_path(const char *path, bool *ambiguous)
1149{
1150 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1151}
1152
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001153typedef struct StringProperty
1154{
1155 char *(*get)(Object *, Error **);
1156 void (*set)(Object *, const char *, Error **);
1157} StringProperty;
1158
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001159static void property_get_str(Object *obj, Visitor *v, void *opaque,
1160 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001161{
1162 StringProperty *prop = opaque;
1163 char *value;
1164
1165 value = prop->get(obj, errp);
1166 if (value) {
1167 visit_type_str(v, &value, name, errp);
1168 g_free(value);
1169 }
1170}
1171
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001172static void property_set_str(Object *obj, Visitor *v, void *opaque,
1173 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001174{
1175 StringProperty *prop = opaque;
1176 char *value;
1177 Error *local_err = NULL;
1178
1179 visit_type_str(v, &value, name, &local_err);
1180 if (local_err) {
1181 error_propagate(errp, local_err);
1182 return;
1183 }
1184
1185 prop->set(obj, value, errp);
1186 g_free(value);
1187}
1188
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001189static void property_release_str(Object *obj, const char *name,
1190 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001191{
1192 StringProperty *prop = opaque;
1193 g_free(prop);
1194}
1195
1196void object_property_add_str(Object *obj, const char *name,
1197 char *(*get)(Object *, Error **),
1198 void (*set)(Object *, const char *, Error **),
1199 Error **errp)
1200{
1201 StringProperty *prop = g_malloc0(sizeof(*prop));
1202
1203 prop->get = get;
1204 prop->set = set;
1205
1206 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001207 get ? property_get_str : NULL,
1208 set ? property_set_str : NULL,
1209 property_release_str,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001210 prop, errp);
1211}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001212
Anthony Liguori0e558842012-06-25 10:32:46 -05001213typedef struct BoolProperty
1214{
1215 bool (*get)(Object *, Error **);
1216 void (*set)(Object *, bool, Error **);
1217} BoolProperty;
1218
1219static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1220 const char *name, Error **errp)
1221{
1222 BoolProperty *prop = opaque;
1223 bool value;
1224
1225 value = prop->get(obj, errp);
1226 visit_type_bool(v, &value, name, errp);
1227}
1228
1229static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1230 const char *name, Error **errp)
1231{
1232 BoolProperty *prop = opaque;
1233 bool value;
1234 Error *local_err = NULL;
1235
1236 visit_type_bool(v, &value, name, &local_err);
1237 if (local_err) {
1238 error_propagate(errp, local_err);
1239 return;
1240 }
1241
1242 prop->set(obj, value, errp);
1243}
1244
1245static void property_release_bool(Object *obj, const char *name,
1246 void *opaque)
1247{
1248 BoolProperty *prop = opaque;
1249 g_free(prop);
1250}
1251
1252void object_property_add_bool(Object *obj, const char *name,
1253 bool (*get)(Object *, Error **),
1254 void (*set)(Object *, bool, Error **),
1255 Error **errp)
1256{
1257 BoolProperty *prop = g_malloc0(sizeof(*prop));
1258
1259 prop->get = get;
1260 prop->set = set;
1261
1262 object_property_add(obj, name, "bool",
1263 get ? property_get_bool : NULL,
1264 set ? property_set_bool : NULL,
1265 property_release_bool,
1266 prop, errp);
1267}
1268
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001269static char *qdev_get_type(Object *obj, Error **errp)
1270{
1271 return g_strdup(object_get_typename(obj));
1272}
1273
1274static void object_instance_init(Object *obj)
1275{
1276 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1277}
1278
Paolo Bonzini745549c2012-03-31 16:45:54 +02001279static void register_types(void)
1280{
1281 static TypeInfo interface_info = {
1282 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10001283 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02001284 .abstract = true,
1285 };
1286
1287 static TypeInfo object_info = {
1288 .name = TYPE_OBJECT,
1289 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001290 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02001291 .abstract = true,
1292 };
1293
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02001294 type_interface = type_register_internal(&interface_info);
1295 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02001296}
1297
1298type_init(register_types)