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