]> git.ipfire.org Git - thirdparty/qemu.git/blob - include/qom/object.h
26df6137b9117d30ff8ade73c618aaf084d5f2dc
[thirdparty/qemu.git] / include / qom / object.h
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 */
13
14 #ifndef QEMU_OBJECT_H
15 #define QEMU_OBJECT_H
16
17 #include "qapi/qapi-builtin-types.h"
18 #include "qemu/module.h"
19
20 struct TypeImpl;
21 typedef struct TypeImpl *Type;
22
23 typedef struct TypeInfo TypeInfo;
24
25 typedef struct InterfaceClass InterfaceClass;
26 typedef struct InterfaceInfo InterfaceInfo;
27
28 #define TYPE_OBJECT "object"
29 #define TYPE_CONTAINER "container"
30
31 typedef struct ObjectProperty ObjectProperty;
32
33 /**
34 * typedef ObjectPropertyAccessor:
35 * @obj: the object that owns the property
36 * @v: the visitor that contains the property data
37 * @name: the name of the property
38 * @opaque: the object property opaque
39 * @errp: a pointer to an Error that is filled if getting/setting fails.
40 *
41 * Called when trying to get/set a property.
42 */
43 typedef void (ObjectPropertyAccessor)(Object *obj,
44 Visitor *v,
45 const char *name,
46 void *opaque,
47 Error **errp);
48
49 /**
50 * typedef ObjectPropertyResolve:
51 * @obj: the object that owns the property
52 * @opaque: the opaque registered with the property
53 * @part: the name of the property
54 *
55 * Resolves the #Object corresponding to property @part.
56 *
57 * The returned object can also be used as a starting point
58 * to resolve a relative path starting with "@part".
59 *
60 * Returns: If @path is the path that led to @obj, the function
61 * returns the #Object corresponding to "@path/@part".
62 * If "@path/@part" is not a valid object path, it returns #NULL.
63 */
64 typedef Object *(ObjectPropertyResolve)(Object *obj,
65 void *opaque,
66 const char *part);
67
68 /**
69 * typedef ObjectPropertyRelease:
70 * @obj: the object that owns the property
71 * @name: the name of the property
72 * @opaque: the opaque registered with the property
73 *
74 * Called when a property is removed from a object.
75 */
76 typedef void (ObjectPropertyRelease)(Object *obj,
77 const char *name,
78 void *opaque);
79
80 /**
81 * typedef ObjectPropertyInit:
82 * @obj: the object that owns the property
83 * @prop: the property to set
84 *
85 * Called when a property is initialized.
86 */
87 typedef void (ObjectPropertyInit)(Object *obj, ObjectProperty *prop);
88
89 struct ObjectProperty
90 {
91 char *name;
92 char *type;
93 char *description;
94 ObjectPropertyAccessor *get;
95 ObjectPropertyAccessor *set;
96 ObjectPropertyResolve *resolve;
97 ObjectPropertyRelease *release;
98 ObjectPropertyInit *init;
99 void *opaque;
100 QObject *defval;
101 };
102
103 /**
104 * typedef ObjectUnparent:
105 * @obj: the object that is being removed from the composition tree
106 *
107 * Called when an object is being removed from the QOM composition tree.
108 * The function should remove any backlinks from children objects to @obj.
109 */
110 typedef void (ObjectUnparent)(Object *obj);
111
112 /**
113 * typedef ObjectFree:
114 * @obj: the object being freed
115 *
116 * Called when an object's last reference is removed.
117 */
118 typedef void (ObjectFree)(void *obj);
119
120 #define OBJECT_CLASS_CAST_CACHE 4
121
122 /**
123 * struct ObjectClass:
124 *
125 * The base for all classes. The only thing that #ObjectClass contains is an
126 * integer type handle.
127 */
128 struct ObjectClass
129 {
130 /* private: */
131 Type type;
132 GSList *interfaces;
133
134 const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
135 const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
136
137 ObjectUnparent *unparent;
138
139 GHashTable *properties;
140 };
141
142 /**
143 * struct Object:
144 *
145 * The base for all objects. The first member of this object is a pointer to
146 * a #ObjectClass. Since C guarantees that the first member of a structure
147 * always begins at byte 0 of that structure, as long as any sub-object places
148 * its parent as the first member, we can cast directly to a #Object.
149 *
150 * As a result, #Object contains a reference to the objects type as its
151 * first member. This allows identification of the real type of the object at
152 * run time.
153 */
154 struct Object
155 {
156 /* private: */
157 ObjectClass *class;
158 ObjectFree *free;
159 GHashTable *properties;
160 uint32_t ref;
161 Object *parent;
162 };
163
164 /**
165 * DECLARE_INSTANCE_CHECKER:
166 * @InstanceType: instance struct name
167 * @OBJ_NAME: the object name in uppercase with underscore separators
168 * @TYPENAME: type name
169 *
170 * Direct usage of this macro should be avoided, and the complete
171 * OBJECT_DECLARE_TYPE macro is recommended instead.
172 *
173 * This macro will provide the instance type cast functions for a
174 * QOM type.
175 */
176 #define DECLARE_INSTANCE_CHECKER(InstanceType, OBJ_NAME, TYPENAME) \
177 static inline G_GNUC_UNUSED InstanceType * \
178 OBJ_NAME(const void *obj) \
179 { return OBJECT_CHECK(InstanceType, obj, TYPENAME); }
180
181 /**
182 * DECLARE_CLASS_CHECKERS:
183 * @ClassType: class struct name
184 * @OBJ_NAME: the object name in uppercase with underscore separators
185 * @TYPENAME: type name
186 *
187 * Direct usage of this macro should be avoided, and the complete
188 * OBJECT_DECLARE_TYPE macro is recommended instead.
189 *
190 * This macro will provide the class type cast functions for a
191 * QOM type.
192 */
193 #define DECLARE_CLASS_CHECKERS(ClassType, OBJ_NAME, TYPENAME) \
194 static inline G_GNUC_UNUSED ClassType * \
195 OBJ_NAME##_GET_CLASS(const void *obj) \
196 { return OBJECT_GET_CLASS(ClassType, obj, TYPENAME); } \
197 \
198 static inline G_GNUC_UNUSED ClassType * \
199 OBJ_NAME##_CLASS(const void *klass) \
200 { return OBJECT_CLASS_CHECK(ClassType, klass, TYPENAME); }
201
202 /**
203 * DECLARE_OBJ_CHECKERS:
204 * @InstanceType: instance struct name
205 * @ClassType: class struct name
206 * @OBJ_NAME: the object name in uppercase with underscore separators
207 * @TYPENAME: type name
208 *
209 * Direct usage of this macro should be avoided, and the complete
210 * OBJECT_DECLARE_TYPE macro is recommended instead.
211 *
212 * This macro will provide the three standard type cast functions for a
213 * QOM type.
214 */
215 #define DECLARE_OBJ_CHECKERS(InstanceType, ClassType, OBJ_NAME, TYPENAME) \
216 DECLARE_INSTANCE_CHECKER(InstanceType, OBJ_NAME, TYPENAME) \
217 \
218 DECLARE_CLASS_CHECKERS(ClassType, OBJ_NAME, TYPENAME)
219
220 /**
221 * OBJECT_DECLARE_TYPE:
222 * @InstanceType: instance struct name
223 * @ClassType: class struct name
224 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
225 *
226 * This macro is typically used in a header file, and will:
227 *
228 * - create the typedefs for the object and class structs
229 * - register the type for use with g_autoptr
230 * - provide three standard type cast functions
231 *
232 * The object struct and class struct need to be declared manually.
233 */
234 #define OBJECT_DECLARE_TYPE(InstanceType, ClassType, MODULE_OBJ_NAME) \
235 typedef struct InstanceType InstanceType; \
236 typedef struct ClassType ClassType; \
237 \
238 G_DEFINE_AUTOPTR_CLEANUP_FUNC(InstanceType, object_unref) \
239 \
240 DECLARE_OBJ_CHECKERS(InstanceType, ClassType, \
241 MODULE_OBJ_NAME, TYPE_##MODULE_OBJ_NAME)
242
243 /**
244 * OBJECT_DECLARE_SIMPLE_TYPE:
245 * @InstanceType: instance struct name
246 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
247 *
248 * This does the same as OBJECT_DECLARE_TYPE(), but with no class struct
249 * declared.
250 *
251 * This macro should be used unless the class struct needs to have
252 * virtual methods declared.
253 */
254 #define OBJECT_DECLARE_SIMPLE_TYPE(InstanceType, MODULE_OBJ_NAME) \
255 typedef struct InstanceType InstanceType; \
256 \
257 G_DEFINE_AUTOPTR_CLEANUP_FUNC(InstanceType, object_unref) \
258 \
259 DECLARE_INSTANCE_CHECKER(InstanceType, MODULE_OBJ_NAME, TYPE_##MODULE_OBJ_NAME)
260
261
262 /**
263 * DO_OBJECT_DEFINE_TYPE_EXTENDED:
264 * @ModuleObjName: the object name with initial caps
265 * @module_obj_name: the object name in lowercase with underscore separators
266 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
267 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
268 * separators
269 * @ABSTRACT: boolean flag to indicate whether the object can be instantiated
270 * @CLASS_SIZE: size of the type's class
271 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
272 *
273 * This is the base macro used to implement all the OBJECT_DEFINE_*
274 * macros. It should never be used directly in a source file.
275 */
276 #define DO_OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
277 MODULE_OBJ_NAME, \
278 PARENT_MODULE_OBJ_NAME, \
279 ABSTRACT, CLASS_SIZE, ...) \
280 static void \
281 module_obj_name##_finalize(Object *obj); \
282 static void \
283 module_obj_name##_class_init(ObjectClass *oc, const void *data); \
284 static void \
285 module_obj_name##_init(Object *obj); \
286 \
287 static const TypeInfo module_obj_name##_info = { \
288 .parent = TYPE_##PARENT_MODULE_OBJ_NAME, \
289 .name = TYPE_##MODULE_OBJ_NAME, \
290 .instance_size = sizeof(ModuleObjName), \
291 .instance_align = __alignof__(ModuleObjName), \
292 .instance_init = module_obj_name##_init, \
293 .instance_finalize = module_obj_name##_finalize, \
294 .class_size = CLASS_SIZE, \
295 .class_init = module_obj_name##_class_init, \
296 .abstract = ABSTRACT, \
297 .interfaces = (const InterfaceInfo[]) { __VA_ARGS__ } , \
298 }; \
299 \
300 static void \
301 module_obj_name##_register_types(void) \
302 { \
303 type_register_static(&module_obj_name##_info); \
304 } \
305 type_init(module_obj_name##_register_types);
306
307 /**
308 * OBJECT_DEFINE_TYPE_EXTENDED:
309 * @ModuleObjName: the object name with initial caps
310 * @module_obj_name: the object name in lowercase with underscore separators
311 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
312 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
313 * separators
314 * @ABSTRACT: boolean flag to indicate whether the object can be instantiated
315 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
316 *
317 * This macro is typically used in a source file, and will:
318 *
319 * - declare prototypes for _finalize, _class_init and _init methods
320 * - declare the TypeInfo struct instance
321 * - provide the constructor to register the type
322 *
323 * After using this macro, implementations of the _finalize, _class_init,
324 * and _init methods need to be written. Any of these can be zero-line
325 * no-op impls if no special logic is required for a given type.
326 *
327 * This macro should rarely be used, instead one of the more specialized
328 * macros is usually a better choice.
329 */
330 #define OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
331 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
332 ABSTRACT, ...) \
333 DO_OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
334 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
335 ABSTRACT, sizeof(ModuleObjName##Class), \
336 __VA_ARGS__)
337
338 /**
339 * OBJECT_DEFINE_TYPE:
340 * @ModuleObjName: the object name with initial caps
341 * @module_obj_name: the object name in lowercase with underscore separators
342 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
343 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
344 * separators
345 *
346 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
347 * for the common case of a non-abstract type, without any interfaces.
348 */
349 #define OBJECT_DEFINE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME, \
350 PARENT_MODULE_OBJ_NAME) \
351 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
352 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
353 false, { NULL })
354
355 /**
356 * OBJECT_DEFINE_TYPE_WITH_INTERFACES:
357 * @ModuleObjName: the object name with initial caps
358 * @module_obj_name: the object name in lowercase with underscore separators
359 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
360 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
361 * separators
362 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
363 *
364 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
365 * for the common case of a non-abstract type, with one or more implemented
366 * interfaces.
367 *
368 * Note when passing the list of interfaces, be sure to include the final
369 * NULL entry, e.g. { TYPE_USER_CREATABLE }, { NULL }
370 */
371 #define OBJECT_DEFINE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \
372 MODULE_OBJ_NAME, \
373 PARENT_MODULE_OBJ_NAME, ...) \
374 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
375 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
376 false, __VA_ARGS__)
377
378 /**
379 * OBJECT_DEFINE_ABSTRACT_TYPE:
380 * @ModuleObjName: the object name with initial caps
381 * @module_obj_name: the object name in lowercase with underscore separators
382 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
383 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
384 * separators
385 *
386 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
387 * for defining an abstract type, without any interfaces.
388 */
389 #define OBJECT_DEFINE_ABSTRACT_TYPE(ModuleObjName, module_obj_name, \
390 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \
391 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
392 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
393 true, { NULL })
394
395 /**
396 * OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES:
397 * @ModuleObjName: the object name with initial caps
398 * @module_obj_name: the object name in lowercase with underscore separators
399 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
400 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
401 * separators
402 *
403 * This is a variant of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable for
404 * the case of a non-abstract type, with interfaces, and with no requirement
405 * for a class struct.
406 */
407 #define OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(ModuleObjName, \
408 module_obj_name, \
409 MODULE_OBJ_NAME, \
410 PARENT_MODULE_OBJ_NAME, ...) \
411 DO_OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
412 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
413 false, 0, __VA_ARGS__)
414
415 /**
416 * OBJECT_DEFINE_SIMPLE_TYPE:
417 * @ModuleObjName: the object name with initial caps
418 * @module_obj_name: the object name in lowercase with underscore separators
419 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
420 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
421 * separators
422 *
423 * This is a variant of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable for
424 * the common case of a non-abstract type, without any interfaces, and with
425 * no requirement for a class struct. If you declared your type with
426 * OBJECT_DECLARE_SIMPLE_TYPE then this is probably the right choice for
427 * defining it.
428 */
429 #define OBJECT_DEFINE_SIMPLE_TYPE(ModuleObjName, module_obj_name, \
430 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \
431 OBJECT_DEFINE_SIMPLE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \
432 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, { NULL })
433
434 /**
435 * struct TypeInfo:
436 * @name: The name of the type.
437 * @parent: The name of the parent type.
438 * @instance_size: The size of the object (derivative of #Object). If
439 * @instance_size is 0, then the size of the object will be the size of the
440 * parent object.
441 * @instance_align: The required alignment of the object. If @instance_align
442 * is 0, then normal malloc alignment is sufficient; if non-zero, then we
443 * must use qemu_memalign for allocation.
444 * @instance_init: This function is called to initialize an object. The parent
445 * class will have already been initialized so the type is only responsible
446 * for initializing its own members.
447 * @instance_post_init: This function is called to finish initialization of
448 * an object, after all @instance_init functions were called, as well as
449 * @instance_post_init functions for the parent classes.
450 * @instance_finalize: This function is called during object destruction. This
451 * is called before the parent @instance_finalize function has been called.
452 * An object should only free the members that are unique to its type in this
453 * function.
454 * @abstract: If this field is true, then the class is considered abstract and
455 * cannot be directly instantiated.
456 * @class_size: The size of the class object (derivative of #ObjectClass)
457 * for this object. If @class_size is 0, then the size of the class will be
458 * assumed to be the size of the parent class. This allows a type to avoid
459 * implementing an explicit class type if they are not adding additional
460 * virtual functions.
461 * @class_init: This function is called after all parent class initialization
462 * has occurred to allow a class to set its default virtual method pointers.
463 * This is also the function to use to override virtual methods from a parent
464 * class.
465 * @class_base_init: This function is called for all base classes after all
466 * parent class initialization has occurred, but before the class itself
467 * is initialized. This is the function to use to undo the effects of
468 * memcpy from the parent class to the descendants.
469 * @class_data: Data to pass to the @class_init,
470 * @class_base_init. This can be useful when building dynamic
471 * classes.
472 * @interfaces: The list of interfaces associated with this type. This
473 * should point to a static array that's terminated with a zero filled
474 * element.
475 */
476 struct TypeInfo
477 {
478 const char *name;
479 const char *parent;
480
481 size_t instance_size;
482 size_t instance_align;
483 void (*instance_init)(Object *obj);
484 void (*instance_post_init)(Object *obj);
485 void (*instance_finalize)(Object *obj);
486
487 bool abstract;
488 size_t class_size;
489
490 void (*class_init)(ObjectClass *klass, const void *data);
491 void (*class_base_init)(ObjectClass *klass, const void *data);
492 const void *class_data;
493
494 const InterfaceInfo *interfaces;
495 };
496
497 /**
498 * OBJECT:
499 * @obj: A derivative of #Object
500 *
501 * Converts an object to a #Object. Since all objects are #Objects,
502 * this function will always succeed.
503 */
504 #define OBJECT(obj) \
505 ((Object *)(obj))
506
507 /**
508 * OBJECT_CLASS:
509 * @class: A derivative of #ObjectClass.
510 *
511 * Converts a class to an #ObjectClass. Since all objects are #Objects,
512 * this function will always succeed.
513 */
514 #define OBJECT_CLASS(class) \
515 ((ObjectClass *)(class))
516
517 /**
518 * OBJECT_CHECK:
519 * @type: The C type to use for the return value.
520 * @obj: A derivative of @type to cast.
521 * @name: The QOM typename of @type
522 *
523 * A type safe version of @object_dynamic_cast_assert. Typically each class
524 * will define a macro based on this type to perform type safe dynamic_casts to
525 * this object type.
526 *
527 * If an invalid object is passed to this function, a run time assert will be
528 * generated.
529 */
530 #define OBJECT_CHECK(type, obj, name) \
531 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
532 __FILE__, __LINE__, __func__))
533
534 /**
535 * OBJECT_CLASS_CHECK:
536 * @class_type: The C type to use for the return value.
537 * @class: A derivative class of @class_type to cast.
538 * @name: the QOM typename of @class_type.
539 *
540 * A type safe version of @object_class_dynamic_cast_assert. This macro is
541 * typically wrapped by each type to perform type safe casts of a class to a
542 * specific class type.
543 */
544 #define OBJECT_CLASS_CHECK(class_type, class, name) \
545 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
546 __FILE__, __LINE__, __func__))
547
548 /**
549 * OBJECT_GET_CLASS:
550 * @class: The C type to use for the return value.
551 * @obj: The object to obtain the class for.
552 * @name: The QOM typename of @obj.
553 *
554 * This function will return a specific class for a given object. Its generally
555 * used by each type to provide a type safe macro to get a specific class type
556 * from an object.
557 */
558 #define OBJECT_GET_CLASS(class, obj, name) \
559 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
560
561 /**
562 * struct InterfaceInfo:
563 * @type: The name of the interface.
564 *
565 * The information associated with an interface.
566 */
567 struct InterfaceInfo {
568 const char *type;
569 };
570
571 /**
572 * struct InterfaceClass:
573 * @parent_class: the base class
574 *
575 * The class for all interfaces. Subclasses of this class should only add
576 * virtual methods.
577 *
578 * Note that most of the fields of ObjectClass are unused (all except
579 * "type", in fact). They are only present in InterfaceClass to allow
580 * @object_class_dynamic_cast to work with both regular classes and interfaces.
581 */
582 struct InterfaceClass
583 {
584 ObjectClass parent_class;
585 /* private: */
586 Type interface_type;
587 };
588
589 #define TYPE_INTERFACE "interface"
590
591 /**
592 * INTERFACE_CLASS:
593 * @klass: class to cast from
594 * Returns: An #InterfaceClass or raise an error if cast is invalid
595 */
596 #define INTERFACE_CLASS(klass) \
597 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
598
599 /**
600 * INTERFACE_CHECK:
601 * @interface: the type to return
602 * @obj: the object to convert to an interface
603 * @name: the interface type name
604 *
605 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
606 */
607 #define INTERFACE_CHECK(interface, obj, name) \
608 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
609 __FILE__, __LINE__, __func__))
610
611 /**
612 * object_new_with_class:
613 * @klass: The class to instantiate.
614 *
615 * This function will initialize a new object using heap allocated memory.
616 * The returned object has a reference count of 1, and will be freed when
617 * the last reference is dropped.
618 *
619 * Returns: The newly allocated and instantiated object.
620 */
621 Object *object_new_with_class(ObjectClass *klass);
622
623 /**
624 * object_new:
625 * @typename: The name of the type of the object to instantiate.
626 *
627 * This function will initialize a new object using heap allocated memory.
628 * The returned object has a reference count of 1, and will be freed when
629 * the last reference is dropped.
630 *
631 * Returns: The newly allocated and instantiated object.
632 */
633 Object *object_new(const char *typename);
634
635 /**
636 * object_new_with_props:
637 * @typename: The name of the type of the object to instantiate.
638 * @parent: the parent object
639 * @id: The unique ID of the object
640 * @errp: pointer to error object
641 * @...: list of property names and values
642 *
643 * This function will initialize a new object using heap allocated memory.
644 * The returned object has a reference count of 1, and will be freed when
645 * the last reference is dropped.
646 *
647 * The @id parameter will be used when registering the object as a
648 * child of @parent in the composition tree.
649 *
650 * The variadic parameters are a list of pairs of (propname, propvalue)
651 * strings. The propname of %NULL indicates the end of the property
652 * list. If the object implements the user creatable interface, the
653 * object will be marked complete once all the properties have been
654 * processed.
655 *
656 * .. code-block:: c
657 * :caption: Creating an object with properties
658 *
659 * Error *err = NULL;
660 * Object *obj;
661 *
662 * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE,
663 * object_get_objects_root(),
664 * "hostmem0",
665 * &err,
666 * "share", "yes",
667 * "mem-path", "/dev/shm/somefile",
668 * "prealloc", "yes",
669 * "size", "1048576",
670 * NULL);
671 *
672 * if (!obj) {
673 * error_reportf_err(err, "Cannot create memory backend: ");
674 * }
675 *
676 * The returned object will have one stable reference maintained
677 * for as long as it is present in the object hierarchy.
678 *
679 * Returns: The newly allocated, instantiated & initialized object.
680 */
681 Object *object_new_with_props(const char *typename,
682 Object *parent,
683 const char *id,
684 Error **errp,
685 ...) G_GNUC_NULL_TERMINATED;
686
687 /**
688 * object_new_with_propv:
689 * @typename: The name of the type of the object to instantiate.
690 * @parent: the parent object
691 * @id: The unique ID of the object
692 * @errp: pointer to error object
693 * @vargs: list of property names and values
694 *
695 * See object_new_with_props() for documentation.
696 */
697 Object *object_new_with_propv(const char *typename,
698 Object *parent,
699 const char *id,
700 Error **errp,
701 va_list vargs);
702
703 bool object_apply_global_props(Object *obj, const GPtrArray *props,
704 Error **errp);
705 void object_set_machine_compat_props(GPtrArray *compat_props);
706 void object_set_accelerator_compat_props(GPtrArray *compat_props);
707 void object_register_sugar_prop(const char *driver, const char *prop,
708 const char *value, bool optional);
709 void object_apply_compat_props(Object *obj);
710
711 /**
712 * object_set_props:
713 * @obj: the object instance to set properties on
714 * @errp: pointer to error object
715 * @...: list of property names and values
716 *
717 * This function will set a list of properties on an existing object
718 * instance.
719 *
720 * The variadic parameters are a list of pairs of (propname, propvalue)
721 * strings. The propname of %NULL indicates the end of the property
722 * list.
723 *
724 * .. code-block:: c
725 * :caption: Update an object's properties
726 *
727 * Error *err = NULL;
728 * Object *obj = ...get / create object...;
729 *
730 * if (!object_set_props(obj,
731 * &err,
732 * "share", "yes",
733 * "mem-path", "/dev/shm/somefile",
734 * "prealloc", "yes",
735 * "size", "1048576",
736 * NULL)) {
737 * error_reportf_err(err, "Cannot set properties: ");
738 * }
739 *
740 * The returned object will have one stable reference maintained
741 * for as long as it is present in the object hierarchy.
742 *
743 * Returns: %true on success, %false on error.
744 */
745 bool object_set_props(Object *obj, Error **errp, ...) G_GNUC_NULL_TERMINATED;
746
747 /**
748 * object_set_propv:
749 * @obj: the object instance to set properties on
750 * @errp: pointer to error object
751 * @vargs: list of property names and values
752 *
753 * See object_set_props() for documentation.
754 *
755 * Returns: %true on success, %false on error.
756 */
757 bool object_set_propv(Object *obj, Error **errp, va_list vargs);
758
759 /**
760 * object_initialize:
761 * @obj: A pointer to the memory to be used for the object.
762 * @size: The maximum size available at @obj for the object.
763 * @typename: The name of the type of the object to instantiate.
764 *
765 * This function will initialize an object. The memory for the object should
766 * have already been allocated. The returned object has a reference count of 1,
767 * and will be finalized when the last reference is dropped.
768 */
769 void object_initialize(void *obj, size_t size, const char *typename);
770
771 /**
772 * object_initialize_child_with_props:
773 * @parentobj: The parent object to add a property to
774 * @propname: The name of the property
775 * @childobj: A pointer to the memory to be used for the object.
776 * @size: The maximum size available at @childobj for the object.
777 * @type: The name of the type of the object to instantiate.
778 * @errp: If an error occurs, a pointer to an area to store the error
779 * @...: list of property names and values
780 *
781 * This function will initialize an object. The memory for the object should
782 * have already been allocated. The object will then be added as child property
783 * to a parent with object_property_add_child() function. The returned object
784 * has a reference count of 1 (for the "child<...>" property from the parent),
785 * so the object will be finalized automatically when the parent gets removed.
786 *
787 * The variadic parameters are a list of pairs of (propname, propvalue)
788 * strings. The propname of %NULL indicates the end of the property list.
789 * If the object implements the user creatable interface, the object will
790 * be marked complete once all the properties have been processed.
791 *
792 * Returns: %true on success, %false on failure.
793 */
794 bool object_initialize_child_with_props(Object *parentobj,
795 const char *propname,
796 void *childobj, size_t size, const char *type,
797 Error **errp, ...) G_GNUC_NULL_TERMINATED;
798
799 /**
800 * object_initialize_child_with_propsv:
801 * @parentobj: The parent object to add a property to
802 * @propname: The name of the property
803 * @childobj: A pointer to the memory to be used for the object.
804 * @size: The maximum size available at @childobj for the object.
805 * @type: The name of the type of the object to instantiate.
806 * @errp: If an error occurs, a pointer to an area to store the error
807 * @vargs: list of property names and values
808 *
809 * See object_initialize_child() for documentation.
810 *
811 * Returns: %true on success, %false on failure.
812 */
813 bool object_initialize_child_with_propsv(Object *parentobj,
814 const char *propname,
815 void *childobj, size_t size, const char *type,
816 Error **errp, va_list vargs);
817
818 /**
819 * object_initialize_child:
820 * @parent: The parent object to add a property to
821 * @propname: The name of the property
822 * @child: A precisely typed pointer to the memory to be used for the
823 * object.
824 * @type: The name of the type of the object to instantiate.
825 *
826 * This is like::
827 *
828 * object_initialize_child_with_props(parent, propname,
829 * child, sizeof(*child), type,
830 * &error_abort, NULL)
831 */
832 #define object_initialize_child(parent, propname, child, type) \
833 object_initialize_child_internal((parent), (propname), \
834 (child), sizeof(*(child)), (type))
835 void object_initialize_child_internal(Object *parent, const char *propname,
836 void *child, size_t size,
837 const char *type);
838
839 /**
840 * object_dynamic_cast:
841 * @obj: The object to cast.
842 * @typename: The @typename to cast to.
843 *
844 * This function will determine if @obj is-a @typename. @obj can refer to an
845 * object or an interface associated with an object.
846 *
847 * Returns: This function returns @obj on success or #NULL on failure.
848 */
849 Object *object_dynamic_cast(Object *obj, const char *typename);
850
851 /**
852 * object_dynamic_cast_assert:
853 * @obj: The object to cast.
854 * @typename: The @typename to cast to.
855 * @file: Source code file where function was called
856 * @line: Source code line where function was called
857 * @func: Name of function where this function was called
858 *
859 * See object_dynamic_cast() for a description of the parameters of this
860 * function. The only difference in behavior is that this function asserts
861 * instead of returning #NULL on failure if QOM cast debugging is enabled.
862 * This function is not meant to be called directly, but only through
863 * the wrapper macro OBJECT_CHECK.
864 */
865 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
866 const char *file, int line, const char *func);
867
868 /**
869 * object_get_class:
870 * @obj: A derivative of #Object
871 *
872 * Returns: The #ObjectClass of the type associated with @obj.
873 */
874 ObjectClass *object_get_class(Object *obj);
875
876 /**
877 * object_get_typename:
878 * @obj: A derivative of #Object.
879 *
880 * Returns: The QOM typename of @obj.
881 */
882 const char *object_get_typename(const Object *obj);
883
884 /**
885 * type_register_static:
886 * @info: The #TypeInfo of the new type.
887 *
888 * Returns: the new #Type.
889 */
890 Type type_register_static(const TypeInfo *info);
891
892 /**
893 * type_register_static_array:
894 * @infos: The array of the new type #TypeInfo structures.
895 * @nr_infos: number of entries in @infos
896 *
897 * @infos and all of the strings it points to should exist for the life time
898 * that the type is registered.
899 */
900 void type_register_static_array(const TypeInfo *infos, int nr_infos);
901
902 /**
903 * DEFINE_TYPES:
904 * @type_array: The array containing #TypeInfo structures to register
905 *
906 * @type_array should be static constant that exists for the life time
907 * that the type is registered.
908 */
909 #define DEFINE_TYPES(type_array) \
910 static void do_qemu_init_ ## type_array(void) \
911 { \
912 type_register_static_array(type_array, ARRAY_SIZE(type_array)); \
913 } \
914 type_init(do_qemu_init_ ## type_array)
915
916 /**
917 * type_print_class_properties:
918 * @type: a QOM class name
919 *
920 * Print the object's class properties to stdout or the monitor.
921 * Return whether an object was found.
922 */
923 bool type_print_class_properties(const char *type);
924
925 /**
926 * object_set_properties_from_keyval:
927 * @obj: a QOM object
928 * @qdict: a dictionary with the properties to be set
929 * @from_json: true if leaf values of @qdict are typed, false if they
930 * are strings
931 * @errp: pointer to error object
932 *
933 * For each key in the dictionary, parse the value string if needed,
934 * then set the corresponding property in @obj.
935 */
936 void object_set_properties_from_keyval(Object *obj, const QDict *qdict,
937 bool from_json, Error **errp);
938
939 /**
940 * object_class_dynamic_cast_assert:
941 * @klass: The #ObjectClass to attempt to cast.
942 * @typename: The QOM typename of the class to cast to.
943 * @file: Source code file where function was called
944 * @line: Source code line where function was called
945 * @func: Name of function where this function was called
946 *
947 * See object_class_dynamic_cast() for a description of the parameters
948 * of this function. The only difference in behavior is that this function
949 * asserts instead of returning #NULL on failure if QOM cast debugging is
950 * enabled. This function is not meant to be called directly, but only through
951 * the wrapper macro OBJECT_CLASS_CHECK.
952 */
953 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
954 const char *typename,
955 const char *file, int line,
956 const char *func);
957
958 /**
959 * object_class_dynamic_cast:
960 * @klass: The #ObjectClass to attempt to cast.
961 * @typename: The QOM typename of the class to cast to.
962 *
963 * Returns: If @typename is a class, this function returns @klass if
964 * @typename is a subtype of @klass, else returns #NULL.
965 *
966 * If @typename is an interface, this function returns the interface
967 * definition for @klass if @klass implements it unambiguously; #NULL
968 * is returned if @klass does not implement the interface or if multiple
969 * classes or interfaces on the hierarchy leading to @klass implement
970 * it. (FIXME: perhaps this can be detected at type definition time?)
971 */
972 ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
973 const char *typename);
974
975 /**
976 * object_class_get_parent:
977 * @klass: The class to obtain the parent for.
978 *
979 * Returns: The parent for @klass or %NULL if none.
980 */
981 ObjectClass *object_class_get_parent(ObjectClass *klass);
982
983 /**
984 * object_class_get_name:
985 * @klass: The class to obtain the QOM typename for.
986 *
987 * Returns: The QOM typename for @klass.
988 */
989 const char *object_class_get_name(ObjectClass *klass);
990
991 /**
992 * object_class_is_abstract:
993 * @klass: The class to obtain the abstractness for.
994 *
995 * Returns: %true if @klass is abstract, %false otherwise.
996 */
997 bool object_class_is_abstract(ObjectClass *klass);
998
999 /**
1000 * object_class_by_name:
1001 * @typename: The QOM typename to obtain the class for.
1002 *
1003 * Returns: The class for @typename or %NULL if not found.
1004 */
1005 ObjectClass *object_class_by_name(const char *typename);
1006
1007 /**
1008 * module_object_class_by_name:
1009 * @typename: The QOM typename to obtain the class for.
1010 *
1011 * For objects which might be provided by a module. Behaves like
1012 * object_class_by_name, but additionally tries to load the module
1013 * needed in case the class is not available.
1014 *
1015 * Returns: The class for @typename or %NULL if not found.
1016 */
1017 ObjectClass *module_object_class_by_name(const char *typename);
1018
1019 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1020 const char *implements_type, bool include_abstract,
1021 void *opaque);
1022
1023 /**
1024 * object_class_get_list:
1025 * @implements_type: The type to filter for, including its derivatives.
1026 * @include_abstract: Whether to include abstract classes.
1027 *
1028 * Returns: A singly-linked list of the classes in reverse hashtable order.
1029 */
1030 GSList *object_class_get_list(const char *implements_type,
1031 bool include_abstract);
1032
1033 /**
1034 * object_class_get_list_sorted:
1035 * @implements_type: The type to filter for, including its derivatives.
1036 * @include_abstract: Whether to include abstract classes.
1037 *
1038 * Returns: A singly-linked list of the classes in alphabetical
1039 * case-insensitive order.
1040 */
1041 GSList *object_class_get_list_sorted(const char *implements_type,
1042 bool include_abstract);
1043
1044 /**
1045 * object_ref:
1046 * @obj: the object
1047 *
1048 * Increase the reference count of a object. A object cannot be freed as long
1049 * as its reference count is greater than zero.
1050 * Returns: @obj
1051 */
1052 Object *object_ref(void *obj);
1053
1054 /**
1055 * object_unref:
1056 * @obj: the object
1057 *
1058 * Decrease the reference count of a object. A object cannot be freed as long
1059 * as its reference count is greater than zero.
1060 */
1061 void object_unref(void *obj);
1062
1063 /**
1064 * object_property_try_add:
1065 * @obj: the object to add a property to
1066 * @name: the name of the property. This can contain any character except for
1067 * a forward slash. In general, you should use hyphens '-' instead of
1068 * underscores '_' when naming properties.
1069 * @type: the type name of the property. This namespace is pretty loosely
1070 * defined. Sub namespaces are constructed by using a prefix and then
1071 * to angle brackets. For instance, the type 'virtio-net-pci' in the
1072 * 'link' namespace would be 'link<virtio-net-pci>'.
1073 * @get: The getter to be called to read a property. If this is NULL, then
1074 * the property cannot be read.
1075 * @set: the setter to be called to write a property. If this is NULL,
1076 * then the property cannot be written.
1077 * @release: called when the property is removed from the object. This is
1078 * meant to allow a property to free its opaque upon object
1079 * destruction. This may be NULL.
1080 * @opaque: an opaque pointer to pass to the callbacks for the property
1081 * @errp: pointer to error object
1082 *
1083 * Returns: The #ObjectProperty; this can be used to set the @resolve
1084 * callback for child and link properties.
1085 */
1086 ObjectProperty *object_property_try_add(Object *obj, const char *name,
1087 const char *type,
1088 ObjectPropertyAccessor *get,
1089 ObjectPropertyAccessor *set,
1090 ObjectPropertyRelease *release,
1091 void *opaque, Error **errp);
1092
1093 /**
1094 * object_property_add:
1095 * Same as object_property_try_add() with @errp hardcoded to
1096 * &error_abort.
1097 *
1098 * @obj: the object to add a property to
1099 * @name: the name of the property. This can contain any character except for
1100 * a forward slash. In general, you should use hyphens '-' instead of
1101 * underscores '_' when naming properties.
1102 * @type: the type name of the property. This namespace is pretty loosely
1103 * defined. Sub namespaces are constructed by using a prefix and then
1104 * to angle brackets. For instance, the type 'virtio-net-pci' in the
1105 * 'link' namespace would be 'link<virtio-net-pci>'.
1106 * @get: The getter to be called to read a property. If this is NULL, then
1107 * the property cannot be read.
1108 * @set: the setter to be called to write a property. If this is NULL,
1109 * then the property cannot be written.
1110 * @release: called when the property is removed from the object. This is
1111 * meant to allow a property to free its opaque upon object
1112 * destruction. This may be NULL.
1113 * @opaque: an opaque pointer to pass to the callbacks for the property
1114 */
1115 ObjectProperty *object_property_add(Object *obj, const char *name,
1116 const char *type,
1117 ObjectPropertyAccessor *get,
1118 ObjectPropertyAccessor *set,
1119 ObjectPropertyRelease *release,
1120 void *opaque);
1121
1122 void object_property_del(Object *obj, const char *name);
1123
1124 ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name,
1125 const char *type,
1126 ObjectPropertyAccessor *get,
1127 ObjectPropertyAccessor *set,
1128 ObjectPropertyRelease *release,
1129 void *opaque);
1130
1131 /**
1132 * object_property_set_default_bool:
1133 * @prop: the property to set
1134 * @value: the value to be written to the property
1135 *
1136 * Set the property default value.
1137 */
1138 void object_property_set_default_bool(ObjectProperty *prop, bool value);
1139
1140 /**
1141 * object_property_set_default_str:
1142 * @prop: the property to set
1143 * @value: the value to be written to the property
1144 *
1145 * Set the property default value.
1146 */
1147 void object_property_set_default_str(ObjectProperty *prop, const char *value);
1148
1149 /**
1150 * object_property_set_default_list:
1151 * @prop: the property to set
1152 *
1153 * Set the property default value to be an empty list.
1154 */
1155 void object_property_set_default_list(ObjectProperty *prop);
1156
1157 /**
1158 * object_property_set_default_int:
1159 * @prop: the property to set
1160 * @value: the value to be written to the property
1161 *
1162 * Set the property default value.
1163 */
1164 void object_property_set_default_int(ObjectProperty *prop, int64_t value);
1165
1166 /**
1167 * object_property_set_default_uint:
1168 * @prop: the property to set
1169 * @value: the value to be written to the property
1170 *
1171 * Set the property default value.
1172 */
1173 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value);
1174
1175 /**
1176 * object_property_find:
1177 * @obj: the object
1178 * @name: the name of the property
1179 *
1180 * Look up a property for an object.
1181 *
1182 * Return its #ObjectProperty if found, or NULL.
1183 */
1184 ObjectProperty *object_property_find(Object *obj, const char *name);
1185
1186 /**
1187 * object_property_find_err:
1188 * @obj: the object
1189 * @name: the name of the property
1190 * @errp: returns an error if this function fails
1191 *
1192 * Look up a property for an object.
1193 *
1194 * Return its #ObjectProperty if found, or NULL.
1195 */
1196 ObjectProperty *object_property_find_err(Object *obj,
1197 const char *name,
1198 Error **errp);
1199
1200 /**
1201 * object_class_property_find:
1202 * @klass: the object class
1203 * @name: the name of the property
1204 *
1205 * Look up a property for an object class.
1206 *
1207 * Return its #ObjectProperty if found, or NULL.
1208 */
1209 ObjectProperty *object_class_property_find(ObjectClass *klass,
1210 const char *name);
1211
1212 /**
1213 * object_class_property_find_err:
1214 * @klass: the object class
1215 * @name: the name of the property
1216 * @errp: returns an error if this function fails
1217 *
1218 * Look up a property for an object class.
1219 *
1220 * Return its #ObjectProperty if found, or NULL.
1221 */
1222 ObjectProperty *object_class_property_find_err(ObjectClass *klass,
1223 const char *name,
1224 Error **errp);
1225
1226 typedef struct ObjectPropertyIterator {
1227 ObjectClass *nextclass;
1228 GHashTableIter iter;
1229 } ObjectPropertyIterator;
1230
1231 /**
1232 * object_property_iter_init:
1233 * @iter: the iterator instance
1234 * @obj: the object
1235 *
1236 * Initializes an iterator for traversing all properties
1237 * registered against an object instance, its class and all parent classes.
1238 *
1239 * It is forbidden to modify the property list while iterating,
1240 * whether removing or adding properties.
1241 *
1242 * Typical usage pattern would be
1243 *
1244 * .. code-block:: c
1245 * :caption: Using object property iterators
1246 *
1247 * ObjectProperty *prop;
1248 * ObjectPropertyIterator iter;
1249 *
1250 * object_property_iter_init(&iter, obj);
1251 * while ((prop = object_property_iter_next(&iter))) {
1252 * ... do something with prop ...
1253 * }
1254 */
1255 void object_property_iter_init(ObjectPropertyIterator *iter,
1256 Object *obj);
1257
1258 /**
1259 * object_class_property_iter_init:
1260 * @iter: the iterator instance
1261 * @klass: the class
1262 *
1263 * Initializes an iterator for traversing all properties
1264 * registered against an object class and all parent classes.
1265 *
1266 * It is forbidden to modify the property list while iterating,
1267 * whether removing or adding properties.
1268 *
1269 * This can be used on abstract classes as it does not create a temporary
1270 * instance.
1271 */
1272 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1273 ObjectClass *klass);
1274
1275 /**
1276 * object_property_iter_next:
1277 * @iter: the iterator instance
1278 *
1279 * Return the next available property. If no further properties
1280 * are available, a %NULL value will be returned and the @iter
1281 * pointer should not be used again after this point without
1282 * re-initializing it.
1283 *
1284 * Returns: the next property, or %NULL when all properties
1285 * have been traversed.
1286 */
1287 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter);
1288
1289 void object_unparent(Object *obj);
1290
1291 /**
1292 * object_property_get:
1293 * @obj: the object
1294 * @name: the name of the property
1295 * @v: the visitor that will receive the property value. This should be an
1296 * Output visitor and the data will be written with @name as the name.
1297 * @errp: returns an error if this function fails
1298 *
1299 * Reads a property from a object.
1300 *
1301 * Returns: %true on success, %false on failure.
1302 */
1303 bool object_property_get(Object *obj, const char *name, Visitor *v,
1304 Error **errp);
1305
1306 /**
1307 * object_property_set_str:
1308 * @obj: the object
1309 * @name: the name of the property
1310 * @value: the value to be written to the property
1311 * @errp: returns an error if this function fails
1312 *
1313 * Writes a string value to a property.
1314 *
1315 * Returns: %true on success, %false on failure.
1316 */
1317 bool object_property_set_str(Object *obj, const char *name,
1318 const char *value, Error **errp);
1319
1320 /**
1321 * object_property_get_str:
1322 * @obj: the object
1323 * @name: the name of the property
1324 * @errp: returns an error if this function fails
1325 *
1326 * Returns: the value of the property, converted to a C string, or NULL if
1327 * an error occurs (including when the property value is not a string).
1328 * The caller should free the string.
1329 */
1330 char *object_property_get_str(Object *obj, const char *name,
1331 Error **errp);
1332
1333 /**
1334 * object_property_set_link:
1335 * @obj: the object
1336 * @name: the name of the property
1337 * @value: the value to be written to the property
1338 * @errp: returns an error if this function fails
1339 *
1340 * Writes an object's canonical path to a property.
1341 *
1342 * If the link property was created with
1343 * %OBJ_PROP_LINK_STRONG bit, the old target object is
1344 * unreferenced, and a reference is added to the new target object.
1345 *
1346 * Returns: %true on success, %false on failure.
1347 */
1348 bool object_property_set_link(Object *obj, const char *name,
1349 Object *value, Error **errp);
1350
1351 /**
1352 * object_property_get_link:
1353 * @obj: the object
1354 * @name: the name of the property
1355 * @errp: returns an error if this function fails
1356 *
1357 * Returns: the value of the property, resolved from a path to an Object,
1358 * or NULL if an error occurs (including when the property value is not a
1359 * string or not a valid object path).
1360 */
1361 Object *object_property_get_link(Object *obj, const char *name,
1362 Error **errp);
1363
1364 /**
1365 * object_property_set_bool:
1366 * @obj: the object
1367 * @name: the name of the property
1368 * @value: the value to be written to the property
1369 * @errp: returns an error if this function fails
1370 *
1371 * Writes a bool value to a property.
1372 *
1373 * Returns: %true on success, %false on failure.
1374 */
1375 bool object_property_set_bool(Object *obj, const char *name,
1376 bool value, Error **errp);
1377
1378 /**
1379 * object_property_get_bool:
1380 * @obj: the object
1381 * @name: the name of the property
1382 * @errp: returns an error if this function fails
1383 *
1384 * Returns: the value of the property, converted to a boolean, or false if
1385 * an error occurs (including when the property value is not a bool).
1386 */
1387 bool object_property_get_bool(Object *obj, const char *name,
1388 Error **errp);
1389
1390 /**
1391 * object_property_set_int:
1392 * @obj: the object
1393 * @name: the name of the property
1394 * @value: the value to be written to the property
1395 * @errp: returns an error if this function fails
1396 *
1397 * Writes an integer value to a property.
1398 *
1399 * Returns: %true on success, %false on failure.
1400 */
1401 bool object_property_set_int(Object *obj, const char *name,
1402 int64_t value, Error **errp);
1403
1404 /**
1405 * object_property_get_int:
1406 * @obj: the object
1407 * @name: the name of the property
1408 * @errp: returns an error if this function fails
1409 *
1410 * Returns: the value of the property, converted to an integer, or -1 if
1411 * an error occurs (including when the property value is not an integer).
1412 */
1413 int64_t object_property_get_int(Object *obj, const char *name,
1414 Error **errp);
1415
1416 /**
1417 * object_property_set_uint:
1418 * @obj: the object
1419 * @name: the name of the property
1420 * @value: the value to be written to the property
1421 * @errp: returns an error if this function fails
1422 *
1423 * Writes an unsigned integer value to a property.
1424 *
1425 * Returns: %true on success, %false on failure.
1426 */
1427 bool object_property_set_uint(Object *obj, const char *name,
1428 uint64_t value, Error **errp);
1429
1430 /**
1431 * object_property_get_uint:
1432 * @obj: the object
1433 * @name: the name of the property
1434 * @errp: returns an error if this function fails
1435 *
1436 * Returns: the value of the property, converted to an unsigned integer, or 0
1437 * an error occurs (including when the property value is not an integer).
1438 */
1439 uint64_t object_property_get_uint(Object *obj, const char *name,
1440 Error **errp);
1441
1442 /**
1443 * object_property_get_enum:
1444 * @obj: the object
1445 * @name: the name of the property
1446 * @typename: the name of the enum data type
1447 * @errp: returns an error if this function fails
1448 *
1449 * Returns: the value of the property, converted to an integer (which
1450 * can't be negative), or -1 on error (including when the property
1451 * value is not an enum).
1452 */
1453 int object_property_get_enum(Object *obj, const char *name,
1454 const char *typename, Error **errp);
1455
1456 /**
1457 * object_property_set:
1458 * @obj: the object
1459 * @name: the name of the property
1460 * @v: the visitor that will be used to write the property value. This should
1461 * be an Input visitor and the data will be first read with @name as the
1462 * name and then written as the property value.
1463 * @errp: returns an error if this function fails
1464 *
1465 * Writes a property to a object.
1466 *
1467 * Returns: %true on success, %false on failure.
1468 */
1469 bool object_property_set(Object *obj, const char *name, Visitor *v,
1470 Error **errp);
1471
1472 /**
1473 * object_property_parse:
1474 * @obj: the object
1475 * @name: the name of the property
1476 * @string: the string that will be used to parse the property value.
1477 * @errp: returns an error if this function fails
1478 *
1479 * Parses a string and writes the result into a property of an object.
1480 *
1481 * Returns: %true on success, %false on failure.
1482 */
1483 bool object_property_parse(Object *obj, const char *name,
1484 const char *string, Error **errp);
1485
1486 /**
1487 * object_property_print:
1488 * @obj: the object
1489 * @name: the name of the property
1490 * @human: if true, print for human consumption
1491 * @errp: returns an error if this function fails
1492 *
1493 * Returns a string representation of the value of the property. The
1494 * caller shall free the string.
1495 */
1496 char *object_property_print(Object *obj, const char *name, bool human,
1497 Error **errp);
1498
1499 /**
1500 * object_property_get_type:
1501 * @obj: the object
1502 * @name: the name of the property
1503 * @errp: returns an error if this function fails
1504 *
1505 * Returns: The type name of the property.
1506 */
1507 const char *object_property_get_type(Object *obj, const char *name,
1508 Error **errp);
1509
1510 /**
1511 * object_get_root:
1512 *
1513 * Returns: the root object of the composition tree
1514 */
1515 Object *object_get_root(void);
1516
1517 /**
1518 * object_get_container:
1519 * @name: the name of container to lookup
1520 *
1521 * Lookup a root level container.
1522 *
1523 * Returns: the container with @name.
1524 */
1525 Object *object_get_container(const char *name);
1526
1527
1528 /**
1529 * object_get_objects_root:
1530 *
1531 * Get the container object that holds user created
1532 * object instances. This is the object at path
1533 * "/objects"
1534 *
1535 * Returns: the user object container
1536 */
1537 Object *object_get_objects_root(void);
1538
1539 /**
1540 * object_get_internal_root:
1541 *
1542 * Get the container object that holds internally used object
1543 * instances. Any object which is put into this container must not be
1544 * user visible, and it will not be exposed in the QOM tree.
1545 *
1546 * Returns: the internal object container
1547 */
1548 Object *object_get_internal_root(void);
1549
1550 /**
1551 * object_get_canonical_path_component:
1552 * @obj: the object
1553 *
1554 * Returns: The final component in the object's canonical path. The canonical
1555 * path is the path within the composition tree starting from the root.
1556 * %NULL if the object doesn't have a parent (and thus a canonical path).
1557 */
1558 const char *object_get_canonical_path_component(const Object *obj);
1559
1560 /**
1561 * object_get_canonical_path:
1562 * @obj: the object
1563 *
1564 * Returns: The canonical path for a object, newly allocated. This is
1565 * the path within the composition tree starting from the root. Use
1566 * g_free() to free it.
1567 */
1568 char *object_get_canonical_path(const Object *obj);
1569
1570 /**
1571 * object_resolve_path:
1572 * @path: the path to resolve
1573 * @ambiguous: (out) (optional): location to store whether the lookup failed
1574 * because it was ambiguous, or %NULL. Set to %false on success.
1575 *
1576 * There are two types of supported paths--absolute paths and partial paths.
1577 *
1578 * Absolute paths are derived from the root object and can follow child<> or
1579 * link<> properties. Since they can follow link<> properties, they can be
1580 * arbitrarily long. Absolute paths look like absolute filenames and are
1581 * prefixed with a leading slash.
1582 *
1583 * Partial paths look like relative filenames. They do not begin with a
1584 * prefix. The matching rules for partial paths are subtle but designed to make
1585 * specifying objects easy. At each level of the composition tree, the partial
1586 * path is matched as an absolute path. The first match is not returned. At
1587 * least two matches are searched for. A successful result is only returned if
1588 * only one match is found. If more than one match is found, a flag is
1589 * returned to indicate that the match was ambiguous.
1590 *
1591 * Returns: The matched object or %NULL on path lookup failure.
1592 */
1593 Object *object_resolve_path(const char *path, bool *ambiguous);
1594
1595 /**
1596 * object_resolve_path_type:
1597 * @path: the path to resolve
1598 * @typename: the type to look for.
1599 * @ambiguous: (out) (optional): location to store whether the lookup failed
1600 * because it was ambiguous, or %NULL. Set to %false on success.
1601 *
1602 * This is similar to object_resolve_path(). However, when looking for a
1603 * partial path only matches that implement the given type are considered.
1604 * This restricts the search and avoids spuriously flagging matches as
1605 * ambiguous.
1606 *
1607 * For both partial and absolute paths, the return value goes through
1608 * a dynamic cast to @typename. This is important if either the link,
1609 * or the typename itself are of interface types.
1610 *
1611 * Returns: The matched object or NULL on path lookup failure.
1612 */
1613 Object *object_resolve_path_type(const char *path, const char *typename,
1614 bool *ambiguous);
1615
1616 /**
1617 * object_resolve_type_unambiguous:
1618 * @typename: the type to look for
1619 * @errp: pointer to error object
1620 *
1621 * Return the only object in the QOM tree of type @typename.
1622 * If no match or more than one match is found, an error is
1623 * returned.
1624 *
1625 * Returns: The matched object or NULL on path lookup failure.
1626 */
1627 Object *object_resolve_type_unambiguous(const char *typename, Error **errp);
1628
1629 /**
1630 * object_resolve_path_at:
1631 * @parent: the object in which to resolve the path
1632 * @path: the path to resolve
1633 *
1634 * This is like object_resolve_path(), except paths not starting with
1635 * a slash are relative to @parent.
1636 *
1637 * Returns: The resolved object or NULL on path lookup failure.
1638 */
1639 Object *object_resolve_path_at(Object *parent, const char *path);
1640
1641 /**
1642 * object_resolve_path_component:
1643 * @parent: the object in which to resolve the path
1644 * @part: the component to resolve.
1645 *
1646 * This is similar to object_resolve_path with an absolute path, but it
1647 * only resolves one element (@part) and takes the others from @parent.
1648 *
1649 * Returns: The resolved object or NULL on path lookup failure.
1650 */
1651 Object *object_resolve_path_component(Object *parent, const char *part);
1652
1653 /**
1654 * object_property_try_add_child:
1655 * @obj: the object to add a property to
1656 * @name: the name of the property
1657 * @child: the child object
1658 * @errp: pointer to error object
1659 *
1660 * Child properties form the composition tree. All objects need to be a child
1661 * of another object. Objects can only be a child of one object.
1662 *
1663 * There is no way for a child to determine what its parent is. It is not
1664 * a bidirectional relationship. This is by design.
1665 *
1666 * The value of a child property as a C string will be the child object's
1667 * canonical path. It can be retrieved using object_property_get_str().
1668 * The child object itself can be retrieved using object_property_get_link().
1669 *
1670 * Returns: The newly added property on success, or %NULL on failure.
1671 */
1672 ObjectProperty *object_property_try_add_child(Object *obj, const char *name,
1673 Object *child, Error **errp);
1674
1675 /**
1676 * object_property_add_child:
1677 * @obj: the object to add a property to
1678 * @name: the name of the property
1679 * @child: the child object
1680 *
1681 * Same as object_property_try_add_child() with @errp hardcoded to
1682 * &error_abort
1683 */
1684 ObjectProperty *object_property_add_child(Object *obj, const char *name,
1685 Object *child);
1686
1687 typedef enum {
1688 /* Unref the link pointer when the property is deleted */
1689 OBJ_PROP_LINK_STRONG = 0x1,
1690
1691 /* private */
1692 OBJ_PROP_LINK_DIRECT = 0x2,
1693 OBJ_PROP_LINK_CLASS = 0x4,
1694 } ObjectPropertyLinkFlags;
1695
1696 /**
1697 * object_property_allow_set_link:
1698 * @obj: the object to add a property to
1699 * @name: the name of the property
1700 * @child: the child object
1701 * @errp: pointer to error object
1702 *
1703 * The default implementation of the object_property_add_link() check()
1704 * callback function. It allows the link property to be set and never returns
1705 * an error.
1706 */
1707 void object_property_allow_set_link(const Object *obj, const char *name,
1708 Object *child, Error **errp);
1709
1710 /**
1711 * object_property_add_link:
1712 * @obj: the object to add a property to
1713 * @name: the name of the property
1714 * @type: the qobj type of the link
1715 * @targetp: a pointer to where the link object reference is stored
1716 * @check: callback to veto setting or NULL if the property is read-only
1717 * @flags: additional options for the link
1718 *
1719 * Links establish relationships between objects. Links are unidirectional
1720 * although two links can be combined to form a bidirectional relationship
1721 * between objects.
1722 *
1723 * Links form the graph in the object model.
1724 *
1725 * The @check() callback is invoked when
1726 * object_property_set_link() is called and can raise an error to prevent the
1727 * link being set. If @check is NULL, the property is read-only
1728 * and cannot be set.
1729 *
1730 * Ownership of the pointer that @child points to is transferred to the
1731 * link property. The reference count for *@child is
1732 * managed by the property from after the function returns till the
1733 * property is deleted with object_property_del(). If the
1734 * @flags %OBJ_PROP_LINK_STRONG bit is set,
1735 * the reference count is decremented when the property is deleted or
1736 * modified.
1737 *
1738 * Returns: The newly added property on success, or %NULL on failure.
1739 */
1740 ObjectProperty *object_property_add_link(Object *obj, const char *name,
1741 const char *type, Object **targetp,
1742 void (*check)(const Object *obj, const char *name,
1743 Object *val, Error **errp),
1744 ObjectPropertyLinkFlags flags);
1745
1746 ObjectProperty *object_class_property_add_link(ObjectClass *oc,
1747 const char *name,
1748 const char *type, ptrdiff_t offset,
1749 void (*check)(const Object *obj, const char *name,
1750 Object *val, Error **errp),
1751 ObjectPropertyLinkFlags flags);
1752
1753 /**
1754 * object_property_add_str:
1755 * @obj: the object to add a property to
1756 * @name: the name of the property
1757 * @get: the getter or NULL if the property is write-only. This function must
1758 * return a string to be freed by g_free().
1759 * @set: the setter or NULL if the property is read-only
1760 *
1761 * Add a string property using getters/setters. This function will add a
1762 * property of type 'string'.
1763 *
1764 * Returns: The newly added property on success, or %NULL on failure.
1765 */
1766 ObjectProperty *object_property_add_str(Object *obj, const char *name,
1767 char *(*get)(Object *, Error **),
1768 void (*set)(Object *, const char *, Error **));
1769
1770 ObjectProperty *object_class_property_add_str(ObjectClass *klass,
1771 const char *name,
1772 char *(*get)(Object *, Error **),
1773 void (*set)(Object *, const char *,
1774 Error **));
1775
1776 /**
1777 * object_property_add_bool:
1778 * @obj: the object to add a property to
1779 * @name: the name of the property
1780 * @get: the getter or NULL if the property is write-only.
1781 * @set: the setter or NULL if the property is read-only
1782 *
1783 * Add a bool property using getters/setters. This function will add a
1784 * property of type 'bool'.
1785 *
1786 * Returns: The newly added property on success, or %NULL on failure.
1787 */
1788 ObjectProperty *object_property_add_bool(Object *obj, const char *name,
1789 bool (*get)(Object *, Error **),
1790 void (*set)(Object *, bool, Error **));
1791
1792 ObjectProperty *object_class_property_add_bool(ObjectClass *klass,
1793 const char *name,
1794 bool (*get)(Object *, Error **),
1795 void (*set)(Object *, bool, Error **));
1796
1797 /**
1798 * object_property_add_enum:
1799 * @obj: the object to add a property to
1800 * @name: the name of the property
1801 * @typename: the name of the enum data type
1802 * @lookup: enum value namelookup table
1803 * @get: the getter or %NULL if the property is write-only.
1804 * @set: the setter or %NULL if the property is read-only
1805 *
1806 * Add an enum property using getters/setters. This function will add a
1807 * property of type '@typename'.
1808 *
1809 * Returns: The newly added property on success, or %NULL on failure.
1810 */
1811 ObjectProperty *object_property_add_enum(Object *obj, const char *name,
1812 const char *typename,
1813 const QEnumLookup *lookup,
1814 int (*get)(Object *, Error **),
1815 void (*set)(Object *, int, Error **));
1816
1817 ObjectProperty *object_class_property_add_enum(ObjectClass *klass,
1818 const char *name,
1819 const char *typename,
1820 const QEnumLookup *lookup,
1821 int (*get)(Object *, Error **),
1822 void (*set)(Object *, int, Error **));
1823
1824 /**
1825 * object_property_add_tm:
1826 * @obj: the object to add a property to
1827 * @name: the name of the property
1828 * @get: the getter or NULL if the property is write-only.
1829 *
1830 * Add a read-only struct tm valued property using a getter function.
1831 * This function will add a property of type 'struct tm'.
1832 *
1833 * Returns: The newly added property on success, or %NULL on failure.
1834 */
1835 ObjectProperty *object_property_add_tm(Object *obj, const char *name,
1836 void (*get)(Object *, struct tm *, Error **));
1837
1838 ObjectProperty *object_class_property_add_tm(ObjectClass *klass,
1839 const char *name,
1840 void (*get)(Object *, struct tm *, Error **));
1841
1842 typedef enum {
1843 /* Automatically add a getter to the property */
1844 OBJ_PROP_FLAG_READ = 1 << 0,
1845 /* Automatically add a setter to the property */
1846 OBJ_PROP_FLAG_WRITE = 1 << 1,
1847 /* Automatically add a getter and a setter to the property */
1848 OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE),
1849 } ObjectPropertyFlags;
1850
1851 /**
1852 * object_property_add_uint8_ptr:
1853 * @obj: the object to add a property to
1854 * @name: the name of the property
1855 * @v: pointer to value
1856 * @flags: bitwise-or'd ObjectPropertyFlags
1857 *
1858 * Add an integer property in memory. This function will add a
1859 * property of type 'uint8'.
1860 *
1861 * Returns: The newly added property on success, or %NULL on failure.
1862 */
1863 ObjectProperty *object_property_add_uint8_ptr(Object *obj, const char *name,
1864 const uint8_t *v,
1865 ObjectPropertyFlags flags);
1866
1867 ObjectProperty *object_class_property_add_uint8_ptr(ObjectClass *klass,
1868 const char *name,
1869 const uint8_t *v,
1870 ObjectPropertyFlags flags);
1871
1872 /**
1873 * object_property_add_uint16_ptr:
1874 * @obj: the object to add a property to
1875 * @name: the name of the property
1876 * @v: pointer to value
1877 * @flags: bitwise-or'd ObjectPropertyFlags
1878 *
1879 * Add an integer property in memory. This function will add a
1880 * property of type 'uint16'.
1881 *
1882 * Returns: The newly added property on success, or %NULL on failure.
1883 */
1884 ObjectProperty *object_property_add_uint16_ptr(Object *obj, const char *name,
1885 const uint16_t *v,
1886 ObjectPropertyFlags flags);
1887
1888 ObjectProperty *object_class_property_add_uint16_ptr(ObjectClass *klass,
1889 const char *name,
1890 const uint16_t *v,
1891 ObjectPropertyFlags flags);
1892
1893 /**
1894 * object_property_add_uint32_ptr:
1895 * @obj: the object to add a property to
1896 * @name: the name of the property
1897 * @v: pointer to value
1898 * @flags: bitwise-or'd ObjectPropertyFlags
1899 *
1900 * Add an integer property in memory. This function will add a
1901 * property of type 'uint32'.
1902 *
1903 * Returns: The newly added property on success, or %NULL on failure.
1904 */
1905 ObjectProperty *object_property_add_uint32_ptr(Object *obj, const char *name,
1906 const uint32_t *v,
1907 ObjectPropertyFlags flags);
1908
1909 ObjectProperty *object_class_property_add_uint32_ptr(ObjectClass *klass,
1910 const char *name,
1911 const uint32_t *v,
1912 ObjectPropertyFlags flags);
1913
1914 /**
1915 * object_property_add_uint64_ptr:
1916 * @obj: the object to add a property to
1917 * @name: the name of the property
1918 * @v: pointer to value
1919 * @flags: bitwise-or'd ObjectPropertyFlags
1920 *
1921 * Add an integer property in memory. This function will add a
1922 * property of type 'uint64'.
1923 *
1924 * Returns: The newly added property on success, or %NULL on failure.
1925 */
1926 ObjectProperty *object_property_add_uint64_ptr(Object *obj, const char *name,
1927 const uint64_t *v,
1928 ObjectPropertyFlags flags);
1929
1930 ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass,
1931 const char *name,
1932 const uint64_t *v,
1933 ObjectPropertyFlags flags);
1934
1935 /**
1936 * object_property_add_alias:
1937 * @obj: the object to add a property to
1938 * @name: the name of the property
1939 * @target_obj: the object to forward property access to
1940 * @target_name: the name of the property on the forwarded object
1941 *
1942 * Add an alias for a property on an object. This function will add a property
1943 * of the same type as the forwarded property.
1944 *
1945 * The caller must ensure that @target_obj stays alive as long as
1946 * this property exists. In the case of a child object or an alias on the same
1947 * object this will be the case. For aliases to other objects the caller is
1948 * responsible for taking a reference.
1949 *
1950 * Returns: The newly added property on success, or %NULL on failure.
1951 */
1952 ObjectProperty *object_property_add_alias(Object *obj, const char *name,
1953 Object *target_obj, const char *target_name);
1954
1955 /**
1956 * object_property_add_const_link:
1957 * @obj: the object to add a property to
1958 * @name: the name of the property
1959 * @target: the object to be referred by the link
1960 *
1961 * Add an unmodifiable link for a property on an object. This function will
1962 * add a property of type link<TYPE> where TYPE is the type of @target.
1963 *
1964 * The caller must ensure that @target stays alive as long as
1965 * this property exists. In the case @target is a child of @obj,
1966 * this will be the case. Otherwise, the caller is responsible for
1967 * taking a reference.
1968 *
1969 * Returns: The newly added property on success, or %NULL on failure.
1970 */
1971 ObjectProperty *object_property_add_const_link(Object *obj, const char *name,
1972 Object *target);
1973
1974 /**
1975 * object_property_set_description:
1976 * @obj: the object owning the property
1977 * @name: the name of the property
1978 * @description: the description of the property on the object
1979 *
1980 * Set an object property's description.
1981 *
1982 * Returns: %true on success, %false on failure.
1983 */
1984 void object_property_set_description(Object *obj, const char *name,
1985 const char *description);
1986 void object_class_property_set_description(ObjectClass *klass, const char *name,
1987 const char *description);
1988
1989 /**
1990 * object_child_foreach:
1991 * @obj: the object whose children will be navigated
1992 * @fn: the iterator function to be called
1993 * @opaque: an opaque value that will be passed to the iterator
1994 *
1995 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1996 * non-zero.
1997 *
1998 * It is forbidden to add or remove children from @obj from the @fn
1999 * callback.
2000 *
2001 * Returns: The last value returned by @fn, or 0 if there is no child.
2002 */
2003 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
2004 void *opaque);
2005
2006 /**
2007 * object_child_foreach_recursive:
2008 * @obj: the object whose children will be navigated
2009 * @fn: the iterator function to be called
2010 * @opaque: an opaque value that will be passed to the iterator
2011 *
2012 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
2013 * non-zero. Calls recursively, all child nodes of @obj will also be passed
2014 * all the way down to the leaf nodes of the tree. Depth first ordering.
2015 *
2016 * It is forbidden to add or remove children from @obj (or its
2017 * child nodes) from the @fn callback.
2018 *
2019 * Returns: The last value returned by @fn, or 0 if there is no child.
2020 */
2021 int object_child_foreach_recursive(Object *obj,
2022 int (*fn)(Object *child, void *opaque),
2023 void *opaque);
2024
2025 /**
2026 * object_property_add_new_container:
2027 * @obj: the parent object
2028 * @name: the name of the parent object's property to add
2029 *
2030 * Add a newly created container object to a parent object.
2031 *
2032 * Returns: the newly created container object. Its reference count is 1,
2033 * and the reference is owned by the parent object.
2034 */
2035 Object *object_property_add_new_container(Object *obj, const char *name);
2036
2037 /**
2038 * object_property_help:
2039 * @name: the name of the property
2040 * @type: the type of the property
2041 * @defval: the default value
2042 * @description: description of the property
2043 *
2044 * Returns: a user-friendly formatted string describing the property
2045 * for help purposes.
2046 */
2047 char *object_property_help(const char *name, const char *type,
2048 QObject *defval, const char *description);
2049
2050 G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref)
2051
2052 #endif