blob: fc19cf676a69027fc7978cf32521c0cc45d852aa [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"
Paolo Bonzinifa131d92013-05-10 14:16:39 +020019#include "trace.h"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060020
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010021/* TODO: replace QObject with a simpler visitor to avoid a dependency
22 * of the QOM core on QObject? */
Paolo Bonzini14cccb62012-12-17 18:19:50 +010023#include "qom/qom-qobject.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010024#include "qapi/qmp/qobject.h"
25#include "qapi/qmp/qbool.h"
26#include "qapi/qmp/qint.h"
27#include "qapi/qmp/qstring.h"
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010028
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060029#define MAX_INTERFACES 32
30
31typedef struct InterfaceImpl InterfaceImpl;
32typedef struct TypeImpl TypeImpl;
33
34struct InterfaceImpl
35{
Anthony Liguori33e95c62012-08-10 13:16:10 +100036 const char *typename;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060037};
38
39struct TypeImpl
40{
41 const char *name;
42
43 size_t class_size;
44
45 size_t instance_size;
46
47 void (*class_init)(ObjectClass *klass, void *data);
Paolo Bonzini3b50e312012-05-02 13:30:55 +020048 void (*class_base_init)(ObjectClass *klass, void *data);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060049 void (*class_finalize)(ObjectClass *klass, void *data);
50
51 void *class_data;
52
53 void (*instance_init)(Object *obj);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -030054 void (*instance_post_init)(Object *obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060055 void (*instance_finalize)(Object *obj);
56
57 bool abstract;
58
59 const char *parent;
60 TypeImpl *parent_type;
61
62 ObjectClass *class;
63
64 int num_interfaces;
65 InterfaceImpl interfaces[MAX_INTERFACES];
66};
67
Paolo Bonzini9970bd82012-02-03 11:51:39 +010068static Type type_interface;
69
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060070static GHashTable *type_table_get(void)
71{
72 static GHashTable *type_table;
73
74 if (type_table == NULL) {
75 type_table = g_hash_table_new(g_str_hash, g_str_equal);
76 }
77
78 return type_table;
79}
80
81static void type_table_add(TypeImpl *ti)
82{
83 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
84}
85
86static TypeImpl *type_table_lookup(const char *name)
87{
88 return g_hash_table_lookup(type_table_get(), name);
89}
90
Paolo Bonzini049cb3c2012-04-04 15:58:40 +020091static TypeImpl *type_register_internal(const TypeInfo *info)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060092{
93 TypeImpl *ti = g_malloc0(sizeof(*ti));
Anthony Liguori33e95c62012-08-10 13:16:10 +100094 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060095
96 g_assert(info->name != NULL);
97
Anthony Liguori73093352012-01-25 13:37:36 -060098 if (type_table_lookup(info->name) != NULL) {
99 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
100 abort();
101 }
102
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600103 ti->name = g_strdup(info->name);
104 ti->parent = g_strdup(info->parent);
105
106 ti->class_size = info->class_size;
107 ti->instance_size = info->instance_size;
108
109 ti->class_init = info->class_init;
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200110 ti->class_base_init = info->class_base_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600111 ti->class_finalize = info->class_finalize;
112 ti->class_data = info->class_data;
113
114 ti->instance_init = info->instance_init;
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300115 ti->instance_post_init = info->instance_post_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600116 ti->instance_finalize = info->instance_finalize;
117
118 ti->abstract = info->abstract;
119
Anthony Liguori33e95c62012-08-10 13:16:10 +1000120 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
121 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600122 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000123 ti->num_interfaces = i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600124
125 type_table_add(ti);
126
127 return ti;
128}
129
Paolo Bonzini049cb3c2012-04-04 15:58:40 +0200130TypeImpl *type_register(const TypeInfo *info)
131{
132 assert(info->parent);
133 return type_register_internal(info);
134}
135
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600136TypeImpl *type_register_static(const TypeInfo *info)
137{
138 return type_register(info);
139}
140
141static 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
150static 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
160static bool type_has_parent(TypeImpl *type)
161{
162 return (type->parent != NULL);
163}
164
165static 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
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400178static size_t type_object_get_size(TypeImpl *ti)
179{
180 if (ti->instance_size) {
181 return ti->instance_size;
182 }
183
184 if (type_has_parent(ti)) {
185 return type_object_get_size(type_get_parent(ti));
186 }
187
188 return 0;
189}
190
Anthony Liguori33e95c62012-08-10 13:16:10 +1000191static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600192{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000193 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600194
Anthony Liguori33e95c62012-08-10 13:16:10 +1000195 /* Check if typename is a direct ancestor of type */
196 while (type) {
197 if (type == target_type) {
198 return true;
199 }
200
201 type = type_get_parent(type);
202 }
203
204 return false;
205}
206
207static void type_initialize(TypeImpl *ti);
208
209static void type_initialize_interface(TypeImpl *ti, const char *parent)
210{
211 InterfaceClass *new_iface;
212 TypeInfo info = { };
213 TypeImpl *iface_impl;
214
215 info.parent = parent;
216 info.name = g_strdup_printf("%s::%s", ti->name, info.parent);
217 info.abstract = true;
218
219 iface_impl = type_register(&info);
220 type_initialize(iface_impl);
221 g_free((char *)info.name);
222
223 new_iface = (InterfaceClass *)iface_impl->class;
224 new_iface->concrete_class = ti->class;
225
226 ti->class->interfaces = g_slist_append(ti->class->interfaces,
227 iface_impl->class);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600228}
229
Igor Mitsyankoac451032012-02-28 15:57:11 +0400230static void type_initialize(TypeImpl *ti)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600231{
Paolo Bonzini745549c2012-03-31 16:45:54 +0200232 TypeImpl *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600233
234 if (ti->class) {
235 return;
236 }
237
238 ti->class_size = type_class_get_size(ti);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400239 ti->instance_size = type_object_get_size(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600240
241 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600242
Paolo Bonzini745549c2012-03-31 16:45:54 +0200243 parent = type_get_parent(ti);
244 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400245 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000246 GSList *e;
247 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600248
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600249 g_assert(parent->class_size <= ti->class_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200250 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000251 ti->class->interfaces = NULL;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000252
253 for (e = parent->class->interfaces; e; e = e->next) {
254 ObjectClass *iface = e->data;
255 type_initialize_interface(ti, object_class_get_name(iface));
256 }
257
258 for (i = 0; i < ti->num_interfaces; i++) {
259 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
260 for (e = ti->class->interfaces; e; e = e->next) {
261 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
262
263 if (type_is_ancestor(target_type, t)) {
264 break;
265 }
266 }
267
268 if (e) {
269 continue;
270 }
271
272 type_initialize_interface(ti, ti->interfaces[i].typename);
273 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600274 }
275
Paolo Bonzini745549c2012-03-31 16:45:54 +0200276 ti->class->type = ti;
277
278 while (parent) {
279 if (parent->class_base_init) {
280 parent->class_base_init(ti->class, ti->class_data);
281 }
282 parent = type_get_parent(parent);
283 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600284
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600285 if (ti->class_init) {
286 ti->class_init(ti->class, ti->class_data);
287 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600288
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600289
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600290}
291
292static void object_init_with_type(Object *obj, TypeImpl *ti)
293{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600294 if (type_has_parent(ti)) {
295 object_init_with_type(obj, type_get_parent(ti));
296 }
297
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600298 if (ti->instance_init) {
299 ti->instance_init(obj);
300 }
301}
302
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300303static void object_post_init_with_type(Object *obj, TypeImpl *ti)
304{
305 if (ti->instance_post_init) {
306 ti->instance_post_init(obj);
307 }
308
309 if (type_has_parent(ti)) {
310 object_post_init_with_type(obj, type_get_parent(ti));
311 }
312}
313
Andreas Färber5b9237f2013-08-30 18:28:37 +0200314void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600315{
316 Object *obj = data;
317
318 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400319 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400320
321 g_assert(type->instance_size >= sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600322 g_assert(type->abstract == false);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200323 g_assert(size >= type->instance_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600324
325 memset(obj, 0, type->instance_size);
326 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100327 object_ref(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600328 QTAILQ_INIT(&obj->properties);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600329 object_init_with_type(obj, type);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300330 object_post_init_with_type(obj, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600331}
332
Andreas Färber213f0c42013-08-23 19:37:12 +0200333void object_initialize(void *data, size_t size, const char *typename)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600334{
335 TypeImpl *type = type_get_by_name(typename);
336
Andreas Färber5b9237f2013-08-30 18:28:37 +0200337 object_initialize_with_type(data, size, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600338}
339
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200340static inline bool object_property_is_child(ObjectProperty *prop)
341{
342 return strstart(prop->type, "child<", NULL);
343}
344
345static inline bool object_property_is_link(ObjectProperty *prop)
346{
347 return strstart(prop->type, "link<", NULL);
348}
349
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600350static void object_property_del_all(Object *obj)
351{
352 while (!QTAILQ_EMPTY(&obj->properties)) {
353 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
354
355 QTAILQ_REMOVE(&obj->properties, prop, node);
356
357 if (prop->release) {
358 prop->release(obj, prop->name, prop->opaque);
359 }
360
361 g_free(prop->name);
362 g_free(prop->type);
363 g_free(prop);
364 }
365}
366
367static void object_property_del_child(Object *obj, Object *child, Error **errp)
368{
369 ObjectProperty *prop;
370
371 QTAILQ_FOREACH(prop, &obj->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200372 if (object_property_is_child(prop) && prop->opaque == child) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600373 object_property_del(obj, prop->name, errp);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100374 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600375 }
376 }
377}
378
379void object_unparent(Object *obj)
380{
Paolo Bonzinie0a83fc2013-04-02 15:50:00 +0200381 if (!obj->parent) {
382 return;
383 }
384
Paolo Bonzini52e636c2013-01-25 14:12:30 +0100385 object_ref(obj);
Paolo Bonzini667d22d2012-11-23 09:47:13 +0100386 if (obj->class->unparent) {
387 (obj->class->unparent)(obj);
388 }
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200389 if (obj->parent) {
390 object_property_del_child(obj->parent, obj, NULL);
391 }
Paolo Bonzini52e636c2013-01-25 14:12:30 +0100392 object_unref(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600393}
394
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600395static void object_deinit(Object *obj, TypeImpl *type)
396{
397 if (type->instance_finalize) {
398 type->instance_finalize(obj);
399 }
400
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600401 if (type_has_parent(type)) {
402 object_deinit(obj, type_get_parent(type));
403 }
404}
405
Paolo Bonzini339c2702012-11-23 09:47:16 +0100406static void object_finalize(void *data)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600407{
408 Object *obj = data;
409 TypeImpl *ti = obj->class->type;
410
411 object_deinit(obj, ti);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600412 object_property_del_all(obj);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600413
414 g_assert(obj->ref == 0);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100415 if (obj->free) {
416 obj->free(obj);
417 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600418}
419
420Object *object_new_with_type(Type type)
421{
422 Object *obj;
423
424 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400425 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600426
427 obj = g_malloc(type->instance_size);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200428 object_initialize_with_type(obj, type->instance_size, type);
Paolo Bonzinifde9bf42012-11-23 09:47:14 +0100429 obj->free = g_free;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600430
431 return obj;
432}
433
434Object *object_new(const char *typename)
435{
436 TypeImpl *ti = type_get_by_name(typename);
437
438 return object_new_with_type(ti);
439}
440
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600441Object *object_dynamic_cast(Object *obj, const char *typename)
442{
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100443 if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100444 return obj;
445 }
446
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600447 return NULL;
448}
449
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200450Object *object_dynamic_cast_assert(Object *obj, const char *typename,
451 const char *file, int line, const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600452{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200453 trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)",
454 typename, file, line, func);
455
Paolo Bonzini3556c232013-05-10 14:16:40 +0200456#ifdef CONFIG_QOM_CAST_DEBUG
Anthony Liguori03587322013-05-13 15:22:24 -0500457 int i;
458 Object *inst;
459
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000460 for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) {
Anthony Liguori03587322013-05-13 15:22:24 -0500461 if (obj->class->cast_cache[i] == typename) {
462 goto out;
463 }
464 }
465
466 inst = object_dynamic_cast(obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600467
Paolo Bonzinib7f43fe2012-11-23 16:56:17 +0100468 if (!inst && obj) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200469 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
470 file, line, func, obj, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600471 abort();
472 }
473
Paolo Bonzini3556c232013-05-10 14:16:40 +0200474 assert(obj == inst);
Anthony Liguori03587322013-05-13 15:22:24 -0500475
Peter Crosthwaite95916ab2013-05-22 11:19:16 +1000476 if (obj && obj == inst) {
Anthony Liguori03587322013-05-13 15:22:24 -0500477 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
478 obj->class->cast_cache[i - 1] = obj->class->cast_cache[i];
479 }
480 obj->class->cast_cache[i - 1] = typename;
481 }
482
483out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200484#endif
485 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600486}
487
488ObjectClass *object_class_dynamic_cast(ObjectClass *class,
489 const char *typename)
490{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000491 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200492 TypeImpl *target_type;
493 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600494
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200495 if (!class) {
496 return NULL;
497 }
498
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200499 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200500 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200501 if (type->name == typename) {
502 return class;
503 }
504
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200505 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200506 if (!target_type) {
507 /* target class type unknown, so fail the cast */
508 return NULL;
509 }
510
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000511 if (type->class->interfaces &&
512 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000513 int found = 0;
514 GSList *i;
515
516 for (i = class->interfaces; i; i = i->next) {
517 ObjectClass *target_class = i->data;
518
519 if (type_is_ancestor(target_class->type, target_type)) {
520 ret = target_class;
521 found++;
522 }
523 }
524
525 /* The match was ambiguous, don't allow a cast */
526 if (found > 1) {
527 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600528 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000529 } else if (type_is_ancestor(type, target_type)) {
530 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600531 }
532
Anthony Liguori33e95c62012-08-10 13:16:10 +1000533 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600534}
535
536ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200537 const char *typename,
538 const char *file, int line,
539 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600540{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200541 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600542
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200543 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
544 typename, file, line, func);
545
Anthony Liguori03587322013-05-13 15:22:24 -0500546#ifdef CONFIG_QOM_CAST_DEBUG
547 int i;
548
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000549 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Anthony Liguori03587322013-05-13 15:22:24 -0500550 if (class->cast_cache[i] == typename) {
551 ret = class;
552 goto out;
553 }
554 }
555#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000556 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +0200557 return class;
558 }
559#endif
560
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200561 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200562 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200563 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
564 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600565 abort();
566 }
567
Anthony Liguori03587322013-05-13 15:22:24 -0500568#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000569 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -0500570 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
571 class->cast_cache[i - 1] = class->cast_cache[i];
572 }
573 class->cast_cache[i - 1] = typename;
574 }
575out:
576#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600577 return ret;
578}
579
580const char *object_get_typename(Object *obj)
581{
582 return obj->class->type->name;
583}
584
585ObjectClass *object_get_class(Object *obj)
586{
587 return obj->class;
588}
589
Andreas Färber17862372013-01-23 12:20:18 +0100590bool object_class_is_abstract(ObjectClass *klass)
591{
592 return klass->type->abstract;
593}
594
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600595const char *object_class_get_name(ObjectClass *klass)
596{
597 return klass->type->name;
598}
599
600ObjectClass *object_class_by_name(const char *typename)
601{
602 TypeImpl *type = type_get_by_name(typename);
603
604 if (!type) {
605 return NULL;
606 }
607
Igor Mitsyankoac451032012-02-28 15:57:11 +0400608 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600609
610 return type->class;
611}
612
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200613ObjectClass *object_class_get_parent(ObjectClass *class)
614{
615 TypeImpl *type = type_get_parent(class->type);
616
617 if (!type) {
618 return NULL;
619 }
620
621 type_initialize(type);
622
623 return type->class;
624}
625
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600626typedef struct OCFData
627{
628 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600629 const char *implements_type;
630 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600631 void *opaque;
632} OCFData;
633
634static void object_class_foreach_tramp(gpointer key, gpointer value,
635 gpointer opaque)
636{
637 OCFData *data = opaque;
638 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600639 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600640
Igor Mitsyankoac451032012-02-28 15:57:11 +0400641 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600642 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600643
Anthony Liguori93c511a2011-12-22 14:11:53 -0600644 if (!data->include_abstract && type->abstract) {
645 return;
646 }
647
648 if (data->implements_type &&
649 !object_class_dynamic_cast(k, data->implements_type)) {
650 return;
651 }
652
653 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600654}
655
656void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600657 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600658 void *opaque)
659{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600660 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600661
662 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
663}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600664
Paolo Bonzini32efc532012-04-11 23:30:20 +0200665int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
666 void *opaque)
667{
668 ObjectProperty *prop;
669 int ret = 0;
670
671 QTAILQ_FOREACH(prop, &obj->properties, node) {
672 if (object_property_is_child(prop)) {
673 ret = fn(prop->opaque, opaque);
674 if (ret != 0) {
675 break;
676 }
677 }
678 }
679 return ret;
680}
681
Andreas Färber418ba9e2012-02-25 23:07:34 +0100682static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
683{
684 GSList **list = opaque;
685
686 *list = g_slist_prepend(*list, klass);
687}
688
689GSList *object_class_get_list(const char *implements_type,
690 bool include_abstract)
691{
692 GSList *list = NULL;
693
694 object_class_foreach(object_class_get_list_tramp,
695 implements_type, include_abstract, &list);
696 return list;
697}
698
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600699void object_ref(Object *obj)
700{
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200701 atomic_inc(&obj->ref);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600702}
703
704void object_unref(Object *obj)
705{
706 g_assert(obj->ref > 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600707
708 /* parent always holds a reference to its children */
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200709 if (atomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600710 object_finalize(obj);
711 }
712}
713
714void object_property_add(Object *obj, const char *name, const char *type,
715 ObjectPropertyAccessor *get,
716 ObjectPropertyAccessor *set,
717 ObjectPropertyRelease *release,
718 void *opaque, Error **errp)
719{
Peter Maydell54852b02013-03-25 13:15:13 +0000720 ObjectProperty *prop;
721
722 QTAILQ_FOREACH(prop, &obj->properties, node) {
723 if (strcmp(prop->name, name) == 0) {
724 error_setg(errp, "attempt to add duplicate property '%s'"
725 " to object (type '%s')", name,
726 object_get_typename(obj));
727 return;
728 }
729 }
730
731 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600732
733 prop->name = g_strdup(name);
734 prop->type = g_strdup(type);
735
736 prop->get = get;
737 prop->set = set;
738 prop->release = release;
739 prop->opaque = opaque;
740
741 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
742}
743
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200744ObjectProperty *object_property_find(Object *obj, const char *name,
745 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600746{
747 ObjectProperty *prop;
748
749 QTAILQ_FOREACH(prop, &obj->properties, node) {
750 if (strcmp(prop->name, name) == 0) {
751 return prop;
752 }
753 }
754
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200755 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600756 return NULL;
757}
758
759void object_property_del(Object *obj, const char *name, Error **errp)
760{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200761 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600762 if (prop == NULL) {
Anthony Liguori0866aca2011-12-23 15:34:39 -0600763 return;
764 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600765
Anthony Liguori0866aca2011-12-23 15:34:39 -0600766 if (prop->release) {
767 prop->release(obj, name, prop->opaque);
768 }
769
770 QTAILQ_REMOVE(&obj->properties, prop, node);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600771
772 g_free(prop->name);
773 g_free(prop->type);
774 g_free(prop);
775}
776
777void object_property_get(Object *obj, Visitor *v, const char *name,
778 Error **errp)
779{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200780 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600781 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600782 return;
783 }
784
785 if (!prop->get) {
786 error_set(errp, QERR_PERMISSION_DENIED);
787 } else {
788 prop->get(obj, v, prop->opaque, name, errp);
789 }
790}
791
792void object_property_set(Object *obj, Visitor *v, const char *name,
793 Error **errp)
794{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200795 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600796 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600797 return;
798 }
799
800 if (!prop->set) {
801 error_set(errp, QERR_PERMISSION_DENIED);
802 } else {
803 prop->set(obj, v, prop->opaque, name, errp);
804 }
805}
806
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100807void object_property_set_str(Object *obj, const char *value,
808 const char *name, Error **errp)
809{
810 QString *qstr = qstring_from_str(value);
811 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
812
813 QDECREF(qstr);
814}
815
816char *object_property_get_str(Object *obj, const char *name,
817 Error **errp)
818{
819 QObject *ret = object_property_get_qobject(obj, name, errp);
820 QString *qstring;
821 char *retval;
822
823 if (!ret) {
824 return NULL;
825 }
826 qstring = qobject_to_qstring(ret);
827 if (!qstring) {
828 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
829 retval = NULL;
830 } else {
831 retval = g_strdup(qstring_get_str(qstring));
832 }
833
834 QDECREF(qstring);
835 return retval;
836}
837
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100838void object_property_set_link(Object *obj, Object *value,
839 const char *name, Error **errp)
840{
Vlad Yasevich2d3aa282013-11-15 12:09:47 -0500841 gchar *path = object_get_canonical_path(value);
842 object_property_set_str(obj, path, name, errp);
843 g_free(path);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100844}
845
846Object *object_property_get_link(Object *obj, const char *name,
847 Error **errp)
848{
849 char *str = object_property_get_str(obj, name, errp);
850 Object *target = NULL;
851
852 if (str && *str) {
853 target = object_resolve_path(str, NULL);
854 if (!target) {
855 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
856 }
857 }
858
859 g_free(str);
860 return target;
861}
862
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100863void object_property_set_bool(Object *obj, bool value,
864 const char *name, Error **errp)
865{
866 QBool *qbool = qbool_from_int(value);
867 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
868
869 QDECREF(qbool);
870}
871
872bool object_property_get_bool(Object *obj, const char *name,
873 Error **errp)
874{
875 QObject *ret = object_property_get_qobject(obj, name, errp);
876 QBool *qbool;
877 bool retval;
878
879 if (!ret) {
880 return false;
881 }
882 qbool = qobject_to_qbool(ret);
883 if (!qbool) {
884 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
885 retval = false;
886 } else {
887 retval = qbool_get_int(qbool);
888 }
889
890 QDECREF(qbool);
891 return retval;
892}
893
894void object_property_set_int(Object *obj, int64_t value,
895 const char *name, Error **errp)
896{
897 QInt *qint = qint_from_int(value);
898 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
899
900 QDECREF(qint);
901}
902
903int64_t object_property_get_int(Object *obj, const char *name,
904 Error **errp)
905{
906 QObject *ret = object_property_get_qobject(obj, name, errp);
907 QInt *qint;
908 int64_t retval;
909
910 if (!ret) {
911 return -1;
912 }
913 qint = qobject_to_qint(ret);
914 if (!qint) {
915 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
916 retval = -1;
917 } else {
918 retval = qint_get_int(qint);
919 }
920
921 QDECREF(qint);
922 return retval;
923}
924
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100925void object_property_parse(Object *obj, const char *string,
926 const char *name, Error **errp)
927{
928 StringInputVisitor *mi;
929 mi = string_input_visitor_new(string);
930 object_property_set(obj, string_input_get_visitor(mi), name, errp);
931
932 string_input_visitor_cleanup(mi);
933}
934
935char *object_property_print(Object *obj, const char *name,
936 Error **errp)
937{
938 StringOutputVisitor *mo;
939 char *string;
940
941 mo = string_output_visitor_new();
Paolo Bonzini8185bfc2012-05-02 13:31:00 +0200942 object_property_get(obj, string_output_get_visitor(mo), name, errp);
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100943 string = string_output_get_string(mo);
944 string_output_visitor_cleanup(mo);
945 return string;
946}
947
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600948const char *object_property_get_type(Object *obj, const char *name, Error **errp)
949{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200950 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600951 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600952 return NULL;
953 }
954
955 return prop->type;
956}
957
958Object *object_get_root(void)
959{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600960 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600961
Anthony Liguori8b45d442011-12-23 09:08:05 -0600962 if (!root) {
963 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600964 }
965
Anthony Liguori8b45d442011-12-23 09:08:05 -0600966 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600967}
968
969static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
970 const char *name, Error **errp)
971{
972 Object *child = opaque;
973 gchar *path;
974
975 path = object_get_canonical_path(child);
976 visit_type_str(v, &path, name, errp);
977 g_free(path);
978}
979
Anthony Liguoridb85b572011-12-23 08:47:39 -0600980static void object_finalize_child_property(Object *obj, const char *name,
981 void *opaque)
982{
983 Object *child = opaque;
984
985 object_unref(child);
986}
987
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600988void object_property_add_child(Object *obj, const char *name,
989 Object *child, Error **errp)
990{
991 gchar *type;
992
993 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
994
995 object_property_add(obj, name, type, object_get_child_property,
Anthony Liguoridb85b572011-12-23 08:47:39 -0600996 NULL, object_finalize_child_property, child, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600997
998 object_ref(child);
999 g_assert(child->parent == NULL);
1000 child->parent = obj;
1001
1002 g_free(type);
1003}
1004
1005static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1006 const char *name, Error **errp)
1007{
1008 Object **child = opaque;
1009 gchar *path;
1010
1011 if (*child) {
1012 path = object_get_canonical_path(*child);
1013 visit_type_str(v, &path, name, errp);
1014 g_free(path);
1015 } else {
1016 path = (gchar *)"";
1017 visit_type_str(v, &path, name, errp);
1018 }
1019}
1020
1021static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1022 const char *name, Error **errp)
1023{
1024 Object **child = opaque;
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001025 Object *old_target;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001026 bool ambiguous = false;
1027 const char *type;
1028 char *path;
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001029 gchar *target_type;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001030
1031 type = object_property_get_type(obj, name, NULL);
1032
1033 visit_type_str(v, &path, name, errp);
1034
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001035 old_target = *child;
1036 *child = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001037
1038 if (strcmp(path, "") != 0) {
1039 Object *target;
1040
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001041 /* Go from link<FOO> to FOO. */
1042 target_type = g_strndup(&type[5], strlen(type) - 6);
1043 target = object_resolve_path_type(path, target_type, &ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001044
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001045 if (ambiguous) {
1046 error_set(errp, QERR_AMBIGUOUS_PATH, path);
1047 } else if (target) {
1048 object_ref(target);
1049 *child = target;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001050 } else {
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001051 target = object_resolve_path(path, &ambiguous);
1052 if (target || ambiguous) {
1053 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1054 } else {
1055 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1056 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001057 }
Paolo Bonzini11e35bf2012-02-02 12:37:53 +01001058 g_free(target_type);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001059 }
1060
1061 g_free(path);
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001062
1063 if (old_target != NULL) {
1064 object_unref(old_target);
1065 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001066}
1067
1068void object_property_add_link(Object *obj, const char *name,
1069 const char *type, Object **child,
1070 Error **errp)
1071{
1072 gchar *full_type;
1073
1074 full_type = g_strdup_printf("link<%s>", type);
1075
1076 object_property_add(obj, name, full_type,
1077 object_get_link_property,
1078 object_set_link_property,
1079 NULL, child, errp);
1080
1081 g_free(full_type);
1082}
1083
1084gchar *object_get_canonical_path(Object *obj)
1085{
1086 Object *root = object_get_root();
1087 char *newpath = NULL, *path = NULL;
1088
1089 while (obj != root) {
1090 ObjectProperty *prop = NULL;
1091
1092 g_assert(obj->parent != NULL);
1093
1094 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001095 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001096 continue;
1097 }
1098
1099 if (prop->opaque == obj) {
1100 if (path) {
1101 newpath = g_strdup_printf("%s/%s", prop->name, path);
1102 g_free(path);
1103 path = newpath;
1104 } else {
1105 path = g_strdup(prop->name);
1106 }
1107 break;
1108 }
1109 }
1110
1111 g_assert(prop != NULL);
1112
1113 obj = obj->parent;
1114 }
1115
1116 newpath = g_strdup_printf("/%s", path);
1117 g_free(path);
1118
1119 return newpath;
1120}
1121
Andreas Färber3e84b482013-01-15 02:55:10 +01001122Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001123{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001124 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001125 if (prop == NULL) {
1126 return NULL;
1127 }
1128
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001129 if (object_property_is_link(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001130 return *(Object **)prop->opaque;
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001131 } else if (object_property_is_child(prop)) {
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001132 return prop->opaque;
1133 } else {
1134 return NULL;
1135 }
1136}
1137
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001138static Object *object_resolve_abs_path(Object *parent,
1139 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001140 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001141 int index)
1142{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001143 Object *child;
1144
1145 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001146 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001147 }
1148
1149 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001150 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001151 }
1152
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001153 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001154 if (!child) {
1155 return NULL;
1156 }
1157
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001158 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001159}
1160
1161static Object *object_resolve_partial_path(Object *parent,
1162 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001163 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001164 bool *ambiguous)
1165{
1166 Object *obj;
1167 ObjectProperty *prop;
1168
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001169 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001170
1171 QTAILQ_FOREACH(prop, &parent->properties, node) {
1172 Object *found;
1173
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001174 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001175 continue;
1176 }
1177
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001178 found = object_resolve_partial_path(prop->opaque, parts,
1179 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001180 if (found) {
1181 if (obj) {
1182 if (ambiguous) {
1183 *ambiguous = true;
1184 }
1185 return NULL;
1186 }
1187 obj = found;
1188 }
1189
1190 if (ambiguous && *ambiguous) {
1191 return NULL;
1192 }
1193 }
1194
1195 return obj;
1196}
1197
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001198Object *object_resolve_path_type(const char *path, const char *typename,
1199 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001200{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001201 Object *obj;
1202 gchar **parts;
1203
1204 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001205 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001206
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001207 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001208 if (ambiguous) {
1209 *ambiguous = false;
1210 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001211 obj = object_resolve_partial_path(object_get_root(), parts,
1212 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001213 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001214 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001215 }
1216
1217 g_strfreev(parts);
1218
1219 return obj;
1220}
1221
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001222Object *object_resolve_path(const char *path, bool *ambiguous)
1223{
1224 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1225}
1226
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001227typedef struct StringProperty
1228{
1229 char *(*get)(Object *, Error **);
1230 void (*set)(Object *, const char *, Error **);
1231} StringProperty;
1232
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001233static void property_get_str(Object *obj, Visitor *v, void *opaque,
1234 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001235{
1236 StringProperty *prop = opaque;
1237 char *value;
1238
1239 value = prop->get(obj, errp);
1240 if (value) {
1241 visit_type_str(v, &value, name, errp);
1242 g_free(value);
1243 }
1244}
1245
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001246static void property_set_str(Object *obj, Visitor *v, void *opaque,
1247 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001248{
1249 StringProperty *prop = opaque;
1250 char *value;
1251 Error *local_err = NULL;
1252
1253 visit_type_str(v, &value, name, &local_err);
1254 if (local_err) {
1255 error_propagate(errp, local_err);
1256 return;
1257 }
1258
1259 prop->set(obj, value, errp);
1260 g_free(value);
1261}
1262
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001263static void property_release_str(Object *obj, const char *name,
1264 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001265{
1266 StringProperty *prop = opaque;
1267 g_free(prop);
1268}
1269
1270void object_property_add_str(Object *obj, const char *name,
1271 char *(*get)(Object *, Error **),
1272 void (*set)(Object *, const char *, Error **),
1273 Error **errp)
1274{
1275 StringProperty *prop = g_malloc0(sizeof(*prop));
1276
1277 prop->get = get;
1278 prop->set = set;
1279
1280 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001281 get ? property_get_str : NULL,
1282 set ? property_set_str : NULL,
1283 property_release_str,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001284 prop, errp);
1285}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001286
Anthony Liguori0e558842012-06-25 10:32:46 -05001287typedef struct BoolProperty
1288{
1289 bool (*get)(Object *, Error **);
1290 void (*set)(Object *, bool, Error **);
1291} BoolProperty;
1292
1293static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1294 const char *name, Error **errp)
1295{
1296 BoolProperty *prop = opaque;
1297 bool value;
1298
1299 value = prop->get(obj, errp);
1300 visit_type_bool(v, &value, name, errp);
1301}
1302
1303static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1304 const char *name, Error **errp)
1305{
1306 BoolProperty *prop = opaque;
1307 bool value;
1308 Error *local_err = NULL;
1309
1310 visit_type_bool(v, &value, name, &local_err);
1311 if (local_err) {
1312 error_propagate(errp, local_err);
1313 return;
1314 }
1315
1316 prop->set(obj, value, errp);
1317}
1318
1319static void property_release_bool(Object *obj, const char *name,
1320 void *opaque)
1321{
1322 BoolProperty *prop = opaque;
1323 g_free(prop);
1324}
1325
1326void object_property_add_bool(Object *obj, const char *name,
1327 bool (*get)(Object *, Error **),
1328 void (*set)(Object *, bool, Error **),
1329 Error **errp)
1330{
1331 BoolProperty *prop = g_malloc0(sizeof(*prop));
1332
1333 prop->get = get;
1334 prop->set = set;
1335
1336 object_property_add(obj, name, "bool",
1337 get ? property_get_bool : NULL,
1338 set ? property_set_bool : NULL,
1339 property_release_bool,
1340 prop, errp);
1341}
1342
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001343static char *qdev_get_type(Object *obj, Error **errp)
1344{
1345 return g_strdup(object_get_typename(obj));
1346}
1347
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03001348static void property_get_uint8_ptr(Object *obj, Visitor *v,
1349 void *opaque, const char *name,
1350 Error **errp)
1351{
1352 uint8_t value = *(uint8_t *)opaque;
1353 visit_type_uint8(v, &value, name, errp);
1354}
1355
1356static void property_get_uint16_ptr(Object *obj, Visitor *v,
1357 void *opaque, const char *name,
1358 Error **errp)
1359{
1360 uint16_t value = *(uint16_t *)opaque;
1361 visit_type_uint16(v, &value, name, errp);
1362}
1363
1364static void property_get_uint32_ptr(Object *obj, Visitor *v,
1365 void *opaque, const char *name,
1366 Error **errp)
1367{
1368 uint32_t value = *(uint32_t *)opaque;
1369 visit_type_uint32(v, &value, name, errp);
1370}
1371
1372static void property_get_uint64_ptr(Object *obj, Visitor *v,
1373 void *opaque, const char *name,
1374 Error **errp)
1375{
1376 uint64_t value = *(uint64_t *)opaque;
1377 visit_type_uint64(v, &value, name, errp);
1378}
1379
1380void object_property_add_uint8_ptr(Object *obj, const char *name,
1381 const uint8_t *v, Error **errp)
1382{
1383 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1384 NULL, NULL, (void *)v, errp);
1385}
1386
1387void object_property_add_uint16_ptr(Object *obj, const char *name,
1388 const uint16_t *v, Error **errp)
1389{
1390 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1391 NULL, NULL, (void *)v, errp);
1392}
1393
1394void object_property_add_uint32_ptr(Object *obj, const char *name,
1395 const uint32_t *v, Error **errp)
1396{
1397 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1398 NULL, NULL, (void *)v, errp);
1399}
1400
1401void object_property_add_uint64_ptr(Object *obj, const char *name,
1402 const uint64_t *v, Error **errp)
1403{
1404 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1405 NULL, NULL, (void *)v, errp);
1406}
1407
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001408static void object_instance_init(Object *obj)
1409{
1410 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1411}
1412
Paolo Bonzini745549c2012-03-31 16:45:54 +02001413static void register_types(void)
1414{
1415 static TypeInfo interface_info = {
1416 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10001417 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02001418 .abstract = true,
1419 };
1420
1421 static TypeInfo object_info = {
1422 .name = TYPE_OBJECT,
1423 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001424 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02001425 .abstract = true,
1426 };
1427
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02001428 type_interface = type_register_internal(&interface_info);
1429 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02001430}
1431
1432type_init(register_types)