]>
Commit | Line | Data |
---|---|---|
2f28d2ff AL |
1 | /* |
2 | * QEMU Object Model | |
3 | * | |
4 | * Copyright IBM, Corp. 2011 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. | |
10 | * See the COPYING file in the top-level directory. | |
11 | */ | |
12 | ||
14cccb61 | 13 | #include "qom/object.h" |
2f28d2ff | 14 | #include "qemu-common.h" |
7b1b5d19 | 15 | #include "qapi/visitor.h" |
b2cd7dee PB |
16 | #include "qapi/string-input-visitor.h" |
17 | #include "qapi/string-output-visitor.h" | |
7b1b5d19 | 18 | #include "qapi/qmp/qerror.h" |
fa131d94 | 19 | #include "trace.h" |
2f28d2ff | 20 | |
7b7b7d18 PB |
21 | /* TODO: replace QObject with a simpler visitor to avoid a dependency |
22 | * of the QOM core on QObject? */ | |
14cccb61 | 23 | #include "qom/qom-qobject.h" |
7b1b5d19 PB |
24 | #include "qapi/qmp/qobject.h" |
25 | #include "qapi/qmp/qbool.h" | |
26 | #include "qapi/qmp/qint.h" | |
27 | #include "qapi/qmp/qstring.h" | |
7b7b7d18 | 28 | |
2f28d2ff AL |
29 | #define MAX_INTERFACES 32 |
30 | ||
31 | typedef struct InterfaceImpl InterfaceImpl; | |
32 | typedef struct TypeImpl TypeImpl; | |
33 | ||
34 | struct InterfaceImpl | |
35 | { | |
33e95c63 | 36 | const char *typename; |
2f28d2ff AL |
37 | }; |
38 | ||
39 | struct TypeImpl | |
40 | { | |
41 | const char *name; | |
42 | ||
43 | size_t class_size; | |
44 | ||
45 | size_t instance_size; | |
46 | ||
47 | void (*class_init)(ObjectClass *klass, void *data); | |
3b50e311 | 48 | void (*class_base_init)(ObjectClass *klass, void *data); |
2f28d2ff AL |
49 | void (*class_finalize)(ObjectClass *klass, void *data); |
50 | ||
51 | void *class_data; | |
52 | ||
53 | void (*instance_init)(Object *obj); | |
54 | void (*instance_finalize)(Object *obj); | |
55 | ||
56 | bool abstract; | |
57 | ||
58 | const char *parent; | |
59 | TypeImpl *parent_type; | |
60 | ||
61 | ObjectClass *class; | |
62 | ||
63 | int num_interfaces; | |
64 | InterfaceImpl interfaces[MAX_INTERFACES]; | |
65 | }; | |
66 | ||
9970bd88 PB |
67 | static Type type_interface; |
68 | ||
2f28d2ff AL |
69 | static GHashTable *type_table_get(void) |
70 | { | |
71 | static GHashTable *type_table; | |
72 | ||
73 | if (type_table == NULL) { | |
74 | type_table = g_hash_table_new(g_str_hash, g_str_equal); | |
75 | } | |
76 | ||
77 | return type_table; | |
78 | } | |
79 | ||
80 | static void type_table_add(TypeImpl *ti) | |
81 | { | |
82 | g_hash_table_insert(type_table_get(), (void *)ti->name, ti); | |
83 | } | |
84 | ||
85 | static TypeImpl *type_table_lookup(const char *name) | |
86 | { | |
87 | return g_hash_table_lookup(type_table_get(), name); | |
88 | } | |
89 | ||
049cb3cf | 90 | static TypeImpl *type_register_internal(const TypeInfo *info) |
2f28d2ff AL |
91 | { |
92 | TypeImpl *ti = g_malloc0(sizeof(*ti)); | |
33e95c63 | 93 | int i; |
2f28d2ff AL |
94 | |
95 | g_assert(info->name != NULL); | |
96 | ||
73093354 AL |
97 | if (type_table_lookup(info->name) != NULL) { |
98 | fprintf(stderr, "Registering `%s' which already exists\n", info->name); | |
99 | abort(); | |
100 | } | |
101 | ||
2f28d2ff AL |
102 | ti->name = g_strdup(info->name); |
103 | ti->parent = g_strdup(info->parent); | |
104 | ||
105 | ti->class_size = info->class_size; | |
106 | ti->instance_size = info->instance_size; | |
107 | ||
108 | ti->class_init = info->class_init; | |
3b50e311 | 109 | ti->class_base_init = info->class_base_init; |
2f28d2ff AL |
110 | ti->class_finalize = info->class_finalize; |
111 | ti->class_data = info->class_data; | |
112 | ||
113 | ti->instance_init = info->instance_init; | |
114 | ti->instance_finalize = info->instance_finalize; | |
115 | ||
116 | ti->abstract = info->abstract; | |
117 | ||
33e95c63 AL |
118 | for (i = 0; info->interfaces && info->interfaces[i].type; i++) { |
119 | ti->interfaces[i].typename = g_strdup(info->interfaces[i].type); | |
2f28d2ff | 120 | } |
33e95c63 | 121 | ti->num_interfaces = i; |
2f28d2ff AL |
122 | |
123 | type_table_add(ti); | |
124 | ||
125 | return ti; | |
126 | } | |
127 | ||
049cb3cf PB |
128 | TypeImpl *type_register(const TypeInfo *info) |
129 | { | |
130 | assert(info->parent); | |
131 | return type_register_internal(info); | |
132 | } | |
133 | ||
2f28d2ff AL |
134 | TypeImpl *type_register_static(const TypeInfo *info) |
135 | { | |
136 | return type_register(info); | |
137 | } | |
138 | ||
139 | static TypeImpl *type_get_by_name(const char *name) | |
140 | { | |
141 | if (name == NULL) { | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | return type_table_lookup(name); | |
146 | } | |
147 | ||
148 | static TypeImpl *type_get_parent(TypeImpl *type) | |
149 | { | |
150 | if (!type->parent_type && type->parent) { | |
151 | type->parent_type = type_get_by_name(type->parent); | |
152 | g_assert(type->parent_type != NULL); | |
153 | } | |
154 | ||
155 | return type->parent_type; | |
156 | } | |
157 | ||
158 | static bool type_has_parent(TypeImpl *type) | |
159 | { | |
160 | return (type->parent != NULL); | |
161 | } | |
162 | ||
163 | static size_t type_class_get_size(TypeImpl *ti) | |
164 | { | |
165 | if (ti->class_size) { | |
166 | return ti->class_size; | |
167 | } | |
168 | ||
169 | if (type_has_parent(ti)) { | |
170 | return type_class_get_size(type_get_parent(ti)); | |
171 | } | |
172 | ||
173 | return sizeof(ObjectClass); | |
174 | } | |
175 | ||
aca59af6 IM |
176 | static size_t type_object_get_size(TypeImpl *ti) |
177 | { | |
178 | if (ti->instance_size) { | |
179 | return ti->instance_size; | |
180 | } | |
181 | ||
182 | if (type_has_parent(ti)) { | |
183 | return type_object_get_size(type_get_parent(ti)); | |
184 | } | |
185 | ||
186 | return 0; | |
187 | } | |
188 | ||
33e95c63 | 189 | static bool type_is_ancestor(TypeImpl *type, TypeImpl *target_type) |
2f28d2ff | 190 | { |
33e95c63 AL |
191 | assert(target_type); |
192 | ||
193 | /* Check if typename is a direct ancestor of type */ | |
194 | while (type) { | |
195 | if (type == target_type) { | |
196 | return true; | |
197 | } | |
2f28d2ff | 198 | |
33e95c63 AL |
199 | type = type_get_parent(type); |
200 | } | |
201 | ||
202 | return false; | |
203 | } | |
204 | ||
205 | static void type_initialize(TypeImpl *ti); | |
206 | ||
207 | static void type_initialize_interface(TypeImpl *ti, const char *parent) | |
208 | { | |
209 | InterfaceClass *new_iface; | |
210 | TypeInfo info = { }; | |
211 | TypeImpl *iface_impl; | |
212 | ||
213 | info.parent = parent; | |
214 | info.name = g_strdup_printf("%s::%s", ti->name, info.parent); | |
215 | info.abstract = true; | |
216 | ||
217 | iface_impl = type_register(&info); | |
218 | type_initialize(iface_impl); | |
219 | g_free((char *)info.name); | |
220 | ||
221 | new_iface = (InterfaceClass *)iface_impl->class; | |
222 | new_iface->concrete_class = ti->class; | |
223 | ||
224 | ti->class->interfaces = g_slist_append(ti->class->interfaces, | |
225 | iface_impl->class); | |
2f28d2ff AL |
226 | } |
227 | ||
ac451033 | 228 | static void type_initialize(TypeImpl *ti) |
2f28d2ff | 229 | { |
745549c8 | 230 | TypeImpl *parent; |
2f28d2ff AL |
231 | |
232 | if (ti->class) { | |
233 | return; | |
234 | } | |
235 | ||
236 | ti->class_size = type_class_get_size(ti); | |
aca59af6 | 237 | ti->instance_size = type_object_get_size(ti); |
2f28d2ff AL |
238 | |
239 | ti->class = g_malloc0(ti->class_size); | |
2f28d2ff | 240 | |
745549c8 PB |
241 | parent = type_get_parent(ti); |
242 | if (parent) { | |
ac451033 | 243 | type_initialize(parent); |
33e95c63 AL |
244 | GSList *e; |
245 | int i; | |
2f28d2ff | 246 | |
2f28d2ff | 247 | g_assert(parent->class_size <= ti->class_size); |
745549c8 | 248 | memcpy(ti->class, parent->class, parent->class_size); |
3e407de4 | 249 | ti->class->interfaces = NULL; |
33e95c63 AL |
250 | |
251 | for (e = parent->class->interfaces; e; e = e->next) { | |
252 | ObjectClass *iface = e->data; | |
253 | type_initialize_interface(ti, object_class_get_name(iface)); | |
254 | } | |
255 | ||
256 | for (i = 0; i < ti->num_interfaces; i++) { | |
257 | TypeImpl *t = type_get_by_name(ti->interfaces[i].typename); | |
258 | for (e = ti->class->interfaces; e; e = e->next) { | |
259 | TypeImpl *target_type = OBJECT_CLASS(e->data)->type; | |
260 | ||
261 | if (type_is_ancestor(target_type, t)) { | |
262 | break; | |
263 | } | |
264 | } | |
265 | ||
266 | if (e) { | |
267 | continue; | |
268 | } | |
269 | ||
270 | type_initialize_interface(ti, ti->interfaces[i].typename); | |
271 | } | |
745549c8 | 272 | } |
2f28d2ff | 273 | |
745549c8 | 274 | ti->class->type = ti; |
3b50e311 | 275 | |
745549c8 PB |
276 | while (parent) { |
277 | if (parent->class_base_init) { | |
278 | parent->class_base_init(ti->class, ti->class_data); | |
3b50e311 | 279 | } |
745549c8 | 280 | parent = type_get_parent(parent); |
2f28d2ff AL |
281 | } |
282 | ||
2f28d2ff AL |
283 | if (ti->class_init) { |
284 | ti->class_init(ti->class, ti->class_data); | |
285 | } | |
2f28d2ff | 286 | |
2f28d2ff | 287 | |
2f28d2ff AL |
288 | } |
289 | ||
290 | static void object_init_with_type(Object *obj, TypeImpl *ti) | |
291 | { | |
2f28d2ff AL |
292 | if (type_has_parent(ti)) { |
293 | object_init_with_type(obj, type_get_parent(ti)); | |
294 | } | |
295 | ||
2f28d2ff AL |
296 | if (ti->instance_init) { |
297 | ti->instance_init(obj); | |
298 | } | |
299 | } | |
300 | ||
301 | void object_initialize_with_type(void *data, TypeImpl *type) | |
302 | { | |
303 | Object *obj = data; | |
304 | ||
305 | g_assert(type != NULL); | |
ac451033 | 306 | type_initialize(type); |
aca59af6 IM |
307 | |
308 | g_assert(type->instance_size >= sizeof(Object)); | |
2f28d2ff AL |
309 | g_assert(type->abstract == false); |
310 | ||
311 | memset(obj, 0, type->instance_size); | |
312 | obj->class = type->class; | |
764b6312 | 313 | object_ref(obj); |
57c9fafe | 314 | QTAILQ_INIT(&obj->properties); |
2f28d2ff AL |
315 | object_init_with_type(obj, type); |
316 | } | |
317 | ||
318 | void object_initialize(void *data, const char *typename) | |
319 | { | |
320 | TypeImpl *type = type_get_by_name(typename); | |
321 | ||
322 | object_initialize_with_type(data, type); | |
323 | } | |
324 | ||
5d9d3f47 AF |
325 | static inline bool object_property_is_child(ObjectProperty *prop) |
326 | { | |
327 | return strstart(prop->type, "child<", NULL); | |
328 | } | |
329 | ||
330 | static inline bool object_property_is_link(ObjectProperty *prop) | |
331 | { | |
332 | return strstart(prop->type, "link<", NULL); | |
333 | } | |
334 | ||
57c9fafe AL |
335 | static void object_property_del_all(Object *obj) |
336 | { | |
337 | while (!QTAILQ_EMPTY(&obj->properties)) { | |
338 | ObjectProperty *prop = QTAILQ_FIRST(&obj->properties); | |
339 | ||
340 | QTAILQ_REMOVE(&obj->properties, prop, node); | |
341 | ||
342 | if (prop->release) { | |
343 | prop->release(obj, prop->name, prop->opaque); | |
344 | } | |
345 | ||
346 | g_free(prop->name); | |
347 | g_free(prop->type); | |
348 | g_free(prop); | |
349 | } | |
350 | } | |
351 | ||
352 | static void object_property_del_child(Object *obj, Object *child, Error **errp) | |
353 | { | |
354 | ObjectProperty *prop; | |
355 | ||
356 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
5d9d3f47 | 357 | if (object_property_is_child(prop) && prop->opaque == child) { |
57c9fafe | 358 | object_property_del(obj, prop->name, errp); |
6c1fdcf9 | 359 | break; |
57c9fafe AL |
360 | } |
361 | } | |
362 | } | |
363 | ||
364 | void object_unparent(Object *obj) | |
365 | { | |
e0a83fc2 PB |
366 | if (!obj->parent) { |
367 | return; | |
368 | } | |
369 | ||
52e636cd | 370 | object_ref(obj); |
667d22d1 PB |
371 | if (obj->class->unparent) { |
372 | (obj->class->unparent)(obj); | |
373 | } | |
e998fa8d MT |
374 | if (obj->parent) { |
375 | object_property_del_child(obj->parent, obj, NULL); | |
376 | } | |
52e636cd | 377 | object_unref(obj); |
57c9fafe AL |
378 | } |
379 | ||
2f28d2ff AL |
380 | static void object_deinit(Object *obj, TypeImpl *type) |
381 | { | |
382 | if (type->instance_finalize) { | |
383 | type->instance_finalize(obj); | |
384 | } | |
385 | ||
2f28d2ff AL |
386 | if (type_has_parent(type)) { |
387 | object_deinit(obj, type_get_parent(type)); | |
388 | } | |
389 | } | |
390 | ||
339c2708 | 391 | static void object_finalize(void *data) |
2f28d2ff AL |
392 | { |
393 | Object *obj = data; | |
394 | TypeImpl *ti = obj->class->type; | |
395 | ||
396 | object_deinit(obj, ti); | |
57c9fafe | 397 | object_property_del_all(obj); |
db85b575 AL |
398 | |
399 | g_assert(obj->ref == 0); | |
fde9bf44 PB |
400 | if (obj->free) { |
401 | obj->free(obj); | |
402 | } | |
2f28d2ff AL |
403 | } |
404 | ||
405 | Object *object_new_with_type(Type type) | |
406 | { | |
407 | Object *obj; | |
408 | ||
409 | g_assert(type != NULL); | |
ac451033 | 410 | type_initialize(type); |
2f28d2ff AL |
411 | |
412 | obj = g_malloc(type->instance_size); | |
413 | object_initialize_with_type(obj, type); | |
fde9bf44 | 414 | obj->free = g_free; |
2f28d2ff AL |
415 | |
416 | return obj; | |
417 | } | |
418 | ||
419 | Object *object_new(const char *typename) | |
420 | { | |
421 | TypeImpl *ti = type_get_by_name(typename); | |
422 | ||
423 | return object_new_with_type(ti); | |
424 | } | |
425 | ||
2f28d2ff AL |
426 | Object *object_dynamic_cast(Object *obj, const char *typename) |
427 | { | |
b7f43fe4 | 428 | if (obj && object_class_dynamic_cast(object_get_class(obj), typename)) { |
acc4af3f PB |
429 | return obj; |
430 | } | |
431 | ||
2f28d2ff AL |
432 | return NULL; |
433 | } | |
434 | ||
be17f18b PB |
435 | Object *object_dynamic_cast_assert(Object *obj, const char *typename, |
436 | const char *file, int line, const char *func) | |
2f28d2ff AL |
437 | { |
438 | Object *inst; | |
439 | ||
fa131d94 PB |
440 | trace_object_dynamic_cast_assert(obj ? obj->class->type->name : "(null)", |
441 | typename, file, line, func); | |
442 | ||
2f28d2ff AL |
443 | inst = object_dynamic_cast(obj, typename); |
444 | ||
b7f43fe4 | 445 | if (!inst && obj) { |
be17f18b PB |
446 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
447 | file, line, func, obj, typename); | |
2f28d2ff AL |
448 | abort(); |
449 | } | |
450 | ||
451 | return inst; | |
452 | } | |
453 | ||
454 | ObjectClass *object_class_dynamic_cast(ObjectClass *class, | |
455 | const char *typename) | |
456 | { | |
33e95c63 | 457 | ObjectClass *ret = NULL; |
bf0fda34 PB |
458 | TypeImpl *target_type; |
459 | TypeImpl *type; | |
2f28d2ff | 460 | |
bf0fda34 PB |
461 | if (!class) { |
462 | return NULL; | |
463 | } | |
464 | ||
793c96b5 | 465 | /* A simple fast path that can trigger a lot for leaf classes. */ |
bf0fda34 | 466 | type = class->type; |
793c96b5 PB |
467 | if (type->name == typename) { |
468 | return class; | |
469 | } | |
470 | ||
bf0fda34 | 471 | target_type = type_get_by_name(typename); |
9ab880b3 AG |
472 | if (!target_type) { |
473 | /* target class type unknown, so fail the cast */ | |
474 | return NULL; | |
475 | } | |
476 | ||
00e2ceae PC |
477 | if (type->class->interfaces && |
478 | type_is_ancestor(target_type, type_interface)) { | |
33e95c63 AL |
479 | int found = 0; |
480 | GSList *i; | |
2f28d2ff | 481 | |
33e95c63 AL |
482 | for (i = class->interfaces; i; i = i->next) { |
483 | ObjectClass *target_class = i->data; | |
484 | ||
485 | if (type_is_ancestor(target_class->type, target_type)) { | |
486 | ret = target_class; | |
487 | found++; | |
488 | } | |
489 | } | |
490 | ||
491 | /* The match was ambiguous, don't allow a cast */ | |
492 | if (found > 1) { | |
493 | ret = NULL; | |
494 | } | |
495 | } else if (type_is_ancestor(type, target_type)) { | |
496 | ret = class; | |
2f28d2ff AL |
497 | } |
498 | ||
33e95c63 | 499 | return ret; |
2f28d2ff AL |
500 | } |
501 | ||
502 | ObjectClass *object_class_dynamic_cast_assert(ObjectClass *class, | |
be17f18b PB |
503 | const char *typename, |
504 | const char *file, int line, | |
505 | const char *func) | |
2f28d2ff | 506 | { |
fa131d94 PB |
507 | ObjectClass *ret; |
508 | ||
509 | trace_object_class_dynamic_cast_assert(class ? class->type->name : "(null)", | |
510 | typename, file, line, func); | |
2f28d2ff | 511 | |
fa131d94 | 512 | ret = object_class_dynamic_cast(class, typename); |
bf0fda34 | 513 | if (!ret && class) { |
be17f18b PB |
514 | fprintf(stderr, "%s:%d:%s: Object %p is not an instance of type %s\n", |
515 | file, line, func, class, typename); | |
2f28d2ff AL |
516 | abort(); |
517 | } | |
518 | ||
519 | return ret; | |
520 | } | |
521 | ||
522 | const char *object_get_typename(Object *obj) | |
523 | { | |
524 | return obj->class->type->name; | |
525 | } | |
526 | ||
527 | ObjectClass *object_get_class(Object *obj) | |
528 | { | |
529 | return obj->class; | |
530 | } | |
531 | ||
17862378 AF |
532 | bool object_class_is_abstract(ObjectClass *klass) |
533 | { | |
534 | return klass->type->abstract; | |
535 | } | |
536 | ||
2f28d2ff AL |
537 | const char *object_class_get_name(ObjectClass *klass) |
538 | { | |
539 | return klass->type->name; | |
540 | } | |
541 | ||
542 | ObjectClass *object_class_by_name(const char *typename) | |
543 | { | |
544 | TypeImpl *type = type_get_by_name(typename); | |
545 | ||
546 | if (!type) { | |
547 | return NULL; | |
548 | } | |
549 | ||
ac451033 | 550 | type_initialize(type); |
2f28d2ff AL |
551 | |
552 | return type->class; | |
553 | } | |
554 | ||
e7cce67f PB |
555 | ObjectClass *object_class_get_parent(ObjectClass *class) |
556 | { | |
557 | TypeImpl *type = type_get_parent(class->type); | |
558 | ||
559 | if (!type) { | |
560 | return NULL; | |
561 | } | |
562 | ||
563 | type_initialize(type); | |
564 | ||
565 | return type->class; | |
566 | } | |
567 | ||
2f28d2ff AL |
568 | typedef struct OCFData |
569 | { | |
570 | void (*fn)(ObjectClass *klass, void *opaque); | |
93c511a1 AL |
571 | const char *implements_type; |
572 | bool include_abstract; | |
2f28d2ff AL |
573 | void *opaque; |
574 | } OCFData; | |
575 | ||
576 | static void object_class_foreach_tramp(gpointer key, gpointer value, | |
577 | gpointer opaque) | |
578 | { | |
579 | OCFData *data = opaque; | |
580 | TypeImpl *type = value; | |
93c511a1 | 581 | ObjectClass *k; |
2f28d2ff | 582 | |
ac451033 | 583 | type_initialize(type); |
93c511a1 | 584 | k = type->class; |
2f28d2ff | 585 | |
93c511a1 AL |
586 | if (!data->include_abstract && type->abstract) { |
587 | return; | |
588 | } | |
589 | ||
590 | if (data->implements_type && | |
591 | !object_class_dynamic_cast(k, data->implements_type)) { | |
592 | return; | |
593 | } | |
594 | ||
595 | data->fn(k, data->opaque); | |
2f28d2ff AL |
596 | } |
597 | ||
598 | void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque), | |
93c511a1 | 599 | const char *implements_type, bool include_abstract, |
2f28d2ff AL |
600 | void *opaque) |
601 | { | |
93c511a1 | 602 | OCFData data = { fn, implements_type, include_abstract, opaque }; |
2f28d2ff AL |
603 | |
604 | g_hash_table_foreach(type_table_get(), object_class_foreach_tramp, &data); | |
605 | } | |
57c9fafe | 606 | |
32efc535 PB |
607 | int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque), |
608 | void *opaque) | |
609 | { | |
610 | ObjectProperty *prop; | |
611 | int ret = 0; | |
612 | ||
613 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
614 | if (object_property_is_child(prop)) { | |
615 | ret = fn(prop->opaque, opaque); | |
616 | if (ret != 0) { | |
617 | break; | |
618 | } | |
619 | } | |
620 | } | |
621 | return ret; | |
622 | } | |
623 | ||
418ba9e5 AF |
624 | static void object_class_get_list_tramp(ObjectClass *klass, void *opaque) |
625 | { | |
626 | GSList **list = opaque; | |
627 | ||
628 | *list = g_slist_prepend(*list, klass); | |
629 | } | |
630 | ||
631 | GSList *object_class_get_list(const char *implements_type, | |
632 | bool include_abstract) | |
633 | { | |
634 | GSList *list = NULL; | |
635 | ||
636 | object_class_foreach(object_class_get_list_tramp, | |
637 | implements_type, include_abstract, &list); | |
638 | return list; | |
639 | } | |
640 | ||
57c9fafe AL |
641 | void object_ref(Object *obj) |
642 | { | |
643 | obj->ref++; | |
644 | } | |
645 | ||
646 | void object_unref(Object *obj) | |
647 | { | |
648 | g_assert(obj->ref > 0); | |
649 | obj->ref--; | |
650 | ||
651 | /* parent always holds a reference to its children */ | |
652 | if (obj->ref == 0) { | |
653 | object_finalize(obj); | |
654 | } | |
655 | } | |
656 | ||
657 | void object_property_add(Object *obj, const char *name, const char *type, | |
658 | ObjectPropertyAccessor *get, | |
659 | ObjectPropertyAccessor *set, | |
660 | ObjectPropertyRelease *release, | |
661 | void *opaque, Error **errp) | |
662 | { | |
54852b03 PM |
663 | ObjectProperty *prop; |
664 | ||
665 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
666 | if (strcmp(prop->name, name) == 0) { | |
667 | error_setg(errp, "attempt to add duplicate property '%s'" | |
668 | " to object (type '%s')", name, | |
669 | object_get_typename(obj)); | |
670 | return; | |
671 | } | |
672 | } | |
673 | ||
674 | prop = g_malloc0(sizeof(*prop)); | |
57c9fafe AL |
675 | |
676 | prop->name = g_strdup(name); | |
677 | prop->type = g_strdup(type); | |
678 | ||
679 | prop->get = get; | |
680 | prop->set = set; | |
681 | prop->release = release; | |
682 | prop->opaque = opaque; | |
683 | ||
684 | QTAILQ_INSERT_TAIL(&obj->properties, prop, node); | |
685 | } | |
686 | ||
89bfe000 PB |
687 | ObjectProperty *object_property_find(Object *obj, const char *name, |
688 | Error **errp) | |
57c9fafe AL |
689 | { |
690 | ObjectProperty *prop; | |
691 | ||
692 | QTAILQ_FOREACH(prop, &obj->properties, node) { | |
693 | if (strcmp(prop->name, name) == 0) { | |
694 | return prop; | |
695 | } | |
696 | } | |
697 | ||
89bfe000 | 698 | error_set(errp, QERR_PROPERTY_NOT_FOUND, "", name); |
57c9fafe AL |
699 | return NULL; |
700 | } | |
701 | ||
702 | void object_property_del(Object *obj, const char *name, Error **errp) | |
703 | { | |
89bfe000 | 704 | ObjectProperty *prop = object_property_find(obj, name, errp); |
0866aca1 | 705 | if (prop == NULL) { |
0866aca1 AL |
706 | return; |
707 | } | |
57c9fafe | 708 | |
0866aca1 AL |
709 | if (prop->release) { |
710 | prop->release(obj, name, prop->opaque); | |
711 | } | |
712 | ||
713 | QTAILQ_REMOVE(&obj->properties, prop, node); | |
57c9fafe AL |
714 | |
715 | g_free(prop->name); | |
716 | g_free(prop->type); | |
717 | g_free(prop); | |
718 | } | |
719 | ||
720 | void object_property_get(Object *obj, Visitor *v, const char *name, | |
721 | Error **errp) | |
722 | { | |
89bfe000 | 723 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 724 | if (prop == NULL) { |
57c9fafe AL |
725 | return; |
726 | } | |
727 | ||
728 | if (!prop->get) { | |
729 | error_set(errp, QERR_PERMISSION_DENIED); | |
730 | } else { | |
731 | prop->get(obj, v, prop->opaque, name, errp); | |
732 | } | |
733 | } | |
734 | ||
735 | void object_property_set(Object *obj, Visitor *v, const char *name, | |
736 | Error **errp) | |
737 | { | |
89bfe000 | 738 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 739 | if (prop == NULL) { |
57c9fafe AL |
740 | return; |
741 | } | |
742 | ||
743 | if (!prop->set) { | |
744 | error_set(errp, QERR_PERMISSION_DENIED); | |
745 | } else { | |
746 | prop->set(obj, v, prop->opaque, name, errp); | |
747 | } | |
748 | } | |
749 | ||
7b7b7d18 PB |
750 | void object_property_set_str(Object *obj, const char *value, |
751 | const char *name, Error **errp) | |
752 | { | |
753 | QString *qstr = qstring_from_str(value); | |
754 | object_property_set_qobject(obj, QOBJECT(qstr), name, errp); | |
755 | ||
756 | QDECREF(qstr); | |
757 | } | |
758 | ||
759 | char *object_property_get_str(Object *obj, const char *name, | |
760 | Error **errp) | |
761 | { | |
762 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
763 | QString *qstring; | |
764 | char *retval; | |
765 | ||
766 | if (!ret) { | |
767 | return NULL; | |
768 | } | |
769 | qstring = qobject_to_qstring(ret); | |
770 | if (!qstring) { | |
771 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "string"); | |
772 | retval = NULL; | |
773 | } else { | |
774 | retval = g_strdup(qstring_get_str(qstring)); | |
775 | } | |
776 | ||
777 | QDECREF(qstring); | |
778 | return retval; | |
779 | } | |
780 | ||
1d9c5a12 PB |
781 | void object_property_set_link(Object *obj, Object *value, |
782 | const char *name, Error **errp) | |
783 | { | |
784 | object_property_set_str(obj, object_get_canonical_path(value), | |
785 | name, errp); | |
786 | } | |
787 | ||
788 | Object *object_property_get_link(Object *obj, const char *name, | |
789 | Error **errp) | |
790 | { | |
791 | char *str = object_property_get_str(obj, name, errp); | |
792 | Object *target = NULL; | |
793 | ||
794 | if (str && *str) { | |
795 | target = object_resolve_path(str, NULL); | |
796 | if (!target) { | |
797 | error_set(errp, QERR_DEVICE_NOT_FOUND, str); | |
798 | } | |
799 | } | |
800 | ||
801 | g_free(str); | |
802 | return target; | |
803 | } | |
804 | ||
7b7b7d18 PB |
805 | void object_property_set_bool(Object *obj, bool value, |
806 | const char *name, Error **errp) | |
807 | { | |
808 | QBool *qbool = qbool_from_int(value); | |
809 | object_property_set_qobject(obj, QOBJECT(qbool), name, errp); | |
810 | ||
811 | QDECREF(qbool); | |
812 | } | |
813 | ||
814 | bool object_property_get_bool(Object *obj, const char *name, | |
815 | Error **errp) | |
816 | { | |
817 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
818 | QBool *qbool; | |
819 | bool retval; | |
820 | ||
821 | if (!ret) { | |
822 | return false; | |
823 | } | |
824 | qbool = qobject_to_qbool(ret); | |
825 | if (!qbool) { | |
826 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "boolean"); | |
827 | retval = false; | |
828 | } else { | |
829 | retval = qbool_get_int(qbool); | |
830 | } | |
831 | ||
832 | QDECREF(qbool); | |
833 | return retval; | |
834 | } | |
835 | ||
836 | void object_property_set_int(Object *obj, int64_t value, | |
837 | const char *name, Error **errp) | |
838 | { | |
839 | QInt *qint = qint_from_int(value); | |
840 | object_property_set_qobject(obj, QOBJECT(qint), name, errp); | |
841 | ||
842 | QDECREF(qint); | |
843 | } | |
844 | ||
845 | int64_t object_property_get_int(Object *obj, const char *name, | |
846 | Error **errp) | |
847 | { | |
848 | QObject *ret = object_property_get_qobject(obj, name, errp); | |
849 | QInt *qint; | |
850 | int64_t retval; | |
851 | ||
852 | if (!ret) { | |
853 | return -1; | |
854 | } | |
855 | qint = qobject_to_qint(ret); | |
856 | if (!qint) { | |
857 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, "int"); | |
858 | retval = -1; | |
859 | } else { | |
860 | retval = qint_get_int(qint); | |
861 | } | |
862 | ||
863 | QDECREF(qint); | |
864 | return retval; | |
b2cd7dee PB |
865 | } |
866 | ||
867 | void object_property_parse(Object *obj, const char *string, | |
868 | const char *name, Error **errp) | |
869 | { | |
870 | StringInputVisitor *mi; | |
871 | mi = string_input_visitor_new(string); | |
872 | object_property_set(obj, string_input_get_visitor(mi), name, errp); | |
873 | ||
874 | string_input_visitor_cleanup(mi); | |
875 | } | |
876 | ||
877 | char *object_property_print(Object *obj, const char *name, | |
878 | Error **errp) | |
879 | { | |
880 | StringOutputVisitor *mo; | |
881 | char *string; | |
882 | ||
883 | mo = string_output_visitor_new(); | |
8185bfc1 | 884 | object_property_get(obj, string_output_get_visitor(mo), name, errp); |
b2cd7dee PB |
885 | string = string_output_get_string(mo); |
886 | string_output_visitor_cleanup(mo); | |
887 | return string; | |
7b7b7d18 PB |
888 | } |
889 | ||
57c9fafe AL |
890 | const char *object_property_get_type(Object *obj, const char *name, Error **errp) |
891 | { | |
89bfe000 | 892 | ObjectProperty *prop = object_property_find(obj, name, errp); |
57c9fafe | 893 | if (prop == NULL) { |
57c9fafe AL |
894 | return NULL; |
895 | } | |
896 | ||
897 | return prop->type; | |
898 | } | |
899 | ||
900 | Object *object_get_root(void) | |
901 | { | |
8b45d447 | 902 | static Object *root; |
57c9fafe | 903 | |
8b45d447 AL |
904 | if (!root) { |
905 | root = object_new("container"); | |
57c9fafe AL |
906 | } |
907 | ||
8b45d447 | 908 | return root; |
57c9fafe AL |
909 | } |
910 | ||
911 | static void object_get_child_property(Object *obj, Visitor *v, void *opaque, | |
912 | const char *name, Error **errp) | |
913 | { | |
914 | Object *child = opaque; | |
915 | gchar *path; | |
916 | ||
917 | path = object_get_canonical_path(child); | |
918 | visit_type_str(v, &path, name, errp); | |
919 | g_free(path); | |
920 | } | |
921 | ||
db85b575 AL |
922 | static void object_finalize_child_property(Object *obj, const char *name, |
923 | void *opaque) | |
924 | { | |
925 | Object *child = opaque; | |
926 | ||
927 | object_unref(child); | |
928 | } | |
929 | ||
57c9fafe AL |
930 | void object_property_add_child(Object *obj, const char *name, |
931 | Object *child, Error **errp) | |
932 | { | |
933 | gchar *type; | |
934 | ||
935 | type = g_strdup_printf("child<%s>", object_get_typename(OBJECT(child))); | |
936 | ||
937 | object_property_add(obj, name, type, object_get_child_property, | |
db85b575 | 938 | NULL, object_finalize_child_property, child, errp); |
57c9fafe AL |
939 | |
940 | object_ref(child); | |
941 | g_assert(child->parent == NULL); | |
942 | child->parent = obj; | |
943 | ||
944 | g_free(type); | |
945 | } | |
946 | ||
947 | static void object_get_link_property(Object *obj, Visitor *v, void *opaque, | |
948 | const char *name, Error **errp) | |
949 | { | |
950 | Object **child = opaque; | |
951 | gchar *path; | |
952 | ||
953 | if (*child) { | |
954 | path = object_get_canonical_path(*child); | |
955 | visit_type_str(v, &path, name, errp); | |
956 | g_free(path); | |
957 | } else { | |
958 | path = (gchar *)""; | |
959 | visit_type_str(v, &path, name, errp); | |
960 | } | |
961 | } | |
962 | ||
963 | static void object_set_link_property(Object *obj, Visitor *v, void *opaque, | |
964 | const char *name, Error **errp) | |
965 | { | |
966 | Object **child = opaque; | |
f0cdc966 | 967 | Object *old_target; |
57c9fafe AL |
968 | bool ambiguous = false; |
969 | const char *type; | |
970 | char *path; | |
11e35bfd | 971 | gchar *target_type; |
57c9fafe AL |
972 | |
973 | type = object_property_get_type(obj, name, NULL); | |
974 | ||
975 | visit_type_str(v, &path, name, errp); | |
976 | ||
f0cdc966 AB |
977 | old_target = *child; |
978 | *child = NULL; | |
57c9fafe AL |
979 | |
980 | if (strcmp(path, "") != 0) { | |
981 | Object *target; | |
982 | ||
11e35bfd PB |
983 | /* Go from link<FOO> to FOO. */ |
984 | target_type = g_strndup(&type[5], strlen(type) - 6); | |
985 | target = object_resolve_path_type(path, target_type, &ambiguous); | |
57c9fafe | 986 | |
11e35bfd PB |
987 | if (ambiguous) { |
988 | error_set(errp, QERR_AMBIGUOUS_PATH, path); | |
989 | } else if (target) { | |
990 | object_ref(target); | |
991 | *child = target; | |
57c9fafe | 992 | } else { |
11e35bfd PB |
993 | target = object_resolve_path(path, &ambiguous); |
994 | if (target || ambiguous) { | |
995 | error_set(errp, QERR_INVALID_PARAMETER_TYPE, name, target_type); | |
996 | } else { | |
997 | error_set(errp, QERR_DEVICE_NOT_FOUND, path); | |
998 | } | |
57c9fafe | 999 | } |
11e35bfd | 1000 | g_free(target_type); |
57c9fafe AL |
1001 | } |
1002 | ||
1003 | g_free(path); | |
f0cdc966 AB |
1004 | |
1005 | if (old_target != NULL) { | |
1006 | object_unref(old_target); | |
1007 | } | |
57c9fafe AL |
1008 | } |
1009 | ||
1010 | void object_property_add_link(Object *obj, const char *name, | |
1011 | const char *type, Object **child, | |
1012 | Error **errp) | |
1013 | { | |
1014 | gchar *full_type; | |
1015 | ||
1016 | full_type = g_strdup_printf("link<%s>", type); | |
1017 | ||
1018 | object_property_add(obj, name, full_type, | |
1019 | object_get_link_property, | |
1020 | object_set_link_property, | |
1021 | NULL, child, errp); | |
1022 | ||
1023 | g_free(full_type); | |
1024 | } | |
1025 | ||
1026 | gchar *object_get_canonical_path(Object *obj) | |
1027 | { | |
1028 | Object *root = object_get_root(); | |
1029 | char *newpath = NULL, *path = NULL; | |
1030 | ||
1031 | while (obj != root) { | |
1032 | ObjectProperty *prop = NULL; | |
1033 | ||
1034 | g_assert(obj->parent != NULL); | |
1035 | ||
1036 | QTAILQ_FOREACH(prop, &obj->parent->properties, node) { | |
5d9d3f47 | 1037 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
1038 | continue; |
1039 | } | |
1040 | ||
1041 | if (prop->opaque == obj) { | |
1042 | if (path) { | |
1043 | newpath = g_strdup_printf("%s/%s", prop->name, path); | |
1044 | g_free(path); | |
1045 | path = newpath; | |
1046 | } else { | |
1047 | path = g_strdup(prop->name); | |
1048 | } | |
1049 | break; | |
1050 | } | |
1051 | } | |
1052 | ||
1053 | g_assert(prop != NULL); | |
1054 | ||
1055 | obj = obj->parent; | |
1056 | } | |
1057 | ||
1058 | newpath = g_strdup_printf("/%s", path); | |
1059 | g_free(path); | |
1060 | ||
1061 | return newpath; | |
1062 | } | |
1063 | ||
3e84b483 | 1064 | Object *object_resolve_path_component(Object *parent, const gchar *part) |
a612b2a6 | 1065 | { |
89bfe000 | 1066 | ObjectProperty *prop = object_property_find(parent, part, NULL); |
a612b2a6 PB |
1067 | if (prop == NULL) { |
1068 | return NULL; | |
1069 | } | |
1070 | ||
5d9d3f47 | 1071 | if (object_property_is_link(prop)) { |
a612b2a6 | 1072 | return *(Object **)prop->opaque; |
5d9d3f47 | 1073 | } else if (object_property_is_child(prop)) { |
a612b2a6 PB |
1074 | return prop->opaque; |
1075 | } else { | |
1076 | return NULL; | |
1077 | } | |
1078 | } | |
1079 | ||
57c9fafe AL |
1080 | static Object *object_resolve_abs_path(Object *parent, |
1081 | gchar **parts, | |
02fe2db6 | 1082 | const char *typename, |
57c9fafe AL |
1083 | int index) |
1084 | { | |
57c9fafe AL |
1085 | Object *child; |
1086 | ||
1087 | if (parts[index] == NULL) { | |
02fe2db6 | 1088 | return object_dynamic_cast(parent, typename); |
57c9fafe AL |
1089 | } |
1090 | ||
1091 | if (strcmp(parts[index], "") == 0) { | |
02fe2db6 | 1092 | return object_resolve_abs_path(parent, parts, typename, index + 1); |
57c9fafe AL |
1093 | } |
1094 | ||
a612b2a6 | 1095 | child = object_resolve_path_component(parent, parts[index]); |
57c9fafe AL |
1096 | if (!child) { |
1097 | return NULL; | |
1098 | } | |
1099 | ||
02fe2db6 | 1100 | return object_resolve_abs_path(child, parts, typename, index + 1); |
57c9fafe AL |
1101 | } |
1102 | ||
1103 | static Object *object_resolve_partial_path(Object *parent, | |
1104 | gchar **parts, | |
02fe2db6 | 1105 | const char *typename, |
57c9fafe AL |
1106 | bool *ambiguous) |
1107 | { | |
1108 | Object *obj; | |
1109 | ObjectProperty *prop; | |
1110 | ||
02fe2db6 | 1111 | obj = object_resolve_abs_path(parent, parts, typename, 0); |
57c9fafe AL |
1112 | |
1113 | QTAILQ_FOREACH(prop, &parent->properties, node) { | |
1114 | Object *found; | |
1115 | ||
5d9d3f47 | 1116 | if (!object_property_is_child(prop)) { |
57c9fafe AL |
1117 | continue; |
1118 | } | |
1119 | ||
02fe2db6 PB |
1120 | found = object_resolve_partial_path(prop->opaque, parts, |
1121 | typename, ambiguous); | |
57c9fafe AL |
1122 | if (found) { |
1123 | if (obj) { | |
1124 | if (ambiguous) { | |
1125 | *ambiguous = true; | |
1126 | } | |
1127 | return NULL; | |
1128 | } | |
1129 | obj = found; | |
1130 | } | |
1131 | ||
1132 | if (ambiguous && *ambiguous) { | |
1133 | return NULL; | |
1134 | } | |
1135 | } | |
1136 | ||
1137 | return obj; | |
1138 | } | |
1139 | ||
02fe2db6 PB |
1140 | Object *object_resolve_path_type(const char *path, const char *typename, |
1141 | bool *ambiguous) | |
57c9fafe | 1142 | { |
57c9fafe AL |
1143 | Object *obj; |
1144 | gchar **parts; | |
1145 | ||
1146 | parts = g_strsplit(path, "/", 0); | |
2e1103f6 | 1147 | assert(parts); |
57c9fafe | 1148 | |
2e1103f6 | 1149 | if (parts[0] == NULL || strcmp(parts[0], "") != 0) { |
57c9fafe AL |
1150 | if (ambiguous) { |
1151 | *ambiguous = false; | |
1152 | } | |
02fe2db6 PB |
1153 | obj = object_resolve_partial_path(object_get_root(), parts, |
1154 | typename, ambiguous); | |
57c9fafe | 1155 | } else { |
02fe2db6 | 1156 | obj = object_resolve_abs_path(object_get_root(), parts, typename, 1); |
57c9fafe AL |
1157 | } |
1158 | ||
1159 | g_strfreev(parts); | |
1160 | ||
1161 | return obj; | |
1162 | } | |
1163 | ||
02fe2db6 PB |
1164 | Object *object_resolve_path(const char *path, bool *ambiguous) |
1165 | { | |
1166 | return object_resolve_path_type(path, TYPE_OBJECT, ambiguous); | |
1167 | } | |
1168 | ||
57c9fafe AL |
1169 | typedef struct StringProperty |
1170 | { | |
1171 | char *(*get)(Object *, Error **); | |
1172 | void (*set)(Object *, const char *, Error **); | |
1173 | } StringProperty; | |
1174 | ||
7b7b7d18 PB |
1175 | static void property_get_str(Object *obj, Visitor *v, void *opaque, |
1176 | const char *name, Error **errp) | |
57c9fafe AL |
1177 | { |
1178 | StringProperty *prop = opaque; | |
1179 | char *value; | |
1180 | ||
1181 | value = prop->get(obj, errp); | |
1182 | if (value) { | |
1183 | visit_type_str(v, &value, name, errp); | |
1184 | g_free(value); | |
1185 | } | |
1186 | } | |
1187 | ||
7b7b7d18 PB |
1188 | static void property_set_str(Object *obj, Visitor *v, void *opaque, |
1189 | const char *name, Error **errp) | |
57c9fafe AL |
1190 | { |
1191 | StringProperty *prop = opaque; | |
1192 | char *value; | |
1193 | Error *local_err = NULL; | |
1194 | ||
1195 | visit_type_str(v, &value, name, &local_err); | |
1196 | if (local_err) { | |
1197 | error_propagate(errp, local_err); | |
1198 | return; | |
1199 | } | |
1200 | ||
1201 | prop->set(obj, value, errp); | |
1202 | g_free(value); | |
1203 | } | |
1204 | ||
7b7b7d18 PB |
1205 | static void property_release_str(Object *obj, const char *name, |
1206 | void *opaque) | |
57c9fafe AL |
1207 | { |
1208 | StringProperty *prop = opaque; | |
1209 | g_free(prop); | |
1210 | } | |
1211 | ||
1212 | void object_property_add_str(Object *obj, const char *name, | |
1213 | char *(*get)(Object *, Error **), | |
1214 | void (*set)(Object *, const char *, Error **), | |
1215 | Error **errp) | |
1216 | { | |
1217 | StringProperty *prop = g_malloc0(sizeof(*prop)); | |
1218 | ||
1219 | prop->get = get; | |
1220 | prop->set = set; | |
1221 | ||
1222 | object_property_add(obj, name, "string", | |
7b7b7d18 PB |
1223 | get ? property_get_str : NULL, |
1224 | set ? property_set_str : NULL, | |
1225 | property_release_str, | |
57c9fafe AL |
1226 | prop, errp); |
1227 | } | |
745549c8 | 1228 | |
0e558843 AL |
1229 | typedef struct BoolProperty |
1230 | { | |
1231 | bool (*get)(Object *, Error **); | |
1232 | void (*set)(Object *, bool, Error **); | |
1233 | } BoolProperty; | |
1234 | ||
1235 | static void property_get_bool(Object *obj, Visitor *v, void *opaque, | |
1236 | const char *name, Error **errp) | |
1237 | { | |
1238 | BoolProperty *prop = opaque; | |
1239 | bool value; | |
1240 | ||
1241 | value = prop->get(obj, errp); | |
1242 | visit_type_bool(v, &value, name, errp); | |
1243 | } | |
1244 | ||
1245 | static void property_set_bool(Object *obj, Visitor *v, void *opaque, | |
1246 | const char *name, Error **errp) | |
1247 | { | |
1248 | BoolProperty *prop = opaque; | |
1249 | bool value; | |
1250 | Error *local_err = NULL; | |
1251 | ||
1252 | visit_type_bool(v, &value, name, &local_err); | |
1253 | if (local_err) { | |
1254 | error_propagate(errp, local_err); | |
1255 | return; | |
1256 | } | |
1257 | ||
1258 | prop->set(obj, value, errp); | |
1259 | } | |
1260 | ||
1261 | static void property_release_bool(Object *obj, const char *name, | |
1262 | void *opaque) | |
1263 | { | |
1264 | BoolProperty *prop = opaque; | |
1265 | g_free(prop); | |
1266 | } | |
1267 | ||
1268 | void object_property_add_bool(Object *obj, const char *name, | |
1269 | bool (*get)(Object *, Error **), | |
1270 | void (*set)(Object *, bool, Error **), | |
1271 | Error **errp) | |
1272 | { | |
1273 | BoolProperty *prop = g_malloc0(sizeof(*prop)); | |
1274 | ||
1275 | prop->get = get; | |
1276 | prop->set = set; | |
1277 | ||
1278 | object_property_add(obj, name, "bool", | |
1279 | get ? property_get_bool : NULL, | |
1280 | set ? property_set_bool : NULL, | |
1281 | property_release_bool, | |
1282 | prop, errp); | |
1283 | } | |
1284 | ||
2f262e06 PB |
1285 | static char *qdev_get_type(Object *obj, Error **errp) |
1286 | { | |
1287 | return g_strdup(object_get_typename(obj)); | |
1288 | } | |
1289 | ||
1290 | static void object_instance_init(Object *obj) | |
1291 | { | |
1292 | object_property_add_str(obj, "type", qdev_get_type, NULL, NULL); | |
1293 | } | |
1294 | ||
745549c8 PB |
1295 | static void register_types(void) |
1296 | { | |
1297 | static TypeInfo interface_info = { | |
1298 | .name = TYPE_INTERFACE, | |
33e95c63 | 1299 | .class_size = sizeof(InterfaceClass), |
745549c8 PB |
1300 | .abstract = true, |
1301 | }; | |
1302 | ||
1303 | static TypeInfo object_info = { | |
1304 | .name = TYPE_OBJECT, | |
1305 | .instance_size = sizeof(Object), | |
2f262e06 | 1306 | .instance_init = object_instance_init, |
745549c8 PB |
1307 | .abstract = true, |
1308 | }; | |
1309 | ||
049cb3cf PB |
1310 | type_interface = type_register_internal(&interface_info); |
1311 | type_register_internal(&object_info); | |
745549c8 PB |
1312 | } |
1313 | ||
1314 | type_init(register_types) |