blob: 575291f10958e8356460cd1fa5f13d9cc9704ffb [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"
Hu Tao1f217722014-05-14 17:43:33 +080016#include "qapi-visit.h"
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +010017#include "qapi/string-input-visitor.h"
18#include "qapi/string-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010019#include "qapi/qmp/qerror.h"
Paolo Bonzinifa131d92013-05-10 14:16:39 +020020#include "trace.h"
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060021
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010022/* TODO: replace QObject with a simpler visitor to avoid a dependency
23 * of the QOM core on QObject? */
Paolo Bonzini14cccb62012-12-17 18:19:50 +010024#include "qom/qom-qobject.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010025#include "qapi/qmp/qobject.h"
26#include "qapi/qmp/qbool.h"
27#include "qapi/qmp/qint.h"
28#include "qapi/qmp/qstring.h"
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +010029
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060030#define MAX_INTERFACES 32
31
32typedef struct InterfaceImpl InterfaceImpl;
33typedef struct TypeImpl TypeImpl;
34
35struct InterfaceImpl
36{
Anthony Liguori33e95c62012-08-10 13:16:10 +100037 const char *typename;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060038};
39
40struct TypeImpl
41{
42 const char *name;
43
44 size_t class_size;
45
46 size_t instance_size;
47
48 void (*class_init)(ObjectClass *klass, void *data);
Paolo Bonzini3b50e312012-05-02 13:30:55 +020049 void (*class_base_init)(ObjectClass *klass, void *data);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060050 void (*class_finalize)(ObjectClass *klass, void *data);
51
52 void *class_data;
53
54 void (*instance_init)(Object *obj);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -030055 void (*instance_post_init)(Object *obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060056 void (*instance_finalize)(Object *obj);
57
58 bool abstract;
59
60 const char *parent;
61 TypeImpl *parent_type;
62
63 ObjectClass *class;
64
65 int num_interfaces;
66 InterfaceImpl interfaces[MAX_INTERFACES];
67};
68
Paolo Bonzini9970bd82012-02-03 11:51:39 +010069static Type type_interface;
70
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060071static GHashTable *type_table_get(void)
72{
73 static GHashTable *type_table;
74
75 if (type_table == NULL) {
76 type_table = g_hash_table_new(g_str_hash, g_str_equal);
77 }
78
79 return type_table;
80}
81
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010082static bool enumerating_types;
83
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060084static void type_table_add(TypeImpl *ti)
85{
Hervé Poussineauf54c19c2013-12-03 16:42:00 +010086 assert(!enumerating_types);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060087 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
88}
89
90static TypeImpl *type_table_lookup(const char *name)
91{
92 return g_hash_table_lookup(type_table_get(), name);
93}
94
Paolo Bonzinib061dc42013-12-03 16:41:59 +010095static TypeImpl *type_new(const TypeInfo *info)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060096{
97 TypeImpl *ti = g_malloc0(sizeof(*ti));
Anthony Liguori33e95c62012-08-10 13:16:10 +100098 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060099
100 g_assert(info->name != NULL);
101
Anthony Liguori73093352012-01-25 13:37:36 -0600102 if (type_table_lookup(info->name) != NULL) {
103 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
104 abort();
105 }
106
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600107 ti->name = g_strdup(info->name);
108 ti->parent = g_strdup(info->parent);
109
110 ti->class_size = info->class_size;
111 ti->instance_size = info->instance_size;
112
113 ti->class_init = info->class_init;
Paolo Bonzini3b50e312012-05-02 13:30:55 +0200114 ti->class_base_init = info->class_base_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600115 ti->class_finalize = info->class_finalize;
116 ti->class_data = info->class_data;
117
118 ti->instance_init = info->instance_init;
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300119 ti->instance_post_init = info->instance_post_init;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600120 ti->instance_finalize = info->instance_finalize;
121
122 ti->abstract = info->abstract;
123
Anthony Liguori33e95c62012-08-10 13:16:10 +1000124 for (i = 0; info->interfaces && info->interfaces[i].type; i++) {
125 ti->interfaces[i].typename = g_strdup(info->interfaces[i].type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600126 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000127 ti->num_interfaces = i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600128
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100129 return ti;
130}
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600131
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100132static TypeImpl *type_register_internal(const TypeInfo *info)
133{
134 TypeImpl *ti;
135 ti = type_new(info);
136
137 type_table_add(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600138 return ti;
139}
140
Paolo Bonzini049cb3c2012-04-04 15:58:40 +0200141TypeImpl *type_register(const TypeInfo *info)
142{
143 assert(info->parent);
144 return type_register_internal(info);
145}
146
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600147TypeImpl *type_register_static(const TypeInfo *info)
148{
149 return type_register(info);
150}
151
152static TypeImpl *type_get_by_name(const char *name)
153{
154 if (name == NULL) {
155 return NULL;
156 }
157
158 return type_table_lookup(name);
159}
160
161static TypeImpl *type_get_parent(TypeImpl *type)
162{
163 if (!type->parent_type && type->parent) {
164 type->parent_type = type_get_by_name(type->parent);
165 g_assert(type->parent_type != NULL);
166 }
167
168 return type->parent_type;
169}
170
171static bool type_has_parent(TypeImpl *type)
172{
173 return (type->parent != NULL);
174}
175
176static size_t type_class_get_size(TypeImpl *ti)
177{
178 if (ti->class_size) {
179 return ti->class_size;
180 }
181
182 if (type_has_parent(ti)) {
183 return type_class_get_size(type_get_parent(ti));
184 }
185
186 return sizeof(ObjectClass);
187}
188
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400189static size_t type_object_get_size(TypeImpl *ti)
190{
191 if (ti->instance_size) {
192 return ti->instance_size;
193 }
194
195 if (type_has_parent(ti)) {
196 return type_object_get_size(type_get_parent(ti));
197 }
198
199 return 0;
200}
201
Anthony Liguori33e95c62012-08-10 13:16:10 +1000202static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600203{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000204 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600205
Anthony Liguori33e95c62012-08-10 13:16:10 +1000206 /* Check if typename is a direct ancestor of type */
207 while (type) {
208 if (type == target_type) {
209 return true;
210 }
211
212 type = type_get_parent(type);
213 }
214
215 return false;
216}
217
218static void type_initialize(TypeImpl *ti);
219
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100220static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type,
221 TypeImpl *parent_type)
Anthony Liguori33e95c62012-08-10 13:16:10 +1000222{
223 InterfaceClass *new_iface;
224 TypeInfo info = { };
225 TypeImpl *iface_impl;
226
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100227 info.parent = parent_type->name;
228 info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000229 info.abstract = true;
230
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100231 iface_impl = type_new(&info);
232 iface_impl->parent_type = parent_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000233 type_initialize(iface_impl);
234 g_free((char *)info.name);
235
236 new_iface = (InterfaceClass *)iface_impl->class;
237 new_iface->concrete_class = ti->class;
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100238 new_iface->interface_type = interface_type;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000239
240 ti->class->interfaces = g_slist_append(ti->class->interfaces,
241 iface_impl->class);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600242}
243
Igor Mitsyankoac451032012-02-28 15:57:11 +0400244static void type_initialize(TypeImpl *ti)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600245{
Paolo Bonzini745549c2012-03-31 16:45:54 +0200246 TypeImpl *parent;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600247
248 if (ti->class) {
249 return;
250 }
251
252 ti->class_size = type_class_get_size(ti);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400253 ti->instance_size = type_object_get_size(ti);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600254
255 ti->class = g_malloc0(ti->class_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600256
Paolo Bonzini745549c2012-03-31 16:45:54 +0200257 parent = type_get_parent(ti);
258 if (parent) {
Igor Mitsyankoac451032012-02-28 15:57:11 +0400259 type_initialize(parent);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000260 GSList *e;
261 int i;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600262
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600263 g_assert(parent->class_size <= ti->class_size);
Paolo Bonzini745549c2012-03-31 16:45:54 +0200264 memcpy(ti->class, parent->class, parent->class_size);
Peter Crosthwaite3e407de2013-02-19 14:02:09 +1000265 ti->class->interfaces = NULL;
Anthony Liguori33e95c62012-08-10 13:16:10 +1000266
267 for (e = parent->class->interfaces; e; e = e->next) {
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100268 InterfaceClass *iface = e->data;
269 ObjectClass *klass = OBJECT_CLASS(iface);
270
271 type_initialize_interface(ti, iface->interface_type, klass->type);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000272 }
273
274 for (i = 0; i < ti->num_interfaces; i++) {
275 TypeImpl *t = type_get_by_name(ti->interfaces[i].typename);
276 for (e = ti->class->interfaces; e; e = e->next) {
277 TypeImpl *target_type = OBJECT_CLASS(e->data)->type;
278
279 if (type_is_ancestor(target_type, t)) {
280 break;
281 }
282 }
283
284 if (e) {
285 continue;
286 }
287
Paolo Bonzinib061dc42013-12-03 16:41:59 +0100288 type_initialize_interface(ti, t, t);
Anthony Liguori33e95c62012-08-10 13:16:10 +1000289 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600290 }
291
Paolo Bonzini745549c2012-03-31 16:45:54 +0200292 ti->class->type = ti;
293
294 while (parent) {
295 if (parent->class_base_init) {
296 parent->class_base_init(ti->class, ti->class_data);
297 }
298 parent = type_get_parent(parent);
299 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600300
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600301 if (ti->class_init) {
302 ti->class_init(ti->class, ti->class_data);
303 }
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600304}
305
306static void object_init_with_type(Object *obj, TypeImpl *ti)
307{
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600308 if (type_has_parent(ti)) {
309 object_init_with_type(obj, type_get_parent(ti));
310 }
311
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600312 if (ti->instance_init) {
313 ti->instance_init(obj);
314 }
315}
316
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300317static void object_post_init_with_type(Object *obj, TypeImpl *ti)
318{
319 if (ti->instance_post_init) {
320 ti->instance_post_init(obj);
321 }
322
323 if (type_has_parent(ti)) {
324 object_post_init_with_type(obj, type_get_parent(ti));
325 }
326}
327
Andreas Färber5b9237f2013-08-30 18:28:37 +0200328void object_initialize_with_type(void *data, size_t size, TypeImpl *type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600329{
330 Object *obj = data;
331
332 g_assert(type != NULL);
Igor Mitsyankoac451032012-02-28 15:57:11 +0400333 type_initialize(type);
Igor Mitsyankoaca59af2012-02-28 15:57:10 +0400334
335 g_assert(type->instance_size >= sizeof(Object));
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600336 g_assert(type->abstract == false);
Andreas Färber5b9237f2013-08-30 18:28:37 +0200337 g_assert(size >= type->instance_size);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600338
339 memset(obj, 0, type->instance_size);
340 obj->class = type->class;
Paolo Bonzini764b6312012-11-23 09:47:12 +0100341 object_ref(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600342 QTAILQ_INIT(&obj->properties);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600343 object_init_with_type(obj, type);
Eduardo Habkost8231c2d2013-07-10 17:08:41 -0300344 object_post_init_with_type(obj, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600345}
346
Andreas Färber213f0c42013-08-23 19:37:12 +0200347void object_initialize(void *data, size_t size, const char *typename)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600348{
349 TypeImpl *type = type_get_by_name(typename);
350
Andreas Färber5b9237f2013-08-30 18:28:37 +0200351 object_initialize_with_type(data, size, type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600352}
353
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200354static inline bool object_property_is_child(ObjectProperty *prop)
355{
356 return strstart(prop->type, "child<", NULL);
357}
358
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600359static void object_property_del_all(Object *obj)
360{
361 while (!QTAILQ_EMPTY(&obj->properties)) {
362 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
363
364 QTAILQ_REMOVE(&obj->properties, prop, node);
365
366 if (prop->release) {
367 prop->release(obj, prop->name, prop->opaque);
368 }
369
370 g_free(prop->name);
371 g_free(prop->type);
372 g_free(prop);
373 }
374}
375
376static void object_property_del_child(Object *obj, Object *child, Error **errp)
377{
378 ObjectProperty *prop;
379
380 QTAILQ_FOREACH(prop, &obj->properties, node) {
Andreas Färber5d9d3f42012-05-27 00:32:40 +0200381 if (object_property_is_child(prop) && prop->opaque == child) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600382 object_property_del(obj, prop->name, errp);
Paolo Bonzini6c1fdcf2012-02-28 09:54:15 +0100383 break;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600384 }
385 }
386}
387
388void object_unparent(Object *obj)
389{
Michael S. Tsirkine998fa82013-03-18 21:01:37 +0200390 if (obj->parent) {
391 object_property_del_child(obj->parent, obj, NULL);
392 }
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
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600411 object_property_del_all(obj);
Paolo Bonzini76a6e1c2014-06-11 11:58:30 +0200412 object_deinit(obj, ti);
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++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800461 if (obj->class->object_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500462 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++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800478 obj->class->object_cast_cache[i - 1] =
479 obj->class->object_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500480 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800481 obj->class->object_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500482 }
483
484out:
Paolo Bonzini3556c232013-05-10 14:16:40 +0200485#endif
486 return obj;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600487}
488
489ObjectClass *object_class_dynamic_cast(ObjectClass *class,
490 const char *typename)
491{
Anthony Liguori33e95c62012-08-10 13:16:10 +1000492 ObjectClass *ret = NULL;
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200493 TypeImpl *target_type;
494 TypeImpl *type;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600495
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200496 if (!class) {
497 return NULL;
498 }
499
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200500 /* A simple fast path that can trigger a lot for leaf classes. */
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200501 type = class->type;
Paolo Bonzini793c96b2013-05-10 14:16:37 +0200502 if (type->name == typename) {
503 return class;
504 }
505
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200506 target_type = type_get_by_name(typename);
Alexander Graf9ab880b2013-04-30 15:02:16 +0200507 if (!target_type) {
508 /* target class type unknown, so fail the cast */
509 return NULL;
510 }
511
Peter Crosthwaite00e2cea2013-02-19 14:02:10 +1000512 if (type->class->interfaces &&
513 type_is_ancestor(target_type, type_interface)) {
Anthony Liguori33e95c62012-08-10 13:16:10 +1000514 int found = 0;
515 GSList *i;
516
517 for (i = class->interfaces; i; i = i->next) {
518 ObjectClass *target_class = i->data;
519
520 if (type_is_ancestor(target_class->type, target_type)) {
521 ret = target_class;
522 found++;
523 }
524 }
525
526 /* The match was ambiguous, don't allow a cast */
527 if (found > 1) {
528 ret = NULL;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600529 }
Anthony Liguori33e95c62012-08-10 13:16:10 +1000530 } else if (type_is_ancestor(type, target_type)) {
531 ret = class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600532 }
533
Anthony Liguori33e95c62012-08-10 13:16:10 +1000534 return ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600535}
536
537ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200538 const char *typename,
539 const char *file, int line,
540 const char *func)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600541{
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200542 ObjectClass *ret;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600543
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200544 trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)",
545 typename, file, line, func);
546
Anthony Liguori03587322013-05-13 15:22:24 -0500547#ifdef CONFIG_QOM_CAST_DEBUG
548 int i;
549
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000550 for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800551 if (class->class_cast_cache[i] == typename) {
Anthony Liguori03587322013-05-13 15:22:24 -0500552 ret = class;
553 goto out;
554 }
555 }
556#else
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000557 if (!class || !class->interfaces) {
Paolo Bonzini3556c232013-05-10 14:16:40 +0200558 return class;
559 }
560#endif
561
Paolo Bonzinifa131d92013-05-10 14:16:39 +0200562 ret = object_class_dynamic_cast(class, typename);
Paolo Bonzinibf0fda32013-05-10 14:16:36 +0200563 if (!ret && class) {
Paolo Bonzinibe17f182013-05-10 14:16:38 +0200564 fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n",
565 file, line, func, class, typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600566 abort();
567 }
568
Anthony Liguori03587322013-05-13 15:22:24 -0500569#ifdef CONFIG_QOM_CAST_DEBUG
Peter Crosthwaite9d6a3d52013-06-18 19:18:59 +1000570 if (class && ret == class) {
Anthony Liguori03587322013-05-13 15:22:24 -0500571 for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) {
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800572 class->class_cast_cache[i - 1] = class->class_cast_cache[i];
Anthony Liguori03587322013-05-13 15:22:24 -0500573 }
Peter Crosthwaite0ab4c942013-11-27 20:27:33 -0800574 class->class_cast_cache[i - 1] = typename;
Anthony Liguori03587322013-05-13 15:22:24 -0500575 }
576out:
577#endif
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600578 return ret;
579}
580
581const char *object_get_typename(Object *obj)
582{
583 return obj->class->type->name;
584}
585
586ObjectClass *object_get_class(Object *obj)
587{
588 return obj->class;
589}
590
Andreas Färber17862372013-01-23 12:20:18 +0100591bool object_class_is_abstract(ObjectClass *klass)
592{
593 return klass->type->abstract;
594}
595
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600596const char *object_class_get_name(ObjectClass *klass)
597{
598 return klass->type->name;
599}
600
601ObjectClass *object_class_by_name(const char *typename)
602{
603 TypeImpl *type = type_get_by_name(typename);
604
605 if (!type) {
606 return NULL;
607 }
608
Igor Mitsyankoac451032012-02-28 15:57:11 +0400609 type_initialize(type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600610
611 return type->class;
612}
613
Paolo Bonzinie7cce672012-05-02 13:30:54 +0200614ObjectClass *object_class_get_parent(ObjectClass *class)
615{
616 TypeImpl *type = type_get_parent(class->type);
617
618 if (!type) {
619 return NULL;
620 }
621
622 type_initialize(type);
623
624 return type->class;
625}
626
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600627typedef struct OCFData
628{
629 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600630 const char *implements_type;
631 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600632 void *opaque;
633} OCFData;
634
635static void object_class_foreach_tramp(gpointer key, gpointer value,
636 gpointer opaque)
637{
638 OCFData *data = opaque;
639 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600640 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600641
Igor Mitsyankoac451032012-02-28 15:57:11 +0400642 type_initialize(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600643 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600644
Anthony Liguori93c511a2011-12-22 14:11:53 -0600645 if (!data->include_abstract && type->abstract) {
646 return;
647 }
648
649 if (data->implements_type &&
650 !object_class_dynamic_cast(k, data->implements_type)) {
651 return;
652 }
653
654 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600655}
656
657void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600658 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600659 void *opaque)
660{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600661 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600662
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100663 enumerating_types = true;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600664 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
Hervé Poussineauf54c19c2013-12-03 16:42:00 +0100665 enumerating_types = false;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600666}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600667
Paolo Bonzini32efc532012-04-11 23:30:20 +0200668int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
669 void *opaque)
670{
Alexey Kardashevskiy8af734c2014-07-14 00:41:08 +1000671 ObjectProperty *prop, *next;
Paolo Bonzini32efc532012-04-11 23:30:20 +0200672 int ret = 0;
673
Alexey Kardashevskiy8af734c2014-07-14 00:41:08 +1000674 QTAILQ_FOREACH_SAFE(prop, &obj->properties, node, next) {
Paolo Bonzini32efc532012-04-11 23:30:20 +0200675 if (object_property_is_child(prop)) {
676 ret = fn(prop->opaque, opaque);
677 if (ret != 0) {
678 break;
679 }
680 }
681 }
682 return ret;
683}
684
Andreas Färber418ba9e2012-02-25 23:07:34 +0100685static void object_class_get_list_tramp(ObjectClass *klass, void *opaque)
686{
687 GSList **list = opaque;
688
689 *list = g_slist_prepend(*list, klass);
690}
691
692GSList *object_class_get_list(const char *implements_type,
693 bool include_abstract)
694{
695 GSList *list = NULL;
696
697 object_class_foreach(object_class_get_list_tramp,
698 implements_type, include_abstract, &list);
699 return list;
700}
701
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600702void object_ref(Object *obj)
703{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700704 if (!obj) {
705 return;
706 }
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200707 atomic_inc(&obj->ref);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600708}
709
710void object_unref(Object *obj)
711{
Peter Crosthwaite8ffad852014-06-05 23:13:36 -0700712 if (!obj) {
713 return;
714 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600715 g_assert(obj->ref > 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600716
717 /* parent always holds a reference to its children */
Jan Kiszkaf08c03f2013-07-02 11:36:39 +0200718 if (atomic_fetch_dec(&obj->ref) == 1) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600719 object_finalize(obj);
720 }
721}
722
Paolo Bonzini64607d02014-06-05 13:11:51 +0200723ObjectProperty *
724object_property_add(Object *obj, const char *name, const char *type,
725 ObjectPropertyAccessor *get,
726 ObjectPropertyAccessor *set,
727 ObjectPropertyRelease *release,
728 void *opaque, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600729{
Peter Maydell54852b02013-03-25 13:15:13 +0000730 ObjectProperty *prop;
Peter Crosthwaite33965902014-08-19 23:55:52 -0700731 size_t name_len = strlen(name);
732
733 if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) {
734 int i;
735 ObjectProperty *ret;
736 char *name_no_array = g_strdup(name);
737
738 name_no_array[name_len - 3] = '\0';
739 for (i = 0; ; ++i) {
740 char *full_name = g_strdup_printf("%s[%d]", name_no_array, i);
741
742 ret = object_property_add(obj, full_name, type, get, set,
743 release, opaque, NULL);
744 g_free(full_name);
745 if (ret) {
746 break;
747 }
748 }
749 g_free(name_no_array);
750 return ret;
751 }
Peter Maydell54852b02013-03-25 13:15:13 +0000752
753 QTAILQ_FOREACH(prop, &obj->properties, node) {
754 if (strcmp(prop->name, name) == 0) {
755 error_setg(errp, "attempt to add duplicate property '%s'"
756 " to object (type '%s')", name,
757 object_get_typename(obj));
Paolo Bonzini64607d02014-06-05 13:11:51 +0200758 return NULL;
Peter Maydell54852b02013-03-25 13:15:13 +0000759 }
760 }
761
762 prop = g_malloc0(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600763
764 prop->name = g_strdup(name);
765 prop->type = g_strdup(type);
766
767 prop->get = get;
768 prop->set = set;
769 prop->release = release;
770 prop->opaque = opaque;
771
772 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
Paolo Bonzini64607d02014-06-05 13:11:51 +0200773 return prop;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600774}
775
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200776ObjectProperty *object_property_find(Object *obj, const char *name,
777 Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600778{
779 ObjectProperty *prop;
780
781 QTAILQ_FOREACH(prop, &obj->properties, node) {
782 if (strcmp(prop->name, name) == 0) {
783 return prop;
784 }
785 }
786
Cole Robinsonf231b882014-03-21 19:42:26 -0400787 error_setg(errp, "Property '.%s' not found", name);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600788 return NULL;
789}
790
791void object_property_del(Object *obj, const char *name, Error **errp)
792{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200793 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori0866aca2011-12-23 15:34:39 -0600794 if (prop == NULL) {
Anthony Liguori0866aca2011-12-23 15:34:39 -0600795 return;
796 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600797
Anthony Liguori0866aca2011-12-23 15:34:39 -0600798 if (prop->release) {
799 prop->release(obj, name, prop->opaque);
800 }
801
802 QTAILQ_REMOVE(&obj->properties, prop, node);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600803
804 g_free(prop->name);
805 g_free(prop->type);
806 g_free(prop);
807}
808
809void object_property_get(Object *obj, Visitor *v, const char *name,
810 Error **errp)
811{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200812 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600813 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600814 return;
815 }
816
817 if (!prop->get) {
818 error_set(errp, QERR_PERMISSION_DENIED);
819 } else {
820 prop->get(obj, v, prop->opaque, name, errp);
821 }
822}
823
824void object_property_set(Object *obj, Visitor *v, const char *name,
825 Error **errp)
826{
Paolo Bonzini89bfe002012-04-12 18:00:18 +0200827 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600828 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600829 return;
830 }
831
832 if (!prop->set) {
833 error_set(errp, QERR_PERMISSION_DENIED);
834 } else {
835 prop->set(obj, v, prop->opaque, name, errp);
836 }
837}
838
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100839void object_property_set_str(Object *obj, const char *value,
840 const char *name, Error **errp)
841{
842 QString *qstr = qstring_from_str(value);
843 object_property_set_qobject(obj, QOBJECT(qstr), name, errp);
844
845 QDECREF(qstr);
846}
847
848char *object_property_get_str(Object *obj, const char *name,
849 Error **errp)
850{
851 QObject *ret = object_property_get_qobject(obj, name, errp);
852 QString *qstring;
853 char *retval;
854
855 if (!ret) {
856 return NULL;
857 }
858 qstring = qobject_to_qstring(ret);
859 if (!qstring) {
860 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string");
861 retval = NULL;
862 } else {
863 retval = g_strdup(qstring_get_str(qstring));
864 }
865
866 QDECREF(qstring);
867 return retval;
868}
869
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100870void object_property_set_link(Object *obj, Object *value,
871 const char *name, Error **errp)
872{
Vlad Yasevich2d3aa282013-11-15 12:09:47 -0500873 gchar *path = object_get_canonical_path(value);
874 object_property_set_str(obj, path, name, errp);
875 g_free(path);
Paolo Bonzini1d9c5a12012-02-02 10:51:57 +0100876}
877
878Object *object_property_get_link(Object *obj, const char *name,
879 Error **errp)
880{
881 char *str = object_property_get_str(obj, name, errp);
882 Object *target = NULL;
883
884 if (str && *str) {
885 target = object_resolve_path(str, NULL);
886 if (!target) {
887 error_set(errp, QERR_DEVICE_NOT_FOUND, str);
888 }
889 }
890
891 g_free(str);
892 return target;
893}
894
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +0100895void object_property_set_bool(Object *obj, bool value,
896 const char *name, Error **errp)
897{
898 QBool *qbool = qbool_from_int(value);
899 object_property_set_qobject(obj, QOBJECT(qbool), name, errp);
900
901 QDECREF(qbool);
902}
903
904bool object_property_get_bool(Object *obj, const char *name,
905 Error **errp)
906{
907 QObject *ret = object_property_get_qobject(obj, name, errp);
908 QBool *qbool;
909 bool retval;
910
911 if (!ret) {
912 return false;
913 }
914 qbool = qobject_to_qbool(ret);
915 if (!qbool) {
916 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean");
917 retval = false;
918 } else {
919 retval = qbool_get_int(qbool);
920 }
921
922 QDECREF(qbool);
923 return retval;
924}
925
926void object_property_set_int(Object *obj, int64_t value,
927 const char *name, Error **errp)
928{
929 QInt *qint = qint_from_int(value);
930 object_property_set_qobject(obj, QOBJECT(qint), name, errp);
931
932 QDECREF(qint);
933}
934
935int64_t object_property_get_int(Object *obj, const char *name,
936 Error **errp)
937{
938 QObject *ret = object_property_get_qobject(obj, name, errp);
939 QInt *qint;
940 int64_t retval;
941
942 if (!ret) {
943 return -1;
944 }
945 qint = qobject_to_qint(ret);
946 if (!qint) {
947 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int");
948 retval = -1;
949 } else {
950 retval = qint_get_int(qint);
951 }
952
953 QDECREF(qint);
954 return retval;
955}
956
Hu Tao1f217722014-05-14 17:43:33 +0800957int object_property_get_enum(Object *obj, const char *name,
958 const char *strings[], Error **errp)
959{
960 StringOutputVisitor *sov;
961 StringInputVisitor *siv;
Chen Fan976620a2014-08-18 14:46:34 +0800962 char *str;
Hu Tao1f217722014-05-14 17:43:33 +0800963 int ret;
964
965 sov = string_output_visitor_new(false);
966 object_property_get(obj, string_output_get_visitor(sov), name, errp);
Chen Fan976620a2014-08-18 14:46:34 +0800967 str = string_output_get_string(sov);
968 siv = string_input_visitor_new(str);
Hu Tao1f217722014-05-14 17:43:33 +0800969 string_output_visitor_cleanup(sov);
970 visit_type_enum(string_input_get_visitor(siv),
971 &ret, strings, NULL, name, errp);
Chen Fan976620a2014-08-18 14:46:34 +0800972
973 g_free(str);
Hu Tao1f217722014-05-14 17:43:33 +0800974 string_input_visitor_cleanup(siv);
975
976 return ret;
977}
978
979void object_property_get_uint16List(Object *obj, const char *name,
980 uint16List **list, Error **errp)
981{
982 StringOutputVisitor *ov;
983 StringInputVisitor *iv;
Chen Fan976620a2014-08-18 14:46:34 +0800984 char *str;
Hu Tao1f217722014-05-14 17:43:33 +0800985
986 ov = string_output_visitor_new(false);
987 object_property_get(obj, string_output_get_visitor(ov),
988 name, errp);
Chen Fan976620a2014-08-18 14:46:34 +0800989 str = string_output_get_string(ov);
990 iv = string_input_visitor_new(str);
Hu Tao1f217722014-05-14 17:43:33 +0800991 visit_type_uint16List(string_input_get_visitor(iv),
992 list, NULL, errp);
Chen Fan976620a2014-08-18 14:46:34 +0800993
994 g_free(str);
Hu Tao1f217722014-05-14 17:43:33 +0800995 string_output_visitor_cleanup(ov);
996 string_input_visitor_cleanup(iv);
997}
998
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +0100999void object_property_parse(Object *obj, const char *string,
1000 const char *name, Error **errp)
1001{
1002 StringInputVisitor *mi;
1003 mi = string_input_visitor_new(string);
1004 object_property_set(obj, string_input_get_visitor(mi), name, errp);
1005
1006 string_input_visitor_cleanup(mi);
1007}
1008
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001009char *object_property_print(Object *obj, const char *name, bool human,
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001010 Error **errp)
1011{
1012 StringOutputVisitor *mo;
Gonglei3a530092014-09-27 13:13:55 +08001013 char *string = NULL;
1014 Error *local_err = NULL;
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001015
Paolo Bonzini0b7593e2014-02-08 11:01:50 +01001016 mo = string_output_visitor_new(human);
Gonglei3a530092014-09-27 13:13:55 +08001017 object_property_get(obj, string_output_get_visitor(mo), name, &local_err);
1018 if (local_err) {
1019 error_propagate(errp, local_err);
1020 goto out;
1021 }
1022
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001023 string = string_output_get_string(mo);
Gonglei3a530092014-09-27 13:13:55 +08001024
1025out:
Paolo Bonzinib2cd7de2012-02-09 09:52:59 +01001026 string_output_visitor_cleanup(mo);
1027 return string;
1028}
1029
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001030const char *object_property_get_type(Object *obj, const char *name, Error **errp)
1031{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001032 ObjectProperty *prop = object_property_find(obj, name, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001033 if (prop == NULL) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001034 return NULL;
1035 }
1036
1037 return prop->type;
1038}
1039
1040Object *object_get_root(void)
1041{
Anthony Liguori8b45d442011-12-23 09:08:05 -06001042 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001043
Anthony Liguori8b45d442011-12-23 09:08:05 -06001044 if (!root) {
1045 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001046 }
1047
Anthony Liguori8b45d442011-12-23 09:08:05 -06001048 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001049}
1050
1051static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
1052 const char *name, Error **errp)
1053{
1054 Object *child = opaque;
1055 gchar *path;
1056
1057 path = object_get_canonical_path(child);
1058 visit_type_str(v, &path, name, errp);
1059 g_free(path);
1060}
1061
Paolo Bonzini64607d02014-06-05 13:11:51 +02001062static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part)
1063{
1064 return opaque;
1065}
1066
Anthony Liguoridb85b572011-12-23 08:47:39 -06001067static void object_finalize_child_property(Object *obj, const char *name,
1068 void *opaque)
1069{
1070 Object *child = opaque;
1071
Paolo Bonzinibffc6872014-06-11 11:57:38 +02001072 if (child->class->unparent) {
1073 (child->class->unparent)(child);
1074 }
1075 child->parent = NULL;
Anthony Liguoridb85b572011-12-23 08:47:39 -06001076 object_unref(child);
1077}
1078
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001079void object_property_add_child(Object *obj, const char *name,
1080 Object *child, Error **errp)
1081{
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001082 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001083 gchar *type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001084 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001085
1086 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
1087
Paolo Bonzini64607d02014-06-05 13:11:51 +02001088 op = object_property_add(obj, name, type, object_get_child_property, NULL,
1089 object_finalize_child_property, child, &local_err);
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001090 if (local_err) {
1091 error_propagate(errp, local_err);
1092 goto out;
1093 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001094
1095 op->resolve = object_resolve_child_property;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001096 object_ref(child);
1097 g_assert(child->parent == NULL);
1098 child->parent = obj;
1099
Paolo Bonzinib0ed5e92013-12-20 23:21:08 +01001100out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001101 g_free(type);
1102}
1103
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001104void object_property_allow_set_link(Object *obj, const char *name,
1105 Object *val, Error **errp)
1106{
1107 /* Allow the link to be set, always */
1108}
1109
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001110typedef struct {
1111 Object **child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001112 void (*check)(Object *, const char *, Object *, Error **);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001113 ObjectPropertyLinkFlags flags;
1114} LinkProperty;
1115
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001116static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
1117 const char *name, Error **errp)
1118{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001119 LinkProperty *lprop = opaque;
1120 Object **child = lprop->child;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001121 gchar *path;
1122
1123 if (*child) {
1124 path = object_get_canonical_path(*child);
1125 visit_type_str(v, &path, name, errp);
1126 g_free(path);
1127 } else {
1128 path = (gchar *)"";
1129 visit_type_str(v, &path, name, errp);
1130 }
1131}
1132
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001133/*
1134 * object_resolve_link:
1135 *
1136 * Lookup an object and ensure its type matches the link property type. This
1137 * is similar to object_resolve_path() except type verification against the
1138 * link property is performed.
1139 *
1140 * Returns: The matched object or NULL on path lookup failures.
1141 */
1142static Object *object_resolve_link(Object *obj, const char *name,
1143 const char *path, Error **errp)
1144{
1145 const char *type;
1146 gchar *target_type;
1147 bool ambiguous = false;
1148 Object *target;
1149
1150 /* Go from link<FOO> to FOO. */
1151 type = object_property_get_type(obj, name, NULL);
1152 target_type = g_strndup(&type[5], strlen(type) - 6);
1153 target = object_resolve_path_type(path, target_type, &ambiguous);
1154
1155 if (ambiguous) {
Cole Robinsonf231b882014-03-21 19:42:26 -04001156 error_set(errp, ERROR_CLASS_GENERIC_ERROR,
1157 "Path '%s' does not uniquely identify an object", path);
Stefan Hajnoczif5ec6702014-03-19 08:58:53 +01001158 } else if (!target) {
1159 target = object_resolve_path(path, &ambiguous);
1160 if (target || ambiguous) {
1161 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type);
1162 } else {
1163 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
1164 }
1165 target = NULL;
1166 }
1167 g_free(target_type);
1168
1169 return target;
1170}
1171
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001172static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
1173 const char *name, Error **errp)
1174{
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001175 Error *local_err = NULL;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001176 LinkProperty *prop = opaque;
1177 Object **child = prop->child;
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001178 Object *old_target = *child;
1179 Object *new_target = NULL;
1180 char *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001181
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001182 visit_type_str(v, &path, name, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001183
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001184 if (!local_err && strcmp(path, "") != 0) {
1185 new_target = object_resolve_link(obj, name, path, &local_err);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001186 }
1187
1188 g_free(path);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001189 if (local_err) {
1190 error_propagate(errp, local_err);
1191 return;
1192 }
Alexander Barabashf0cdc962012-02-22 19:22:26 +02001193
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001194 prop->check(obj, name, new_target, &local_err);
1195 if (local_err) {
1196 error_propagate(errp, local_err);
1197 return;
1198 }
1199
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001200 object_ref(new_target);
Stefan Hajnoczic6aed982014-03-19 08:58:54 +01001201 *child = new_target;
Peter Crosthwaite8ffad852014-06-05 23:13:36 -07001202 object_unref(old_target);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001203}
1204
Paolo Bonzini64607d02014-06-05 13:11:51 +02001205static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part)
1206{
1207 LinkProperty *lprop = opaque;
1208
1209 return *lprop->child;
1210}
1211
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001212static void object_release_link_property(Object *obj, const char *name,
1213 void *opaque)
1214{
1215 LinkProperty *prop = opaque;
1216
1217 if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) {
1218 object_unref(*prop->child);
1219 }
1220 g_free(prop);
1221}
1222
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001223void object_property_add_link(Object *obj, const char *name,
1224 const char *type, Object **child,
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001225 void (*check)(Object *, const char *,
1226 Object *, Error **),
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001227 ObjectPropertyLinkFlags flags,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001228 Error **errp)
1229{
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001230 Error *local_err = NULL;
1231 LinkProperty *prop = g_malloc(sizeof(*prop));
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001232 gchar *full_type;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001233 ObjectProperty *op;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001234
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001235 prop->child = child;
Stefan Hajnoczi39f72ef2014-03-19 08:58:56 +01001236 prop->check = check;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001237 prop->flags = flags;
1238
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001239 full_type = g_strdup_printf("link<%s>", type);
1240
Paolo Bonzini64607d02014-06-05 13:11:51 +02001241 op = object_property_add(obj, name, full_type,
1242 object_get_link_property,
1243 check ? object_set_link_property : NULL,
1244 object_release_link_property,
1245 prop,
1246 &local_err);
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001247 if (local_err) {
1248 error_propagate(errp, local_err);
1249 g_free(prop);
Paolo Bonzini64607d02014-06-05 13:11:51 +02001250 goto out;
Stefan Hajnoczi9561fda2014-03-19 08:58:55 +01001251 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001252
Paolo Bonzini64607d02014-06-05 13:11:51 +02001253 op->resolve = object_resolve_link_property;
1254
1255out:
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001256 g_free(full_type);
1257}
1258
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001259gchar *object_get_canonical_path_component(Object *obj)
1260{
1261 ObjectProperty *prop = NULL;
1262
1263 g_assert(obj);
1264 g_assert(obj->parent != NULL);
1265
1266 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
1267 if (!object_property_is_child(prop)) {
1268 continue;
1269 }
1270
1271 if (prop->opaque == obj) {
1272 return g_strdup(prop->name);
1273 }
1274 }
1275
1276 /* obj had a parent but was not a child, should never happen */
1277 g_assert_not_reached();
1278 return NULL;
1279}
1280
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001281gchar *object_get_canonical_path(Object *obj)
1282{
1283 Object *root = object_get_root();
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001284 char *newpath, *path = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001285
1286 while (obj != root) {
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001287 char *component = object_get_canonical_path_component(obj);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001288
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001289 if (path) {
1290 newpath = g_strdup_printf("%s/%s", component, path);
1291 g_free(component);
1292 g_free(path);
1293 path = newpath;
1294 } else {
1295 path = component;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001296 }
1297
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001298 obj = obj->parent;
1299 }
1300
Stefan Hajnoczi11f590b2014-03-03 11:30:02 +01001301 newpath = g_strdup_printf("/%s", path ? path : "");
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001302 g_free(path);
1303
1304 return newpath;
1305}
1306
Andreas Färber3e84b482013-01-15 02:55:10 +01001307Object *object_resolve_path_component(Object *parent, const gchar *part)
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001308{
Paolo Bonzini89bfe002012-04-12 18:00:18 +02001309 ObjectProperty *prop = object_property_find(parent, part, NULL);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001310 if (prop == NULL) {
1311 return NULL;
1312 }
1313
Paolo Bonzini64607d02014-06-05 13:11:51 +02001314 if (prop->resolve) {
1315 return prop->resolve(parent, prop->opaque, part);
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001316 } else {
1317 return NULL;
1318 }
1319}
1320
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001321static Object *object_resolve_abs_path(Object *parent,
1322 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001323 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001324 int index)
1325{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001326 Object *child;
1327
1328 if (parts[index] == NULL) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001329 return object_dynamic_cast(parent, typename);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001330 }
1331
1332 if (strcmp(parts[index], "") == 0) {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001333 return object_resolve_abs_path(parent, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001334 }
1335
Paolo Bonzinia612b2a2012-03-27 18:38:45 +02001336 child = object_resolve_path_component(parent, parts[index]);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001337 if (!child) {
1338 return NULL;
1339 }
1340
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001341 return object_resolve_abs_path(child, parts, typename, index + 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001342}
1343
1344static Object *object_resolve_partial_path(Object *parent,
1345 gchar **parts,
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001346 const char *typename,
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001347 bool *ambiguous)
1348{
1349 Object *obj;
1350 ObjectProperty *prop;
1351
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001352 obj = object_resolve_abs_path(parent, parts, typename, 0);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001353
1354 QTAILQ_FOREACH(prop, &parent->properties, node) {
1355 Object *found;
1356
Andreas Färber5d9d3f42012-05-27 00:32:40 +02001357 if (!object_property_is_child(prop)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001358 continue;
1359 }
1360
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001361 found = object_resolve_partial_path(prop->opaque, parts,
1362 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001363 if (found) {
1364 if (obj) {
1365 if (ambiguous) {
1366 *ambiguous = true;
1367 }
1368 return NULL;
1369 }
1370 obj = found;
1371 }
1372
1373 if (ambiguous && *ambiguous) {
1374 return NULL;
1375 }
1376 }
1377
1378 return obj;
1379}
1380
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001381Object *object_resolve_path_type(const char *path, const char *typename,
1382 bool *ambiguous)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001383{
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001384 Object *obj;
1385 gchar **parts;
1386
1387 parts = g_strsplit(path, "/", 0);
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001388 assert(parts);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001389
Paolo Bonzini2e1103f2013-04-18 18:44:02 +02001390 if (parts[0] == NULL || strcmp(parts[0], "") != 0) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001391 if (ambiguous) {
1392 *ambiguous = false;
1393 }
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001394 obj = object_resolve_partial_path(object_get_root(), parts,
1395 typename, ambiguous);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001396 } else {
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001397 obj = object_resolve_abs_path(object_get_root(), parts, typename, 1);
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001398 }
1399
1400 g_strfreev(parts);
1401
1402 return obj;
1403}
1404
Paolo Bonzini02fe2db2012-02-03 11:21:01 +01001405Object *object_resolve_path(const char *path, bool *ambiguous)
1406{
1407 return object_resolve_path_type(path, TYPE_OBJECT, ambiguous);
1408}
1409
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001410typedef struct StringProperty
1411{
1412 char *(*get)(Object *, Error **);
1413 void (*set)(Object *, const char *, Error **);
1414} StringProperty;
1415
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001416static void property_get_str(Object *obj, Visitor *v, void *opaque,
1417 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001418{
1419 StringProperty *prop = opaque;
1420 char *value;
1421
1422 value = prop->get(obj, errp);
1423 if (value) {
1424 visit_type_str(v, &value, name, errp);
1425 g_free(value);
1426 }
1427}
1428
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001429static void property_set_str(Object *obj, Visitor *v, void *opaque,
1430 const char *name, Error **errp)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001431{
1432 StringProperty *prop = opaque;
1433 char *value;
1434 Error *local_err = NULL;
1435
1436 visit_type_str(v, &value, name, &local_err);
1437 if (local_err) {
1438 error_propagate(errp, local_err);
1439 return;
1440 }
1441
1442 prop->set(obj, value, errp);
1443 g_free(value);
1444}
1445
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001446static void property_release_str(Object *obj, const char *name,
1447 void *opaque)
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001448{
1449 StringProperty *prop = opaque;
1450 g_free(prop);
1451}
1452
1453void object_property_add_str(Object *obj, const char *name,
1454 char *(*get)(Object *, Error **),
1455 void (*set)(Object *, const char *, Error **),
1456 Error **errp)
1457{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001458 Error *local_err = NULL;
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001459 StringProperty *prop = g_malloc0(sizeof(*prop));
1460
1461 prop->get = get;
1462 prop->set = set;
1463
1464 object_property_add(obj, name, "string",
Paolo Bonzini7b7b7d12012-02-01 17:16:22 +01001465 get ? property_get_str : NULL,
1466 set ? property_set_str : NULL,
1467 property_release_str,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001468 prop, &local_err);
1469 if (local_err) {
1470 error_propagate(errp, local_err);
1471 g_free(prop);
1472 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -06001473}
Paolo Bonzini745549c2012-03-31 16:45:54 +02001474
Anthony Liguori0e558842012-06-25 10:32:46 -05001475typedef struct BoolProperty
1476{
1477 bool (*get)(Object *, Error **);
1478 void (*set)(Object *, bool, Error **);
1479} BoolProperty;
1480
1481static void property_get_bool(Object *obj, Visitor *v, void *opaque,
1482 const char *name, Error **errp)
1483{
1484 BoolProperty *prop = opaque;
1485 bool value;
1486
1487 value = prop->get(obj, errp);
1488 visit_type_bool(v, &value, name, errp);
1489}
1490
1491static void property_set_bool(Object *obj, Visitor *v, void *opaque,
1492 const char *name, Error **errp)
1493{
1494 BoolProperty *prop = opaque;
1495 bool value;
1496 Error *local_err = NULL;
1497
1498 visit_type_bool(v, &value, name, &local_err);
1499 if (local_err) {
1500 error_propagate(errp, local_err);
1501 return;
1502 }
1503
1504 prop->set(obj, value, errp);
1505}
1506
1507static void property_release_bool(Object *obj, const char *name,
1508 void *opaque)
1509{
1510 BoolProperty *prop = opaque;
1511 g_free(prop);
1512}
1513
1514void object_property_add_bool(Object *obj, const char *name,
1515 bool (*get)(Object *, Error **),
1516 void (*set)(Object *, bool, Error **),
1517 Error **errp)
1518{
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001519 Error *local_err = NULL;
Anthony Liguori0e558842012-06-25 10:32:46 -05001520 BoolProperty *prop = g_malloc0(sizeof(*prop));
1521
1522 prop->get = get;
1523 prop->set = set;
1524
1525 object_property_add(obj, name, "bool",
1526 get ? property_get_bool : NULL,
1527 set ? property_set_bool : NULL,
1528 property_release_bool,
Stefan Hajnoczia01aedc2014-03-04 15:28:18 +01001529 prop, &local_err);
1530 if (local_err) {
1531 error_propagate(errp, local_err);
1532 g_free(prop);
1533 }
Anthony Liguori0e558842012-06-25 10:32:46 -05001534}
1535
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001536static char *qdev_get_type(Object *obj, Error **errp)
1537{
1538 return g_strdup(object_get_typename(obj));
1539}
1540
Michael S. Tsirkine732ea62013-09-22 10:10:17 +03001541static void property_get_uint8_ptr(Object *obj, Visitor *v,
1542 void *opaque, const char *name,
1543 Error **errp)
1544{
1545 uint8_t value = *(uint8_t *)opaque;
1546 visit_type_uint8(v, &value, name, errp);
1547}
1548
1549static void property_get_uint16_ptr(Object *obj, Visitor *v,
1550 void *opaque, const char *name,
1551 Error **errp)
1552{
1553 uint16_t value = *(uint16_t *)opaque;
1554 visit_type_uint16(v, &value, name, errp);
1555}
1556
1557static void property_get_uint32_ptr(Object *obj, Visitor *v,
1558 void *opaque, const char *name,
1559 Error **errp)
1560{
1561 uint32_t value = *(uint32_t *)opaque;
1562 visit_type_uint32(v, &value, name, errp);
1563}
1564
1565static void property_get_uint64_ptr(Object *obj, Visitor *v,
1566 void *opaque, const char *name,
1567 Error **errp)
1568{
1569 uint64_t value = *(uint64_t *)opaque;
1570 visit_type_uint64(v, &value, name, errp);
1571}
1572
1573void object_property_add_uint8_ptr(Object *obj, const char *name,
1574 const uint8_t *v, Error **errp)
1575{
1576 object_property_add(obj, name, "uint8", property_get_uint8_ptr,
1577 NULL, NULL, (void *)v, errp);
1578}
1579
1580void object_property_add_uint16_ptr(Object *obj, const char *name,
1581 const uint16_t *v, Error **errp)
1582{
1583 object_property_add(obj, name, "uint16", property_get_uint16_ptr,
1584 NULL, NULL, (void *)v, errp);
1585}
1586
1587void object_property_add_uint32_ptr(Object *obj, const char *name,
1588 const uint32_t *v, Error **errp)
1589{
1590 object_property_add(obj, name, "uint32", property_get_uint32_ptr,
1591 NULL, NULL, (void *)v, errp);
1592}
1593
1594void object_property_add_uint64_ptr(Object *obj, const char *name,
1595 const uint64_t *v, Error **errp)
1596{
1597 object_property_add(obj, name, "uint64", property_get_uint64_ptr,
1598 NULL, NULL, (void *)v, errp);
1599}
1600
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001601typedef struct {
1602 Object *target_obj;
1603 const char *target_name;
1604} AliasProperty;
1605
1606static void property_get_alias(Object *obj, struct Visitor *v, void *opaque,
1607 const char *name, Error **errp)
1608{
1609 AliasProperty *prop = opaque;
1610
1611 object_property_get(prop->target_obj, v, prop->target_name, errp);
1612}
1613
1614static void property_set_alias(Object *obj, struct Visitor *v, void *opaque,
1615 const char *name, Error **errp)
1616{
1617 AliasProperty *prop = opaque;
1618
1619 object_property_set(prop->target_obj, v, prop->target_name, errp);
1620}
1621
Paolo Bonzini64607d02014-06-05 13:11:51 +02001622static Object *property_resolve_alias(Object *obj, void *opaque,
1623 const gchar *part)
1624{
1625 AliasProperty *prop = opaque;
1626
1627 return object_resolve_path_component(prop->target_obj, prop->target_name);
1628}
1629
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001630static void property_release_alias(Object *obj, const char *name, void *opaque)
1631{
1632 AliasProperty *prop = opaque;
1633
1634 g_free(prop);
1635}
1636
1637void object_property_add_alias(Object *obj, const char *name,
1638 Object *target_obj, const char *target_name,
1639 Error **errp)
1640{
1641 AliasProperty *prop;
Paolo Bonzini64607d02014-06-05 13:11:51 +02001642 ObjectProperty *op;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001643 ObjectProperty *target_prop;
Paolo Bonzinid1906982014-06-10 11:17:35 +02001644 gchar *prop_type;
Gonglei8ae9a9e2014-09-27 13:13:56 +08001645 Error *local_err = NULL;
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001646
1647 target_prop = object_property_find(target_obj, target_name, errp);
1648 if (!target_prop) {
1649 return;
1650 }
1651
Paolo Bonzinid1906982014-06-10 11:17:35 +02001652 if (object_property_is_child(target_prop)) {
1653 prop_type = g_strdup_printf("link%s",
1654 target_prop->type + strlen("child"));
1655 } else {
1656 prop_type = g_strdup(target_prop->type);
1657 }
1658
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001659 prop = g_malloc(sizeof(*prop));
1660 prop->target_obj = target_obj;
1661 prop->target_name = target_name;
1662
Paolo Bonzinid1906982014-06-10 11:17:35 +02001663 op = object_property_add(obj, name, prop_type,
Paolo Bonzini64607d02014-06-05 13:11:51 +02001664 property_get_alias,
1665 property_set_alias,
1666 property_release_alias,
Gonglei8ae9a9e2014-09-27 13:13:56 +08001667 prop, &local_err);
1668 if (local_err) {
1669 error_propagate(errp, local_err);
1670 g_free(prop);
1671 goto out;
1672 }
Paolo Bonzini64607d02014-06-05 13:11:51 +02001673 op->resolve = property_resolve_alias;
Paolo Bonzinid1906982014-06-10 11:17:35 +02001674
Gonglei8ae9a9e2014-09-27 13:13:56 +08001675out:
Paolo Bonzinid1906982014-06-10 11:17:35 +02001676 g_free(prop_type);
Stefan Hajnoczief7c7ff2014-06-18 17:58:28 +08001677}
1678
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001679static void object_instance_init(Object *obj)
1680{
1681 object_property_add_str(obj, "type", qdev_get_type, NULL, NULL);
1682}
1683
Paolo Bonzini745549c2012-03-31 16:45:54 +02001684static void register_types(void)
1685{
1686 static TypeInfo interface_info = {
1687 .name = TYPE_INTERFACE,
Anthony Liguori33e95c62012-08-10 13:16:10 +10001688 .class_size = sizeof(InterfaceClass),
Paolo Bonzini745549c2012-03-31 16:45:54 +02001689 .abstract = true,
1690 };
1691
1692 static TypeInfo object_info = {
1693 .name = TYPE_OBJECT,
1694 .instance_size = sizeof(Object),
Paolo Bonzini2f262e02012-04-02 17:33:51 +02001695 .instance_init = object_instance_init,
Paolo Bonzini745549c2012-03-31 16:45:54 +02001696 .abstract = true,
1697 };
1698
Paolo Bonzini049cb3c2012-04-04 15:58:40 +02001699 type_interface = type_register_internal(&interface_info);
1700 type_register_internal(&object_info);
Paolo Bonzini745549c2012-03-31 16:45:54 +02001701}
1702
1703type_init(register_types)