]>
Commit | Line | Data |
---|---|---|
2f28d2ff AL |
1 | /* |
2 | * QEMU Object Model | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | ||
14cccb61 | 13 | #include "qom/object.h" |
a31bdae5 | 14 | #include "qom/object_interfaces.h" |
2f28d2ff | 15 | #include "qemu-common.h" |
7b1b5d19 | 16 | #include "qapi/visitor.h" |
1f21772d | 17 | #include "qapi-visit.h" |
b2cd7dee PB |
18 | #include "qapi/string-input-visitor.h" |
19 | #include "qapi/string-output-visitor.h" | |
7b1b5d19 | 20 | #include "qapi/qmp/qerror.h" |
fa131d94 | 21 | #include "trace.h" |
2f28d2ff | 22 | |
7b7b7d18 PB |
23 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
24 | * of the QOM core on QObject? */ | |
14cccb61 | 25 | #include "qom/qom-qobject.h" |
7b1b5d19 PB |
26 | #include "qapi/qmp/qobject.h" |
27 | #include "qapi/qmp/qbool.h" | |
28 | #include "qapi/qmp/qint.h" | |
29 | #include "qapi/qmp/qstring.h" | |
7b7b7d18 | 30 | |
2f28d2ff AL |
31 | #define MAX_INTERFACES 32 |
32 | ||
33 | typedef struct InterfaceImpl InterfaceImpl; | |
34 | typedef struct TypeImpl TypeImpl; | |
35 | ||
36 | struct InterfaceImpl | |
37 | { | |
33e95c63 | 38 | const char *typename; |
2f28d2ff AL |
39 | }; |
40 | ||
41 | struct TypeImpl | |
42 | { | |
43 | const char *name; | |
44 | ||
45 | size_t class_size; | |
46 | ||
47 | size_t instance_size; | |
48 | ||
49 | void (*class_init)(ObjectClass *klass, void *data); | |
3b50e311 | 50 | void (*class_base_init)(ObjectClass *klass, void *data); |
2f28d2ff AL |
51 | void (*class_finalize)(ObjectClass *klass, void *data); |
52 | ||
53 | void *class_data; | |
54 | ||
55 | void (*instance_init)(Object *obj); | |
8231c2dd | 56 | void (*instance_post_init)(Object *obj); |
2f28d2ff AL |
57 | void (*instance_finalize)(Object *obj); |
58 | ||
59 | bool abstract; | |
60 | ||
61 | const char *parent; | |
62 | TypeImpl *parent_type; | |
63 | ||
64 | ObjectClass *class; | |
65 | ||
66 | int num_interfaces; | |
67 | InterfaceImpl interfaces[MAX_INTERFACES]; | |
68 | }; | |
69 | ||
9970bd88 PB |
70 | static Type type_interface; |
71 | ||
2f28d2ff AL |
72 | static GHashTable *type_table_get(void) |
73 | { | |
74 | static GHashTable *type_table; | |
75 | ||
76 | if (type_table == NULL) { | |
77 | type_table = g_hash_table_new(g_str_hash, g_str_equal); | |
78 | } | |
79 | ||
80 | return type_table; | |
81 | } | |
82 | ||
f54c19ca HP |
83 | static bool enumerating_types; |
84 | ||
2f28d2ff AL |
85 | static void type_table_add(TypeImpl *ti) |
86 | { | |
f54c19ca | 87 | assert(!enumerating_types); |
2f28d2ff AL |
88 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); |
89 | } | |
90 | ||
91 | static TypeImpl *type_table_lookup(const char *name) | |
92 | { | |
93 | return g_hash_table_lookup(type_table_get(), name); | |
94 | } | |
95 | ||
b061dc41 | 96 | static TypeImpl *type_new(const TypeInfo *info) |
2f28d2ff AL |
97 | { |
98 | TypeImpl *ti = g_malloc0(sizeof(*ti)); | |
33e95c63 | 99 | int i; |
2f28d2ff AL |
100 | |
101 | g_assert(info->name != NULL); | |
102 | ||
73093354 AL |
103 | if (type_table_lookup(info->name) != NULL) { |
104 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); | |
105 | abort(); | |
106 | } | |
107 | ||
2f28d2ff AL |
108 | ti->name = g_strdup(info->name); |
109 | ti->parent = g_strdup(info->parent); | |
110 | ||
111 | ti->class_size = info->class_size; | |
112 | ti->instance_size = info->instance_size; | |
113 | ||
114 | ti->class_init = info->class_init; | |
3b50e311 | 115 | ti->class_base_init = info->class_base_init; |
2f28d2ff AL |
116 | ti->class_finalize = info->class_finalize; |
117 | ti->class_data = info->class_data; | |
118 | ||
119 | ti->instance_init = info->instance_init; | |
8231c2dd | 120 | ti->instance_post_init = info->instance_post_init; |
2f28d2ff AL |
121 | ti->instance_finalize = info->instance_finalize; |
122 | ||
123 | ti->abstract = info->abstract; | |
124 | ||
33e95c63 AL |
125 | for (i = 0; info->interfaces && info->interfaces[i].type; i++) { |
126 | ti->interfaces[i].typename = g_strdup(info->interfaces[i].type); | |
2f28d2ff | 127 | } |
33e95c63 | 128 | ti->num_interfaces = i; |
2f28d2ff | 129 | |
b061dc41 PB |
130 | return ti; |
131 | } | |
2f28d2ff | 132 | |
b061dc41 PB |
133 | static TypeImpl *type_register_internal(const TypeInfo *info) |
134 | { | |
135 | TypeImpl *ti; | |
136 | ti = type_new(info); | |
137 | ||
138 | type_table_add(ti); | |
2f28d2ff AL |
139 | return ti; |
140 | } | |
141 | ||
049cb3cf PB |
142 | TypeImpl *type_register(const TypeInfo *info) |
143 | { | |
144 | assert(info->parent); | |
145 | return type_register_internal(info); | |
146 | } | |
147 | ||
2f28d2ff AL |
148 | TypeImpl *type_register_static(const TypeInfo *info) |
149 | { | |
150 | return type_register(info); | |
151 | } | |
152 | ||
153 | static TypeImpl *type_get_by_name(const char *name) | |
154 | { | |
155 | if (name == NULL) { | |
156 | return NULL; | |
157 | } | |
158 | ||
159 | return type_table_lookup(name); | |
160 | } | |
161 | ||
162 | static TypeImpl *type_get_parent(TypeImpl *type) | |
163 | { | |
164 | if (!type->parent_type && type->parent) { | |
165 | type->parent_type = type_get_by_name(type->parent); | |
166 | g_assert(type->parent_type != NULL); | |
167 | } | |
168 | ||
169 | return type->parent_type; | |
170 | } | |
171 | ||
172 | static bool type_has_parent(TypeImpl *type) | |
173 | { | |
174 | return (type->parent != NULL); | |
175 | } | |
176 | ||
177 | static size_t type_class_get_size(TypeImpl *ti) | |
178 | { | |
179 | if (ti->class_size) { | |
180 | return ti->class_size; | |
181 | } | |
182 | ||
183 | if (type_has_parent(ti)) { | |
184 | return type_class_get_size(type_get_parent(ti)); | |
185 | } | |
186 | ||
187 | return sizeof(ObjectClass); | |
188 | } | |
189 | ||
aca59af6 IM |
190 | static size_t type_object_get_size(TypeImpl *ti) |
191 | { | |
192 | if (ti->instance_size) { | |
193 | return ti->instance_size; | |
194 | } | |
195 | ||
196 | if (type_has_parent(ti)) { | |
197 | return type_object_get_size(type_get_parent(ti)); | |
198 | } | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
33e95c63 | 203 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
2f28d2ff | 204 | { |
33e95c63 AL |
205 | assert(target_type); |
206 | ||
b30d8054 | 207 | /* Check if target_type is a direct ancestor of type */ |
33e95c63 AL |
208 | while (type) { |
209 | if (type == target_type) { | |
210 | return true; | |
211 | } | |
2f28d2ff | 212 | |
33e95c63 AL |
213 | type = type_get_parent(type); |
214 | } | |
215 | ||
216 | return false; | |
217 | } | |
218 | ||
219 | static void type_initialize(TypeImpl *ti); | |
220 | ||
b061dc41 PB |
221 | static void type_initialize_interface(TypeImpl *ti, TypeImpl *interface_type, |
222 | TypeImpl *parent_type) | |
33e95c63 AL |
223 | { |
224 | InterfaceClass *new_iface; | |
225 | TypeInfo info = { }; | |
226 | TypeImpl *iface_impl; | |
227 | ||
b061dc41 PB |
228 | info.parent = parent_type->name; |
229 | info.name = g_strdup_printf("%s::%s", ti->name, interface_type->name); | |
33e95c63 AL |
230 | info.abstract = true; |
231 | ||
b061dc41 PB |
232 | iface_impl = type_new(&info); |
233 | iface_impl->parent_type = parent_type; | |
33e95c63 AL |
234 | type_initialize(iface_impl); |
235 | g_free((char *)info.name); | |
236 | ||
237 | new_iface = (InterfaceClass *)iface_impl->class; | |
238 | new_iface->concrete_class = ti->class; | |
b061dc41 | 239 | new_iface->interface_type = interface_type; |
33e95c63 AL |
240 | |
241 | ti->class->interfaces = g_slist_append(ti->class->interfaces, | |
242 | iface_impl->class); | |
2f28d2ff AL |
243 | } |
244 | ||
16bf7f52 DB |
245 | static void object_property_free(gpointer data) |
246 | { | |
247 | ObjectProperty *prop = data; | |
248 | ||
249 | g_free(prop->name); | |
250 | g_free(prop->type); | |
251 | g_free(prop->description); | |
252 | g_free(prop); | |
253 | } | |
254 | ||
ac451033 | 255 | static void type_initialize(TypeImpl *ti) |
2f28d2ff | 256 | { |
745549c8 | 257 | TypeImpl *parent; |
2f28d2ff AL |
258 | |
259 | if (ti->class) { | |
260 | return; | |
261 | } | |
262 | ||
263 | ti->class_size = type_class_get_size(ti); | |
aca59af6 | 264 | ti->instance_size = type_object_get_size(ti); |
2f28d2ff AL |
265 | |
266 | ti->class = g_malloc0(ti->class_size); | |
2f28d2ff | 267 | |
745549c8 PB |
268 | parent = type_get_parent(ti); |
269 | if (parent) { | |
ac451033 | 270 | type_initialize(parent); |
33e95c63 AL |
271 | GSList *e; |
272 | int i; | |
2f28d2ff | 273 | |
8438a135 | 274 | g_assert_cmpint(parent->class_size, <=, ti->class_size); |
745549c8 | 275 | memcpy(ti->class, parent->class, parent->class_size); |
3e407de4 | 276 | ti->class->interfaces = NULL; |
16bf7f52 DB |
277 | ti->class->properties = g_hash_table_new_full( |
278 | g_str_hash, g_str_equal, g_free, object_property_free); | |
33e95c63 AL |
279 | |
280 | for (e = parent->class->interfaces; e; e = e->next) { | |
b061dc41 PB |
281 | InterfaceClass *iface = e->data; |
282 | ObjectClass *klass = OBJECT_CLASS(iface); | |
283 | ||
284 | type_initialize_interface(ti, iface->interface_type, klass->type); | |
33e95c63 AL |
285 | } |
286 | ||
287 | for (i = 0; i < ti->num_interfaces; i++) { | |
288 | TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); | |
289 | for (e = ti->class->interfaces; e; e = e->next) { | |
290 | TypeImpl *target_type = OBJECT_CLASS(e->data)->type; | |
291 | ||
292 | if (type_is_ancestor(target_type, t)) { | |
293 | break; | |
294 | } | |
295 | } | |
296 | ||
297 | if (e) { | |
298 | continue; | |
299 | } | |
300 | ||
b061dc41 | 301 | type_initialize_interface(ti, t, t); |
33e95c63 | 302 | } |
16bf7f52 DB |
303 | } else { |
304 | ti->class->properties = g_hash_table_new_full( | |
305 | g_str_hash, g_str_equal, g_free, object_property_free); | |
745549c8 | 306 | } |
2f28d2ff | 307 | |
745549c8 | 308 | ti->class->type = ti; |
3b50e311 | 309 | |
745549c8 PB |
310 | while (parent) { |
311 | if (parent->class_base_init) { | |
312 | parent->class_base_init(ti->class, ti->class_data); | |
3b50e311 | 313 | } |
745549c8 | 314 | parent = type_get_parent(parent); |
2f28d2ff AL |
315 | } |
316 | ||
2f28d2ff AL |
317 | if (ti->class_init) { |
318 | ti->class_init(ti->class, ti->class_data); | |
319 | } | |
2f28d2ff AL |
320 | } |
321 | ||
322 | static void object_init_with_type(Object *obj, TypeImpl *ti) | |
323 | { | |
2f28d2ff AL |
324 | if (type_has_parent(ti)) { |
325 | object_init_with_type(obj, type_get_parent(ti)); | |
326 | } | |
327 | ||
2f28d2ff AL |
328 | if (ti->instance_init) { |
329 | ti->instance_init(obj); | |
330 | } | |
331 | } | |
332 | ||
8231c2dd EH |
333 | static void object_post_init_with_type(Object *obj, TypeImpl *ti) |
334 | { | |
335 | if (ti->instance_post_init) { | |
336 | ti->instance_post_init(obj); | |
337 | } | |
338 | ||
339 | if (type_has_parent(ti)) { | |
340 | object_post_init_with_type(obj, type_get_parent(ti)); | |
341 | } | |
342 | } | |
343 | ||
5b9237f6 | 344 | void object_initialize_with_type(void *data, size_t size, TypeImpl *type) |
2f28d2ff AL |
345 | { |
346 | Object *obj = data; | |
347 | ||
348 | g_assert(type != NULL); | |
ac451033 | 349 | type_initialize(type); |
aca59af6 | 350 | |
8438a135 | 351 | g_assert_cmpint(type->instance_size, >=, sizeof(Object)); |
2f28d2ff | 352 | g_assert(type->abstract == false); |
8438a135 | 353 | g_assert_cmpint(size, >=, type->instance_size); |
2f28d2ff AL |
354 | |
355 | memset(obj, 0, type->instance_size); | |
356 | obj->class = type->class; | |
764b6312 | 357 | object_ref(obj); |
b604a854 PF |
358 | obj->properties = g_hash_table_new_full(g_str_hash, g_str_equal, |
359 | NULL, object_property_free); | |
2f28d2ff | 360 | object_init_with_type(obj, type); |
8231c2dd | 361 | object_post_init_with_type(obj, type); |
2f28d2ff AL |
362 | } |
363 | ||
213f0c4f | 364 | void object_initialize(void *data, size_t size, const char *typename) |
2f28d2ff AL |
365 | { |
366 | TypeImpl *type = type_get_by_name(typename); | |
367 | ||
5b9237f6 | 368 | object_initialize_with_type(data, size, type); |
2f28d2ff AL |
369 | } |
370 | ||
5d9d3f47 AF |
371 | static inline bool object_property_is_child(ObjectProperty *prop) |
372 | { | |
373 | return strstart(prop->type, "child<", NULL); | |
374 | } | |
375 | ||
57c9fafe AL |
376 | static void object_property_del_all(Object *obj) |
377 | { | |
b604a854 PF |
378 | ObjectProperty *prop; |
379 | GHashTableIter iter; | |
380 | gpointer key, value; | |
381 | bool released; | |
382 | ||
383 | do { | |
384 | released = false; | |
385 | g_hash_table_iter_init(&iter, obj->properties); | |
386 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
387 | prop = value; | |
388 | if (prop->release) { | |
389 | prop->release(obj, prop->name, prop->opaque); | |
390 | prop->release = NULL; | |
391 | released = true; | |
392 | break; | |
393 | } | |
394 | g_hash_table_iter_remove(&iter); | |
57c9fafe | 395 | } |
b604a854 | 396 | } while (released); |
57c9fafe | 397 | |
b604a854 | 398 | g_hash_table_unref(obj->properties); |
57c9fafe AL |
399 | } |
400 | ||
401 | static void object_property_del_child(Object *obj, Object *child, Error **errp) | |
402 | { | |
403 | ObjectProperty *prop; | |
b604a854 PF |
404 | GHashTableIter iter; |
405 | gpointer key, value; | |
57c9fafe | 406 | |
b604a854 PF |
407 | g_hash_table_iter_init(&iter, obj->properties); |
408 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
409 | prop = value; | |
410 | if (object_property_is_child(prop) && prop->opaque == child) { | |
411 | if (prop->release) { | |
412 | prop->release(obj, prop->name, prop->opaque); | |
413 | prop->release = NULL; | |
414 | } | |
415 | break; | |
416 | } | |
417 | } | |
418 | g_hash_table_iter_init(&iter, obj->properties); | |
419 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
420 | prop = value; | |
5d9d3f47 | 421 | if (object_property_is_child(prop) && prop->opaque == child) { |
b604a854 | 422 | g_hash_table_iter_remove(&iter); |
6c1fdcf9 | 423 | break; |
57c9fafe AL |
424 | } |
425 | } | |
426 | } | |
427 | ||
428 | void object_unparent(Object *obj) | |
429 | { | |
e998fa8d MT |
430 | if (obj->parent) { |
431 | object_property_del_child(obj->parent, obj, NULL); | |
432 | } | |
57c9fafe AL |
433 | } |
434 | ||
2f28d2ff AL |
435 | static void object_deinit(Object *obj, TypeImpl *type) |
436 | { | |
437 | if (type->instance_finalize) { | |
438 | type->instance_finalize(obj); | |
439 | } | |
440 | ||
2f28d2ff AL |
441 | if (type_has_parent(type)) { |
442 | object_deinit(obj, type_get_parent(type)); | |
443 | } | |
444 | } | |
445 | ||
339c2708 | 446 | static void object_finalize(void *data) |
2f28d2ff AL |
447 | { |
448 | Object *obj = data; | |
449 | TypeImpl *ti = obj->class->type; | |
450 | ||
57c9fafe | 451 | object_property_del_all(obj); |
76a6e1cc | 452 | object_deinit(obj, ti); |
db85b575 | 453 | |
8438a135 | 454 | g_assert_cmpint(obj->ref, ==, 0); |
fde9bf44 PB |
455 | if (obj->free) { |
456 | obj->free(obj); | |
457 | } | |
2f28d2ff AL |
458 | } |
459 | ||
460 | Object *object_new_with_type(Type type) | |
461 | { | |
462 | Object *obj; | |
463 | ||
464 | g_assert(type != NULL); | |
ac451033 | 465 | type_initialize(type); |
2f28d2ff AL |
466 | |
467 | obj = g_malloc(type->instance_size); | |
5b9237f6 | 468 | object_initialize_with_type(obj, type->instance_size, type); |
fde9bf44 | 469 | obj->free = g_free; |
2f28d2ff AL |
470 | |
471 | return obj; | |
472 | } | |
473 | ||
474 | Object *object_new(const char *typename) | |
475 | { | |
476 | TypeImpl *ti = type_get_by_name(typename); | |
477 | ||
478 | return object_new_with_type(ti); | |
479 | } | |
480 | ||
a31bdae5 DB |
481 | |
482 | Object *object_new_with_props(const char *typename, | |
483 | Object *parent, | |
484 | const char *id, | |
485 | Error **errp, | |
486 | ...) | |
487 | { | |
488 | va_list vargs; | |
489 | Object *obj; | |
490 | ||
491 | va_start(vargs, errp); | |
492 | obj = object_new_with_propv(typename, parent, id, errp, vargs); | |
493 | va_end(vargs); | |
494 | ||
495 | return obj; | |
496 | } | |
497 | ||
498 | ||
499 | Object *object_new_with_propv(const char *typename, | |
500 | Object *parent, | |
501 | const char *id, | |
502 | Error **errp, | |
503 | va_list vargs) | |
504 | { | |
505 | Object *obj; | |
506 | ObjectClass *klass; | |
507 | Error *local_err = NULL; | |
508 | ||
509 | klass = object_class_by_name(typename); | |
510 | if (!klass) { | |
511 | error_setg(errp, "invalid object type: %s", typename); | |
512 | return NULL; | |
513 | } | |
514 | ||
515 | if (object_class_is_abstract(klass)) { | |
516 | error_setg(errp, "object type '%s' is abstract", typename); | |
517 | return NULL; | |
518 | } | |
519 | obj = object_new(typename); | |
520 | ||
521 | if (object_set_propv(obj, &local_err, vargs) < 0) { | |
522 | goto error; | |
523 | } | |
524 | ||
525 | object_property_add_child(parent, id, obj, &local_err); | |
526 | if (local_err) { | |
527 | goto error; | |
528 | } | |
529 | ||
530 | if (object_dynamic_cast(obj, TYPE_USER_CREATABLE)) { | |
531 | user_creatable_complete(obj, &local_err); | |
532 | if (local_err) { | |
533 | object_unparent(obj); | |
534 | goto error; | |
535 | } | |
536 | } | |
537 | ||
538 | object_unref(OBJECT(obj)); | |
539 | return obj; | |
540 | ||
541 | error: | |
542 | if (local_err) { | |
543 | error_propagate(errp, local_err); | |
544 | } | |
545 | object_unref(obj); | |
546 | return NULL; | |
547 | } | |
548 | ||
549 | ||
550 | int object_set_props(Object *obj, | |
551 | Error **errp, | |
552 | ...) | |
553 | { | |
554 | va_list vargs; | |
555 | int ret; | |
556 | ||
557 | va_start(vargs, errp); | |
558 | ret = object_set_propv(obj, errp, vargs); | |
559 | va_end(vargs); | |
560 | ||
561 | return ret; | |
562 | } | |
563 | ||
564 | ||
565 | int object_set_propv(Object *obj, | |
566 | Error **errp, | |
567 | va_list vargs) | |
568 | { | |
569 | const char *propname; | |
570 | Error *local_err = NULL; | |
571 | ||
572 | propname = va_arg(vargs, char *); | |
573 | while (propname != NULL) { | |
574 | const char *value = va_arg(vargs, char *); | |
575 | ||
576 | g_assert(value != NULL); | |
577 | object_property_parse(obj, value, propname, &local_err); | |
578 | if (local_err) { | |
579 | error_propagate(errp, local_err); | |
580 | return -1; | |
581 | } | |
582 | propname = va_arg(vargs, char *); | |
583 | } | |
584 | ||
585 | return 0; | |
586 | } | |
587 | ||
588 | ||
2f28d2ff AL |
589 | Object *object_dynamic_cast(Object *obj, const char *typename) |
590 | { | |
b7f43fe4 | 591 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
acc4af3f PB |
592 | return obj; |
593 | } | |
594 | ||
2f28d2ff AL |
595 | return NULL; |
596 | } | |
597 | ||
be17f18b PB |
598 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
599 | const char *file, int line, const char *func) | |
2f28d2ff | 600 | { |
fa131d94 PB |
601 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
602 | typename, file, line, func); | |
603 | ||
3556c233 | 604 | #ifdef CONFIG_QOM_CAST_DEBUG |
03587328 AL |
605 | int i; |
606 | Object *inst; | |
607 | ||
95916abc | 608 | for (i = 0; obj && i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c | 609 | if (obj->class->object_cast_cache[i] == typename) { |
03587328 AL |
610 | goto out; |
611 | } | |
612 | } | |
613 | ||
614 | inst = object_dynamic_cast(obj, typename); | |
2f28d2ff | 615 | |
b7f43fe4 | 616 | if (!inst && obj) { |
be17f18b PB |
617 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
618 | file, line, func, obj, typename); | |
2f28d2ff AL |
619 | abort(); |
620 | } | |
621 | ||
3556c233 | 622 | assert(obj == inst); |
03587328 | 623 | |
95916abc | 624 | if (obj && obj == inst) { |
03587328 | 625 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c PC |
626 | obj->class->object_cast_cache[i - 1] = |
627 | obj->class->object_cast_cache[i]; | |
03587328 | 628 | } |
0ab4c94c | 629 | obj->class->object_cast_cache[i - 1] = typename; |
03587328 AL |
630 | } |
631 | ||
632 | out: | |
3556c233 PB |
633 | #endif |
634 | return obj; | |
2f28d2ff AL |
635 | } |
636 | ||
637 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
638 | const char *typename) | |
639 | { | |
33e95c63 | 640 | ObjectClass *ret = NULL; |
bf0fda34 PB |
641 | TypeImpl *target_type; |
642 | TypeImpl *type; | |
2f28d2ff | 643 | |
bf0fda34 PB |
644 | if (!class) { |
645 | return NULL; | |
646 | } | |
647 | ||
793c96b5 | 648 | /* A simple fast path that can trigger a lot for leaf classes. */ |
bf0fda34 | 649 | type = class->type; |
793c96b5 PB |
650 | if (type->name == typename) { |
651 | return class; | |
652 | } | |
653 | ||
bf0fda34 | 654 | target_type = type_get_by_name(typename); |
9ab880b3 AG |
655 | if (!target_type) { |
656 | /* target class type unknown, so fail the cast */ | |
657 | return NULL; | |
658 | } | |
659 | ||
00e2ceae PC |
660 | if (type->class->interfaces && |
661 | type_is_ancestor(target_type, type_interface)) { | |
33e95c63 AL |
662 | int found = 0; |
663 | GSList *i; | |
2f28d2ff | 664 | |
33e95c63 AL |
665 | for (i = class->interfaces; i; i = i->next) { |
666 | ObjectClass *target_class = i->data; | |
667 | ||
668 | if (type_is_ancestor(target_class->type, target_type)) { | |
669 | ret = target_class; | |
670 | found++; | |
671 | } | |
672 | } | |
673 | ||
674 | /* The match was ambiguous, don't allow a cast */ | |
675 | if (found > 1) { | |
676 | ret = NULL; | |
677 | } | |
678 | } else if (type_is_ancestor(type, target_type)) { | |
679 | ret = class; | |
2f28d2ff AL |
680 | } |
681 | ||
33e95c63 | 682 | return ret; |
2f28d2ff AL |
683 | } |
684 | ||
685 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
be17f18b PB |
686 | const char *typename, |
687 | const char *file, int line, | |
688 | const char *func) | |
2f28d2ff | 689 | { |
fa131d94 PB |
690 | ObjectClass *ret; |
691 | ||
692 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", | |
693 | typename, file, line, func); | |
2f28d2ff | 694 | |
03587328 AL |
695 | #ifdef CONFIG_QOM_CAST_DEBUG |
696 | int i; | |
697 | ||
9d6a3d58 | 698 | for (i = 0; class && i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c | 699 | if (class->class_cast_cache[i] == typename) { |
03587328 AL |
700 | ret = class; |
701 | goto out; | |
702 | } | |
703 | } | |
704 | #else | |
9d6a3d58 | 705 | if (!class || !class->interfaces) { |
3556c233 PB |
706 | return class; |
707 | } | |
708 | #endif | |
709 | ||
fa131d94 | 710 | ret = object_class_dynamic_cast(class, typename); |
bf0fda34 | 711 | if (!ret && class) { |
be17f18b PB |
712 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
713 | file, line, func, class, typename); | |
2f28d2ff AL |
714 | abort(); |
715 | } | |
716 | ||
03587328 | 717 | #ifdef CONFIG_QOM_CAST_DEBUG |
9d6a3d58 | 718 | if (class && ret == class) { |
03587328 | 719 | for (i = 1; i < OBJECT_CLASS_CAST_CACHE; i++) { |
0ab4c94c | 720 | class->class_cast_cache[i - 1] = class->class_cast_cache[i]; |
03587328 | 721 | } |
0ab4c94c | 722 | class->class_cast_cache[i - 1] = typename; |
03587328 AL |
723 | } |
724 | out: | |
725 | #endif | |
2f28d2ff AL |
726 | return ret; |
727 | } | |
728 | ||
729 | const char *object_get_typename(Object *obj) | |
730 | { | |
731 | return obj->class->type->name; | |
732 | } | |
733 | ||
734 | ObjectClass *object_get_class(Object *obj) | |
735 | { | |
736 | return obj->class; | |
737 | } | |
738 | ||
17862378 AF |
739 | bool object_class_is_abstract(ObjectClass *klass) |
740 | { | |
741 | return klass->type->abstract; | |
742 | } | |
743 | ||
2f28d2ff AL |
744 | const char *object_class_get_name(ObjectClass *klass) |
745 | { | |
746 | return klass->type->name; | |
747 | } | |
748 | ||
749 | ObjectClass *object_class_by_name(const char *typename) | |
750 | { | |
751 | TypeImpl *type = type_get_by_name(typename); | |
752 | ||
753 | if (!type) { | |
754 | return NULL; | |
755 | } | |
756 | ||
ac451033 | 757 | type_initialize(type); |
2f28d2ff AL |
758 | |
759 | return type->class; | |
760 | } | |
761 | ||
e7cce67f PB |
762 | ObjectClass *object_class_get_parent(ObjectClass *class) |
763 | { | |
764 | TypeImpl *type = type_get_parent(class->type); | |
765 | ||
766 | if (!type) { | |
767 | return NULL; | |
768 | } | |
769 | ||
770 | type_initialize(type); | |
771 | ||
772 | return type->class; | |
773 | } | |
774 | ||
2f28d2ff AL |
775 | typedef struct OCFData |
776 | { | |
777 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
778 | const char *implements_type; |
779 | bool include_abstract; | |
2f28d2ff AL |
780 | void *opaque; |
781 | } OCFData; | |
782 | ||
783 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
784 | gpointer opaque) | |
785 | { | |
786 | OCFData *data = opaque; | |
787 | TypeImpl *type = value; | |
93c511a1 | 788 | ObjectClass *k; |
2f28d2ff | 789 | |
ac451033 | 790 | type_initialize(type); |
93c511a1 | 791 | k = type->class; |
2f28d2ff | 792 | |
93c511a1 AL |
793 | if (!data->include_abstract && type->abstract) { |
794 | return; | |
795 | } | |
796 | ||
797 | if (data->implements_type && | |
798 | !object_class_dynamic_cast(k, data->implements_type)) { | |
799 | return; | |
800 | } | |
801 | ||
802 | data->fn(k, data->opaque); | |
2f28d2ff AL |
803 | } |
804 | ||
805 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 806 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
807 | void *opaque) |
808 | { | |
93c511a1 | 809 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff | 810 | |
f54c19ca | 811 | enumerating_types = true; |
2f28d2ff | 812 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); |
f54c19ca | 813 | enumerating_types = false; |
2f28d2ff | 814 | } |
57c9fafe | 815 | |
d714b8de PC |
816 | static int do_object_child_foreach(Object *obj, |
817 | int (*fn)(Object *child, void *opaque), | |
818 | void *opaque, bool recurse) | |
32efc535 | 819 | { |
b604a854 PF |
820 | GHashTableIter iter; |
821 | ObjectProperty *prop; | |
32efc535 PB |
822 | int ret = 0; |
823 | ||
b604a854 PF |
824 | g_hash_table_iter_init(&iter, obj->properties); |
825 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
32efc535 | 826 | if (object_property_is_child(prop)) { |
d714b8de PC |
827 | Object *child = prop->opaque; |
828 | ||
829 | ret = fn(child, opaque); | |
32efc535 PB |
830 | if (ret != 0) { |
831 | break; | |
832 | } | |
d714b8de PC |
833 | if (recurse) { |
834 | do_object_child_foreach(child, fn, opaque, true); | |
835 | } | |
32efc535 PB |
836 | } |
837 | } | |
838 | return ret; | |
839 | } | |
840 | ||
d714b8de PC |
841 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
842 | void *opaque) | |
843 | { | |
844 | return do_object_child_foreach(obj, fn, opaque, false); | |
845 | } | |
846 | ||
847 | int object_child_foreach_recursive(Object *obj, | |
848 | int (*fn)(Object *child, void *opaque), | |
849 | void *opaque) | |
850 | { | |
851 | return do_object_child_foreach(obj, fn, opaque, true); | |
852 | } | |
853 | ||
418ba9e5 AF |
854 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
855 | { | |
856 | GSList **list = opaque; | |
857 | ||
858 | *list = g_slist_prepend(*list, klass); | |
859 | } | |
860 | ||
861 | GSList *object_class_get_list(const char *implements_type, | |
862 | bool include_abstract) | |
863 | { | |
864 | GSList *list = NULL; | |
865 | ||
866 | object_class_foreach(object_class_get_list_tramp, | |
867 | implements_type, include_abstract, &list); | |
868 | return list; | |
869 | } | |
870 | ||
57c9fafe AL |
871 | void object_ref(Object *obj) |
872 | { | |
8ffad850 PC |
873 | if (!obj) { |
874 | return; | |
875 | } | |
8438a135 | 876 | atomic_inc(&obj->ref); |
57c9fafe AL |
877 | } |
878 | ||
879 | void object_unref(Object *obj) | |
880 | { | |
8ffad850 PC |
881 | if (!obj) { |
882 | return; | |
883 | } | |
8438a135 | 884 | g_assert_cmpint(obj->ref, >, 0); |
57c9fafe AL |
885 | |
886 | /* parent always holds a reference to its children */ | |
f08c03f3 | 887 | if (atomic_fetch_dec(&obj->ref) == 1) { |
57c9fafe AL |
888 | object_finalize(obj); |
889 | } | |
890 | } | |
891 | ||
64607d08 PB |
892 | ObjectProperty * |
893 | object_property_add(Object *obj, const char *name, const char *type, | |
894 | ObjectPropertyAccessor *get, | |
895 | ObjectPropertyAccessor *set, | |
896 | ObjectPropertyRelease *release, | |
897 | void *opaque, Error **errp) | |
57c9fafe | 898 | { |
54852b03 | 899 | ObjectProperty *prop; |
33965904 PC |
900 | size_t name_len = strlen(name); |
901 | ||
902 | if (name_len >= 3 && !memcmp(name + name_len - 3, "[*]", 4)) { | |
903 | int i; | |
904 | ObjectProperty *ret; | |
905 | char *name_no_array = g_strdup(name); | |
906 | ||
907 | name_no_array[name_len - 3] = '\0'; | |
908 | for (i = 0; ; ++i) { | |
909 | char *full_name = g_strdup_printf("%s[%d]", name_no_array, i); | |
910 | ||
911 | ret = object_property_add(obj, full_name, type, get, set, | |
912 | release, opaque, NULL); | |
913 | g_free(full_name); | |
914 | if (ret) { | |
915 | break; | |
916 | } | |
917 | } | |
918 | g_free(name_no_array); | |
919 | return ret; | |
920 | } | |
54852b03 | 921 | |
16bf7f52 | 922 | if (object_property_find(obj, name, NULL) != NULL) { |
b604a854 | 923 | error_setg(errp, "attempt to add duplicate property '%s'" |
16bf7f52 DB |
924 | " to object (type '%s')", name, |
925 | object_get_typename(obj)); | |
b604a854 | 926 | return NULL; |
54852b03 PM |
927 | } |
928 | ||
929 | prop = g_malloc0(sizeof(*prop)); | |
57c9fafe AL |
930 | |
931 | prop->name = g_strdup(name); | |
932 | prop->type = g_strdup(type); | |
933 | ||
934 | prop->get = get; | |
935 | prop->set = set; | |
936 | prop->release = release; | |
937 | prop->opaque = opaque; | |
938 | ||
b604a854 | 939 | g_hash_table_insert(obj->properties, prop->name, prop); |
64607d08 | 940 | return prop; |
57c9fafe AL |
941 | } |
942 | ||
16bf7f52 DB |
943 | ObjectProperty * |
944 | object_class_property_add(ObjectClass *klass, | |
945 | const char *name, | |
946 | const char *type, | |
947 | ObjectPropertyAccessor *get, | |
948 | ObjectPropertyAccessor *set, | |
949 | ObjectPropertyRelease *release, | |
950 | void *opaque, | |
951 | Error **errp) | |
952 | { | |
953 | ObjectProperty *prop; | |
954 | ||
955 | if (object_class_property_find(klass, name, NULL) != NULL) { | |
956 | error_setg(errp, "attempt to add duplicate property '%s'" | |
957 | " to object (type '%s')", name, | |
958 | object_class_get_name(klass)); | |
959 | return NULL; | |
960 | } | |
961 | ||
962 | prop = g_malloc0(sizeof(*prop)); | |
963 | ||
964 | prop->name = g_strdup(name); | |
965 | prop->type = g_strdup(type); | |
966 | ||
967 | prop->get = get; | |
968 | prop->set = set; | |
969 | prop->release = release; | |
970 | prop->opaque = opaque; | |
971 | ||
972 | g_hash_table_insert(klass->properties, g_strdup(name), prop); | |
973 | ||
974 | return prop; | |
975 | } | |
976 | ||
89bfe000 PB |
977 | ObjectProperty *object_property_find(Object *obj, const char *name, |
978 | Error **errp) | |
57c9fafe AL |
979 | { |
980 | ObjectProperty *prop; | |
16bf7f52 DB |
981 | ObjectClass *klass = object_get_class(obj); |
982 | ||
983 | prop = object_class_property_find(klass, name, NULL); | |
984 | if (prop) { | |
985 | return prop; | |
986 | } | |
57c9fafe | 987 | |
b604a854 PF |
988 | prop = g_hash_table_lookup(obj->properties, name); |
989 | if (prop) { | |
990 | return prop; | |
57c9fafe AL |
991 | } |
992 | ||
f231b88d | 993 | error_setg(errp, "Property '.%s' not found", name); |
57c9fafe AL |
994 | return NULL; |
995 | } | |
996 | ||
7746abd8 DB |
997 | void object_property_iter_init(ObjectPropertyIterator *iter, |
998 | Object *obj) | |
a00c9482 | 999 | { |
7746abd8 DB |
1000 | g_hash_table_iter_init(&iter->iter, obj->properties); |
1001 | iter->nextclass = object_get_class(obj); | |
a00c9482 DB |
1002 | } |
1003 | ||
1004 | ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter) | |
1005 | { | |
b604a854 | 1006 | gpointer key, val; |
16bf7f52 DB |
1007 | while (!g_hash_table_iter_next(&iter->iter, &key, &val)) { |
1008 | if (!iter->nextclass) { | |
1009 | return NULL; | |
1010 | } | |
1011 | g_hash_table_iter_init(&iter->iter, iter->nextclass->properties); | |
1012 | iter->nextclass = object_class_get_parent(iter->nextclass); | |
a00c9482 | 1013 | } |
b604a854 | 1014 | return val; |
a00c9482 DB |
1015 | } |
1016 | ||
16bf7f52 DB |
1017 | ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name, |
1018 | Error **errp) | |
1019 | { | |
1020 | ObjectProperty *prop; | |
1021 | ObjectClass *parent_klass; | |
1022 | ||
1023 | parent_klass = object_class_get_parent(klass); | |
1024 | if (parent_klass) { | |
1025 | prop = object_class_property_find(parent_klass, name, NULL); | |
1026 | if (prop) { | |
1027 | return prop; | |
1028 | } | |
1029 | } | |
1030 | ||
1031 | prop = g_hash_table_lookup(klass->properties, name); | |
1032 | if (!prop) { | |
1033 | error_setg(errp, "Property '.%s' not found", name); | |
1034 | } | |
1035 | return prop; | |
1036 | } | |
1037 | ||
57c9fafe AL |
1038 | void object_property_del(Object *obj, const char *name, Error **errp) |
1039 | { | |
b604a854 PF |
1040 | ObjectProperty *prop = g_hash_table_lookup(obj->properties, name); |
1041 | ||
1042 | if (!prop) { | |
1043 | error_setg(errp, "Property '.%s' not found", name); | |
0866aca1 AL |
1044 | return; |
1045 | } | |
57c9fafe | 1046 | |
0866aca1 AL |
1047 | if (prop->release) { |
1048 | prop->release(obj, name, prop->opaque); | |
1049 | } | |
b604a854 | 1050 | g_hash_table_remove(obj->properties, name); |
57c9fafe AL |
1051 | } |
1052 | ||
1053 | void object_property_get(Object *obj, Visitor *v, const char *name, | |
1054 | Error **errp) | |
1055 | { | |
89bfe000 | 1056 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1057 | if (prop == NULL) { |
57c9fafe AL |
1058 | return; |
1059 | } | |
1060 | ||
1061 | if (!prop->get) { | |
c6bd8c70 | 1062 | error_setg(errp, QERR_PERMISSION_DENIED); |
57c9fafe AL |
1063 | } else { |
1064 | prop->get(obj, v, prop->opaque, name, errp); | |
1065 | } | |
1066 | } | |
1067 | ||
1068 | void object_property_set(Object *obj, Visitor *v, const char *name, | |
1069 | Error **errp) | |
1070 | { | |
89bfe000 | 1071 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1072 | if (prop == NULL) { |
57c9fafe AL |
1073 | return; |
1074 | } | |
1075 | ||
1076 | if (!prop->set) { | |
c6bd8c70 | 1077 | error_setg(errp, QERR_PERMISSION_DENIED); |
57c9fafe AL |
1078 | } else { |
1079 | prop->set(obj, v, prop->opaque, name, errp); | |
1080 | } | |
1081 | } | |
1082 | ||
7b7b7d18 PB |
1083 | void object_property_set_str(Object *obj, const char *value, |
1084 | const char *name, Error **errp) | |
1085 | { | |
1086 | QString *qstr = qstring_from_str(value); | |
1087 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); | |
1088 | ||
1089 | QDECREF(qstr); | |
1090 | } | |
1091 | ||
1092 | char *object_property_get_str(Object *obj, const char *name, | |
1093 | Error **errp) | |
1094 | { | |
1095 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1096 | QString *qstring; | |
1097 | char *retval; | |
1098 | ||
1099 | if (!ret) { | |
1100 | return NULL; | |
1101 | } | |
1102 | qstring = qobject_to_qstring(ret); | |
1103 | if (!qstring) { | |
c6bd8c70 | 1104 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); |
7b7b7d18 PB |
1105 | retval = NULL; |
1106 | } else { | |
1107 | retval = g_strdup(qstring_get_str(qstring)); | |
1108 | } | |
1109 | ||
1110 | QDECREF(qstring); | |
1111 | return retval; | |
1112 | } | |
1113 | ||
1d9c5a12 PB |
1114 | void object_property_set_link(Object *obj, Object *value, |
1115 | const char *name, Error **errp) | |
1116 | { | |
d3c49316 PC |
1117 | if (value) { |
1118 | gchar *path = object_get_canonical_path(value); | |
1119 | object_property_set_str(obj, path, name, errp); | |
1120 | g_free(path); | |
1121 | } else { | |
1122 | object_property_set_str(obj, "", name, errp); | |
1123 | } | |
1d9c5a12 PB |
1124 | } |
1125 | ||
1126 | Object *object_property_get_link(Object *obj, const char *name, | |
1127 | Error **errp) | |
1128 | { | |
1129 | char *str = object_property_get_str(obj, name, errp); | |
1130 | Object *target = NULL; | |
1131 | ||
1132 | if (str && *str) { | |
1133 | target = object_resolve_path(str, NULL); | |
1134 | if (!target) { | |
75158ebb MA |
1135 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1136 | "Device '%s' not found", str); | |
1d9c5a12 PB |
1137 | } |
1138 | } | |
1139 | ||
1140 | g_free(str); | |
1141 | return target; | |
1142 | } | |
1143 | ||
7b7b7d18 PB |
1144 | void object_property_set_bool(Object *obj, bool value, |
1145 | const char *name, Error **errp) | |
1146 | { | |
fc48ffc3 | 1147 | QBool *qbool = qbool_from_bool(value); |
7b7b7d18 PB |
1148 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); |
1149 | ||
1150 | QDECREF(qbool); | |
1151 | } | |
1152 | ||
1153 | bool object_property_get_bool(Object *obj, const char *name, | |
1154 | Error **errp) | |
1155 | { | |
1156 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1157 | QBool *qbool; | |
1158 | bool retval; | |
1159 | ||
1160 | if (!ret) { | |
1161 | return false; | |
1162 | } | |
1163 | qbool = qobject_to_qbool(ret); | |
1164 | if (!qbool) { | |
c6bd8c70 | 1165 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); |
7b7b7d18 PB |
1166 | retval = false; |
1167 | } else { | |
fc48ffc3 | 1168 | retval = qbool_get_bool(qbool); |
7b7b7d18 PB |
1169 | } |
1170 | ||
1171 | QDECREF(qbool); | |
1172 | return retval; | |
1173 | } | |
1174 | ||
1175 | void object_property_set_int(Object *obj, int64_t value, | |
1176 | const char *name, Error **errp) | |
1177 | { | |
1178 | QInt *qint = qint_from_int(value); | |
1179 | object_property_set_qobject(obj, QOBJECT(qint), name, errp); | |
1180 | ||
1181 | QDECREF(qint); | |
1182 | } | |
1183 | ||
1184 | int64_t object_property_get_int(Object *obj, const char *name, | |
1185 | Error **errp) | |
1186 | { | |
1187 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
1188 | QInt *qint; | |
1189 | int64_t retval; | |
1190 | ||
1191 | if (!ret) { | |
1192 | return -1; | |
1193 | } | |
1194 | qint = qobject_to_qint(ret); | |
1195 | if (!qint) { | |
c6bd8c70 | 1196 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); |
7b7b7d18 PB |
1197 | retval = -1; |
1198 | } else { | |
1199 | retval = qint_get_int(qint); | |
1200 | } | |
1201 | ||
1202 | QDECREF(qint); | |
1203 | return retval; | |
b2cd7dee PB |
1204 | } |
1205 | ||
a8e3fbed DB |
1206 | typedef struct EnumProperty { |
1207 | const char * const *strings; | |
1208 | int (*get)(Object *, Error **); | |
1209 | void (*set)(Object *, int, Error **); | |
1210 | } EnumProperty; | |
1211 | ||
1f21772d | 1212 | int object_property_get_enum(Object *obj, const char *name, |
a3590dac | 1213 | const char *typename, Error **errp) |
1f21772d | 1214 | { |
4715d42e | 1215 | Error *err = NULL; |
1f21772d HT |
1216 | StringOutputVisitor *sov; |
1217 | StringInputVisitor *siv; | |
976620ac | 1218 | char *str; |
1f21772d | 1219 | int ret; |
a3590dac DB |
1220 | ObjectProperty *prop = object_property_find(obj, name, errp); |
1221 | EnumProperty *enumprop; | |
1222 | ||
1223 | if (prop == NULL) { | |
1224 | return 0; | |
1225 | } | |
1226 | ||
1227 | if (!g_str_equal(prop->type, typename)) { | |
1228 | error_setg(errp, "Property %s on %s is not '%s' enum type", | |
1229 | name, object_class_get_name( | |
1230 | object_get_class(obj)), typename); | |
1231 | return 0; | |
1232 | } | |
1233 | ||
1234 | enumprop = prop->opaque; | |
1f21772d HT |
1235 | |
1236 | sov = string_output_visitor_new(false); | |
4715d42e MA |
1237 | object_property_get(obj, string_output_get_visitor(sov), name, &err); |
1238 | if (err) { | |
1239 | error_propagate(errp, err); | |
1240 | string_output_visitor_cleanup(sov); | |
1241 | return 0; | |
1242 | } | |
976620ac CF |
1243 | str = string_output_get_string(sov); |
1244 | siv = string_input_visitor_new(str); | |
1f21772d HT |
1245 | string_output_visitor_cleanup(sov); |
1246 | visit_type_enum(string_input_get_visitor(siv), | |
a3590dac | 1247 | &ret, enumprop->strings, NULL, name, errp); |
976620ac CF |
1248 | |
1249 | g_free(str); | |
1f21772d HT |
1250 | string_input_visitor_cleanup(siv); |
1251 | ||
1252 | return ret; | |
1253 | } | |
1254 | ||
1255 | void object_property_get_uint16List(Object *obj, const char *name, | |
1256 | uint16List **list, Error **errp) | |
1257 | { | |
4715d42e | 1258 | Error *err = NULL; |
1f21772d HT |
1259 | StringOutputVisitor *ov; |
1260 | StringInputVisitor *iv; | |
976620ac | 1261 | char *str; |
1f21772d HT |
1262 | |
1263 | ov = string_output_visitor_new(false); | |
1264 | object_property_get(obj, string_output_get_visitor(ov), | |
4715d42e MA |
1265 | name, &err); |
1266 | if (err) { | |
1267 | error_propagate(errp, err); | |
1268 | goto out; | |
1269 | } | |
976620ac CF |
1270 | str = string_output_get_string(ov); |
1271 | iv = string_input_visitor_new(str); | |
1f21772d HT |
1272 | visit_type_uint16List(string_input_get_visitor(iv), |
1273 | list, NULL, errp); | |
976620ac CF |
1274 | |
1275 | g_free(str); | |
1f21772d | 1276 | string_input_visitor_cleanup(iv); |
4715d42e MA |
1277 | out: |
1278 | string_output_visitor_cleanup(ov); | |
1f21772d HT |
1279 | } |
1280 | ||
b2cd7dee PB |
1281 | void object_property_parse(Object *obj, const char *string, |
1282 | const char *name, Error **errp) | |
1283 | { | |
f8b7f1a8 EB |
1284 | StringInputVisitor *siv; |
1285 | siv = string_input_visitor_new(string); | |
1286 | object_property_set(obj, string_input_get_visitor(siv), name, errp); | |
b2cd7dee | 1287 | |
f8b7f1a8 | 1288 | string_input_visitor_cleanup(siv); |
b2cd7dee PB |
1289 | } |
1290 | ||
0b7593e0 | 1291 | char *object_property_print(Object *obj, const char *name, bool human, |
b2cd7dee PB |
1292 | Error **errp) |
1293 | { | |
f8b7f1a8 | 1294 | StringOutputVisitor *sov; |
3a53009f GA |
1295 | char *string = NULL; |
1296 | Error *local_err = NULL; | |
b2cd7dee | 1297 | |
f8b7f1a8 EB |
1298 | sov = string_output_visitor_new(human); |
1299 | object_property_get(obj, string_output_get_visitor(sov), name, &local_err); | |
3a53009f GA |
1300 | if (local_err) { |
1301 | error_propagate(errp, local_err); | |
1302 | goto out; | |
1303 | } | |
1304 | ||
f8b7f1a8 | 1305 | string = string_output_get_string(sov); |
3a53009f GA |
1306 | |
1307 | out: | |
f8b7f1a8 | 1308 | string_output_visitor_cleanup(sov); |
b2cd7dee | 1309 | return string; |
7b7b7d18 PB |
1310 | } |
1311 | ||
57c9fafe AL |
1312 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
1313 | { | |
89bfe000 | 1314 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 1315 | if (prop == NULL) { |
57c9fafe AL |
1316 | return NULL; |
1317 | } | |
1318 | ||
1319 | return prop->type; | |
1320 | } | |
1321 | ||
1322 | Object *object_get_root(void) | |
1323 | { | |
8b45d447 | 1324 | static Object *root; |
57c9fafe | 1325 | |
8b45d447 AL |
1326 | if (!root) { |
1327 | root = object_new("container"); | |
57c9fafe AL |
1328 | } |
1329 | ||
8b45d447 | 1330 | return root; |
57c9fafe AL |
1331 | } |
1332 | ||
bc2256c4 DB |
1333 | Object *object_get_objects_root(void) |
1334 | { | |
1335 | return container_get(object_get_root(), "/objects"); | |
1336 | } | |
1337 | ||
57c9fafe AL |
1338 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, |
1339 | const char *name, Error **errp) | |
1340 | { | |
1341 | Object *child = opaque; | |
1342 | gchar *path; | |
1343 | ||
1344 | path = object_get_canonical_path(child); | |
1345 | visit_type_str(v, &path, name, errp); | |
1346 | g_free(path); | |
1347 | } | |
1348 | ||
64607d08 PB |
1349 | static Object *object_resolve_child_property(Object *parent, void *opaque, const gchar *part) |
1350 | { | |
1351 | return opaque; | |
1352 | } | |
1353 | ||
db85b575 AL |
1354 | static void object_finalize_child_property(Object *obj, const char *name, |
1355 | void *opaque) | |
1356 | { | |
1357 | Object *child = opaque; | |
1358 | ||
bffc687d PB |
1359 | if (child->class->unparent) { |
1360 | (child->class->unparent)(child); | |
1361 | } | |
1362 | child->parent = NULL; | |
db85b575 AL |
1363 | object_unref(child); |
1364 | } | |
1365 | ||
57c9fafe AL |
1366 | void object_property_add_child(Object *obj, const char *name, |
1367 | Object *child, Error **errp) | |
1368 | { | |
b0ed5e9f | 1369 | Error *local_err = NULL; |
57c9fafe | 1370 | gchar *type; |
64607d08 | 1371 | ObjectProperty *op; |
57c9fafe | 1372 | |
8faa2f85 PC |
1373 | if (child->parent != NULL) { |
1374 | error_setg(errp, "child object is already parented"); | |
1375 | return; | |
1376 | } | |
1377 | ||
57c9fafe AL |
1378 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); |
1379 | ||
64607d08 PB |
1380 | op = object_property_add(obj, name, type, object_get_child_property, NULL, |
1381 | object_finalize_child_property, child, &local_err); | |
b0ed5e9f PB |
1382 | if (local_err) { |
1383 | error_propagate(errp, local_err); | |
1384 | goto out; | |
1385 | } | |
64607d08 PB |
1386 | |
1387 | op->resolve = object_resolve_child_property; | |
57c9fafe | 1388 | object_ref(child); |
57c9fafe AL |
1389 | child->parent = obj; |
1390 | ||
b0ed5e9f | 1391 | out: |
57c9fafe AL |
1392 | g_free(type); |
1393 | } | |
1394 | ||
39f72ef9 SH |
1395 | void object_property_allow_set_link(Object *obj, const char *name, |
1396 | Object *val, Error **errp) | |
1397 | { | |
1398 | /* Allow the link to be set, always */ | |
1399 | } | |
1400 | ||
9561fda8 SH |
1401 | typedef struct { |
1402 | Object **child; | |
39f72ef9 | 1403 | void (*check)(Object *, const char *, Object *, Error **); |
9561fda8 SH |
1404 | ObjectPropertyLinkFlags flags; |
1405 | } LinkProperty; | |
1406 | ||
57c9fafe AL |
1407 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, |
1408 | const char *name, Error **errp) | |
1409 | { | |
9561fda8 SH |
1410 | LinkProperty *lprop = opaque; |
1411 | Object **child = lprop->child; | |
57c9fafe AL |
1412 | gchar *path; |
1413 | ||
1414 | if (*child) { | |
1415 | path = object_get_canonical_path(*child); | |
1416 | visit_type_str(v, &path, name, errp); | |
1417 | g_free(path); | |
1418 | } else { | |
1419 | path = (gchar *)""; | |
1420 | visit_type_str(v, &path, name, errp); | |
1421 | } | |
1422 | } | |
1423 | ||
f5ec6704 SH |
1424 | /* |
1425 | * object_resolve_link: | |
1426 | * | |
1427 | * Lookup an object and ensure its type matches the link property type. This | |
1428 | * is similar to object_resolve_path() except type verification against the | |
1429 | * link property is performed. | |
1430 | * | |
1431 | * Returns: The matched object or NULL on path lookup failures. | |
1432 | */ | |
1433 | static Object *object_resolve_link(Object *obj, const char *name, | |
1434 | const char *path, Error **errp) | |
1435 | { | |
1436 | const char *type; | |
1437 | gchar *target_type; | |
1438 | bool ambiguous = false; | |
1439 | Object *target; | |
1440 | ||
1441 | /* Go from link<FOO> to FOO. */ | |
1442 | type = object_property_get_type(obj, name, NULL); | |
1443 | target_type = g_strndup(&type[5], strlen(type) - 6); | |
1444 | target = object_resolve_path_type(path, target_type, &ambiguous); | |
1445 | ||
1446 | if (ambiguous) { | |
455b0fde EB |
1447 | error_setg(errp, "Path '%s' does not uniquely identify an object", |
1448 | path); | |
f5ec6704 SH |
1449 | } else if (!target) { |
1450 | target = object_resolve_path(path, &ambiguous); | |
1451 | if (target || ambiguous) { | |
c6bd8c70 | 1452 | error_setg(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); |
f5ec6704 | 1453 | } else { |
75158ebb MA |
1454 | error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, |
1455 | "Device '%s' not found", path); | |
f5ec6704 SH |
1456 | } |
1457 | target = NULL; | |
1458 | } | |
1459 | g_free(target_type); | |
1460 | ||
1461 | return target; | |
1462 | } | |
1463 | ||
57c9fafe AL |
1464 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, |
1465 | const char *name, Error **errp) | |
1466 | { | |
c6aed983 | 1467 | Error *local_err = NULL; |
9561fda8 SH |
1468 | LinkProperty *prop = opaque; |
1469 | Object **child = prop->child; | |
c6aed983 SH |
1470 | Object *old_target = *child; |
1471 | Object *new_target = NULL; | |
1472 | char *path = NULL; | |
57c9fafe | 1473 | |
c6aed983 | 1474 | visit_type_str(v, &path, name, &local_err); |
57c9fafe | 1475 | |
c6aed983 SH |
1476 | if (!local_err && strcmp(path, "") != 0) { |
1477 | new_target = object_resolve_link(obj, name, path, &local_err); | |
57c9fafe AL |
1478 | } |
1479 | ||
1480 | g_free(path); | |
c6aed983 SH |
1481 | if (local_err) { |
1482 | error_propagate(errp, local_err); | |
1483 | return; | |
1484 | } | |
f0cdc966 | 1485 | |
39f72ef9 SH |
1486 | prop->check(obj, name, new_target, &local_err); |
1487 | if (local_err) { | |
1488 | error_propagate(errp, local_err); | |
1489 | return; | |
1490 | } | |
1491 | ||
8ffad850 | 1492 | object_ref(new_target); |
c6aed983 | 1493 | *child = new_target; |
8ffad850 | 1494 | object_unref(old_target); |
57c9fafe AL |
1495 | } |
1496 | ||
64607d08 PB |
1497 | static Object *object_resolve_link_property(Object *parent, void *opaque, const gchar *part) |
1498 | { | |
1499 | LinkProperty *lprop = opaque; | |
1500 | ||
1501 | return *lprop->child; | |
1502 | } | |
1503 | ||
9561fda8 SH |
1504 | static void object_release_link_property(Object *obj, const char *name, |
1505 | void *opaque) | |
1506 | { | |
1507 | LinkProperty *prop = opaque; | |
1508 | ||
1509 | if ((prop->flags & OBJ_PROP_LINK_UNREF_ON_RELEASE) && *prop->child) { | |
1510 | object_unref(*prop->child); | |
1511 | } | |
1512 | g_free(prop); | |
1513 | } | |
1514 | ||
57c9fafe AL |
1515 | void object_property_add_link(Object *obj, const char *name, |
1516 | const char *type, Object **child, | |
39f72ef9 SH |
1517 | void (*check)(Object *, const char *, |
1518 | Object *, Error **), | |
9561fda8 | 1519 | ObjectPropertyLinkFlags flags, |
57c9fafe AL |
1520 | Error **errp) |
1521 | { | |
9561fda8 SH |
1522 | Error *local_err = NULL; |
1523 | LinkProperty *prop = g_malloc(sizeof(*prop)); | |
57c9fafe | 1524 | gchar *full_type; |
64607d08 | 1525 | ObjectProperty *op; |
57c9fafe | 1526 | |
9561fda8 | 1527 | prop->child = child; |
39f72ef9 | 1528 | prop->check = check; |
9561fda8 SH |
1529 | prop->flags = flags; |
1530 | ||
57c9fafe AL |
1531 | full_type = g_strdup_printf("link<%s>", type); |
1532 | ||
64607d08 PB |
1533 | op = object_property_add(obj, name, full_type, |
1534 | object_get_link_property, | |
1535 | check ? object_set_link_property : NULL, | |
1536 | object_release_link_property, | |
1537 | prop, | |
1538 | &local_err); | |
9561fda8 SH |
1539 | if (local_err) { |
1540 | error_propagate(errp, local_err); | |
1541 | g_free(prop); | |
64607d08 | 1542 | goto out; |
9561fda8 | 1543 | } |
57c9fafe | 1544 | |
64607d08 PB |
1545 | op->resolve = object_resolve_link_property; |
1546 | ||
1547 | out: | |
57c9fafe AL |
1548 | g_free(full_type); |
1549 | } | |
1550 | ||
fb9e7e33 PB |
1551 | void object_property_add_const_link(Object *obj, const char *name, |
1552 | Object *target, Error **errp) | |
1553 | { | |
1554 | char *link_type; | |
1555 | ObjectProperty *op; | |
1556 | ||
1557 | link_type = g_strdup_printf("link<%s>", object_get_typename(target)); | |
1558 | op = object_property_add(obj, name, link_type, | |
1559 | object_get_child_property, NULL, | |
1560 | NULL, target, errp); | |
1561 | if (op != NULL) { | |
1562 | op->resolve = object_resolve_child_property; | |
1563 | } | |
1564 | g_free(link_type); | |
1565 | } | |
1566 | ||
11f590b1 SH |
1567 | gchar *object_get_canonical_path_component(Object *obj) |
1568 | { | |
1569 | ObjectProperty *prop = NULL; | |
b604a854 | 1570 | GHashTableIter iter; |
11f590b1 SH |
1571 | |
1572 | g_assert(obj); | |
1573 | g_assert(obj->parent != NULL); | |
1574 | ||
b604a854 PF |
1575 | g_hash_table_iter_init(&iter, obj->parent->properties); |
1576 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
11f590b1 SH |
1577 | if (!object_property_is_child(prop)) { |
1578 | continue; | |
1579 | } | |
1580 | ||
1581 | if (prop->opaque == obj) { | |
1582 | return g_strdup(prop->name); | |
1583 | } | |
1584 | } | |
1585 | ||
1586 | /* obj had a parent but was not a child, should never happen */ | |
1587 | g_assert_not_reached(); | |
1588 | return NULL; | |
1589 | } | |
1590 | ||
57c9fafe AL |
1591 | gchar *object_get_canonical_path(Object *obj) |
1592 | { | |
1593 | Object *root = object_get_root(); | |
11f590b1 | 1594 | char *newpath, *path = NULL; |
57c9fafe AL |
1595 | |
1596 | while (obj != root) { | |
11f590b1 | 1597 | char *component = object_get_canonical_path_component(obj); |
57c9fafe | 1598 | |
11f590b1 SH |
1599 | if (path) { |
1600 | newpath = g_strdup_printf("%s/%s", component, path); | |
1601 | g_free(component); | |
1602 | g_free(path); | |
1603 | path = newpath; | |
1604 | } else { | |
1605 | path = component; | |
57c9fafe AL |
1606 | } |
1607 | ||
57c9fafe AL |
1608 | obj = obj->parent; |
1609 | } | |
1610 | ||
11f590b1 | 1611 | newpath = g_strdup_printf("/%s", path ? path : ""); |
57c9fafe AL |
1612 | g_free(path); |
1613 | ||
1614 | return newpath; | |
1615 | } | |
1616 | ||
3e84b483 | 1617 | Object *object_resolve_path_component(Object *parent, const gchar *part) |
a612b2a6 | 1618 | { |
89bfe000 | 1619 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
a612b2a6 PB |
1620 | if (prop == NULL) { |
1621 | return NULL; | |
1622 | } | |
1623 | ||
64607d08 PB |
1624 | if (prop->resolve) { |
1625 | return prop->resolve(parent, prop->opaque, part); | |
a612b2a6 PB |
1626 | } else { |
1627 | return NULL; | |
1628 | } | |
1629 | } | |
1630 | ||
57c9fafe AL |
1631 | static Object *object_resolve_abs_path(Object *parent, |
1632 | gchar **parts, | |
02fe2db6 | 1633 | const char *typename, |
57c9fafe AL |
1634 | int index) |
1635 | { | |
57c9fafe AL |
1636 | Object *child; |
1637 | ||
1638 | if (parts[index] == NULL) { | |
02fe2db6 | 1639 | return object_dynamic_cast(parent, typename); |
57c9fafe AL |
1640 | } |
1641 | ||
1642 | if (strcmp(parts[index], "") == 0) { | |
02fe2db6 | 1643 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
57c9fafe AL |
1644 | } |
1645 | ||
a612b2a6 | 1646 | child = object_resolve_path_component(parent, parts[index]); |
57c9fafe AL |
1647 | if (!child) { |
1648 | return NULL; | |
1649 | } | |
1650 | ||
02fe2db6 | 1651 | return object_resolve_abs_path(child, parts, typename, index + 1); |
57c9fafe AL |
1652 | } |
1653 | ||
1654 | static Object *object_resolve_partial_path(Object *parent, | |
1655 | gchar **parts, | |
02fe2db6 | 1656 | const char *typename, |
57c9fafe AL |
1657 | bool *ambiguous) |
1658 | { | |
1659 | Object *obj; | |
b604a854 | 1660 | GHashTableIter iter; |
57c9fafe AL |
1661 | ObjectProperty *prop; |
1662 | ||
02fe2db6 | 1663 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
57c9fafe | 1664 | |
b604a854 PF |
1665 | g_hash_table_iter_init(&iter, parent->properties); |
1666 | while (g_hash_table_iter_next(&iter, NULL, (gpointer *)&prop)) { | |
57c9fafe AL |
1667 | Object *found; |
1668 | ||
5d9d3f47 | 1669 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
1670 | continue; |
1671 | } | |
1672 | ||
02fe2db6 PB |
1673 | found = object_resolve_partial_path(prop->opaque, parts, |
1674 | typename, ambiguous); | |
57c9fafe AL |
1675 | if (found) { |
1676 | if (obj) { | |
1677 | if (ambiguous) { | |
1678 | *ambiguous = true; | |
1679 | } | |
1680 | return NULL; | |
1681 | } | |
1682 | obj = found; | |
1683 | } | |
1684 | ||
1685 | if (ambiguous && *ambiguous) { | |
1686 | return NULL; | |
1687 | } | |
1688 | } | |
1689 | ||
1690 | return obj; | |
1691 | } | |
1692 | ||
02fe2db6 PB |
1693 | Object *object_resolve_path_type(const char *path, const char *typename, |
1694 | bool *ambiguous) | |
57c9fafe | 1695 | { |
57c9fafe AL |
1696 | Object *obj; |
1697 | gchar **parts; | |
1698 | ||
1699 | parts = g_strsplit(path, "/", 0); | |
2e1103f6 | 1700 | assert(parts); |
57c9fafe | 1701 | |
2e1103f6 | 1702 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
57c9fafe AL |
1703 | if (ambiguous) { |
1704 | *ambiguous = false; | |
1705 | } | |
02fe2db6 PB |
1706 | obj = object_resolve_partial_path(object_get_root(), parts, |
1707 | typename, ambiguous); | |
57c9fafe | 1708 | } else { |
02fe2db6 | 1709 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
57c9fafe AL |
1710 | } |
1711 | ||
1712 | g_strfreev(parts); | |
1713 | ||
1714 | return obj; | |
1715 | } | |
1716 | ||
02fe2db6 PB |
1717 | Object *object_resolve_path(const char *path, bool *ambiguous) |
1718 | { | |
1719 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); | |
1720 | } | |
1721 | ||
57c9fafe AL |
1722 | typedef struct StringProperty |
1723 | { | |
1724 | char *(*get)(Object *, Error **); | |
1725 | void (*set)(Object *, const char *, Error **); | |
1726 | } StringProperty; | |
1727 | ||
7b7b7d18 PB |
1728 | static void property_get_str(Object *obj, Visitor *v, void *opaque, |
1729 | const char *name, Error **errp) | |
57c9fafe AL |
1730 | { |
1731 | StringProperty *prop = opaque; | |
1732 | char *value; | |
e1c8237d | 1733 | Error *err = NULL; |
57c9fafe | 1734 | |
e1c8237d MA |
1735 | value = prop->get(obj, &err); |
1736 | if (err) { | |
1737 | error_propagate(errp, err); | |
1738 | return; | |
57c9fafe | 1739 | } |
e1c8237d MA |
1740 | |
1741 | visit_type_str(v, &value, name, errp); | |
1742 | g_free(value); | |
57c9fafe AL |
1743 | } |
1744 | ||
7b7b7d18 PB |
1745 | static void property_set_str(Object *obj, Visitor *v, void *opaque, |
1746 | const char *name, Error **errp) | |
57c9fafe AL |
1747 | { |
1748 | StringProperty *prop = opaque; | |
1749 | char *value; | |
1750 | Error *local_err = NULL; | |
1751 | ||
1752 | visit_type_str(v, &value, name, &local_err); | |
1753 | if (local_err) { | |
1754 | error_propagate(errp, local_err); | |
1755 | return; | |
1756 | } | |
1757 | ||
1758 | prop->set(obj, value, errp); | |
1759 | g_free(value); | |
1760 | } | |
1761 | ||
7b7b7d18 PB |
1762 | static void property_release_str(Object *obj, const char *name, |
1763 | void *opaque) | |
57c9fafe AL |
1764 | { |
1765 | StringProperty *prop = opaque; | |
1766 | g_free(prop); | |
1767 | } | |
1768 | ||
1769 | void object_property_add_str(Object *obj, const char *name, | |
1770 | char *(*get)(Object *, Error **), | |
1771 | void (*set)(Object *, const char *, Error **), | |
1772 | Error **errp) | |
1773 | { | |
a01aedc8 | 1774 | Error *local_err = NULL; |
57c9fafe AL |
1775 | StringProperty *prop = g_malloc0(sizeof(*prop)); |
1776 | ||
1777 | prop->get = get; | |
1778 | prop->set = set; | |
1779 | ||
1780 | object_property_add(obj, name, "string", | |
7b7b7d18 PB |
1781 | get ? property_get_str : NULL, |
1782 | set ? property_set_str : NULL, | |
1783 | property_release_str, | |
a01aedc8 SH |
1784 | prop, &local_err); |
1785 | if (local_err) { | |
1786 | error_propagate(errp, local_err); | |
1787 | g_free(prop); | |
1788 | } | |
57c9fafe | 1789 | } |
745549c8 | 1790 | |
16bf7f52 DB |
1791 | void object_class_property_add_str(ObjectClass *klass, const char *name, |
1792 | char *(*get)(Object *, Error **), | |
1793 | void (*set)(Object *, const char *, | |
1794 | Error **), | |
1795 | Error **errp) | |
1796 | { | |
1797 | Error *local_err = NULL; | |
1798 | StringProperty *prop = g_malloc0(sizeof(*prop)); | |
1799 | ||
1800 | prop->get = get; | |
1801 | prop->set = set; | |
1802 | ||
1803 | object_class_property_add(klass, name, "string", | |
1804 | get ? property_get_str : NULL, | |
1805 | set ? property_set_str : NULL, | |
1806 | property_release_str, | |
1807 | prop, &local_err); | |
1808 | if (local_err) { | |
1809 | error_propagate(errp, local_err); | |
1810 | g_free(prop); | |
1811 | } | |
1812 | } | |
1813 | ||
0e558843 AL |
1814 | typedef struct BoolProperty |
1815 | { | |
1816 | bool (*get)(Object *, Error **); | |
1817 | void (*set)(Object *, bool, Error **); | |
1818 | } BoolProperty; | |
1819 | ||
1820 | static void property_get_bool(Object *obj, Visitor *v, void *opaque, | |
1821 | const char *name, Error **errp) | |
1822 | { | |
1823 | BoolProperty *prop = opaque; | |
1824 | bool value; | |
4715d42e MA |
1825 | Error *err = NULL; |
1826 | ||
1827 | value = prop->get(obj, &err); | |
1828 | if (err) { | |
1829 | error_propagate(errp, err); | |
1830 | return; | |
1831 | } | |
0e558843 | 1832 | |
0e558843 AL |
1833 | visit_type_bool(v, &value, name, errp); |
1834 | } | |
1835 | ||
1836 | static void property_set_bool(Object *obj, Visitor *v, void *opaque, | |
1837 | const char *name, Error **errp) | |
1838 | { | |
1839 | BoolProperty *prop = opaque; | |
1840 | bool value; | |
1841 | Error *local_err = NULL; | |
1842 | ||
1843 | visit_type_bool(v, &value, name, &local_err); | |
1844 | if (local_err) { | |
1845 | error_propagate(errp, local_err); | |
1846 | return; | |
1847 | } | |
1848 | ||
1849 | prop->set(obj, value, errp); | |
1850 | } | |
1851 | ||
1852 | static void property_release_bool(Object *obj, const char *name, | |
1853 | void *opaque) | |
1854 | { | |
1855 | BoolProperty *prop = opaque; | |
1856 | g_free(prop); | |
1857 | } | |
1858 | ||
1859 | void object_property_add_bool(Object *obj, const char *name, | |
1860 | bool (*get)(Object *, Error **), | |
1861 | void (*set)(Object *, bool, Error **), | |
1862 | Error **errp) | |
1863 | { | |
a01aedc8 | 1864 | Error *local_err = NULL; |
0e558843 AL |
1865 | BoolProperty *prop = g_malloc0(sizeof(*prop)); |
1866 | ||
1867 | prop->get = get; | |
1868 | prop->set = set; | |
1869 | ||
1870 | object_property_add(obj, name, "bool", | |
1871 | get ? property_get_bool : NULL, | |
1872 | set ? property_set_bool : NULL, | |
1873 | property_release_bool, | |
a01aedc8 SH |
1874 | prop, &local_err); |
1875 | if (local_err) { | |
1876 | error_propagate(errp, local_err); | |
1877 | g_free(prop); | |
1878 | } | |
0e558843 AL |
1879 | } |
1880 | ||
16bf7f52 DB |
1881 | void object_class_property_add_bool(ObjectClass *klass, const char *name, |
1882 | bool (*get)(Object *, Error **), | |
1883 | void (*set)(Object *, bool, Error **), | |
1884 | Error **errp) | |
1885 | { | |
1886 | Error *local_err = NULL; | |
1887 | BoolProperty *prop = g_malloc0(sizeof(*prop)); | |
1888 | ||
1889 | prop->get = get; | |
1890 | prop->set = set; | |
1891 | ||
1892 | object_class_property_add(klass, name, "bool", | |
1893 | get ? property_get_bool : NULL, | |
1894 | set ? property_set_bool : NULL, | |
1895 | property_release_bool, | |
1896 | prop, &local_err); | |
1897 | if (local_err) { | |
1898 | error_propagate(errp, local_err); | |
1899 | g_free(prop); | |
1900 | } | |
1901 | } | |
1902 | ||
a8e3fbed DB |
1903 | static void property_get_enum(Object *obj, Visitor *v, void *opaque, |
1904 | const char *name, Error **errp) | |
1905 | { | |
1906 | EnumProperty *prop = opaque; | |
1907 | int value; | |
4715d42e MA |
1908 | Error *err = NULL; |
1909 | ||
1910 | value = prop->get(obj, &err); | |
1911 | if (err) { | |
1912 | error_propagate(errp, err); | |
1913 | return; | |
1914 | } | |
a8e3fbed | 1915 | |
a8e3fbed DB |
1916 | visit_type_enum(v, &value, prop->strings, NULL, name, errp); |
1917 | } | |
1918 | ||
1919 | static void property_set_enum(Object *obj, Visitor *v, void *opaque, | |
1920 | const char *name, Error **errp) | |
1921 | { | |
1922 | EnumProperty *prop = opaque; | |
1923 | int value; | |
4715d42e | 1924 | Error *err = NULL; |
a8e3fbed | 1925 | |
4715d42e MA |
1926 | visit_type_enum(v, &value, prop->strings, NULL, name, &err); |
1927 | if (err) { | |
1928 | error_propagate(errp, err); | |
1929 | return; | |
1930 | } | |
a8e3fbed DB |
1931 | prop->set(obj, value, errp); |
1932 | } | |
1933 | ||
1934 | static void property_release_enum(Object *obj, const char *name, | |
1935 | void *opaque) | |
1936 | { | |
1937 | EnumProperty *prop = opaque; | |
1938 | g_free(prop); | |
1939 | } | |
1940 | ||
1941 | void object_property_add_enum(Object *obj, const char *name, | |
1942 | const char *typename, | |
1943 | const char * const *strings, | |
1944 | int (*get)(Object *, Error **), | |
1945 | void (*set)(Object *, int, Error **), | |
1946 | Error **errp) | |
1947 | { | |
1948 | Error *local_err = NULL; | |
1949 | EnumProperty *prop = g_malloc(sizeof(*prop)); | |
1950 | ||
1951 | prop->strings = strings; | |
1952 | prop->get = get; | |
1953 | prop->set = set; | |
1954 | ||
1955 | object_property_add(obj, name, typename, | |
1956 | get ? property_get_enum : NULL, | |
1957 | set ? property_set_enum : NULL, | |
1958 | property_release_enum, | |
1959 | prop, &local_err); | |
1960 | if (local_err) { | |
1961 | error_propagate(errp, local_err); | |
1962 | g_free(prop); | |
1963 | } | |
1964 | } | |
1965 | ||
16bf7f52 DB |
1966 | void object_class_property_add_enum(ObjectClass *klass, const char *name, |
1967 | const char *typename, | |
1968 | const char * const *strings, | |
1969 | int (*get)(Object *, Error **), | |
1970 | void (*set)(Object *, int, Error **), | |
1971 | Error **errp) | |
1972 | { | |
1973 | Error *local_err = NULL; | |
1974 | EnumProperty *prop = g_malloc(sizeof(*prop)); | |
1975 | ||
1976 | prop->strings = strings; | |
1977 | prop->get = get; | |
1978 | prop->set = set; | |
1979 | ||
1980 | object_class_property_add(klass, name, typename, | |
1981 | get ? property_get_enum : NULL, | |
1982 | set ? property_set_enum : NULL, | |
1983 | property_release_enum, | |
1984 | prop, &local_err); | |
1985 | if (local_err) { | |
1986 | error_propagate(errp, local_err); | |
1987 | g_free(prop); | |
1988 | } | |
1989 | } | |
1990 | ||
8e099d14 DG |
1991 | typedef struct TMProperty { |
1992 | void (*get)(Object *, struct tm *, Error **); | |
1993 | } TMProperty; | |
1994 | ||
1995 | static void property_get_tm(Object *obj, Visitor *v, void *opaque, | |
1996 | const char *name, Error **errp) | |
1997 | { | |
1998 | TMProperty *prop = opaque; | |
1999 | Error *err = NULL; | |
2000 | struct tm value; | |
2001 | ||
2002 | prop->get(obj, &value, &err); | |
2003 | if (err) { | |
2004 | goto out; | |
2005 | } | |
2006 | ||
2007 | visit_start_struct(v, NULL, "struct tm", name, 0, &err); | |
2008 | if (err) { | |
2009 | goto out; | |
2010 | } | |
2011 | visit_type_int32(v, &value.tm_year, "tm_year", &err); | |
2012 | if (err) { | |
2013 | goto out_end; | |
2014 | } | |
2015 | visit_type_int32(v, &value.tm_mon, "tm_mon", &err); | |
2016 | if (err) { | |
2017 | goto out_end; | |
2018 | } | |
2019 | visit_type_int32(v, &value.tm_mday, "tm_mday", &err); | |
2020 | if (err) { | |
2021 | goto out_end; | |
2022 | } | |
2023 | visit_type_int32(v, &value.tm_hour, "tm_hour", &err); | |
2024 | if (err) { | |
2025 | goto out_end; | |
2026 | } | |
2027 | visit_type_int32(v, &value.tm_min, "tm_min", &err); | |
2028 | if (err) { | |
2029 | goto out_end; | |
2030 | } | |
2031 | visit_type_int32(v, &value.tm_sec, "tm_sec", &err); | |
2032 | if (err) { | |
2033 | goto out_end; | |
2034 | } | |
2035 | out_end: | |
2036 | error_propagate(errp, err); | |
2037 | err = NULL; | |
2038 | visit_end_struct(v, errp); | |
2039 | out: | |
2040 | error_propagate(errp, err); | |
2041 | ||
2042 | } | |
2043 | ||
2044 | static void property_release_tm(Object *obj, const char *name, | |
2045 | void *opaque) | |
2046 | { | |
2047 | TMProperty *prop = opaque; | |
2048 | g_free(prop); | |
2049 | } | |
2050 | ||
2051 | void object_property_add_tm(Object *obj, const char *name, | |
2052 | void (*get)(Object *, struct tm *, Error **), | |
2053 | Error **errp) | |
2054 | { | |
2055 | Error *local_err = NULL; | |
2056 | TMProperty *prop = g_malloc0(sizeof(*prop)); | |
2057 | ||
2058 | prop->get = get; | |
2059 | ||
2060 | object_property_add(obj, name, "struct tm", | |
2061 | get ? property_get_tm : NULL, NULL, | |
2062 | property_release_tm, | |
2063 | prop, &local_err); | |
2064 | if (local_err) { | |
2065 | error_propagate(errp, local_err); | |
2066 | g_free(prop); | |
2067 | } | |
2068 | } | |
2069 | ||
16bf7f52 DB |
2070 | void object_class_property_add_tm(ObjectClass *klass, const char *name, |
2071 | void (*get)(Object *, struct tm *, Error **), | |
2072 | Error **errp) | |
2073 | { | |
2074 | Error *local_err = NULL; | |
2075 | TMProperty *prop = g_malloc0(sizeof(*prop)); | |
2076 | ||
2077 | prop->get = get; | |
2078 | ||
2079 | object_class_property_add(klass, name, "struct tm", | |
2080 | get ? property_get_tm : NULL, NULL, | |
2081 | property_release_tm, | |
2082 | prop, &local_err); | |
2083 | if (local_err) { | |
2084 | error_propagate(errp, local_err); | |
2085 | g_free(prop); | |
2086 | } | |
2087 | } | |
2088 | ||
2f262e06 PB |
2089 | static char *qdev_get_type(Object *obj, Error **errp) |
2090 | { | |
2091 | return g_strdup(object_get_typename(obj)); | |
2092 | } | |
2093 | ||
e732ea63 MT |
2094 | static void property_get_uint8_ptr(Object *obj, Visitor *v, |
2095 | void *opaque, const char *name, | |
2096 | Error **errp) | |
2097 | { | |
2098 | uint8_t value = *(uint8_t *)opaque; | |
2099 | visit_type_uint8(v, &value, name, errp); | |
2100 | } | |
2101 | ||
2102 | static void property_get_uint16_ptr(Object *obj, Visitor *v, | |
2103 | void *opaque, const char *name, | |
2104 | Error **errp) | |
2105 | { | |
2106 | uint16_t value = *(uint16_t *)opaque; | |
2107 | visit_type_uint16(v, &value, name, errp); | |
2108 | } | |
2109 | ||
2110 | static void property_get_uint32_ptr(Object *obj, Visitor *v, | |
2111 | void *opaque, const char *name, | |
2112 | Error **errp) | |
2113 | { | |
2114 | uint32_t value = *(uint32_t *)opaque; | |
2115 | visit_type_uint32(v, &value, name, errp); | |
2116 | } | |
2117 | ||
2118 | static void property_get_uint64_ptr(Object *obj, Visitor *v, | |
2119 | void *opaque, const char *name, | |
2120 | Error **errp) | |
2121 | { | |
2122 | uint64_t value = *(uint64_t *)opaque; | |
2123 | visit_type_uint64(v, &value, name, errp); | |
2124 | } | |
2125 | ||
2126 | void object_property_add_uint8_ptr(Object *obj, const char *name, | |
2127 | const uint8_t *v, Error **errp) | |
2128 | { | |
2129 | object_property_add(obj, name, "uint8", property_get_uint8_ptr, | |
2130 | NULL, NULL, (void *)v, errp); | |
2131 | } | |
2132 | ||
16bf7f52 DB |
2133 | void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name, |
2134 | const uint8_t *v, Error **errp) | |
2135 | { | |
2136 | object_class_property_add(klass, name, "uint8", property_get_uint8_ptr, | |
2137 | NULL, NULL, (void *)v, errp); | |
2138 | } | |
2139 | ||
e732ea63 MT |
2140 | void object_property_add_uint16_ptr(Object *obj, const char *name, |
2141 | const uint16_t *v, Error **errp) | |
2142 | { | |
2143 | object_property_add(obj, name, "uint16", property_get_uint16_ptr, | |
2144 | NULL, NULL, (void *)v, errp); | |
2145 | } | |
2146 | ||
16bf7f52 DB |
2147 | void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name, |
2148 | const uint16_t *v, Error **errp) | |
2149 | { | |
2150 | object_class_property_add(klass, name, "uint16", property_get_uint16_ptr, | |
2151 | NULL, NULL, (void *)v, errp); | |
2152 | } | |
2153 | ||
e732ea63 MT |
2154 | void object_property_add_uint32_ptr(Object *obj, const char *name, |
2155 | const uint32_t *v, Error **errp) | |
2156 | { | |
2157 | object_property_add(obj, name, "uint32", property_get_uint32_ptr, | |
2158 | NULL, NULL, (void *)v, errp); | |
2159 | } | |
2160 | ||
16bf7f52 DB |
2161 | void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name, |
2162 | const uint32_t *v, Error **errp) | |
2163 | { | |
2164 | object_class_property_add(klass, name, "uint32", property_get_uint32_ptr, | |
2165 | NULL, NULL, (void *)v, errp); | |
2166 | } | |
2167 | ||
e732ea63 MT |
2168 | void object_property_add_uint64_ptr(Object *obj, const char *name, |
2169 | const uint64_t *v, Error **errp) | |
2170 | { | |
2171 | object_property_add(obj, name, "uint64", property_get_uint64_ptr, | |
2172 | NULL, NULL, (void *)v, errp); | |
2173 | } | |
2174 | ||
16bf7f52 DB |
2175 | void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name, |
2176 | const uint64_t *v, Error **errp) | |
2177 | { | |
2178 | object_class_property_add(klass, name, "uint64", property_get_uint64_ptr, | |
2179 | NULL, NULL, (void *)v, errp); | |
2180 | } | |
2181 | ||
ef7c7ff6 SH |
2182 | typedef struct { |
2183 | Object *target_obj; | |
1590d266 | 2184 | char *target_name; |
ef7c7ff6 SH |
2185 | } AliasProperty; |
2186 | ||
2187 | static void property_get_alias(Object *obj, struct Visitor *v, void *opaque, | |
2188 | const char *name, Error **errp) | |
2189 | { | |
2190 | AliasProperty *prop = opaque; | |
2191 | ||
2192 | object_property_get(prop->target_obj, v, prop->target_name, errp); | |
2193 | } | |
2194 | ||
2195 | static void property_set_alias(Object *obj, struct Visitor *v, void *opaque, | |
2196 | const char *name, Error **errp) | |
2197 | { | |
2198 | AliasProperty *prop = opaque; | |
2199 | ||
2200 | object_property_set(prop->target_obj, v, prop->target_name, errp); | |
2201 | } | |
2202 | ||
64607d08 PB |
2203 | static Object *property_resolve_alias(Object *obj, void *opaque, |
2204 | const gchar *part) | |
2205 | { | |
2206 | AliasProperty *prop = opaque; | |
2207 | ||
2208 | return object_resolve_path_component(prop->target_obj, prop->target_name); | |
2209 | } | |
2210 | ||
ef7c7ff6 SH |
2211 | static void property_release_alias(Object *obj, const char *name, void *opaque) |
2212 | { | |
2213 | AliasProperty *prop = opaque; | |
2214 | ||
1590d266 | 2215 | g_free(prop->target_name); |
ef7c7ff6 SH |
2216 | g_free(prop); |
2217 | } | |
2218 | ||
2219 | void object_property_add_alias(Object *obj, const char *name, | |
2220 | Object *target_obj, const char *target_name, | |
2221 | Error **errp) | |
2222 | { | |
2223 | AliasProperty *prop; | |
64607d08 | 2224 | ObjectProperty *op; |
ef7c7ff6 | 2225 | ObjectProperty *target_prop; |
d190698e | 2226 | gchar *prop_type; |
8ae9a9ef | 2227 | Error *local_err = NULL; |
ef7c7ff6 SH |
2228 | |
2229 | target_prop = object_property_find(target_obj, target_name, errp); | |
2230 | if (!target_prop) { | |
2231 | return; | |
2232 | } | |
2233 | ||
d190698e PB |
2234 | if (object_property_is_child(target_prop)) { |
2235 | prop_type = g_strdup_printf("link%s", | |
2236 | target_prop->type + strlen("child")); | |
2237 | } else { | |
2238 | prop_type = g_strdup(target_prop->type); | |
2239 | } | |
2240 | ||
ef7c7ff6 SH |
2241 | prop = g_malloc(sizeof(*prop)); |
2242 | prop->target_obj = target_obj; | |
1590d266 | 2243 | prop->target_name = g_strdup(target_name); |
ef7c7ff6 | 2244 | |
d190698e | 2245 | op = object_property_add(obj, name, prop_type, |
64607d08 PB |
2246 | property_get_alias, |
2247 | property_set_alias, | |
2248 | property_release_alias, | |
8ae9a9ef GA |
2249 | prop, &local_err); |
2250 | if (local_err) { | |
2251 | error_propagate(errp, local_err); | |
2252 | g_free(prop); | |
2253 | goto out; | |
2254 | } | |
64607d08 | 2255 | op->resolve = property_resolve_alias; |
d190698e | 2256 | |
a18bb417 | 2257 | object_property_set_description(obj, op->name, |
80742642 GA |
2258 | target_prop->description, |
2259 | &error_abort); | |
2260 | ||
8ae9a9ef | 2261 | out: |
d190698e | 2262 | g_free(prop_type); |
ef7c7ff6 SH |
2263 | } |
2264 | ||
80742642 GA |
2265 | void object_property_set_description(Object *obj, const char *name, |
2266 | const char *description, Error **errp) | |
2267 | { | |
2268 | ObjectProperty *op; | |
2269 | ||
2270 | op = object_property_find(obj, name, errp); | |
2271 | if (!op) { | |
2272 | return; | |
2273 | } | |
2274 | ||
2275 | g_free(op->description); | |
2276 | op->description = g_strdup(description); | |
2277 | } | |
2278 | ||
16bf7f52 DB |
2279 | void object_class_property_set_description(ObjectClass *klass, |
2280 | const char *name, | |
2281 | const char *description, | |
2282 | Error **errp) | |
2283 | { | |
2284 | ObjectProperty *op; | |
2285 | ||
2286 | op = g_hash_table_lookup(klass->properties, name); | |
2287 | if (!op) { | |
2288 | error_setg(errp, "Property '.%s' not found", name); | |
2289 | return; | |
2290 | } | |
2291 | ||
2292 | g_free(op->description); | |
2293 | op->description = g_strdup(description); | |
2294 | } | |
2295 | ||
2f262e06 PB |
2296 | static void object_instance_init(Object *obj) |
2297 | { | |
2298 | object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); | |
2299 | } | |
2300 | ||
745549c8 PB |
2301 | static void register_types(void) |
2302 | { | |
2303 | static TypeInfo interface_info = { | |
2304 | .name = TYPE_INTERFACE, | |
33e95c63 | 2305 | .class_size = sizeof(InterfaceClass), |
745549c8 PB |
2306 | .abstract = true, |
2307 | }; | |
2308 | ||
2309 | static TypeInfo object_info = { | |
2310 | .name = TYPE_OBJECT, | |
2311 | .instance_size = sizeof(Object), | |
2f262e06 | 2312 | .instance_init = object_instance_init, |
745549c8 PB |
2313 | .abstract = true, |
2314 | }; | |
2315 | ||
049cb3cf PB |
2316 | type_interface = type_register_internal(&interface_info); |
2317 | type_register_internal(&object_info); | |
745549c8 PB |
2318 | } |
2319 | ||
2320 | type_init(register_types) |