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