blob: ffd50a35c01b113f03255354bdd6fd14673da45b [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
13#include "qemu/object.h"
14#include "qemu-common.h"
Anthony Liguori57c9faf2012-01-30 08:55:55 -060015#include "qapi/qapi-visit-core.h"
16#include "hw/qdev.h"
17// FIXME remove above
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060018
19#define MAX_INTERFACES 32
20
21typedef struct InterfaceImpl InterfaceImpl;
22typedef struct TypeImpl TypeImpl;
23
24struct InterfaceImpl
25{
26 const char *parent;
27 void (*interface_initfn)(ObjectClass *class, void *data);
28 TypeImpl *type;
29};
30
31struct TypeImpl
32{
33 const char *name;
34
35 size_t class_size;
36
37 size_t instance_size;
38
39 void (*class_init)(ObjectClass *klass, void *data);
40 void (*class_finalize)(ObjectClass *klass, void *data);
41
42 void *class_data;
43
44 void (*instance_init)(Object *obj);
45 void (*instance_finalize)(Object *obj);
46
47 bool abstract;
48
49 const char *parent;
50 TypeImpl *parent_type;
51
52 ObjectClass *class;
53
54 int num_interfaces;
55 InterfaceImpl interfaces[MAX_INTERFACES];
56};
57
58typedef struct Interface
59{
60 Object parent;
61 Object *obj;
62} Interface;
63
64#define INTERFACE(obj) OBJECT_CHECK(Interface, obj, TYPE_INTERFACE)
65
Paolo Bonzini9970bd82012-02-03 11:51:39 +010066static Type type_interface;
67
Anthony Liguori2f28d2f2011-12-03 17:10:08 -060068static GHashTable *type_table_get(void)
69{
70 static GHashTable *type_table;
71
72 if (type_table == NULL) {
73 type_table = g_hash_table_new(g_str_hash, g_str_equal);
74 }
75
76 return type_table;
77}
78
79static void type_table_add(TypeImpl *ti)
80{
81 g_hash_table_insert(type_table_get(), (void *)ti->name, ti);
82}
83
84static TypeImpl *type_table_lookup(const char *name)
85{
86 return g_hash_table_lookup(type_table_get(), name);
87}
88
89TypeImpl *type_register(const TypeInfo *info)
90{
91 TypeImpl *ti = g_malloc0(sizeof(*ti));
92
93 g_assert(info->name != NULL);
94
Anthony Liguori73093352012-01-25 13:37:36 -060095 if (type_table_lookup(info->name) != NULL) {
96 fprintf(stderr, "Registering `%s' which already exists\n", info->name);
97 abort();
98 }
99
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600100 ti->name = g_strdup(info->name);
101 ti->parent = g_strdup(info->parent);
102
103 ti->class_size = info->class_size;
104 ti->instance_size = info->instance_size;
105
106 ti->class_init = info->class_init;
107 ti->class_finalize = info->class_finalize;
108 ti->class_data = info->class_data;
109
110 ti->instance_init = info->instance_init;
111 ti->instance_finalize = info->instance_finalize;
112
113 ti->abstract = info->abstract;
114
115 if (info->interfaces) {
116 int i;
117
118 for (i = 0; info->interfaces[i].type; i++) {
119 ti->interfaces[i].parent = info->interfaces[i].type;
120 ti->interfaces[i].interface_initfn = info->interfaces[i].interface_initfn;
121 ti->num_interfaces++;
122 }
123 }
124
125 type_table_add(ti);
126
127 return ti;
128}
129
130TypeImpl *type_register_static(const TypeInfo *info)
131{
132 return type_register(info);
133}
134
135static TypeImpl *type_get_by_name(const char *name)
136{
137 if (name == NULL) {
138 return NULL;
139 }
140
141 return type_table_lookup(name);
142}
143
144static TypeImpl *type_get_parent(TypeImpl *type)
145{
146 if (!type->parent_type && type->parent) {
147 type->parent_type = type_get_by_name(type->parent);
148 g_assert(type->parent_type != NULL);
149 }
150
151 return type->parent_type;
152}
153
154static bool type_has_parent(TypeImpl *type)
155{
156 return (type->parent != NULL);
157}
158
159static size_t type_class_get_size(TypeImpl *ti)
160{
161 if (ti->class_size) {
162 return ti->class_size;
163 }
164
165 if (type_has_parent(ti)) {
166 return type_class_get_size(type_get_parent(ti));
167 }
168
169 return sizeof(ObjectClass);
170}
171
172static void type_class_interface_init(TypeImpl *ti, InterfaceImpl *iface)
173{
174 TypeInfo info = {
175 .instance_size = sizeof(Interface),
176 .parent = iface->parent,
177 .class_size = sizeof(InterfaceClass),
178 .class_init = iface->interface_initfn,
179 .abstract = true,
180 };
181 char *name = g_strdup_printf("<%s::%s>", ti->name, iface->parent);
182
183 info.name = name;
184 iface->type = type_register(&info);
185 g_free(name);
186}
187
188static void type_class_init(TypeImpl *ti)
189{
190 size_t class_size = sizeof(ObjectClass);
191 int i;
192
193 if (ti->class) {
194 return;
195 }
196
197 ti->class_size = type_class_get_size(ti);
198
199 ti->class = g_malloc0(ti->class_size);
200 ti->class->type = ti;
201
202 if (type_has_parent(ti)) {
203 TypeImpl *parent = type_get_parent(ti);
204
205 type_class_init(parent);
206
207 class_size = parent->class_size;
208 g_assert(parent->class_size <= ti->class_size);
209
210 memcpy((void *)ti->class + sizeof(ObjectClass),
211 (void *)parent->class + sizeof(ObjectClass),
212 parent->class_size - sizeof(ObjectClass));
213 }
214
215 memset((void *)ti->class + class_size, 0, ti->class_size - class_size);
216
217 for (i = 0; i < ti->num_interfaces; i++) {
218 type_class_interface_init(ti, &ti->interfaces[i]);
219 }
220
221 if (ti->class_init) {
222 ti->class_init(ti->class, ti->class_data);
223 }
224}
225
226static void object_interface_init(Object *obj, InterfaceImpl *iface)
227{
228 TypeImpl *ti = iface->type;
229 Interface *iface_obj;
230
231 iface_obj = INTERFACE(object_new(ti->name));
232 iface_obj->obj = obj;
233
234 obj->interfaces = g_slist_prepend(obj->interfaces, iface_obj);
235}
236
237static void object_init_with_type(Object *obj, TypeImpl *ti)
238{
239 int i;
240
241 if (type_has_parent(ti)) {
242 object_init_with_type(obj, type_get_parent(ti));
243 }
244
245 for (i = 0; i < ti->num_interfaces; i++) {
246 object_interface_init(obj, &ti->interfaces[i]);
247 }
248
249 if (ti->instance_init) {
250 ti->instance_init(obj);
251 }
252}
253
254void object_initialize_with_type(void *data, TypeImpl *type)
255{
256 Object *obj = data;
257
258 g_assert(type != NULL);
259 g_assert(type->instance_size >= sizeof(ObjectClass));
260
261 type_class_init(type);
262 g_assert(type->abstract == false);
263
264 memset(obj, 0, type->instance_size);
265 obj->class = type->class;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600266 QTAILQ_INIT(&obj->properties);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600267 object_init_with_type(obj, type);
268}
269
270void object_initialize(void *data, const char *typename)
271{
272 TypeImpl *type = type_get_by_name(typename);
273
274 object_initialize_with_type(data, type);
275}
276
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600277static void object_property_del_all(Object *obj)
278{
279 while (!QTAILQ_EMPTY(&obj->properties)) {
280 ObjectProperty *prop = QTAILQ_FIRST(&obj->properties);
281
282 QTAILQ_REMOVE(&obj->properties, prop, node);
283
284 if (prop->release) {
285 prop->release(obj, prop->name, prop->opaque);
286 }
287
288 g_free(prop->name);
289 g_free(prop->type);
290 g_free(prop);
291 }
292}
293
294static void object_property_del_child(Object *obj, Object *child, Error **errp)
295{
296 ObjectProperty *prop;
297
298 QTAILQ_FOREACH(prop, &obj->properties, node) {
299 if (!strstart(prop->type, "child<", NULL)) {
300 continue;
301 }
302
303 if (prop->opaque == child) {
304 object_property_del(obj, prop->name, errp);
305 }
306 }
307}
308
309void object_unparent(Object *obj)
310{
311 if (obj->parent) {
312 object_property_del_child(obj->parent, obj, NULL);
313 }
314}
315
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600316static void object_deinit(Object *obj, TypeImpl *type)
317{
318 if (type->instance_finalize) {
319 type->instance_finalize(obj);
320 }
321
322 while (obj->interfaces) {
323 Interface *iface_obj = obj->interfaces->data;
324 obj->interfaces = g_slist_delete_link(obj->interfaces, obj->interfaces);
325 object_delete(OBJECT(iface_obj));
326 }
327
328 if (type_has_parent(type)) {
329 object_deinit(obj, type_get_parent(type));
330 }
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600331
332 object_unparent(obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600333}
334
335void object_finalize(void *data)
336{
337 Object *obj = data;
338 TypeImpl *ti = obj->class->type;
339
340 object_deinit(obj, ti);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600341 object_property_del_all(obj);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600342
343 g_assert(obj->ref == 0);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600344}
345
346Object *object_new_with_type(Type type)
347{
348 Object *obj;
349
350 g_assert(type != NULL);
351
352 obj = g_malloc(type->instance_size);
353 object_initialize_with_type(obj, type);
Anthony Liguoridb85b572011-12-23 08:47:39 -0600354 object_ref(obj);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600355
356 return obj;
357}
358
359Object *object_new(const char *typename)
360{
361 TypeImpl *ti = type_get_by_name(typename);
362
363 return object_new_with_type(ti);
364}
365
366void object_delete(Object *obj)
367{
Anthony Liguoridb85b572011-12-23 08:47:39 -0600368 object_unref(obj);
369 g_assert(obj->ref == 0);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600370 g_free(obj);
371}
372
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100373static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type)
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600374{
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100375 assert(target_type);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600376
377 /* Check if typename is a direct ancestor of type */
378 while (type) {
379 if (type == target_type) {
380 return true;
381 }
382
383 type = type_get_parent(type);
384 }
385
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600386 return false;
387}
388
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100389static bool object_is_type(Object *obj, TypeImpl *target_type)
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100390{
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100391 return !target_type || type_is_ancestor(obj->class->type, target_type);
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100392}
393
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600394Object *object_dynamic_cast(Object *obj, const char *typename)
395{
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100396 TypeImpl *target_type = type_get_by_name(typename);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600397 GSList *i;
398
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100399 /* Check if typename is a direct ancestor. Special-case TYPE_OBJECT,
400 * we want to go back from interfaces to the parent.
401 */
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100402 if (target_type && object_is_type(obj, target_type)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100403 return obj;
404 }
405
406 /* Check if obj is an interface and its containing object is a direct
407 * ancestor of typename. In principle we could do this test at the very
408 * beginning of object_dynamic_cast, avoiding a second call to
409 * object_is_type. However, casting between interfaces is relatively
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100410 * rare, and object_is_type(obj, type_interface) would fail almost always.
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100411 *
412 * Perhaps we could add a magic value to the object header for increased
413 * (run-time) type safety and to speed up tests like this one. If we ever
414 * do that we can revisit the order here.
415 */
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100416 if (object_is_type(obj, type_interface)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100417 assert(!obj->interfaces);
418 obj = INTERFACE(obj)->obj;
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100419 if (object_is_type(obj, target_type)) {
Paolo Bonziniacc4af32012-02-03 11:57:23 +0100420 return obj;
421 }
422 }
423
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100424 if (!target_type) {
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600425 return obj;
426 }
427
428 /* Check if obj has an interface of typename */
429 for (i = obj->interfaces; i; i = i->next) {
430 Interface *iface = i->data;
431
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100432 if (object_is_type(OBJECT(iface), target_type)) {
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600433 return OBJECT(iface);
434 }
435 }
436
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600437 return NULL;
438}
439
440
441static void register_interface(void)
442{
443 static TypeInfo interface_info = {
444 .name = TYPE_INTERFACE,
445 .instance_size = sizeof(Interface),
446 .abstract = true,
447 };
448
Paolo Bonzini9970bd82012-02-03 11:51:39 +0100449 type_interface = type_register_static(&interface_info);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600450}
451
452device_init(register_interface);
453
454Object *object_dynamic_cast_assert(Object *obj, const char *typename)
455{
456 Object *inst;
457
458 inst = object_dynamic_cast(obj, typename);
459
460 if (!inst) {
461 fprintf(stderr, "Object %p is not an instance of type %s\n",
462 obj, typename);
463 abort();
464 }
465
466 return inst;
467}
468
469ObjectClass *object_class_dynamic_cast(ObjectClass *class,
470 const char *typename)
471{
472 TypeImpl *target_type = type_get_by_name(typename);
473 TypeImpl *type = class->type;
474
475 while (type) {
476 if (type == target_type) {
477 return class;
478 }
479
480 type = type_get_parent(type);
481 }
482
483 return NULL;
484}
485
486ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class,
487 const char *typename)
488{
489 ObjectClass *ret = object_class_dynamic_cast(class, typename);
490
491 if (!ret) {
492 fprintf(stderr, "Object %p is not an instance of type %s\n",
493 class, typename);
494 abort();
495 }
496
497 return ret;
498}
499
500const char *object_get_typename(Object *obj)
501{
502 return obj->class->type->name;
503}
504
505ObjectClass *object_get_class(Object *obj)
506{
507 return obj->class;
508}
509
510const char *object_class_get_name(ObjectClass *klass)
511{
512 return klass->type->name;
513}
514
515ObjectClass *object_class_by_name(const char *typename)
516{
517 TypeImpl *type = type_get_by_name(typename);
518
519 if (!type) {
520 return NULL;
521 }
522
523 type_class_init(type);
524
525 return type->class;
526}
527
528typedef struct OCFData
529{
530 void (*fn)(ObjectClass *klass, void *opaque);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600531 const char *implements_type;
532 bool include_abstract;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600533 void *opaque;
534} OCFData;
535
536static void object_class_foreach_tramp(gpointer key, gpointer value,
537 gpointer opaque)
538{
539 OCFData *data = opaque;
540 TypeImpl *type = value;
Anthony Liguori93c511a2011-12-22 14:11:53 -0600541 ObjectClass *k;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600542
543 type_class_init(type);
Anthony Liguori93c511a2011-12-22 14:11:53 -0600544 k = type->class;
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600545
Anthony Liguori93c511a2011-12-22 14:11:53 -0600546 if (!data->include_abstract && type->abstract) {
547 return;
548 }
549
550 if (data->implements_type &&
551 !object_class_dynamic_cast(k, data->implements_type)) {
552 return;
553 }
554
555 data->fn(k, data->opaque);
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600556}
557
558void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
Anthony Liguori93c511a2011-12-22 14:11:53 -0600559 const char *implements_type, bool include_abstract,
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600560 void *opaque)
561{
Anthony Liguori93c511a2011-12-22 14:11:53 -0600562 OCFData data = { fn, implements_type, include_abstract, opaque };
Anthony Liguori2f28d2f2011-12-03 17:10:08 -0600563
564 g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data);
565}
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600566
567void object_ref(Object *obj)
568{
569 obj->ref++;
570}
571
572void object_unref(Object *obj)
573{
574 g_assert(obj->ref > 0);
575 obj->ref--;
576
577 /* parent always holds a reference to its children */
578 if (obj->ref == 0) {
579 object_finalize(obj);
580 }
581}
582
583void object_property_add(Object *obj, const char *name, const char *type,
584 ObjectPropertyAccessor *get,
585 ObjectPropertyAccessor *set,
586 ObjectPropertyRelease *release,
587 void *opaque, Error **errp)
588{
589 ObjectProperty *prop = g_malloc0(sizeof(*prop));
590
591 prop->name = g_strdup(name);
592 prop->type = g_strdup(type);
593
594 prop->get = get;
595 prop->set = set;
596 prop->release = release;
597 prop->opaque = opaque;
598
599 QTAILQ_INSERT_TAIL(&obj->properties, prop, node);
600}
601
602static ObjectProperty *object_property_find(Object *obj, const char *name)
603{
604 ObjectProperty *prop;
605
606 QTAILQ_FOREACH(prop, &obj->properties, node) {
607 if (strcmp(prop->name, name) == 0) {
608 return prop;
609 }
610 }
611
612 return NULL;
613}
614
615void object_property_del(Object *obj, const char *name, Error **errp)
616{
617 ObjectProperty *prop = object_property_find(obj, name);
618
619 QTAILQ_REMOVE(&obj->properties, prop, node);
620
621 prop->release(obj, prop->name, prop->opaque);
622
623 g_free(prop->name);
624 g_free(prop->type);
625 g_free(prop);
626}
627
628void object_property_get(Object *obj, Visitor *v, const char *name,
629 Error **errp)
630{
631 ObjectProperty *prop = object_property_find(obj, name);
632
633 if (prop == NULL) {
634 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
635 return;
636 }
637
638 if (!prop->get) {
639 error_set(errp, QERR_PERMISSION_DENIED);
640 } else {
641 prop->get(obj, v, prop->opaque, name, errp);
642 }
643}
644
645void object_property_set(Object *obj, Visitor *v, const char *name,
646 Error **errp)
647{
648 ObjectProperty *prop = object_property_find(obj, name);
649
650 if (prop == NULL) {
651 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
652 return;
653 }
654
655 if (!prop->set) {
656 error_set(errp, QERR_PERMISSION_DENIED);
657 } else {
658 prop->set(obj, v, prop->opaque, name, errp);
659 }
660}
661
662const char *object_property_get_type(Object *obj, const char *name, Error **errp)
663{
664 ObjectProperty *prop = object_property_find(obj, name);
665
666 if (prop == NULL) {
667 error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name);
668 return NULL;
669 }
670
671 return prop->type;
672}
673
674Object *object_get_root(void)
675{
Anthony Liguori8b45d442011-12-23 09:08:05 -0600676 static Object *root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600677
Anthony Liguori8b45d442011-12-23 09:08:05 -0600678 if (!root) {
679 root = object_new("container");
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600680 }
681
Anthony Liguori8b45d442011-12-23 09:08:05 -0600682 return root;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600683}
684
685static void object_get_child_property(Object *obj, Visitor *v, void *opaque,
686 const char *name, Error **errp)
687{
688 Object *child = opaque;
689 gchar *path;
690
691 path = object_get_canonical_path(child);
692 visit_type_str(v, &path, name, errp);
693 g_free(path);
694}
695
Anthony Liguoridb85b572011-12-23 08:47:39 -0600696static void object_finalize_child_property(Object *obj, const char *name,
697 void *opaque)
698{
699 Object *child = opaque;
700
701 object_unref(child);
702}
703
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600704void object_property_add_child(Object *obj, const char *name,
705 Object *child, Error **errp)
706{
707 gchar *type;
708
709 type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child)));
710
711 object_property_add(obj, name, type, object_get_child_property,
Anthony Liguoridb85b572011-12-23 08:47:39 -0600712 NULL, object_finalize_child_property, child, errp);
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600713
714 object_ref(child);
715 g_assert(child->parent == NULL);
716 child->parent = obj;
717
718 g_free(type);
719}
720
721static void object_get_link_property(Object *obj, Visitor *v, void *opaque,
722 const char *name, Error **errp)
723{
724 Object **child = opaque;
725 gchar *path;
726
727 if (*child) {
728 path = object_get_canonical_path(*child);
729 visit_type_str(v, &path, name, errp);
730 g_free(path);
731 } else {
732 path = (gchar *)"";
733 visit_type_str(v, &path, name, errp);
734 }
735}
736
737static void object_set_link_property(Object *obj, Visitor *v, void *opaque,
738 const char *name, Error **errp)
739{
740 Object **child = opaque;
741 bool ambiguous = false;
742 const char *type;
743 char *path;
744
745 type = object_property_get_type(obj, name, NULL);
746
747 visit_type_str(v, &path, name, errp);
748
749 if (*child) {
750 object_unref(*child);
751 }
752
753 if (strcmp(path, "") != 0) {
754 Object *target;
755
756 target = object_resolve_path(path, &ambiguous);
757 if (target) {
758 gchar *target_type;
759
Anthony Liguorife40e622011-12-23 08:35:43 -0600760 target_type = g_strdup(&type[5]);
761 target_type[strlen(target_type) - 2] = 0;
762
763 if (object_dynamic_cast(target, target_type)) {
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600764 object_ref(target);
Anthony Liguorife40e622011-12-23 08:35:43 -0600765 *child = target;
Anthony Liguori57c9faf2012-01-30 08:55:55 -0600766 } else {
767 error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, type);
768 }
769
770 g_free(target_type);
771 } else {
772 error_set(errp, QERR_DEVICE_NOT_FOUND, path);
773 }
774 } else {
775 *child = NULL;
776 }
777
778 g_free(path);
779}
780
781void object_property_add_link(Object *obj, const char *name,
782 const char *type, Object **child,
783 Error **errp)
784{
785 gchar *full_type;
786
787 full_type = g_strdup_printf("link<%s>", type);
788
789 object_property_add(obj, name, full_type,
790 object_get_link_property,
791 object_set_link_property,
792 NULL, child, errp);
793
794 g_free(full_type);
795}
796
797gchar *object_get_canonical_path(Object *obj)
798{
799 Object *root = object_get_root();
800 char *newpath = NULL, *path = NULL;
801
802 while (obj != root) {
803 ObjectProperty *prop = NULL;
804
805 g_assert(obj->parent != NULL);
806
807 QTAILQ_FOREACH(prop, &obj->parent->properties, node) {
808 if (!strstart(prop->type, "child<", NULL)) {
809 continue;
810 }
811
812 if (prop->opaque == obj) {
813 if (path) {
814 newpath = g_strdup_printf("%s/%s", prop->name, path);
815 g_free(path);
816 path = newpath;
817 } else {
818 path = g_strdup(prop->name);
819 }
820 break;
821 }
822 }
823
824 g_assert(prop != NULL);
825
826 obj = obj->parent;
827 }
828
829 newpath = g_strdup_printf("/%s", path);
830 g_free(path);
831
832 return newpath;
833}
834
835static Object *object_resolve_abs_path(Object *parent,
836 gchar **parts,
837 int index)
838{
839 ObjectProperty *prop;
840 Object *child;
841
842 if (parts[index] == NULL) {
843 return parent;
844 }
845
846 if (strcmp(parts[index], "") == 0) {
847 return object_resolve_abs_path(parent, parts, index + 1);
848 }
849
850 prop = object_property_find(parent, parts[index]);
851 if (prop == NULL) {
852 return NULL;
853 }
854
855 child = NULL;
856 if (strstart(prop->type, "link<", NULL)) {
857 Object **pchild = prop->opaque;
858 if (*pchild) {
859 child = *pchild;
860 }
861 } else if (strstart(prop->type, "child<", NULL)) {
862 child = prop->opaque;
863 }
864
865 if (!child) {
866 return NULL;
867 }
868
869 return object_resolve_abs_path(child, parts, index + 1);
870}
871
872static Object *object_resolve_partial_path(Object *parent,
873 gchar **parts,
874 bool *ambiguous)
875{
876 Object *obj;
877 ObjectProperty *prop;
878
879 obj = object_resolve_abs_path(parent, parts, 0);
880
881 QTAILQ_FOREACH(prop, &parent->properties, node) {
882 Object *found;
883
884 if (!strstart(prop->type, "child<", NULL)) {
885 continue;
886 }
887
888 found = object_resolve_partial_path(prop->opaque, parts, ambiguous);
889 if (found) {
890 if (obj) {
891 if (ambiguous) {
892 *ambiguous = true;
893 }
894 return NULL;
895 }
896 obj = found;
897 }
898
899 if (ambiguous && *ambiguous) {
900 return NULL;
901 }
902 }
903
904 return obj;
905}
906
907Object *object_resolve_path(const char *path, bool *ambiguous)
908{
909 bool partial_path = true;
910 Object *obj;
911 gchar **parts;
912
913 parts = g_strsplit(path, "/", 0);
914 if (parts == NULL || parts[0] == NULL) {
915 g_strfreev(parts);
916 return object_get_root();
917 }
918
919 if (strcmp(parts[0], "") == 0) {
920 partial_path = false;
921 }
922
923 if (partial_path) {
924 if (ambiguous) {
925 *ambiguous = false;
926 }
927 obj = object_resolve_partial_path(object_get_root(), parts, ambiguous);
928 } else {
929 obj = object_resolve_abs_path(object_get_root(), parts, 1);
930 }
931
932 g_strfreev(parts);
933
934 return obj;
935}
936
937typedef struct StringProperty
938{
939 char *(*get)(Object *, Error **);
940 void (*set)(Object *, const char *, Error **);
941} StringProperty;
942
943static void object_property_get_str(Object *obj, Visitor *v, void *opaque,
944 const char *name, Error **errp)
945{
946 StringProperty *prop = opaque;
947 char *value;
948
949 value = prop->get(obj, errp);
950 if (value) {
951 visit_type_str(v, &value, name, errp);
952 g_free(value);
953 }
954}
955
956static void object_property_set_str(Object *obj, Visitor *v, void *opaque,
957 const char *name, Error **errp)
958{
959 StringProperty *prop = opaque;
960 char *value;
961 Error *local_err = NULL;
962
963 visit_type_str(v, &value, name, &local_err);
964 if (local_err) {
965 error_propagate(errp, local_err);
966 return;
967 }
968
969 prop->set(obj, value, errp);
970 g_free(value);
971}
972
973static void object_property_release_str(Object *obj, const char *name,
974 void *opaque)
975{
976 StringProperty *prop = opaque;
977 g_free(prop);
978}
979
980void object_property_add_str(Object *obj, const char *name,
981 char *(*get)(Object *, Error **),
982 void (*set)(Object *, const char *, Error **),
983 Error **errp)
984{
985 StringProperty *prop = g_malloc0(sizeof(*prop));
986
987 prop->get = get;
988 prop->set = set;
989
990 object_property_add(obj, name, "string",
991 get ? object_property_get_str : NULL,
992 set ? object_property_set_str : NULL,
993 object_property_release_str,
994 prop, errp);
995}