4 * Copyright IBM, Corp. 2011
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.
17 #include "qapi/qapi-builtin-types.h"
18 #include "qemu/module.h"
21 typedef struct TypeImpl *Type;
23 typedef struct TypeInfo TypeInfo;
25 typedef struct InterfaceClass InterfaceClass;
26 typedef struct InterfaceInfo InterfaceInfo;
28 #define TYPE_OBJECT "object"
32 * @title:Base Object Type System
33 * @short_description: interfaces for creating new types and objects
35 * The QEMU Object Model provides a framework for registering user creatable
36 * types and instantiating objects from those types. QOM provides the following
39 * - System for dynamically registering types
40 * - Support for single-inheritance of types
41 * - Multiple inheritance of stateless interfaces
44 * <title>Creating a minimal type</title>
48 * #define TYPE_MY_DEVICE "my-device"
50 * // No new virtual functions: we can reuse the typedef for the
52 * typedef DeviceClass MyDeviceClass;
53 * typedef struct MyDevice
57 * int reg0, reg1, reg2;
60 * static const TypeInfo my_device_info = {
61 * .name = TYPE_MY_DEVICE,
62 * .parent = TYPE_DEVICE,
63 * .instance_size = sizeof(MyDevice),
66 * static void my_device_register_types(void)
68 * type_register_static(&my_device_info);
71 * type_init(my_device_register_types)
75 * In the above example, we create a simple type that is described by #TypeInfo.
76 * #TypeInfo describes information about the type including what it inherits
77 * from, the instance and class size, and constructor/destructor hooks.
79 * Alternatively several static types could be registered using helper macro
84 * static const TypeInfo device_types_info[] = {
86 * .name = TYPE_MY_DEVICE_A,
87 * .parent = TYPE_DEVICE,
88 * .instance_size = sizeof(MyDeviceA),
91 * .name = TYPE_MY_DEVICE_B,
92 * .parent = TYPE_DEVICE,
93 * .instance_size = sizeof(MyDeviceB),
97 * DEFINE_TYPES(device_types_info)
101 * Every type has an #ObjectClass associated with it. #ObjectClass derivatives
102 * are instantiated dynamically but there is only ever one instance for any
103 * given type. The #ObjectClass typically holds a table of function pointers
104 * for the virtual methods implemented by this type.
106 * Using object_new(), a new #Object derivative will be instantiated. You can
107 * cast an #Object to a subclass (or base-class) type using
108 * object_dynamic_cast(). You typically want to define macro wrappers around
109 * OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
113 * <title>Typecasting macros</title>
115 * #define MY_DEVICE_GET_CLASS(obj) \
116 * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
117 * #define MY_DEVICE_CLASS(klass) \
118 * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
119 * #define MY_DEVICE(obj) \
120 * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
124 * # Class Initialization #
126 * Before an object is initialized, the class for the object must be
127 * initialized. There is only one class object for all instance objects
128 * that is created lazily.
130 * Classes are initialized by first initializing any parent classes (if
131 * necessary). After the parent class object has initialized, it will be
132 * copied into the current class object and any additional storage in the
133 * class object is zero filled.
135 * The effect of this is that classes automatically inherit any virtual
136 * function pointers that the parent class has already initialized. All
137 * other fields will be zero filled.
139 * Once all of the parent classes have been initialized, #TypeInfo::class_init
140 * is called to let the class being instantiated provide default initialize for
141 * its virtual functions. Here is how the above example might be modified
142 * to introduce an overridden virtual function:
145 * <title>Overriding a virtual function</title>
149 * void my_device_class_init(ObjectClass *klass, void *class_data)
151 * DeviceClass *dc = DEVICE_CLASS(klass);
152 * dc->reset = my_device_reset;
155 * static const TypeInfo my_device_info = {
156 * .name = TYPE_MY_DEVICE,
157 * .parent = TYPE_DEVICE,
158 * .instance_size = sizeof(MyDevice),
159 * .class_init = my_device_class_init,
164 * Introducing new virtual methods requires a class to define its own
165 * struct and to add a .class_size member to the #TypeInfo. Each method
166 * will also have a wrapper function to call it easily:
169 * <title>Defining an abstract class</title>
173 * typedef struct MyDeviceClass
175 * DeviceClass parent;
177 * void (*frobnicate) (MyDevice *obj);
180 * static const TypeInfo my_device_info = {
181 * .name = TYPE_MY_DEVICE,
182 * .parent = TYPE_DEVICE,
183 * .instance_size = sizeof(MyDevice),
184 * .abstract = true, // or set a default in my_device_class_init
185 * .class_size = sizeof(MyDeviceClass),
188 * void my_device_frobnicate(MyDevice *obj)
190 * MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
192 * klass->frobnicate(obj);
199 * Interfaces allow a limited form of multiple inheritance. Instances are
200 * similar to normal types except for the fact that are only defined by
201 * their classes and never carry any state. As a consequence, a pointer to
202 * an interface instance should always be of incomplete type in order to be
203 * sure it cannot be dereferenced. That is, you should define the
204 * 'typedef struct SomethingIf SomethingIf' so that you can pass around
205 * 'SomethingIf *si' arguments, but not define a 'struct SomethingIf { ... }'.
206 * The only things you can validly do with a 'SomethingIf *' are to pass it as
207 * an argument to a method on its corresponding SomethingIfClass, or to
208 * dynamically cast it to an object that implements the interface.
212 * A <emphasis>method</emphasis> is a function within the namespace scope of
213 * a class. It usually operates on the object instance by passing it as a
214 * strongly-typed first argument.
215 * If it does not operate on an object instance, it is dubbed
216 * <emphasis>class method</emphasis>.
218 * Methods cannot be overloaded. That is, the #ObjectClass and method name
219 * uniquely identity the function to be called; the signature does not vary
220 * except for trailing varargs.
222 * Methods are always <emphasis>virtual</emphasis>. Overriding a method in
223 * #TypeInfo.class_init of a subclass leads to any user of the class obtained
224 * via OBJECT_GET_CLASS() accessing the overridden function.
225 * The original function is not automatically invoked. It is the responsibility
226 * of the overriding class to determine whether and when to invoke the method
229 * To invoke the method being overridden, the preferred solution is to store
230 * the original value in the overriding class before overriding the method.
231 * This corresponds to |[ {super,base}.method(...) ]| in Java and C#
232 * respectively; this frees the overriding class from hardcoding its parent
233 * class, which someone might choose to change at some point.
236 * <title>Overriding a virtual method</title>
238 * typedef struct MyState MyState;
240 * typedef void (*MyDoSomething)(MyState *obj);
242 * typedef struct MyClass {
243 * ObjectClass parent_class;
245 * MyDoSomething do_something;
248 * static void my_do_something(MyState *obj)
253 * static void my_class_init(ObjectClass *oc, void *data)
255 * MyClass *mc = MY_CLASS(oc);
257 * mc->do_something = my_do_something;
260 * static const TypeInfo my_type_info = {
262 * .parent = TYPE_OBJECT,
263 * .instance_size = sizeof(MyState),
264 * .class_size = sizeof(MyClass),
265 * .class_init = my_class_init,
268 * typedef struct DerivedClass {
269 * MyClass parent_class;
271 * MyDoSomething parent_do_something;
274 * static void derived_do_something(MyState *obj)
276 * DerivedClass *dc = DERIVED_GET_CLASS(obj);
278 * // do something here
279 * dc->parent_do_something(obj);
280 * // do something else here
283 * static void derived_class_init(ObjectClass *oc, void *data)
285 * MyClass *mc = MY_CLASS(oc);
286 * DerivedClass *dc = DERIVED_CLASS(oc);
288 * dc->parent_do_something = mc->do_something;
289 * mc->do_something = derived_do_something;
292 * static const TypeInfo derived_type_info = {
293 * .name = TYPE_DERIVED,
295 * .class_size = sizeof(DerivedClass),
296 * .class_init = derived_class_init,
301 * Alternatively, object_class_by_name() can be used to obtain the class and
302 * its non-overridden methods for a specific type. This would correspond to
303 * |[ MyClass::method(...) ]| in C++.
305 * The first example of such a QOM method was #CPUClass.reset,
306 * another example is #DeviceClass.realize.
308 * # Standard type declaration and definition macros #
310 * A lot of the code outlined above follows a standard pattern and naming
311 * convention. To reduce the amount of boilerplate code that needs to be
312 * written for a new type there are two sets of macros to generate the
313 * common parts in a standard format.
315 * A type is declared using the OBJECT_DECLARE macro family. In types
316 * which do not require any virtual functions in the class, the
317 * OBJECT_DECLARE_SIMPLE_TYPE macro is suitable, and is commonly placed
318 * in the header file:
321 * <title>Declaring a simple type</title>
323 * OBJECT_DECLARE_SIMPLE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
327 * This is equivalent to the following:
330 * <title>Expansion from declaring a simple type</title>
332 * typedef struct MyDevice MyDevice;
333 * typedef struct MyDeviceClass MyDeviceClass;
335 * G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDeviceClass, object_unref)
337 * #define MY_DEVICE_GET_CLASS(void *obj) \
338 * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
339 * #define MY_DEVICE_CLASS(void *klass) \
340 * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
341 * #define MY_DEVICE(void *obj)
342 * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
344 * struct MyDeviceClass {
345 * DeviceClass parent_class;
350 * The 'struct MyDevice' needs to be declared separately.
351 * If the type requires virtual functions to be declared in the class
352 * struct, then the alternative OBJECT_DECLARE_TYPE() macro can be
353 * used. This does the same as OBJECT_DECLARE_SIMPLE_TYPE(), but without
354 * the 'struct MyDeviceClass' definition.
356 * To implement the type, the OBJECT_DEFINE macro family is available.
357 * In the simple case the OBJECT_DEFINE_TYPE macro is suitable:
360 * <title>Defining a simple type</title>
362 * OBJECT_DEFINE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
366 * This is equivalent to the following:
369 * <title>Expansion from defining a simple type</title>
371 * static void my_device_finalize(Object *obj);
372 * static void my_device_class_init(ObjectClass *oc, void *data);
373 * static void my_device_init(Object *obj);
375 * static const TypeInfo my_device_info = {
376 * .parent = TYPE_DEVICE,
377 * .name = TYPE_MY_DEVICE,
378 * .instance_size = sizeof(MyDevice),
379 * .instance_init = my_device_init,
380 * .instance_finalize = my_device_finalize,
381 * .class_size = sizeof(MyDeviceClass),
382 * .class_init = my_device_class_init,
386 * my_device_register_types(void)
388 * type_register_static(&my_device_info);
390 * type_init(my_device_register_types);
394 * This is sufficient to get the type registered with the type
395 * system, and the three standard methods now need to be implemented
396 * along with any other logic required for the type.
398 * If the type needs to implement one or more interfaces, then the
399 * OBJECT_DEFINE_TYPE_WITH_INTERFACES() macro can be used instead.
400 * This accepts an array of interface type names.
403 * <title>Defining a simple type implementing interfaces</title>
405 * OBJECT_DEFINE_TYPE_WITH_INTERFACES(MyDevice, my_device,
407 * { TYPE_USER_CREATABLE }, { NULL })
411 * If the type is not intended to be instantiated, then then
412 * the OBJECT_DEFINE_ABSTRACT_TYPE() macro can be used instead:
415 * <title>Defining a simple type</title>
417 * OBJECT_DEFINE_ABSTRACT_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
423 typedef struct ObjectProperty ObjectProperty;
426 * ObjectPropertyAccessor:
427 * @obj: the object that owns the property
428 * @v: the visitor that contains the property data
429 * @name: the name of the property
430 * @opaque: the object property opaque
431 * @errp: a pointer to an Error that is filled if getting/setting fails.
433 * Called when trying to get/set a property.
435 typedef void (ObjectPropertyAccessor)(Object *obj,
442 * ObjectPropertyResolve:
443 * @obj: the object that owns the property
444 * @opaque: the opaque registered with the property
445 * @part: the name of the property
447 * Resolves the #Object corresponding to property @part.
449 * The returned object can also be used as a starting point
450 * to resolve a relative path starting with "@part".
452 * Returns: If @path is the path that led to @obj, the function
453 * returns the #Object corresponding to "@path/@part".
454 * If "@path/@part" is not a valid object path, it returns #NULL.
456 typedef Object *(ObjectPropertyResolve)(Object *obj,
461 * ObjectPropertyRelease:
462 * @obj: the object that owns the property
463 * @name: the name of the property
464 * @opaque: the opaque registered with the property
466 * Called when a property is removed from a object.
468 typedef void (ObjectPropertyRelease)(Object *obj,
473 * ObjectPropertyInit:
474 * @obj: the object that owns the property
475 * @prop: the property to set
477 * Called when a property is initialized.
479 typedef void (ObjectPropertyInit)(Object *obj, ObjectProperty *prop);
481 struct ObjectProperty
486 ObjectPropertyAccessor *get;
487 ObjectPropertyAccessor *set;
488 ObjectPropertyResolve *resolve;
489 ObjectPropertyRelease *release;
490 ObjectPropertyInit *init;
497 * @obj: the object that is being removed from the composition tree
499 * Called when an object is being removed from the QOM composition tree.
500 * The function should remove any backlinks from children objects to @obj.
502 typedef void (ObjectUnparent)(Object *obj);
506 * @obj: the object being freed
508 * Called when an object's last reference is removed.
510 typedef void (ObjectFree)(void *obj);
512 #define OBJECT_CLASS_CAST_CACHE 4
517 * The base for all classes. The only thing that #ObjectClass contains is an
518 * integer type handle.
526 const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
527 const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
529 ObjectUnparent *unparent;
531 GHashTable *properties;
537 * The base for all objects. The first member of this object is a pointer to
538 * a #ObjectClass. Since C guarantees that the first member of a structure
539 * always begins at byte 0 of that structure, as long as any sub-object places
540 * its parent as the first member, we can cast directly to a #Object.
542 * As a result, #Object contains a reference to the objects type as its
543 * first member. This allows identification of the real type of the object at
551 GHashTable *properties;
557 * OBJECT_DECLARE_TYPE:
558 * @ModuleObjName: the object name with initial capitalization
559 * @module_obj_name: the object name in lowercase with underscore separators
560 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
562 * This macro is typically used in a header file, and will:
564 * - create the typedefs for the object and class structs
565 * - register the type for use with g_autoptr
566 * - provide three standard type cast functions
568 * The object struct and class struct need to be declared manually.
570 #define OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \
571 typedef struct ModuleObjName ModuleObjName; \
572 typedef struct ModuleObjName##Class ModuleObjName##Class; \
574 G_DEFINE_AUTOPTR_CLEANUP_FUNC(ModuleObjName, object_unref) \
576 static inline G_GNUC_UNUSED ModuleObjName##Class * \
577 MODULE_OBJ_NAME##_GET_CLASS(void *obj) \
578 { return OBJECT_GET_CLASS(ModuleObjName##Class, obj, \
579 TYPE_##MODULE_OBJ_NAME); } \
581 static inline G_GNUC_UNUSED ModuleObjName##Class * \
582 MODULE_OBJ_NAME##_CLASS(void *klass) \
583 { return OBJECT_CLASS_CHECK(ModuleObjName##Class, klass, \
584 TYPE_##MODULE_OBJ_NAME); } \
586 static inline G_GNUC_UNUSED ModuleObjName * \
587 MODULE_OBJ_NAME(void *obj) \
588 { return OBJECT_CHECK(ModuleObjName, obj, \
589 TYPE_##MODULE_OBJ_NAME); }
592 * OBJECT_DECLARE_SIMPLE_TYPE:
593 * @ModuleObjName: the object name with initial caps
594 * @module_obj_name: the object name in lowercase with underscore separators
595 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
596 * @ParentModuleObjName: the parent object name with initial caps
598 * This does the same as OBJECT_DECLARE_TYPE(), but also declares
599 * the class struct, thus only the object struct needs to be declare
602 * This macro should be used unless the class struct needs to have
603 * virtual methods declared.
605 #define OBJECT_DECLARE_SIMPLE_TYPE(ModuleObjName, module_obj_name, \
606 MODULE_OBJ_NAME, ParentModuleObjName) \
607 OBJECT_DECLARE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME) \
608 struct ModuleObjName##Class { ParentModuleObjName##Class parent_class; };
612 * OBJECT_DEFINE_TYPE_EXTENDED:
613 * @ModuleObjName: the object name with initial caps
614 * @module_obj_name: the object name in lowercase with underscore separators
615 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
616 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
618 * @ABSTRACT: boolean flag to indicate whether the object can be instantiated
619 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
621 * This macro is typically used in a source file, and will:
623 * - declare prototypes for _finalize, _class_init and _init methods
624 * - declare the TypeInfo struct instance
625 * - provide the constructor to register the type
627 * After using this macro, implementations of the _finalize, _class_init,
628 * and _init methods need to be written. Any of these can be zero-line
629 * no-op impls if no special logic is required for a given type.
631 * This macro should rarely be used, instead one of the more specialized
632 * macros is usually a better choice.
634 #define OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
635 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
638 module_obj_name##_finalize(Object *obj); \
640 module_obj_name##_class_init(ObjectClass *oc, void *data); \
642 module_obj_name##_init(Object *obj); \
644 static const TypeInfo module_obj_name##_info = { \
645 .parent = TYPE_##PARENT_MODULE_OBJ_NAME, \
646 .name = TYPE_##MODULE_OBJ_NAME, \
647 .instance_size = sizeof(ModuleObjName), \
648 .instance_init = module_obj_name##_init, \
649 .instance_finalize = module_obj_name##_finalize, \
650 .class_size = sizeof(ModuleObjName##Class), \
651 .class_init = module_obj_name##_class_init, \
652 .abstract = ABSTRACT, \
653 .interfaces = (InterfaceInfo[]) { __VA_ARGS__ } , \
657 module_obj_name##_register_types(void) \
659 type_register_static(&module_obj_name##_info); \
661 type_init(module_obj_name##_register_types);
664 * OBJECT_DEFINE_TYPE:
665 * @ModuleObjName: the object name with initial caps
666 * @module_obj_name: the object name in lowercase with underscore separators
667 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
668 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
671 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
672 * for the common case of a non-abstract type, without any interfaces.
674 #define OBJECT_DEFINE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME, \
675 PARENT_MODULE_OBJ_NAME) \
676 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
677 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
681 * OBJECT_DEFINE_TYPE_WITH_INTERFACES:
682 * @ModuleObjName: the object name with initial caps
683 * @module_obj_name: the object name in lowercase with underscore separators
684 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
685 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
687 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
689 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
690 * for the common case of a non-abstract type, with one or more implemented
693 * Note when passing the list of interfaces, be sure to include the final
694 * NULL entry, e.g. { TYPE_USER_CREATABLE }, { NULL }
696 #define OBJECT_DEFINE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \
698 PARENT_MODULE_OBJ_NAME, ...) \
699 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
700 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
704 * OBJECT_DEFINE_ABSTRACT_TYPE:
705 * @ModuleObjName: the object name with initial caps
706 * @module_obj_name: the object name in lowercase with underscore separators
707 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
708 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
711 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
712 * for defining an abstract type, without any interfaces.
714 #define OBJECT_DEFINE_ABSTRACT_TYPE(ModuleObjName, module_obj_name, \
715 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \
716 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
717 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
722 * @name: The name of the type.
723 * @parent: The name of the parent type.
724 * @instance_size: The size of the object (derivative of #Object). If
725 * @instance_size is 0, then the size of the object will be the size of the
727 * @instance_init: This function is called to initialize an object. The parent
728 * class will have already been initialized so the type is only responsible
729 * for initializing its own members.
730 * @instance_post_init: This function is called to finish initialization of
731 * an object, after all @instance_init functions were called.
732 * @instance_finalize: This function is called during object destruction. This
733 * is called before the parent @instance_finalize function has been called.
734 * An object should only free the members that are unique to its type in this
736 * @abstract: If this field is true, then the class is considered abstract and
737 * cannot be directly instantiated.
738 * @class_size: The size of the class object (derivative of #ObjectClass)
739 * for this object. If @class_size is 0, then the size of the class will be
740 * assumed to be the size of the parent class. This allows a type to avoid
741 * implementing an explicit class type if they are not adding additional
743 * @class_init: This function is called after all parent class initialization
744 * has occurred to allow a class to set its default virtual method pointers.
745 * This is also the function to use to override virtual methods from a parent
747 * @class_base_init: This function is called for all base classes after all
748 * parent class initialization has occurred, but before the class itself
749 * is initialized. This is the function to use to undo the effects of
750 * memcpy from the parent class to the descendants.
751 * @class_data: Data to pass to the @class_init,
752 * @class_base_init. This can be useful when building dynamic
754 * @interfaces: The list of interfaces associated with this type. This
755 * should point to a static array that's terminated with a zero filled
763 size_t instance_size;
764 void (*instance_init)(Object *obj);
765 void (*instance_post_init)(Object *obj);
766 void (*instance_finalize)(Object *obj);
771 void (*class_init)(ObjectClass *klass, void *data);
772 void (*class_base_init)(ObjectClass *klass, void *data);
775 InterfaceInfo *interfaces;
780 * @obj: A derivative of #Object
782 * Converts an object to a #Object. Since all objects are #Objects,
783 * this function will always succeed.
785 #define OBJECT(obj) \
790 * @class: A derivative of #ObjectClass.
792 * Converts a class to an #ObjectClass. Since all objects are #Objects,
793 * this function will always succeed.
795 #define OBJECT_CLASS(class) \
796 ((ObjectClass *)(class))
800 * @type: The C type to use for the return value.
801 * @obj: A derivative of @type to cast.
802 * @name: The QOM typename of @type
804 * A type safe version of @object_dynamic_cast_assert. Typically each class
805 * will define a macro based on this type to perform type safe dynamic_casts to
808 * If an invalid object is passed to this function, a run time assert will be
811 #define OBJECT_CHECK(type, obj, name) \
812 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
813 __FILE__, __LINE__, __func__))
816 * OBJECT_CLASS_CHECK:
817 * @class_type: The C type to use for the return value.
818 * @class: A derivative class of @class_type to cast.
819 * @name: the QOM typename of @class_type.
821 * A type safe version of @object_class_dynamic_cast_assert. This macro is
822 * typically wrapped by each type to perform type safe casts of a class to a
823 * specific class type.
825 #define OBJECT_CLASS_CHECK(class_type, class, name) \
826 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
827 __FILE__, __LINE__, __func__))
831 * @class: The C type to use for the return value.
832 * @obj: The object to obtain the class for.
833 * @name: The QOM typename of @obj.
835 * This function will return a specific class for a given object. Its generally
836 * used by each type to provide a type safe macro to get a specific class type
839 #define OBJECT_GET_CLASS(class, obj, name) \
840 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
844 * @type: The name of the interface.
846 * The information associated with an interface.
848 struct InterfaceInfo {
854 * @parent_class: the base class
856 * The class for all interfaces. Subclasses of this class should only add
859 struct InterfaceClass
861 ObjectClass parent_class;
863 ObjectClass *concrete_class;
867 #define TYPE_INTERFACE "interface"
871 * @klass: class to cast from
872 * Returns: An #InterfaceClass or raise an error if cast is invalid
874 #define INTERFACE_CLASS(klass) \
875 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
879 * @interface: the type to return
880 * @obj: the object to convert to an interface
881 * @name: the interface type name
883 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
885 #define INTERFACE_CHECK(interface, obj, name) \
886 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
887 __FILE__, __LINE__, __func__))
890 * object_new_with_class:
891 * @klass: The class to instantiate.
893 * This function will initialize a new object using heap allocated memory.
894 * The returned object has a reference count of 1, and will be freed when
895 * the last reference is dropped.
897 * Returns: The newly allocated and instantiated object.
899 Object *object_new_with_class(ObjectClass *klass);
903 * @typename: The name of the type of the object to instantiate.
905 * This function will initialize a new object using heap allocated memory.
906 * The returned object has a reference count of 1, and will be freed when
907 * the last reference is dropped.
909 * Returns: The newly allocated and instantiated object.
911 Object *object_new(const char *typename);
914 * object_new_with_props:
915 * @typename: The name of the type of the object to instantiate.
916 * @parent: the parent object
917 * @id: The unique ID of the object
918 * @errp: pointer to error object
919 * @...: list of property names and values
921 * This function will initialize a new object using heap allocated memory.
922 * The returned object has a reference count of 1, and will be freed when
923 * the last reference is dropped.
925 * The @id parameter will be used when registering the object as a
926 * child of @parent in the composition tree.
928 * The variadic parameters are a list of pairs of (propname, propvalue)
929 * strings. The propname of %NULL indicates the end of the property
930 * list. If the object implements the user creatable interface, the
931 * object will be marked complete once all the properties have been
935 * <title>Creating an object with properties</title>
940 * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE,
941 * object_get_objects_root(),
945 * "mem-path", "/dev/shm/somefile",
951 * error_reportf_err(err, "Cannot create memory backend: ");
956 * The returned object will have one stable reference maintained
957 * for as long as it is present in the object hierarchy.
959 * Returns: The newly allocated, instantiated & initialized object.
961 Object *object_new_with_props(const char *typename,
968 * object_new_with_propv:
969 * @typename: The name of the type of the object to instantiate.
970 * @parent: the parent object
971 * @id: The unique ID of the object
972 * @errp: pointer to error object
973 * @vargs: list of property names and values
975 * See object_new_with_props() for documentation.
977 Object *object_new_with_propv(const char *typename,
983 bool object_apply_global_props(Object *obj, const GPtrArray *props,
985 void object_set_machine_compat_props(GPtrArray *compat_props);
986 void object_set_accelerator_compat_props(GPtrArray *compat_props);
987 void object_register_sugar_prop(const char *driver, const char *prop, const char *value);
988 void object_apply_compat_props(Object *obj);
992 * @obj: the object instance to set properties on
993 * @errp: pointer to error object
994 * @...: list of property names and values
996 * This function will set a list of properties on an existing object
999 * The variadic parameters are a list of pairs of (propname, propvalue)
1000 * strings. The propname of %NULL indicates the end of the property
1004 * <title>Update an object's properties</title>
1006 * Error *err = NULL;
1007 * Object *obj = ...get / create object...;
1009 * if (!object_set_props(obj,
1012 * "mem-path", "/dev/shm/somefile",
1013 * "prealloc", "yes",
1014 * "size", "1048576",
1016 * error_reportf_err(err, "Cannot set properties: ");
1021 * The returned object will have one stable reference maintained
1022 * for as long as it is present in the object hierarchy.
1024 * Returns: %true on success, %false on error.
1026 bool object_set_props(Object *obj, Error **errp, ...) QEMU_SENTINEL;
1030 * @obj: the object instance to set properties on
1031 * @errp: pointer to error object
1032 * @vargs: list of property names and values
1034 * See object_set_props() for documentation.
1036 * Returns: %true on success, %false on error.
1038 bool object_set_propv(Object *obj, Error **errp, va_list vargs);
1041 * object_initialize:
1042 * @obj: A pointer to the memory to be used for the object.
1043 * @size: The maximum size available at @obj for the object.
1044 * @typename: The name of the type of the object to instantiate.
1046 * This function will initialize an object. The memory for the object should
1047 * have already been allocated. The returned object has a reference count of 1,
1048 * and will be finalized when the last reference is dropped.
1050 void object_initialize(void *obj, size_t size, const char *typename);
1053 * object_initialize_child_with_props:
1054 * @parentobj: The parent object to add a property to
1055 * @propname: The name of the property
1056 * @childobj: A pointer to the memory to be used for the object.
1057 * @size: The maximum size available at @childobj for the object.
1058 * @type: The name of the type of the object to instantiate.
1059 * @errp: If an error occurs, a pointer to an area to store the error
1060 * @...: list of property names and values
1062 * This function will initialize an object. The memory for the object should
1063 * have already been allocated. The object will then be added as child property
1064 * to a parent with object_property_add_child() function. The returned object
1065 * has a reference count of 1 (for the "child<...>" property from the parent),
1066 * so the object will be finalized automatically when the parent gets removed.
1068 * The variadic parameters are a list of pairs of (propname, propvalue)
1069 * strings. The propname of %NULL indicates the end of the property list.
1070 * If the object implements the user creatable interface, the object will
1071 * be marked complete once all the properties have been processed.
1073 * Returns: %true on success, %false on failure.
1075 bool object_initialize_child_with_props(Object *parentobj,
1076 const char *propname,
1077 void *childobj, size_t size, const char *type,
1078 Error **errp, ...) QEMU_SENTINEL;
1081 * object_initialize_child_with_propsv:
1082 * @parentobj: The parent object to add a property to
1083 * @propname: The name of the property
1084 * @childobj: A pointer to the memory to be used for the object.
1085 * @size: The maximum size available at @childobj for the object.
1086 * @type: The name of the type of the object to instantiate.
1087 * @errp: If an error occurs, a pointer to an area to store the error
1088 * @vargs: list of property names and values
1090 * See object_initialize_child() for documentation.
1092 * Returns: %true on success, %false on failure.
1094 bool object_initialize_child_with_propsv(Object *parentobj,
1095 const char *propname,
1096 void *childobj, size_t size, const char *type,
1097 Error **errp, va_list vargs);
1100 * object_initialize_child:
1101 * @parent: The parent object to add a property to
1102 * @propname: The name of the property
1103 * @child: A precisely typed pointer to the memory to be used for the
1105 * @type: The name of the type of the object to instantiate.
1108 * object_initialize_child_with_props(parent, propname,
1109 * child, sizeof(*child), type,
1110 * &error_abort, NULL)
1112 #define object_initialize_child(parent, propname, child, type) \
1113 object_initialize_child_internal((parent), (propname), \
1114 (child), sizeof(*(child)), (type))
1115 void object_initialize_child_internal(Object *parent, const char *propname,
1116 void *child, size_t size,
1120 * object_dynamic_cast:
1121 * @obj: The object to cast.
1122 * @typename: The @typename to cast to.
1124 * This function will determine if @obj is-a @typename. @obj can refer to an
1125 * object or an interface associated with an object.
1127 * Returns: This function returns @obj on success or #NULL on failure.
1129 Object *object_dynamic_cast(Object *obj, const char *typename);
1132 * object_dynamic_cast_assert:
1134 * See object_dynamic_cast() for a description of the parameters of this
1135 * function. The only difference in behavior is that this function asserts
1136 * instead of returning #NULL on failure if QOM cast debugging is enabled.
1137 * This function is not meant to be called directly, but only through
1138 * the wrapper macro OBJECT_CHECK.
1140 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
1141 const char *file, int line, const char *func);
1145 * @obj: A derivative of #Object
1147 * Returns: The #ObjectClass of the type associated with @obj.
1149 ObjectClass *object_get_class(Object *obj);
1152 * object_get_typename:
1153 * @obj: A derivative of #Object.
1155 * Returns: The QOM typename of @obj.
1157 const char *object_get_typename(const Object *obj);
1160 * type_register_static:
1161 * @info: The #TypeInfo of the new type.
1163 * @info and all of the strings it points to should exist for the life time
1164 * that the type is registered.
1166 * Returns: the new #Type.
1168 Type type_register_static(const TypeInfo *info);
1172 * @info: The #TypeInfo of the new type
1174 * Unlike type_register_static(), this call does not require @info or its
1175 * string members to continue to exist after the call returns.
1177 * Returns: the new #Type.
1179 Type type_register(const TypeInfo *info);
1182 * type_register_static_array:
1183 * @infos: The array of the new type #TypeInfo structures.
1184 * @nr_infos: number of entries in @infos
1186 * @infos and all of the strings it points to should exist for the life time
1187 * that the type is registered.
1189 void type_register_static_array(const TypeInfo *infos, int nr_infos);
1193 * @type_array: The array containing #TypeInfo structures to register
1195 * @type_array should be static constant that exists for the life time
1196 * that the type is registered.
1198 #define DEFINE_TYPES(type_array) \
1199 static void do_qemu_init_ ## type_array(void) \
1201 type_register_static_array(type_array, ARRAY_SIZE(type_array)); \
1203 type_init(do_qemu_init_ ## type_array)
1206 * object_class_dynamic_cast_assert:
1207 * @klass: The #ObjectClass to attempt to cast.
1208 * @typename: The QOM typename of the class to cast to.
1210 * See object_class_dynamic_cast() for a description of the parameters
1211 * of this function. The only difference in behavior is that this function
1212 * asserts instead of returning #NULL on failure if QOM cast debugging is
1213 * enabled. This function is not meant to be called directly, but only through
1214 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
1216 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
1217 const char *typename,
1218 const char *file, int line,
1222 * object_class_dynamic_cast:
1223 * @klass: The #ObjectClass to attempt to cast.
1224 * @typename: The QOM typename of the class to cast to.
1226 * Returns: If @typename is a class, this function returns @klass if
1227 * @typename is a subtype of @klass, else returns #NULL.
1229 * If @typename is an interface, this function returns the interface
1230 * definition for @klass if @klass implements it unambiguously; #NULL
1231 * is returned if @klass does not implement the interface or if multiple
1232 * classes or interfaces on the hierarchy leading to @klass implement
1233 * it. (FIXME: perhaps this can be detected at type definition time?)
1235 ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
1236 const char *typename);
1239 * object_class_get_parent:
1240 * @klass: The class to obtain the parent for.
1242 * Returns: The parent for @klass or %NULL if none.
1244 ObjectClass *object_class_get_parent(ObjectClass *klass);
1247 * object_class_get_name:
1248 * @klass: The class to obtain the QOM typename for.
1250 * Returns: The QOM typename for @klass.
1252 const char *object_class_get_name(ObjectClass *klass);
1255 * object_class_is_abstract:
1256 * @klass: The class to obtain the abstractness for.
1258 * Returns: %true if @klass is abstract, %false otherwise.
1260 bool object_class_is_abstract(ObjectClass *klass);
1263 * object_class_by_name:
1264 * @typename: The QOM typename to obtain the class for.
1266 * Returns: The class for @typename or %NULL if not found.
1268 ObjectClass *object_class_by_name(const char *typename);
1271 * module_object_class_by_name:
1272 * @typename: The QOM typename to obtain the class for.
1274 * For objects which might be provided by a module. Behaves like
1275 * object_class_by_name, but additionally tries to load the module
1276 * needed in case the class is not available.
1278 * Returns: The class for @typename or %NULL if not found.
1280 ObjectClass *module_object_class_by_name(const char *typename);
1282 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
1283 const char *implements_type, bool include_abstract,
1287 * object_class_get_list:
1288 * @implements_type: The type to filter for, including its derivatives.
1289 * @include_abstract: Whether to include abstract classes.
1291 * Returns: A singly-linked list of the classes in reverse hashtable order.
1293 GSList *object_class_get_list(const char *implements_type,
1294 bool include_abstract);
1297 * object_class_get_list_sorted:
1298 * @implements_type: The type to filter for, including its derivatives.
1299 * @include_abstract: Whether to include abstract classes.
1301 * Returns: A singly-linked list of the classes in alphabetical
1302 * case-insensitive order.
1304 GSList *object_class_get_list_sorted(const char *implements_type,
1305 bool include_abstract);
1311 * Increase the reference count of a object. A object cannot be freed as long
1312 * as its reference count is greater than zero.
1315 Object *object_ref(void *obj);
1321 * Decrease the reference count of a object. A object cannot be freed as long
1322 * as its reference count is greater than zero.
1324 void object_unref(void *obj);
1327 * object_property_try_add:
1328 * @obj: the object to add a property to
1329 * @name: the name of the property. This can contain any character except for
1330 * a forward slash. In general, you should use hyphens '-' instead of
1331 * underscores '_' when naming properties.
1332 * @type: the type name of the property. This namespace is pretty loosely
1333 * defined. Sub namespaces are constructed by using a prefix and then
1334 * to angle brackets. For instance, the type 'virtio-net-pci' in the
1335 * 'link' namespace would be 'link<virtio-net-pci>'.
1336 * @get: The getter to be called to read a property. If this is NULL, then
1337 * the property cannot be read.
1338 * @set: the setter to be called to write a property. If this is NULL,
1339 * then the property cannot be written.
1340 * @release: called when the property is removed from the object. This is
1341 * meant to allow a property to free its opaque upon object
1342 * destruction. This may be NULL.
1343 * @opaque: an opaque pointer to pass to the callbacks for the property
1344 * @errp: pointer to error object
1346 * Returns: The #ObjectProperty; this can be used to set the @resolve
1347 * callback for child and link properties.
1349 ObjectProperty *object_property_try_add(Object *obj, const char *name,
1351 ObjectPropertyAccessor *get,
1352 ObjectPropertyAccessor *set,
1353 ObjectPropertyRelease *release,
1354 void *opaque, Error **errp);
1357 * object_property_add:
1358 * Same as object_property_try_add() with @errp hardcoded to
1361 ObjectProperty *object_property_add(Object *obj, const char *name,
1363 ObjectPropertyAccessor *get,
1364 ObjectPropertyAccessor *set,
1365 ObjectPropertyRelease *release,
1368 void object_property_del(Object *obj, const char *name);
1370 ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name,
1372 ObjectPropertyAccessor *get,
1373 ObjectPropertyAccessor *set,
1374 ObjectPropertyRelease *release,
1378 * object_property_set_default_bool:
1379 * @prop: the property to set
1380 * @value: the value to be written to the property
1382 * Set the property default value.
1384 void object_property_set_default_bool(ObjectProperty *prop, bool value);
1387 * object_property_set_default_str:
1388 * @prop: the property to set
1389 * @value: the value to be written to the property
1391 * Set the property default value.
1393 void object_property_set_default_str(ObjectProperty *prop, const char *value);
1396 * object_property_set_default_int:
1397 * @prop: the property to set
1398 * @value: the value to be written to the property
1400 * Set the property default value.
1402 void object_property_set_default_int(ObjectProperty *prop, int64_t value);
1405 * object_property_set_default_uint:
1406 * @prop: the property to set
1407 * @value: the value to be written to the property
1409 * Set the property default value.
1411 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value);
1414 * object_property_find:
1416 * @name: the name of the property
1417 * @errp: returns an error if this function fails
1419 * Look up a property for an object and return its #ObjectProperty if found.
1421 ObjectProperty *object_property_find(Object *obj, const char *name,
1423 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1426 typedef struct ObjectPropertyIterator {
1427 ObjectClass *nextclass;
1428 GHashTableIter iter;
1429 } ObjectPropertyIterator;
1432 * object_property_iter_init:
1435 * Initializes an iterator for traversing all properties
1436 * registered against an object instance, its class and all parent classes.
1438 * It is forbidden to modify the property list while iterating,
1439 * whether removing or adding properties.
1441 * Typical usage pattern would be
1444 * <title>Using object property iterators</title>
1446 * ObjectProperty *prop;
1447 * ObjectPropertyIterator iter;
1449 * object_property_iter_init(&iter, obj);
1450 * while ((prop = object_property_iter_next(&iter))) {
1451 * ... do something with prop ...
1456 void object_property_iter_init(ObjectPropertyIterator *iter,
1460 * object_class_property_iter_init:
1463 * Initializes an iterator for traversing all properties
1464 * registered against an object class and all parent classes.
1466 * It is forbidden to modify the property list while iterating,
1467 * whether removing or adding properties.
1469 * This can be used on abstract classes as it does not create a temporary
1472 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1473 ObjectClass *klass);
1476 * object_property_iter_next:
1477 * @iter: the iterator instance
1479 * Return the next available property. If no further properties
1480 * are available, a %NULL value will be returned and the @iter
1481 * pointer should not be used again after this point without
1482 * re-initializing it.
1484 * Returns: the next property, or %NULL when all properties
1485 * have been traversed.
1487 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter);
1489 void object_unparent(Object *obj);
1492 * object_property_get:
1494 * @name: the name of the property
1495 * @v: the visitor that will receive the property value. This should be an
1496 * Output visitor and the data will be written with @name as the name.
1497 * @errp: returns an error if this function fails
1499 * Reads a property from a object.
1501 * Returns: %true on success, %false on failure.
1503 bool object_property_get(Object *obj, const char *name, Visitor *v,
1507 * object_property_set_str:
1508 * @name: the name of the property
1509 * @value: the value to be written to the property
1510 * @errp: returns an error if this function fails
1512 * Writes a string value to a property.
1514 * Returns: %true on success, %false on failure.
1516 bool object_property_set_str(Object *obj, const char *name,
1517 const char *value, Error **errp);
1520 * object_property_get_str:
1522 * @name: the name of the property
1523 * @errp: returns an error if this function fails
1525 * Returns: the value of the property, converted to a C string, or NULL if
1526 * an error occurs (including when the property value is not a string).
1527 * The caller should free the string.
1529 char *object_property_get_str(Object *obj, const char *name,
1533 * object_property_set_link:
1534 * @name: the name of the property
1535 * @value: the value to be written to the property
1536 * @errp: returns an error if this function fails
1538 * Writes an object's canonical path to a property.
1540 * If the link property was created with
1541 * <code>OBJ_PROP_LINK_STRONG</code> bit, the old target object is
1542 * unreferenced, and a reference is added to the new target object.
1544 * Returns: %true on success, %false on failure.
1546 bool object_property_set_link(Object *obj, const char *name,
1547 Object *value, Error **errp);
1550 * object_property_get_link:
1552 * @name: the name of the property
1553 * @errp: returns an error if this function fails
1555 * Returns: the value of the property, resolved from a path to an Object,
1556 * or NULL if an error occurs (including when the property value is not a
1557 * string or not a valid object path).
1559 Object *object_property_get_link(Object *obj, const char *name,
1563 * object_property_set_bool:
1564 * @name: the name of the property
1565 * @value: the value to be written to the property
1566 * @errp: returns an error if this function fails
1568 * Writes a bool value to a property.
1570 * Returns: %true on success, %false on failure.
1572 bool object_property_set_bool(Object *obj, const char *name,
1573 bool value, Error **errp);
1576 * object_property_get_bool:
1578 * @name: the name of the property
1579 * @errp: returns an error if this function fails
1581 * Returns: the value of the property, converted to a boolean, or NULL if
1582 * an error occurs (including when the property value is not a bool).
1584 bool object_property_get_bool(Object *obj, const char *name,
1588 * object_property_set_int:
1589 * @name: the name of the property
1590 * @value: the value to be written to the property
1591 * @errp: returns an error if this function fails
1593 * Writes an integer value to a property.
1595 * Returns: %true on success, %false on failure.
1597 bool object_property_set_int(Object *obj, const char *name,
1598 int64_t value, Error **errp);
1601 * object_property_get_int:
1603 * @name: the name of the property
1604 * @errp: returns an error if this function fails
1606 * Returns: the value of the property, converted to an integer, or negative if
1607 * an error occurs (including when the property value is not an integer).
1609 int64_t object_property_get_int(Object *obj, const char *name,
1613 * object_property_set_uint:
1614 * @name: the name of the property
1615 * @value: the value to be written to the property
1616 * @errp: returns an error if this function fails
1618 * Writes an unsigned integer value to a property.
1620 * Returns: %true on success, %false on failure.
1622 bool object_property_set_uint(Object *obj, const char *name,
1623 uint64_t value, Error **errp);
1626 * object_property_get_uint:
1628 * @name: the name of the property
1629 * @errp: returns an error if this function fails
1631 * Returns: the value of the property, converted to an unsigned integer, or 0
1632 * an error occurs (including when the property value is not an integer).
1634 uint64_t object_property_get_uint(Object *obj, const char *name,
1638 * object_property_get_enum:
1640 * @name: the name of the property
1641 * @typename: the name of the enum data type
1642 * @errp: returns an error if this function fails
1644 * Returns: the value of the property, converted to an integer, or
1645 * undefined if an error occurs (including when the property value is not
1648 int object_property_get_enum(Object *obj, const char *name,
1649 const char *typename, Error **errp);
1652 * object_property_set:
1654 * @name: the name of the property
1655 * @v: the visitor that will be used to write the property value. This should
1656 * be an Input visitor and the data will be first read with @name as the
1657 * name and then written as the property value.
1658 * @errp: returns an error if this function fails
1660 * Writes a property to a object.
1662 * Returns: %true on success, %false on failure.
1664 bool object_property_set(Object *obj, const char *name, Visitor *v,
1668 * object_property_parse:
1670 * @name: the name of the property
1671 * @string: the string that will be used to parse the property value.
1672 * @errp: returns an error if this function fails
1674 * Parses a string and writes the result into a property of an object.
1676 * Returns: %true on success, %false on failure.
1678 bool object_property_parse(Object *obj, const char *name,
1679 const char *string, Error **errp);
1682 * object_property_print:
1684 * @name: the name of the property
1685 * @human: if true, print for human consumption
1686 * @errp: returns an error if this function fails
1688 * Returns a string representation of the value of the property. The
1689 * caller shall free the string.
1691 char *object_property_print(Object *obj, const char *name, bool human,
1695 * object_property_get_type:
1697 * @name: the name of the property
1698 * @errp: returns an error if this function fails
1700 * Returns: The type name of the property.
1702 const char *object_property_get_type(Object *obj, const char *name,
1708 * Returns: the root object of the composition tree
1710 Object *object_get_root(void);
1714 * object_get_objects_root:
1716 * Get the container object that holds user created
1717 * object instances. This is the object at path
1720 * Returns: the user object container
1722 Object *object_get_objects_root(void);
1725 * object_get_internal_root:
1727 * Get the container object that holds internally used object
1728 * instances. Any object which is put into this container must not be
1729 * user visible, and it will not be exposed in the QOM tree.
1731 * Returns: the internal object container
1733 Object *object_get_internal_root(void);
1736 * object_get_canonical_path_component:
1738 * Returns: The final component in the object's canonical path. The canonical
1739 * path is the path within the composition tree starting from the root.
1740 * %NULL if the object doesn't have a parent (and thus a canonical path).
1742 const char *object_get_canonical_path_component(const Object *obj);
1745 * object_get_canonical_path:
1747 * Returns: The canonical path for a object, newly allocated. This is
1748 * the path within the composition tree starting from the root. Use
1749 * g_free() to free it.
1751 char *object_get_canonical_path(const Object *obj);
1754 * object_resolve_path:
1755 * @path: the path to resolve
1756 * @ambiguous: returns true if the path resolution failed because of an
1759 * There are two types of supported paths--absolute paths and partial paths.
1761 * Absolute paths are derived from the root object and can follow child<> or
1762 * link<> properties. Since they can follow link<> properties, they can be
1763 * arbitrarily long. Absolute paths look like absolute filenames and are
1764 * prefixed with a leading slash.
1766 * Partial paths look like relative filenames. They do not begin with a
1767 * prefix. The matching rules for partial paths are subtle but designed to make
1768 * specifying objects easy. At each level of the composition tree, the partial
1769 * path is matched as an absolute path. The first match is not returned. At
1770 * least two matches are searched for. A successful result is only returned if
1771 * only one match is found. If more than one match is found, a flag is
1772 * returned to indicate that the match was ambiguous.
1774 * Returns: The matched object or NULL on path lookup failure.
1776 Object *object_resolve_path(const char *path, bool *ambiguous);
1779 * object_resolve_path_type:
1780 * @path: the path to resolve
1781 * @typename: the type to look for.
1782 * @ambiguous: returns true if the path resolution failed because of an
1785 * This is similar to object_resolve_path. However, when looking for a
1786 * partial path only matches that implement the given type are considered.
1787 * This restricts the search and avoids spuriously flagging matches as
1790 * For both partial and absolute paths, the return value goes through
1791 * a dynamic cast to @typename. This is important if either the link,
1792 * or the typename itself are of interface types.
1794 * Returns: The matched object or NULL on path lookup failure.
1796 Object *object_resolve_path_type(const char *path, const char *typename,
1800 * object_resolve_path_component:
1801 * @parent: the object in which to resolve the path
1802 * @part: the component to resolve.
1804 * This is similar to object_resolve_path with an absolute path, but it
1805 * only resolves one element (@part) and takes the others from @parent.
1807 * Returns: The resolved object or NULL on path lookup failure.
1809 Object *object_resolve_path_component(Object *parent, const char *part);
1812 * object_property_try_add_child:
1813 * @obj: the object to add a property to
1814 * @name: the name of the property
1815 * @child: the child object
1816 * @errp: pointer to error object
1818 * Child properties form the composition tree. All objects need to be a child
1819 * of another object. Objects can only be a child of one object.
1821 * There is no way for a child to determine what its parent is. It is not
1822 * a bidirectional relationship. This is by design.
1824 * The value of a child property as a C string will be the child object's
1825 * canonical path. It can be retrieved using object_property_get_str().
1826 * The child object itself can be retrieved using object_property_get_link().
1828 * Returns: The newly added property on success, or %NULL on failure.
1830 ObjectProperty *object_property_try_add_child(Object *obj, const char *name,
1831 Object *child, Error **errp);
1834 * object_property_add_child:
1835 * Same as object_property_try_add_child() with @errp hardcoded to
1838 ObjectProperty *object_property_add_child(Object *obj, const char *name,
1842 /* Unref the link pointer when the property is deleted */
1843 OBJ_PROP_LINK_STRONG = 0x1,
1846 OBJ_PROP_LINK_DIRECT = 0x2,
1847 OBJ_PROP_LINK_CLASS = 0x4,
1848 } ObjectPropertyLinkFlags;
1851 * object_property_allow_set_link:
1853 * The default implementation of the object_property_add_link() check()
1854 * callback function. It allows the link property to be set and never returns
1857 void object_property_allow_set_link(const Object *, const char *,
1858 Object *, Error **);
1861 * object_property_add_link:
1862 * @obj: the object to add a property to
1863 * @name: the name of the property
1864 * @type: the qobj type of the link
1865 * @targetp: a pointer to where the link object reference is stored
1866 * @check: callback to veto setting or NULL if the property is read-only
1867 * @flags: additional options for the link
1869 * Links establish relationships between objects. Links are unidirectional
1870 * although two links can be combined to form a bidirectional relationship
1873 * Links form the graph in the object model.
1875 * The <code>@check()</code> callback is invoked when
1876 * object_property_set_link() is called and can raise an error to prevent the
1877 * link being set. If <code>@check</code> is NULL, the property is read-only
1878 * and cannot be set.
1880 * Ownership of the pointer that @child points to is transferred to the
1881 * link property. The reference count for <code>*@child</code> is
1882 * managed by the property from after the function returns till the
1883 * property is deleted with object_property_del(). If the
1884 * <code>@flags</code> <code>OBJ_PROP_LINK_STRONG</code> bit is set,
1885 * the reference count is decremented when the property is deleted or
1888 * Returns: The newly added property on success, or %NULL on failure.
1890 ObjectProperty *object_property_add_link(Object *obj, const char *name,
1891 const char *type, Object **targetp,
1892 void (*check)(const Object *obj, const char *name,
1893 Object *val, Error **errp),
1894 ObjectPropertyLinkFlags flags);
1896 ObjectProperty *object_class_property_add_link(ObjectClass *oc,
1898 const char *type, ptrdiff_t offset,
1899 void (*check)(const Object *obj, const char *name,
1900 Object *val, Error **errp),
1901 ObjectPropertyLinkFlags flags);
1904 * object_property_add_str:
1905 * @obj: the object to add a property to
1906 * @name: the name of the property
1907 * @get: the getter or NULL if the property is write-only. This function must
1908 * return a string to be freed by g_free().
1909 * @set: the setter or NULL if the property is read-only
1911 * Add a string property using getters/setters. This function will add a
1912 * property of type 'string'.
1914 * Returns: The newly added property on success, or %NULL on failure.
1916 ObjectProperty *object_property_add_str(Object *obj, const char *name,
1917 char *(*get)(Object *, Error **),
1918 void (*set)(Object *, const char *, Error **));
1920 ObjectProperty *object_class_property_add_str(ObjectClass *klass,
1922 char *(*get)(Object *, Error **),
1923 void (*set)(Object *, const char *,
1927 * object_property_add_bool:
1928 * @obj: the object to add a property to
1929 * @name: the name of the property
1930 * @get: the getter or NULL if the property is write-only.
1931 * @set: the setter or NULL if the property is read-only
1933 * Add a bool property using getters/setters. This function will add a
1934 * property of type 'bool'.
1936 * Returns: The newly added property on success, or %NULL on failure.
1938 ObjectProperty *object_property_add_bool(Object *obj, const char *name,
1939 bool (*get)(Object *, Error **),
1940 void (*set)(Object *, bool, Error **));
1942 ObjectProperty *object_class_property_add_bool(ObjectClass *klass,
1944 bool (*get)(Object *, Error **),
1945 void (*set)(Object *, bool, Error **));
1948 * object_property_add_enum:
1949 * @obj: the object to add a property to
1950 * @name: the name of the property
1951 * @typename: the name of the enum data type
1952 * @get: the getter or %NULL if the property is write-only.
1953 * @set: the setter or %NULL if the property is read-only
1955 * Add an enum property using getters/setters. This function will add a
1956 * property of type '@typename'.
1958 * Returns: The newly added property on success, or %NULL on failure.
1960 ObjectProperty *object_property_add_enum(Object *obj, const char *name,
1961 const char *typename,
1962 const QEnumLookup *lookup,
1963 int (*get)(Object *, Error **),
1964 void (*set)(Object *, int, Error **));
1966 ObjectProperty *object_class_property_add_enum(ObjectClass *klass,
1968 const char *typename,
1969 const QEnumLookup *lookup,
1970 int (*get)(Object *, Error **),
1971 void (*set)(Object *, int, Error **));
1974 * object_property_add_tm:
1975 * @obj: the object to add a property to
1976 * @name: the name of the property
1977 * @get: the getter or NULL if the property is write-only.
1979 * Add a read-only struct tm valued property using a getter function.
1980 * This function will add a property of type 'struct tm'.
1982 * Returns: The newly added property on success, or %NULL on failure.
1984 ObjectProperty *object_property_add_tm(Object *obj, const char *name,
1985 void (*get)(Object *, struct tm *, Error **));
1987 ObjectProperty *object_class_property_add_tm(ObjectClass *klass,
1989 void (*get)(Object *, struct tm *, Error **));
1992 /* Automatically add a getter to the property */
1993 OBJ_PROP_FLAG_READ = 1 << 0,
1994 /* Automatically add a setter to the property */
1995 OBJ_PROP_FLAG_WRITE = 1 << 1,
1996 /* Automatically add a getter and a setter to the property */
1997 OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE),
1998 } ObjectPropertyFlags;
2001 * object_property_add_uint8_ptr:
2002 * @obj: the object to add a property to
2003 * @name: the name of the property
2004 * @v: pointer to value
2005 * @flags: bitwise-or'd ObjectPropertyFlags
2007 * Add an integer property in memory. This function will add a
2008 * property of type 'uint8'.
2010 * Returns: The newly added property on success, or %NULL on failure.
2012 ObjectProperty *object_property_add_uint8_ptr(Object *obj, const char *name,
2014 ObjectPropertyFlags flags);
2016 ObjectProperty *object_class_property_add_uint8_ptr(ObjectClass *klass,
2019 ObjectPropertyFlags flags);
2022 * object_property_add_uint16_ptr:
2023 * @obj: the object to add a property to
2024 * @name: the name of the property
2025 * @v: pointer to value
2026 * @flags: bitwise-or'd ObjectPropertyFlags
2028 * Add an integer property in memory. This function will add a
2029 * property of type 'uint16'.
2031 * Returns: The newly added property on success, or %NULL on failure.
2033 ObjectProperty *object_property_add_uint16_ptr(Object *obj, const char *name,
2035 ObjectPropertyFlags flags);
2037 ObjectProperty *object_class_property_add_uint16_ptr(ObjectClass *klass,
2040 ObjectPropertyFlags flags);
2043 * object_property_add_uint32_ptr:
2044 * @obj: the object to add a property to
2045 * @name: the name of the property
2046 * @v: pointer to value
2047 * @flags: bitwise-or'd ObjectPropertyFlags
2049 * Add an integer property in memory. This function will add a
2050 * property of type 'uint32'.
2052 * Returns: The newly added property on success, or %NULL on failure.
2054 ObjectProperty *object_property_add_uint32_ptr(Object *obj, const char *name,
2056 ObjectPropertyFlags flags);
2058 ObjectProperty *object_class_property_add_uint32_ptr(ObjectClass *klass,
2061 ObjectPropertyFlags flags);
2064 * object_property_add_uint64_ptr:
2065 * @obj: the object to add a property to
2066 * @name: the name of the property
2067 * @v: pointer to value
2068 * @flags: bitwise-or'd ObjectPropertyFlags
2070 * Add an integer property in memory. This function will add a
2071 * property of type 'uint64'.
2073 * Returns: The newly added property on success, or %NULL on failure.
2075 ObjectProperty *object_property_add_uint64_ptr(Object *obj, const char *name,
2077 ObjectPropertyFlags flags);
2079 ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass,
2082 ObjectPropertyFlags flags);
2085 * object_property_add_alias:
2086 * @obj: the object to add a property to
2087 * @name: the name of the property
2088 * @target_obj: the object to forward property access to
2089 * @target_name: the name of the property on the forwarded object
2091 * Add an alias for a property on an object. This function will add a property
2092 * of the same type as the forwarded property.
2094 * The caller must ensure that <code>@target_obj</code> stays alive as long as
2095 * this property exists. In the case of a child object or an alias on the same
2096 * object this will be the case. For aliases to other objects the caller is
2097 * responsible for taking a reference.
2099 * Returns: The newly added property on success, or %NULL on failure.
2101 ObjectProperty *object_property_add_alias(Object *obj, const char *name,
2102 Object *target_obj, const char *target_name);
2105 * object_property_add_const_link:
2106 * @obj: the object to add a property to
2107 * @name: the name of the property
2108 * @target: the object to be referred by the link
2110 * Add an unmodifiable link for a property on an object. This function will
2111 * add a property of type link<TYPE> where TYPE is the type of @target.
2113 * The caller must ensure that @target stays alive as long as
2114 * this property exists. In the case @target is a child of @obj,
2115 * this will be the case. Otherwise, the caller is responsible for
2116 * taking a reference.
2118 * Returns: The newly added property on success, or %NULL on failure.
2120 ObjectProperty *object_property_add_const_link(Object *obj, const char *name,
2124 * object_property_set_description:
2125 * @obj: the object owning the property
2126 * @name: the name of the property
2127 * @description: the description of the property on the object
2129 * Set an object property's description.
2131 * Returns: %true on success, %false on failure.
2133 void object_property_set_description(Object *obj, const char *name,
2134 const char *description);
2135 void object_class_property_set_description(ObjectClass *klass, const char *name,
2136 const char *description);
2139 * object_child_foreach:
2140 * @obj: the object whose children will be navigated
2141 * @fn: the iterator function to be called
2142 * @opaque: an opaque value that will be passed to the iterator
2144 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
2147 * It is forbidden to add or remove children from @obj from the @fn
2150 * Returns: The last value returned by @fn, or 0 if there is no child.
2152 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
2156 * object_child_foreach_recursive:
2157 * @obj: the object whose children will be navigated
2158 * @fn: the iterator function to be called
2159 * @opaque: an opaque value that will be passed to the iterator
2161 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
2162 * non-zero. Calls recursively, all child nodes of @obj will also be passed
2163 * all the way down to the leaf nodes of the tree. Depth first ordering.
2165 * It is forbidden to add or remove children from @obj (or its
2166 * child nodes) from the @fn callback.
2168 * Returns: The last value returned by @fn, or 0 if there is no child.
2170 int object_child_foreach_recursive(Object *obj,
2171 int (*fn)(Object *child, void *opaque),
2175 * @root: root of the #path, e.g., object_get_root()
2176 * @path: path to the container
2178 * Return a container object whose path is @path. Create more containers
2179 * along the path if necessary.
2181 * Returns: the container object.
2183 Object *container_get(Object *root, const char *path);
2186 * object_type_get_instance_size:
2187 * @typename: Name of the Type whose instance_size is required
2189 * Returns the instance_size of the given @typename.
2191 size_t object_type_get_instance_size(const char *typename);
2194 * object_property_help:
2195 * @name: the name of the property
2196 * @type: the type of the property
2197 * @defval: the default value
2198 * @description: description of the property
2200 * Returns: a user-friendly formatted string describing the property
2201 * for help purposes.
2203 char *object_property_help(const char *name, const char *type,
2204 QObject *defval, const char *description);
2206 G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref)