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