4 * Copyright IBM, Corp. 2011
7 * Anthony Liguori <aliguori@us.ibm.com>
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"
19 #include "qom/object.h"
22 typedef struct TypeImpl
*Type
;
24 typedef struct TypeInfo TypeInfo
;
26 typedef struct InterfaceClass InterfaceClass
;
27 typedef struct InterfaceInfo InterfaceInfo
;
29 #define TYPE_OBJECT "object"
33 * @title:Base Object Type System
34 * @short_description: interfaces for creating new types and objects
36 * The QEMU Object Model provides a framework for registering user creatable
37 * types and instantiating objects from those types. QOM provides the following
40 * - System for dynamically registering types
41 * - Support for single-inheritance of types
42 * - Multiple inheritance of stateless interfaces
45 * <title>Creating a minimal type</title>
49 * #define TYPE_MY_DEVICE "my-device"
51 * // No new virtual functions: we can reuse the typedef for the
53 * typedef DeviceClass MyDeviceClass;
54 * typedef struct MyDevice
58 * int reg0, reg1, reg2;
61 * static const TypeInfo my_device_info = {
62 * .name = TYPE_MY_DEVICE,
63 * .parent = TYPE_DEVICE,
64 * .instance_size = sizeof(MyDevice),
67 * static void my_device_register_types(void)
69 * type_register_static(&my_device_info);
72 * type_init(my_device_register_types)
76 * In the above example, we create a simple type that is described by #TypeInfo.
77 * #TypeInfo describes information about the type including what it inherits
78 * from, the instance and class size, and constructor/destructor hooks.
80 * Alternatively several static types could be registered using helper macro
85 * static const TypeInfo device_types_info[] = {
87 * .name = TYPE_MY_DEVICE_A,
88 * .parent = TYPE_DEVICE,
89 * .instance_size = sizeof(MyDeviceA),
92 * .name = TYPE_MY_DEVICE_B,
93 * .parent = TYPE_DEVICE,
94 * .instance_size = sizeof(MyDeviceB),
98 * DEFINE_TYPES(device_types_info)
102 * Every type has an #ObjectClass associated with it. #ObjectClass derivatives
103 * are instantiated dynamically but there is only ever one instance for any
104 * given type. The #ObjectClass typically holds a table of function pointers
105 * for the virtual methods implemented by this type.
107 * Using object_new(), a new #Object derivative will be instantiated. You can
108 * cast an #Object to a subclass (or base-class) type using
109 * object_dynamic_cast(). You typically want to define macro wrappers around
110 * OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
114 * <title>Typecasting macros</title>
116 * #define MY_DEVICE_GET_CLASS(obj) \
117 * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
118 * #define MY_DEVICE_CLASS(klass) \
119 * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
120 * #define MY_DEVICE(obj) \
121 * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
125 * # Class Initialization #
127 * Before an object is initialized, the class for the object must be
128 * initialized. There is only one class object for all instance objects
129 * that is created lazily.
131 * Classes are initialized by first initializing any parent classes (if
132 * necessary). After the parent class object has initialized, it will be
133 * copied into the current class object and any additional storage in the
134 * class object is zero filled.
136 * The effect of this is that classes automatically inherit any virtual
137 * function pointers that the parent class has already initialized. All
138 * other fields will be zero filled.
140 * Once all of the parent classes have been initialized, #TypeInfo::class_init
141 * is called to let the class being instantiated provide default initialize for
142 * its virtual functions. Here is how the above example might be modified
143 * to introduce an overridden virtual function:
146 * <title>Overriding a virtual function</title>
150 * void my_device_class_init(ObjectClass *klass, void *class_data)
152 * DeviceClass *dc = DEVICE_CLASS(klass);
153 * dc->reset = my_device_reset;
156 * static const TypeInfo my_device_info = {
157 * .name = TYPE_MY_DEVICE,
158 * .parent = TYPE_DEVICE,
159 * .instance_size = sizeof(MyDevice),
160 * .class_init = my_device_class_init,
165 * Introducing new virtual methods requires a class to define its own
166 * struct and to add a .class_size member to the #TypeInfo. Each method
167 * will also have a wrapper function to call it easily:
170 * <title>Defining an abstract class</title>
174 * typedef struct MyDeviceClass
176 * DeviceClass parent;
178 * void (*frobnicate) (MyDevice *obj);
181 * static const TypeInfo my_device_info = {
182 * .name = TYPE_MY_DEVICE,
183 * .parent = TYPE_DEVICE,
184 * .instance_size = sizeof(MyDevice),
185 * .abstract = true, // or set a default in my_device_class_init
186 * .class_size = sizeof(MyDeviceClass),
189 * void my_device_frobnicate(MyDevice *obj)
191 * MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
193 * klass->frobnicate(obj);
200 * Interfaces allow a limited form of multiple inheritance. Instances are
201 * similar to normal types except for the fact that are only defined by
202 * their classes and never carry any state. As a consequence, a pointer to
203 * an interface instance should always be of incomplete type in order to be
204 * sure it cannot be dereferenced. That is, you should define the
205 * 'typedef struct SomethingIf SomethingIf' so that you can pass around
206 * 'SomethingIf *si' arguments, but not define a 'struct SomethingIf { ... }'.
207 * The only things you can validly do with a 'SomethingIf *' are to pass it as
208 * an argument to a method on its corresponding SomethingIfClass, or to
209 * dynamically cast it to an object that implements the interface.
213 * A <emphasis>method</emphasis> is a function within the namespace scope of
214 * a class. It usually operates on the object instance by passing it as a
215 * strongly-typed first argument.
216 * If it does not operate on an object instance, it is dubbed
217 * <emphasis>class method</emphasis>.
219 * Methods cannot be overloaded. That is, the #ObjectClass and method name
220 * uniquely identity the function to be called; the signature does not vary
221 * except for trailing varargs.
223 * Methods are always <emphasis>virtual</emphasis>. Overriding a method in
224 * #TypeInfo.class_init of a subclass leads to any user of the class obtained
225 * via OBJECT_GET_CLASS() accessing the overridden function.
226 * The original function is not automatically invoked. It is the responsibility
227 * of the overriding class to determine whether and when to invoke the method
230 * To invoke the method being overridden, the preferred solution is to store
231 * the original value in the overriding class before overriding the method.
232 * This corresponds to |[ {super,base}.method(...) ]| in Java and C#
233 * respectively; this frees the overriding class from hardcoding its parent
234 * class, which someone might choose to change at some point.
237 * <title>Overriding a virtual method</title>
239 * typedef struct MyState MyState;
241 * typedef void (*MyDoSomething)(MyState *obj);
243 * typedef struct MyClass {
244 * ObjectClass parent_class;
246 * MyDoSomething do_something;
249 * static void my_do_something(MyState *obj)
254 * static void my_class_init(ObjectClass *oc, void *data)
256 * MyClass *mc = MY_CLASS(oc);
258 * mc->do_something = my_do_something;
261 * static const TypeInfo my_type_info = {
263 * .parent = TYPE_OBJECT,
264 * .instance_size = sizeof(MyState),
265 * .class_size = sizeof(MyClass),
266 * .class_init = my_class_init,
269 * typedef struct DerivedClass {
270 * MyClass parent_class;
272 * MyDoSomething parent_do_something;
275 * static void derived_do_something(MyState *obj)
277 * DerivedClass *dc = DERIVED_GET_CLASS(obj);
279 * // do something here
280 * dc->parent_do_something(obj);
281 * // do something else here
284 * static void derived_class_init(ObjectClass *oc, void *data)
286 * MyClass *mc = MY_CLASS(oc);
287 * DerivedClass *dc = DERIVED_CLASS(oc);
289 * dc->parent_do_something = mc->do_something;
290 * mc->do_something = derived_do_something;
293 * static const TypeInfo derived_type_info = {
294 * .name = TYPE_DERIVED,
296 * .class_size = sizeof(DerivedClass),
297 * .class_init = derived_class_init,
302 * Alternatively, object_class_by_name() can be used to obtain the class and
303 * its non-overridden methods for a specific type. This would correspond to
304 * |[ MyClass::method(...) ]| in C++.
306 * The first example of such a QOM method was #CPUClass.reset,
307 * another example is #DeviceClass.realize.
309 * # Standard type declaration and definition macros #
311 * A lot of the code outlined above follows a standard pattern and naming
312 * convention. To reduce the amount of boilerplate code that needs to be
313 * written for a new type there are two sets of macros to generate the
314 * common parts in a standard format.
316 * A type is declared using the OBJECT_DECLARE macro family. In types
317 * which do not require any virtual functions in the class, the
318 * OBJECT_DECLARE_SIMPLE_TYPE macro is suitable, and is commonly placed
319 * in the header file:
322 * <title>Declaring a simple type</title>
324 * OBJECT_DECLARE_SIMPLE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
328 * This is equivalent to the following:
331 * <title>Expansion from declaring a simple type</title>
333 * typedef struct MyDevice MyDevice;
334 * typedef struct MyDeviceClass MyDeviceClass;
336 * G_DEFINE_AUTOPTR_CLEANUP_FUNC(MyDeviceClass, object_unref)
338 * #define MY_DEVICE_GET_CLASS(void *obj) \
339 * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
340 * #define MY_DEVICE_CLASS(void *klass) \
341 * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
342 * #define MY_DEVICE(void *obj)
343 * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
345 * struct MyDeviceClass {
346 * DeviceClass parent_class;
351 * The 'struct MyDevice' needs to be declared separately.
352 * If the type requires virtual functions to be declared in the class
353 * struct, then the alternative OBJECT_DECLARE_TYPE() macro can be
354 * used. This does the same as OBJECT_DECLARE_SIMPLE_TYPE(), but without
355 * the 'struct MyDeviceClass' definition.
357 * To implement the type, the OBJECT_DEFINE macro family is available.
358 * In the simple case the OBJECT_DEFINE_TYPE macro is suitable:
361 * <title>Defining a simple type</title>
363 * OBJECT_DEFINE_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
367 * This is equivalent to the following:
370 * <title>Expansion from defining a simple type</title>
372 * static void my_device_finalize(Object *obj);
373 * static void my_device_class_init(ObjectClass *oc, void *data);
374 * static void my_device_init(Object *obj);
376 * static const TypeInfo my_device_info = {
377 * .parent = TYPE_DEVICE,
378 * .name = TYPE_MY_DEVICE,
379 * .instance_size = sizeof(MyDevice),
380 * .instance_init = my_device_init,
381 * .instance_finalize = my_device_finalize,
382 * .class_size = sizeof(MyDeviceClass),
383 * .class_init = my_device_class_init,
387 * my_device_register_types(void)
389 * type_register_static(&my_device_info);
391 * type_init(my_device_register_types);
395 * This is sufficient to get the type registered with the type
396 * system, and the three standard methods now need to be implemented
397 * along with any other logic required for the type.
399 * If the type needs to implement one or more interfaces, then the
400 * OBJECT_DEFINE_TYPE_WITH_INTERFACES() macro can be used instead.
401 * This accepts an array of interface type names.
404 * <title>Defining a simple type implementing interfaces</title>
406 * OBJECT_DEFINE_TYPE_WITH_INTERFACES(MyDevice, my_device,
408 * { TYPE_USER_CREATABLE }, { NULL })
412 * If the type is not intended to be instantiated, then then
413 * the OBJECT_DEFINE_ABSTRACT_TYPE() macro can be used instead:
416 * <title>Defining a simple type</title>
418 * OBJECT_DEFINE_ABSTRACT_TYPE(MyDevice, my_device, MY_DEVICE, DEVICE)
424 typedef struct ObjectProperty ObjectProperty
;
427 * ObjectPropertyAccessor:
428 * @obj: the object that owns the property
429 * @v: the visitor that contains the property data
430 * @name: the name of the property
431 * @opaque: the object property opaque
432 * @errp: a pointer to an Error that is filled if getting/setting fails.
434 * Called when trying to get/set a property.
436 typedef void (ObjectPropertyAccessor
)(Object
*obj
,
443 * ObjectPropertyResolve:
444 * @obj: the object that owns the property
445 * @opaque: the opaque registered with the property
446 * @part: the name of the property
448 * Resolves the #Object corresponding to property @part.
450 * The returned object can also be used as a starting point
451 * to resolve a relative path starting with "@part".
453 * Returns: If @path is the path that led to @obj, the function
454 * returns the #Object corresponding to "@path/@part".
455 * If "@path/@part" is not a valid object path, it returns #NULL.
457 typedef Object
*(ObjectPropertyResolve
)(Object
*obj
,
462 * ObjectPropertyRelease:
463 * @obj: the object that owns the property
464 * @name: the name of the property
465 * @opaque: the opaque registered with the property
467 * Called when a property is removed from a object.
469 typedef void (ObjectPropertyRelease
)(Object
*obj
,
474 * ObjectPropertyInit:
475 * @obj: the object that owns the property
476 * @prop: the property to set
478 * Called when a property is initialized.
480 typedef void (ObjectPropertyInit
)(Object
*obj
, ObjectProperty
*prop
);
482 struct ObjectProperty
487 ObjectPropertyAccessor
*get
;
488 ObjectPropertyAccessor
*set
;
489 ObjectPropertyResolve
*resolve
;
490 ObjectPropertyRelease
*release
;
491 ObjectPropertyInit
*init
;
498 * @obj: the object that is being removed from the composition tree
500 * Called when an object is being removed from the QOM composition tree.
501 * The function should remove any backlinks from children objects to @obj.
503 typedef void (ObjectUnparent
)(Object
*obj
);
507 * @obj: the object being freed
509 * Called when an object's last reference is removed.
511 typedef void (ObjectFree
)(void *obj
);
513 #define OBJECT_CLASS_CAST_CACHE 4
518 * The base for all classes. The only thing that #ObjectClass contains is an
519 * integer type handle.
527 const char *object_cast_cache
[OBJECT_CLASS_CAST_CACHE
];
528 const char *class_cast_cache
[OBJECT_CLASS_CAST_CACHE
];
530 ObjectUnparent
*unparent
;
532 GHashTable
*properties
;
538 * The base for all objects. The first member of this object is a pointer to
539 * a #ObjectClass. Since C guarantees that the first member of a structure
540 * always begins at byte 0 of that structure, as long as any sub-object places
541 * its parent as the first member, we can cast directly to a #Object.
543 * As a result, #Object contains a reference to the objects type as its
544 * first member. This allows identification of the real type of the object at
552 GHashTable
*properties
;
558 * DECLARE_INSTANCE_CHECKER:
559 * @InstanceType: instance struct name
560 * @OBJ_NAME: the object name in uppercase with underscore separators
561 * @TYPENAME: type name
563 * Direct usage of this macro should be avoided, and the complete
564 * OBJECT_DECLARE_TYPE macro is recommended instead.
566 * This macro will provide the three standard type cast functions for a
569 #define DECLARE_INSTANCE_CHECKER(InstanceType, OBJ_NAME, TYPENAME) \
570 static inline G_GNUC_UNUSED InstanceType * \
571 OBJ_NAME(const void *obj) \
572 { return OBJECT_CHECK(InstanceType, obj, TYPENAME); }
575 * DECLARE_CLASS_CHECKERS:
576 * @ClassType: class struct name
577 * @OBJ_NAME: the object name in uppercase with underscore separators
578 * @TYPENAME: type name
580 * Direct usage of this macro should be avoided, and the complete
581 * OBJECT_DECLARE_TYPE macro is recommended instead.
583 * This macro will provide the three standard type cast functions for a
586 #define DECLARE_CLASS_CHECKERS(ClassType, OBJ_NAME, TYPENAME) \
587 static inline G_GNUC_UNUSED ClassType * \
588 OBJ_NAME##_GET_CLASS(const void *obj) \
589 { return OBJECT_GET_CLASS(ClassType, obj, TYPENAME); } \
591 static inline G_GNUC_UNUSED ClassType * \
592 OBJ_NAME##_CLASS(const void *klass) \
593 { return OBJECT_CLASS_CHECK(ClassType, klass, TYPENAME); }
596 * DECLARE_OBJ_CHECKERS:
597 * @InstanceType: instance struct name
598 * @ClassType: class struct name
599 * @OBJ_NAME: the object name in uppercase with underscore separators
600 * @TYPENAME: type name
602 * Direct usage of this macro should be avoided, and the complete
603 * OBJECT_DECLARE_TYPE macro is recommended instead.
605 * This macro will provide the three standard type cast functions for a
608 #define DECLARE_OBJ_CHECKERS(InstanceType, ClassType, OBJ_NAME, TYPENAME) \
609 DECLARE_INSTANCE_CHECKER(InstanceType, OBJ_NAME, TYPENAME) \
611 DECLARE_CLASS_CHECKERS(ClassType, OBJ_NAME, TYPENAME)
614 * OBJECT_DECLARE_TYPE:
615 * @InstanceType: instance struct name
616 * @ClassType: class struct name
617 * @module_obj_name: the object name in lowercase with underscore separators
618 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
620 * This macro is typically used in a header file, and will:
622 * - create the typedefs for the object and class structs
623 * - register the type for use with g_autoptr
624 * - provide three standard type cast functions
626 * The object struct and class struct need to be declared manually.
628 #define OBJECT_DECLARE_TYPE(InstanceType, ClassType, module_obj_name, MODULE_OBJ_NAME) \
629 typedef struct InstanceType InstanceType; \
630 typedef struct ClassType ClassType; \
632 G_DEFINE_AUTOPTR_CLEANUP_FUNC(InstanceType, object_unref) \
634 DECLARE_OBJ_CHECKERS(InstanceType, ClassType, \
635 MODULE_OBJ_NAME, TYPE_##MODULE_OBJ_NAME)
638 * OBJECT_DECLARE_SIMPLE_TYPE:
639 * @InstanceType: instance struct name
640 * @module_obj_name: the object name in lowercase with underscore separators
641 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
642 * @ParentClassType: class struct name of parent type
644 * This does the same as OBJECT_DECLARE_TYPE(), but also declares
645 * the class struct, thus only the object struct needs to be declare
648 * This macro should be used unless the class struct needs to have
649 * virtual methods declared.
651 #define OBJECT_DECLARE_SIMPLE_TYPE(InstanceType, module_obj_name, \
652 MODULE_OBJ_NAME, ParentClassType) \
653 OBJECT_DECLARE_TYPE(InstanceType, InstanceType##Class, module_obj_name, MODULE_OBJ_NAME) \
654 struct InstanceType##Class { ParentClassType parent_class; };
658 * OBJECT_DEFINE_TYPE_EXTENDED:
659 * @ModuleObjName: the object name with initial caps
660 * @module_obj_name: the object name in lowercase with underscore separators
661 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
662 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
664 * @ABSTRACT: boolean flag to indicate whether the object can be instantiated
665 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
667 * This macro is typically used in a source file, and will:
669 * - declare prototypes for _finalize, _class_init and _init methods
670 * - declare the TypeInfo struct instance
671 * - provide the constructor to register the type
673 * After using this macro, implementations of the _finalize, _class_init,
674 * and _init methods need to be written. Any of these can be zero-line
675 * no-op impls if no special logic is required for a given type.
677 * This macro should rarely be used, instead one of the more specialized
678 * macros is usually a better choice.
680 #define OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
681 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
684 module_obj_name##_finalize(Object *obj); \
686 module_obj_name##_class_init(ObjectClass *oc, void *data); \
688 module_obj_name##_init(Object *obj); \
690 static const TypeInfo module_obj_name##_info = { \
691 .parent = TYPE_##PARENT_MODULE_OBJ_NAME, \
692 .name = TYPE_##MODULE_OBJ_NAME, \
693 .instance_size = sizeof(ModuleObjName), \
694 .instance_init = module_obj_name##_init, \
695 .instance_finalize = module_obj_name##_finalize, \
696 .class_size = sizeof(ModuleObjName##Class), \
697 .class_init = module_obj_name##_class_init, \
698 .abstract = ABSTRACT, \
699 .interfaces = (InterfaceInfo[]) { __VA_ARGS__ } , \
703 module_obj_name##_register_types(void) \
705 type_register_static(&module_obj_name##_info); \
707 type_init(module_obj_name##_register_types);
710 * OBJECT_DEFINE_TYPE:
711 * @ModuleObjName: the object name with initial caps
712 * @module_obj_name: the object name in lowercase with underscore separators
713 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
714 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
717 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
718 * for the common case of a non-abstract type, without any interfaces.
720 #define OBJECT_DEFINE_TYPE(ModuleObjName, module_obj_name, MODULE_OBJ_NAME, \
721 PARENT_MODULE_OBJ_NAME) \
722 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
723 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
727 * OBJECT_DEFINE_TYPE_WITH_INTERFACES:
728 * @ModuleObjName: the object name with initial caps
729 * @module_obj_name: the object name in lowercase with underscore separators
730 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
731 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
733 * @...: list of initializers for "InterfaceInfo" to declare implemented interfaces
735 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
736 * for the common case of a non-abstract type, with one or more implemented
739 * Note when passing the list of interfaces, be sure to include the final
740 * NULL entry, e.g. { TYPE_USER_CREATABLE }, { NULL }
742 #define OBJECT_DEFINE_TYPE_WITH_INTERFACES(ModuleObjName, module_obj_name, \
744 PARENT_MODULE_OBJ_NAME, ...) \
745 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
746 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
750 * OBJECT_DEFINE_ABSTRACT_TYPE:
751 * @ModuleObjName: the object name with initial caps
752 * @module_obj_name: the object name in lowercase with underscore separators
753 * @MODULE_OBJ_NAME: the object name in uppercase with underscore separators
754 * @PARENT_MODULE_OBJ_NAME: the parent object name in uppercase with underscore
757 * This is a specialization of OBJECT_DEFINE_TYPE_EXTENDED, which is suitable
758 * for defining an abstract type, without any interfaces.
760 #define OBJECT_DEFINE_ABSTRACT_TYPE(ModuleObjName, module_obj_name, \
761 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME) \
762 OBJECT_DEFINE_TYPE_EXTENDED(ModuleObjName, module_obj_name, \
763 MODULE_OBJ_NAME, PARENT_MODULE_OBJ_NAME, \
768 * @name: The name of the type.
769 * @parent: The name of the parent type.
770 * @instance_size: The size of the object (derivative of #Object). If
771 * @instance_size is 0, then the size of the object will be the size of the
773 * @instance_init: This function is called to initialize an object. The parent
774 * class will have already been initialized so the type is only responsible
775 * for initializing its own members.
776 * @instance_post_init: This function is called to finish initialization of
777 * an object, after all @instance_init functions were called.
778 * @instance_finalize: This function is called during object destruction. This
779 * is called before the parent @instance_finalize function has been called.
780 * An object should only free the members that are unique to its type in this
782 * @abstract: If this field is true, then the class is considered abstract and
783 * cannot be directly instantiated.
784 * @class_size: The size of the class object (derivative of #ObjectClass)
785 * for this object. If @class_size is 0, then the size of the class will be
786 * assumed to be the size of the parent class. This allows a type to avoid
787 * implementing an explicit class type if they are not adding additional
789 * @class_init: This function is called after all parent class initialization
790 * has occurred to allow a class to set its default virtual method pointers.
791 * This is also the function to use to override virtual methods from a parent
793 * @class_base_init: This function is called for all base classes after all
794 * parent class initialization has occurred, but before the class itself
795 * is initialized. This is the function to use to undo the effects of
796 * memcpy from the parent class to the descendants.
797 * @class_data: Data to pass to the @class_init,
798 * @class_base_init. This can be useful when building dynamic
800 * @interfaces: The list of interfaces associated with this type. This
801 * should point to a static array that's terminated with a zero filled
809 size_t instance_size
;
810 void (*instance_init
)(Object
*obj
);
811 void (*instance_post_init
)(Object
*obj
);
812 void (*instance_finalize
)(Object
*obj
);
817 void (*class_init
)(ObjectClass
*klass
, void *data
);
818 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
821 InterfaceInfo
*interfaces
;
826 * @obj: A derivative of #Object
828 * Converts an object to a #Object. Since all objects are #Objects,
829 * this function will always succeed.
831 #define OBJECT(obj) \
836 * @class: A derivative of #ObjectClass.
838 * Converts a class to an #ObjectClass. Since all objects are #Objects,
839 * this function will always succeed.
841 #define OBJECT_CLASS(class) \
842 ((ObjectClass *)(class))
846 * @type: The C type to use for the return value.
847 * @obj: A derivative of @type to cast.
848 * @name: The QOM typename of @type
850 * A type safe version of @object_dynamic_cast_assert. Typically each class
851 * will define a macro based on this type to perform type safe dynamic_casts to
854 * If an invalid object is passed to this function, a run time assert will be
857 #define OBJECT_CHECK(type, obj, name) \
858 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
859 __FILE__, __LINE__, __func__))
862 * OBJECT_CLASS_CHECK:
863 * @class_type: The C type to use for the return value.
864 * @class: A derivative class of @class_type to cast.
865 * @name: the QOM typename of @class_type.
867 * A type safe version of @object_class_dynamic_cast_assert. This macro is
868 * typically wrapped by each type to perform type safe casts of a class to a
869 * specific class type.
871 #define OBJECT_CLASS_CHECK(class_type, class, name) \
872 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
873 __FILE__, __LINE__, __func__))
877 * @class: The C type to use for the return value.
878 * @obj: The object to obtain the class for.
879 * @name: The QOM typename of @obj.
881 * This function will return a specific class for a given object. Its generally
882 * used by each type to provide a type safe macro to get a specific class type
885 #define OBJECT_GET_CLASS(class, obj, name) \
886 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
890 * @type: The name of the interface.
892 * The information associated with an interface.
894 struct InterfaceInfo
{
900 * @parent_class: the base class
902 * The class for all interfaces. Subclasses of this class should only add
905 struct InterfaceClass
907 ObjectClass parent_class
;
909 ObjectClass
*concrete_class
;
913 #define TYPE_INTERFACE "interface"
917 * @klass: class to cast from
918 * Returns: An #InterfaceClass or raise an error if cast is invalid
920 #define INTERFACE_CLASS(klass) \
921 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
925 * @interface: the type to return
926 * @obj: the object to convert to an interface
927 * @name: the interface type name
929 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
931 #define INTERFACE_CHECK(interface, obj, name) \
932 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
933 __FILE__, __LINE__, __func__))
936 * object_new_with_class:
937 * @klass: The class to instantiate.
939 * This function will initialize a new object using heap allocated memory.
940 * The returned object has a reference count of 1, and will be freed when
941 * the last reference is dropped.
943 * Returns: The newly allocated and instantiated object.
945 Object
*object_new_with_class(ObjectClass
*klass
);
949 * @typename: The name of the type of the object to instantiate.
951 * This function will initialize a new object using heap allocated memory.
952 * The returned object has a reference count of 1, and will be freed when
953 * the last reference is dropped.
955 * Returns: The newly allocated and instantiated object.
957 Object
*object_new(const char *typename
);
960 * object_new_with_props:
961 * @typename: The name of the type of the object to instantiate.
962 * @parent: the parent object
963 * @id: The unique ID of the object
964 * @errp: pointer to error object
965 * @...: list of property names and values
967 * This function will initialize a new object using heap allocated memory.
968 * The returned object has a reference count of 1, and will be freed when
969 * the last reference is dropped.
971 * The @id parameter will be used when registering the object as a
972 * child of @parent in the composition tree.
974 * The variadic parameters are a list of pairs of (propname, propvalue)
975 * strings. The propname of %NULL indicates the end of the property
976 * list. If the object implements the user creatable interface, the
977 * object will be marked complete once all the properties have been
981 * <title>Creating an object with properties</title>
986 * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE,
987 * object_get_objects_root(),
991 * "mem-path", "/dev/shm/somefile",
997 * error_reportf_err(err, "Cannot create memory backend: ");
1002 * The returned object will have one stable reference maintained
1003 * for as long as it is present in the object hierarchy.
1005 * Returns: The newly allocated, instantiated & initialized object.
1007 Object
*object_new_with_props(const char *typename
,
1014 * object_new_with_propv:
1015 * @typename: The name of the type of the object to instantiate.
1016 * @parent: the parent object
1017 * @id: The unique ID of the object
1018 * @errp: pointer to error object
1019 * @vargs: list of property names and values
1021 * See object_new_with_props() for documentation.
1023 Object
*object_new_with_propv(const char *typename
,
1029 bool object_apply_global_props(Object
*obj
, const GPtrArray
*props
,
1031 void object_set_machine_compat_props(GPtrArray
*compat_props
);
1032 void object_set_accelerator_compat_props(GPtrArray
*compat_props
);
1033 void object_register_sugar_prop(const char *driver
, const char *prop
, const char *value
);
1034 void object_apply_compat_props(Object
*obj
);
1038 * @obj: the object instance to set properties on
1039 * @errp: pointer to error object
1040 * @...: list of property names and values
1042 * This function will set a list of properties on an existing object
1045 * The variadic parameters are a list of pairs of (propname, propvalue)
1046 * strings. The propname of %NULL indicates the end of the property
1050 * <title>Update an object's properties</title>
1052 * Error *err = NULL;
1053 * Object *obj = ...get / create object...;
1055 * if (!object_set_props(obj,
1058 * "mem-path", "/dev/shm/somefile",
1059 * "prealloc", "yes",
1060 * "size", "1048576",
1062 * error_reportf_err(err, "Cannot set properties: ");
1067 * The returned object will have one stable reference maintained
1068 * for as long as it is present in the object hierarchy.
1070 * Returns: %true on success, %false on error.
1072 bool object_set_props(Object
*obj
, Error
**errp
, ...) QEMU_SENTINEL
;
1076 * @obj: the object instance to set properties on
1077 * @errp: pointer to error object
1078 * @vargs: list of property names and values
1080 * See object_set_props() for documentation.
1082 * Returns: %true on success, %false on error.
1084 bool object_set_propv(Object
*obj
, Error
**errp
, va_list vargs
);
1087 * object_initialize:
1088 * @obj: A pointer to the memory to be used for the object.
1089 * @size: The maximum size available at @obj for the object.
1090 * @typename: The name of the type of the object to instantiate.
1092 * This function will initialize an object. The memory for the object should
1093 * have already been allocated. The returned object has a reference count of 1,
1094 * and will be finalized when the last reference is dropped.
1096 void object_initialize(void *obj
, size_t size
, const char *typename
);
1099 * object_initialize_child_with_props:
1100 * @parentobj: The parent object to add a property to
1101 * @propname: The name of the property
1102 * @childobj: A pointer to the memory to be used for the object.
1103 * @size: The maximum size available at @childobj for the object.
1104 * @type: The name of the type of the object to instantiate.
1105 * @errp: If an error occurs, a pointer to an area to store the error
1106 * @...: list of property names and values
1108 * This function will initialize an object. The memory for the object should
1109 * have already been allocated. The object will then be added as child property
1110 * to a parent with object_property_add_child() function. The returned object
1111 * has a reference count of 1 (for the "child<...>" property from the parent),
1112 * so the object will be finalized automatically when the parent gets removed.
1114 * The variadic parameters are a list of pairs of (propname, propvalue)
1115 * strings. The propname of %NULL indicates the end of the property list.
1116 * If the object implements the user creatable interface, the object will
1117 * be marked complete once all the properties have been processed.
1119 * Returns: %true on success, %false on failure.
1121 bool object_initialize_child_with_props(Object
*parentobj
,
1122 const char *propname
,
1123 void *childobj
, size_t size
, const char *type
,
1124 Error
**errp
, ...) QEMU_SENTINEL
;
1127 * object_initialize_child_with_propsv:
1128 * @parentobj: The parent object to add a property to
1129 * @propname: The name of the property
1130 * @childobj: A pointer to the memory to be used for the object.
1131 * @size: The maximum size available at @childobj for the object.
1132 * @type: The name of the type of the object to instantiate.
1133 * @errp: If an error occurs, a pointer to an area to store the error
1134 * @vargs: list of property names and values
1136 * See object_initialize_child() for documentation.
1138 * Returns: %true on success, %false on failure.
1140 bool object_initialize_child_with_propsv(Object
*parentobj
,
1141 const char *propname
,
1142 void *childobj
, size_t size
, const char *type
,
1143 Error
**errp
, va_list vargs
);
1146 * object_initialize_child:
1147 * @parent: The parent object to add a property to
1148 * @propname: The name of the property
1149 * @child: A precisely typed pointer to the memory to be used for the
1151 * @type: The name of the type of the object to instantiate.
1154 * object_initialize_child_with_props(parent, propname,
1155 * child, sizeof(*child), type,
1156 * &error_abort, NULL)
1158 #define object_initialize_child(parent, propname, child, type) \
1159 object_initialize_child_internal((parent), (propname), \
1160 (child), sizeof(*(child)), (type))
1161 void object_initialize_child_internal(Object
*parent
, const char *propname
,
1162 void *child
, size_t size
,
1166 * object_dynamic_cast:
1167 * @obj: The object to cast.
1168 * @typename: The @typename to cast to.
1170 * This function will determine if @obj is-a @typename. @obj can refer to an
1171 * object or an interface associated with an object.
1173 * Returns: This function returns @obj on success or #NULL on failure.
1175 Object
*object_dynamic_cast(Object
*obj
, const char *typename
);
1178 * object_dynamic_cast_assert:
1180 * See object_dynamic_cast() for a description of the parameters of this
1181 * function. The only difference in behavior is that this function asserts
1182 * instead of returning #NULL on failure if QOM cast debugging is enabled.
1183 * This function is not meant to be called directly, but only through
1184 * the wrapper macro OBJECT_CHECK.
1186 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
1187 const char *file
, int line
, const char *func
);
1191 * @obj: A derivative of #Object
1193 * Returns: The #ObjectClass of the type associated with @obj.
1195 ObjectClass
*object_get_class(Object
*obj
);
1198 * object_get_typename:
1199 * @obj: A derivative of #Object.
1201 * Returns: The QOM typename of @obj.
1203 const char *object_get_typename(const Object
*obj
);
1206 * type_register_static:
1207 * @info: The #TypeInfo of the new type.
1209 * @info and all of the strings it points to should exist for the life time
1210 * that the type is registered.
1212 * Returns: the new #Type.
1214 Type
type_register_static(const TypeInfo
*info
);
1218 * @info: The #TypeInfo of the new type
1220 * Unlike type_register_static(), this call does not require @info or its
1221 * string members to continue to exist after the call returns.
1223 * Returns: the new #Type.
1225 Type
type_register(const TypeInfo
*info
);
1228 * type_register_static_array:
1229 * @infos: The array of the new type #TypeInfo structures.
1230 * @nr_infos: number of entries in @infos
1232 * @infos and all of the strings it points to should exist for the life time
1233 * that the type is registered.
1235 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
);
1239 * @type_array: The array containing #TypeInfo structures to register
1241 * @type_array should be static constant that exists for the life time
1242 * that the type is registered.
1244 #define DEFINE_TYPES(type_array) \
1245 static void do_qemu_init_ ## type_array(void) \
1247 type_register_static_array(type_array, ARRAY_SIZE(type_array)); \
1249 type_init(do_qemu_init_ ## type_array)
1252 * object_class_dynamic_cast_assert:
1253 * @klass: The #ObjectClass to attempt to cast.
1254 * @typename: The QOM typename of the class to cast to.
1256 * See object_class_dynamic_cast() for a description of the parameters
1257 * of this function. The only difference in behavior is that this function
1258 * asserts instead of returning #NULL on failure if QOM cast debugging is
1259 * enabled. This function is not meant to be called directly, but only through
1260 * the wrapper macro OBJECT_CLASS_CHECK.
1262 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*klass
,
1263 const char *typename
,
1264 const char *file
, int line
,
1268 * object_class_dynamic_cast:
1269 * @klass: The #ObjectClass to attempt to cast.
1270 * @typename: The QOM typename of the class to cast to.
1272 * Returns: If @typename is a class, this function returns @klass if
1273 * @typename is a subtype of @klass, else returns #NULL.
1275 * If @typename is an interface, this function returns the interface
1276 * definition for @klass if @klass implements it unambiguously; #NULL
1277 * is returned if @klass does not implement the interface or if multiple
1278 * classes or interfaces on the hierarchy leading to @klass implement
1279 * it. (FIXME: perhaps this can be detected at type definition time?)
1281 ObjectClass
*object_class_dynamic_cast(ObjectClass
*klass
,
1282 const char *typename
);
1285 * object_class_get_parent:
1286 * @klass: The class to obtain the parent for.
1288 * Returns: The parent for @klass or %NULL if none.
1290 ObjectClass
*object_class_get_parent(ObjectClass
*klass
);
1293 * object_class_get_name:
1294 * @klass: The class to obtain the QOM typename for.
1296 * Returns: The QOM typename for @klass.
1298 const char *object_class_get_name(ObjectClass
*klass
);
1301 * object_class_is_abstract:
1302 * @klass: The class to obtain the abstractness for.
1304 * Returns: %true if @klass is abstract, %false otherwise.
1306 bool object_class_is_abstract(ObjectClass
*klass
);
1309 * object_class_by_name:
1310 * @typename: The QOM typename to obtain the class for.
1312 * Returns: The class for @typename or %NULL if not found.
1314 ObjectClass
*object_class_by_name(const char *typename
);
1317 * module_object_class_by_name:
1318 * @typename: The QOM typename to obtain the class for.
1320 * For objects which might be provided by a module. Behaves like
1321 * object_class_by_name, but additionally tries to load the module
1322 * needed in case the class is not available.
1324 * Returns: The class for @typename or %NULL if not found.
1326 ObjectClass
*module_object_class_by_name(const char *typename
);
1328 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
1329 const char *implements_type
, bool include_abstract
,
1333 * object_class_get_list:
1334 * @implements_type: The type to filter for, including its derivatives.
1335 * @include_abstract: Whether to include abstract classes.
1337 * Returns: A singly-linked list of the classes in reverse hashtable order.
1339 GSList
*object_class_get_list(const char *implements_type
,
1340 bool include_abstract
);
1343 * object_class_get_list_sorted:
1344 * @implements_type: The type to filter for, including its derivatives.
1345 * @include_abstract: Whether to include abstract classes.
1347 * Returns: A singly-linked list of the classes in alphabetical
1348 * case-insensitive order.
1350 GSList
*object_class_get_list_sorted(const char *implements_type
,
1351 bool include_abstract
);
1357 * Increase the reference count of a object. A object cannot be freed as long
1358 * as its reference count is greater than zero.
1361 Object
*object_ref(void *obj
);
1367 * Decrease the reference count of a object. A object cannot be freed as long
1368 * as its reference count is greater than zero.
1370 void object_unref(void *obj
);
1373 * object_property_try_add:
1374 * @obj: the object to add a property to
1375 * @name: the name of the property. This can contain any character except for
1376 * a forward slash. In general, you should use hyphens '-' instead of
1377 * underscores '_' when naming properties.
1378 * @type: the type name of the property. This namespace is pretty loosely
1379 * defined. Sub namespaces are constructed by using a prefix and then
1380 * to angle brackets. For instance, the type 'virtio-net-pci' in the
1381 * 'link' namespace would be 'link<virtio-net-pci>'.
1382 * @get: The getter to be called to read a property. If this is NULL, then
1383 * the property cannot be read.
1384 * @set: the setter to be called to write a property. If this is NULL,
1385 * then the property cannot be written.
1386 * @release: called when the property is removed from the object. This is
1387 * meant to allow a property to free its opaque upon object
1388 * destruction. This may be NULL.
1389 * @opaque: an opaque pointer to pass to the callbacks for the property
1390 * @errp: pointer to error object
1392 * Returns: The #ObjectProperty; this can be used to set the @resolve
1393 * callback for child and link properties.
1395 ObjectProperty
*object_property_try_add(Object
*obj
, const char *name
,
1397 ObjectPropertyAccessor
*get
,
1398 ObjectPropertyAccessor
*set
,
1399 ObjectPropertyRelease
*release
,
1400 void *opaque
, Error
**errp
);
1403 * object_property_add:
1404 * Same as object_property_try_add() with @errp hardcoded to
1407 ObjectProperty
*object_property_add(Object
*obj
, const char *name
,
1409 ObjectPropertyAccessor
*get
,
1410 ObjectPropertyAccessor
*set
,
1411 ObjectPropertyRelease
*release
,
1414 void object_property_del(Object
*obj
, const char *name
);
1416 ObjectProperty
*object_class_property_add(ObjectClass
*klass
, const char *name
,
1418 ObjectPropertyAccessor
*get
,
1419 ObjectPropertyAccessor
*set
,
1420 ObjectPropertyRelease
*release
,
1424 * object_property_set_default_bool:
1425 * @prop: the property to set
1426 * @value: the value to be written to the property
1428 * Set the property default value.
1430 void object_property_set_default_bool(ObjectProperty
*prop
, bool value
);
1433 * object_property_set_default_str:
1434 * @prop: the property to set
1435 * @value: the value to be written to the property
1437 * Set the property default value.
1439 void object_property_set_default_str(ObjectProperty
*prop
, const char *value
);
1442 * object_property_set_default_int:
1443 * @prop: the property to set
1444 * @value: the value to be written to the property
1446 * Set the property default value.
1448 void object_property_set_default_int(ObjectProperty
*prop
, int64_t value
);
1451 * object_property_set_default_uint:
1452 * @prop: the property to set
1453 * @value: the value to be written to the property
1455 * Set the property default value.
1457 void object_property_set_default_uint(ObjectProperty
*prop
, uint64_t value
);
1460 * object_property_find:
1462 * @name: the name of the property
1463 * @errp: returns an error if this function fails
1465 * Look up a property for an object and return its #ObjectProperty if found.
1467 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1469 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1472 typedef struct ObjectPropertyIterator
{
1473 ObjectClass
*nextclass
;
1474 GHashTableIter iter
;
1475 } ObjectPropertyIterator
;
1478 * object_property_iter_init:
1481 * Initializes an iterator for traversing all properties
1482 * registered against an object instance, its class and all parent classes.
1484 * It is forbidden to modify the property list while iterating,
1485 * whether removing or adding properties.
1487 * Typical usage pattern would be
1490 * <title>Using object property iterators</title>
1492 * ObjectProperty *prop;
1493 * ObjectPropertyIterator iter;
1495 * object_property_iter_init(&iter, obj);
1496 * while ((prop = object_property_iter_next(&iter))) {
1497 * ... do something with prop ...
1502 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1506 * object_class_property_iter_init:
1509 * Initializes an iterator for traversing all properties
1510 * registered against an object class and all parent classes.
1512 * It is forbidden to modify the property list while iterating,
1513 * whether removing or adding properties.
1515 * This can be used on abstract classes as it does not create a temporary
1518 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1519 ObjectClass
*klass
);
1522 * object_property_iter_next:
1523 * @iter: the iterator instance
1525 * Return the next available property. If no further properties
1526 * are available, a %NULL value will be returned and the @iter
1527 * pointer should not be used again after this point without
1528 * re-initializing it.
1530 * Returns: the next property, or %NULL when all properties
1531 * have been traversed.
1533 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
);
1535 void object_unparent(Object
*obj
);
1538 * object_property_get:
1540 * @name: the name of the property
1541 * @v: the visitor that will receive the property value. This should be an
1542 * Output visitor and the data will be written with @name as the name.
1543 * @errp: returns an error if this function fails
1545 * Reads a property from a object.
1547 * Returns: %true on success, %false on failure.
1549 bool object_property_get(Object
*obj
, const char *name
, Visitor
*v
,
1553 * object_property_set_str:
1554 * @name: the name of the property
1555 * @value: the value to be written to the property
1556 * @errp: returns an error if this function fails
1558 * Writes a string value to a property.
1560 * Returns: %true on success, %false on failure.
1562 bool object_property_set_str(Object
*obj
, const char *name
,
1563 const char *value
, Error
**errp
);
1566 * object_property_get_str:
1568 * @name: the name of the property
1569 * @errp: returns an error if this function fails
1571 * Returns: the value of the property, converted to a C string, or NULL if
1572 * an error occurs (including when the property value is not a string).
1573 * The caller should free the string.
1575 char *object_property_get_str(Object
*obj
, const char *name
,
1579 * object_property_set_link:
1580 * @name: the name of the property
1581 * @value: the value to be written to the property
1582 * @errp: returns an error if this function fails
1584 * Writes an object's canonical path to a property.
1586 * If the link property was created with
1587 * <code>OBJ_PROP_LINK_STRONG</code> bit, the old target object is
1588 * unreferenced, and a reference is added to the new target object.
1590 * Returns: %true on success, %false on failure.
1592 bool object_property_set_link(Object
*obj
, const char *name
,
1593 Object
*value
, Error
**errp
);
1596 * object_property_get_link:
1598 * @name: the name of the property
1599 * @errp: returns an error if this function fails
1601 * Returns: the value of the property, resolved from a path to an Object,
1602 * or NULL if an error occurs (including when the property value is not a
1603 * string or not a valid object path).
1605 Object
*object_property_get_link(Object
*obj
, const char *name
,
1609 * object_property_set_bool:
1610 * @name: the name of the property
1611 * @value: the value to be written to the property
1612 * @errp: returns an error if this function fails
1614 * Writes a bool value to a property.
1616 * Returns: %true on success, %false on failure.
1618 bool object_property_set_bool(Object
*obj
, const char *name
,
1619 bool value
, Error
**errp
);
1622 * object_property_get_bool:
1624 * @name: the name of the property
1625 * @errp: returns an error if this function fails
1627 * Returns: the value of the property, converted to a boolean, or NULL if
1628 * an error occurs (including when the property value is not a bool).
1630 bool object_property_get_bool(Object
*obj
, const char *name
,
1634 * object_property_set_int:
1635 * @name: the name of the property
1636 * @value: the value to be written to the property
1637 * @errp: returns an error if this function fails
1639 * Writes an integer value to a property.
1641 * Returns: %true on success, %false on failure.
1643 bool object_property_set_int(Object
*obj
, const char *name
,
1644 int64_t value
, Error
**errp
);
1647 * object_property_get_int:
1649 * @name: the name of the property
1650 * @errp: returns an error if this function fails
1652 * Returns: the value of the property, converted to an integer, or negative if
1653 * an error occurs (including when the property value is not an integer).
1655 int64_t object_property_get_int(Object
*obj
, const char *name
,
1659 * object_property_set_uint:
1660 * @name: the name of the property
1661 * @value: the value to be written to the property
1662 * @errp: returns an error if this function fails
1664 * Writes an unsigned integer value to a property.
1666 * Returns: %true on success, %false on failure.
1668 bool object_property_set_uint(Object
*obj
, const char *name
,
1669 uint64_t value
, Error
**errp
);
1672 * object_property_get_uint:
1674 * @name: the name of the property
1675 * @errp: returns an error if this function fails
1677 * Returns: the value of the property, converted to an unsigned integer, or 0
1678 * an error occurs (including when the property value is not an integer).
1680 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1684 * object_property_get_enum:
1686 * @name: the name of the property
1687 * @typename: the name of the enum data type
1688 * @errp: returns an error if this function fails
1690 * Returns: the value of the property, converted to an integer (which
1691 * can't be negative), or -1 on error (including when the property
1692 * value is not an enum).
1694 int object_property_get_enum(Object
*obj
, const char *name
,
1695 const char *typename
, Error
**errp
);
1698 * object_property_set:
1700 * @name: the name of the property
1701 * @v: the visitor that will be used to write the property value. This should
1702 * be an Input visitor and the data will be first read with @name as the
1703 * name and then written as the property value.
1704 * @errp: returns an error if this function fails
1706 * Writes a property to a object.
1708 * Returns: %true on success, %false on failure.
1710 bool object_property_set(Object
*obj
, const char *name
, Visitor
*v
,
1714 * object_property_parse:
1716 * @name: the name of the property
1717 * @string: the string that will be used to parse the property value.
1718 * @errp: returns an error if this function fails
1720 * Parses a string and writes the result into a property of an object.
1722 * Returns: %true on success, %false on failure.
1724 bool object_property_parse(Object
*obj
, const char *name
,
1725 const char *string
, Error
**errp
);
1728 * object_property_print:
1730 * @name: the name of the property
1731 * @human: if true, print for human consumption
1732 * @errp: returns an error if this function fails
1734 * Returns a string representation of the value of the property. The
1735 * caller shall free the string.
1737 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1741 * object_property_get_type:
1743 * @name: the name of the property
1744 * @errp: returns an error if this function fails
1746 * Returns: The type name of the property.
1748 const char *object_property_get_type(Object
*obj
, const char *name
,
1754 * Returns: the root object of the composition tree
1756 Object
*object_get_root(void);
1760 * object_get_objects_root:
1762 * Get the container object that holds user created
1763 * object instances. This is the object at path
1766 * Returns: the user object container
1768 Object
*object_get_objects_root(void);
1771 * object_get_internal_root:
1773 * Get the container object that holds internally used object
1774 * instances. Any object which is put into this container must not be
1775 * user visible, and it will not be exposed in the QOM tree.
1777 * Returns: the internal object container
1779 Object
*object_get_internal_root(void);
1782 * object_get_canonical_path_component:
1784 * Returns: The final component in the object's canonical path. The canonical
1785 * path is the path within the composition tree starting from the root.
1786 * %NULL if the object doesn't have a parent (and thus a canonical path).
1788 const char *object_get_canonical_path_component(const Object
*obj
);
1791 * object_get_canonical_path:
1793 * Returns: The canonical path for a object, newly allocated. This is
1794 * the path within the composition tree starting from the root. Use
1795 * g_free() to free it.
1797 char *object_get_canonical_path(const Object
*obj
);
1800 * object_resolve_path:
1801 * @path: the path to resolve
1802 * @ambiguous: returns true if the path resolution failed because of an
1805 * There are two types of supported paths--absolute paths and partial paths.
1807 * Absolute paths are derived from the root object and can follow child<> or
1808 * link<> properties. Since they can follow link<> properties, they can be
1809 * arbitrarily long. Absolute paths look like absolute filenames and are
1810 * prefixed with a leading slash.
1812 * Partial paths look like relative filenames. They do not begin with a
1813 * prefix. The matching rules for partial paths are subtle but designed to make
1814 * specifying objects easy. At each level of the composition tree, the partial
1815 * path is matched as an absolute path. The first match is not returned. At
1816 * least two matches are searched for. A successful result is only returned if
1817 * only one match is found. If more than one match is found, a flag is
1818 * returned to indicate that the match was ambiguous.
1820 * Returns: The matched object or NULL on path lookup failure.
1822 Object
*object_resolve_path(const char *path
, bool *ambiguous
);
1825 * object_resolve_path_type:
1826 * @path: the path to resolve
1827 * @typename: the type to look for.
1828 * @ambiguous: returns true if the path resolution failed because of an
1831 * This is similar to object_resolve_path. However, when looking for a
1832 * partial path only matches that implement the given type are considered.
1833 * This restricts the search and avoids spuriously flagging matches as
1836 * For both partial and absolute paths, the return value goes through
1837 * a dynamic cast to @typename. This is important if either the link,
1838 * or the typename itself are of interface types.
1840 * Returns: The matched object or NULL on path lookup failure.
1842 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1846 * object_resolve_path_component:
1847 * @parent: the object in which to resolve the path
1848 * @part: the component to resolve.
1850 * This is similar to object_resolve_path with an absolute path, but it
1851 * only resolves one element (@part) and takes the others from @parent.
1853 * Returns: The resolved object or NULL on path lookup failure.
1855 Object
*object_resolve_path_component(Object
*parent
, const char *part
);
1858 * object_property_try_add_child:
1859 * @obj: the object to add a property to
1860 * @name: the name of the property
1861 * @child: the child object
1862 * @errp: pointer to error object
1864 * Child properties form the composition tree. All objects need to be a child
1865 * of another object. Objects can only be a child of one object.
1867 * There is no way for a child to determine what its parent is. It is not
1868 * a bidirectional relationship. This is by design.
1870 * The value of a child property as a C string will be the child object's
1871 * canonical path. It can be retrieved using object_property_get_str().
1872 * The child object itself can be retrieved using object_property_get_link().
1874 * Returns: The newly added property on success, or %NULL on failure.
1876 ObjectProperty
*object_property_try_add_child(Object
*obj
, const char *name
,
1877 Object
*child
, Error
**errp
);
1880 * object_property_add_child:
1881 * Same as object_property_try_add_child() with @errp hardcoded to
1884 ObjectProperty
*object_property_add_child(Object
*obj
, const char *name
,
1888 /* Unref the link pointer when the property is deleted */
1889 OBJ_PROP_LINK_STRONG
= 0x1,
1892 OBJ_PROP_LINK_DIRECT
= 0x2,
1893 OBJ_PROP_LINK_CLASS
= 0x4,
1894 } ObjectPropertyLinkFlags
;
1897 * object_property_allow_set_link:
1899 * The default implementation of the object_property_add_link() check()
1900 * callback function. It allows the link property to be set and never returns
1903 void object_property_allow_set_link(const Object
*, const char *,
1904 Object
*, Error
**);
1907 * object_property_add_link:
1908 * @obj: the object to add a property to
1909 * @name: the name of the property
1910 * @type: the qobj type of the link
1911 * @targetp: a pointer to where the link object reference is stored
1912 * @check: callback to veto setting or NULL if the property is read-only
1913 * @flags: additional options for the link
1915 * Links establish relationships between objects. Links are unidirectional
1916 * although two links can be combined to form a bidirectional relationship
1919 * Links form the graph in the object model.
1921 * The <code>@check()</code> callback is invoked when
1922 * object_property_set_link() is called and can raise an error to prevent the
1923 * link being set. If <code>@check</code> is NULL, the property is read-only
1924 * and cannot be set.
1926 * Ownership of the pointer that @child points to is transferred to the
1927 * link property. The reference count for <code>*@child</code> is
1928 * managed by the property from after the function returns till the
1929 * property is deleted with object_property_del(). If the
1930 * <code>@flags</code> <code>OBJ_PROP_LINK_STRONG</code> bit is set,
1931 * the reference count is decremented when the property is deleted or
1934 * Returns: The newly added property on success, or %NULL on failure.
1936 ObjectProperty
*object_property_add_link(Object
*obj
, const char *name
,
1937 const char *type
, Object
**targetp
,
1938 void (*check
)(const Object
*obj
, const char *name
,
1939 Object
*val
, Error
**errp
),
1940 ObjectPropertyLinkFlags flags
);
1942 ObjectProperty
*object_class_property_add_link(ObjectClass
*oc
,
1944 const char *type
, ptrdiff_t offset
,
1945 void (*check
)(const Object
*obj
, const char *name
,
1946 Object
*val
, Error
**errp
),
1947 ObjectPropertyLinkFlags flags
);
1950 * object_property_add_str:
1951 * @obj: the object to add a property to
1952 * @name: the name of the property
1953 * @get: the getter or NULL if the property is write-only. This function must
1954 * return a string to be freed by g_free().
1955 * @set: the setter or NULL if the property is read-only
1957 * Add a string property using getters/setters. This function will add a
1958 * property of type 'string'.
1960 * Returns: The newly added property on success, or %NULL on failure.
1962 ObjectProperty
*object_property_add_str(Object
*obj
, const char *name
,
1963 char *(*get
)(Object
*, Error
**),
1964 void (*set
)(Object
*, const char *, Error
**));
1966 ObjectProperty
*object_class_property_add_str(ObjectClass
*klass
,
1968 char *(*get
)(Object
*, Error
**),
1969 void (*set
)(Object
*, const char *,
1973 * object_property_add_bool:
1974 * @obj: the object to add a property to
1975 * @name: the name of the property
1976 * @get: the getter or NULL if the property is write-only.
1977 * @set: the setter or NULL if the property is read-only
1979 * Add a bool property using getters/setters. This function will add a
1980 * property of type 'bool'.
1982 * Returns: The newly added property on success, or %NULL on failure.
1984 ObjectProperty
*object_property_add_bool(Object
*obj
, const char *name
,
1985 bool (*get
)(Object
*, Error
**),
1986 void (*set
)(Object
*, bool, Error
**));
1988 ObjectProperty
*object_class_property_add_bool(ObjectClass
*klass
,
1990 bool (*get
)(Object
*, Error
**),
1991 void (*set
)(Object
*, bool, Error
**));
1994 * object_property_add_enum:
1995 * @obj: the object to add a property to
1996 * @name: the name of the property
1997 * @typename: the name of the enum data type
1998 * @get: the getter or %NULL if the property is write-only.
1999 * @set: the setter or %NULL if the property is read-only
2001 * Add an enum property using getters/setters. This function will add a
2002 * property of type '@typename'.
2004 * Returns: The newly added property on success, or %NULL on failure.
2006 ObjectProperty
*object_property_add_enum(Object
*obj
, const char *name
,
2007 const char *typename
,
2008 const QEnumLookup
*lookup
,
2009 int (*get
)(Object
*, Error
**),
2010 void (*set
)(Object
*, int, Error
**));
2012 ObjectProperty
*object_class_property_add_enum(ObjectClass
*klass
,
2014 const char *typename
,
2015 const QEnumLookup
*lookup
,
2016 int (*get
)(Object
*, Error
**),
2017 void (*set
)(Object
*, int, Error
**));
2020 * object_property_add_tm:
2021 * @obj: the object to add a property to
2022 * @name: the name of the property
2023 * @get: the getter or NULL if the property is write-only.
2025 * Add a read-only struct tm valued property using a getter function.
2026 * This function will add a property of type 'struct tm'.
2028 * Returns: The newly added property on success, or %NULL on failure.
2030 ObjectProperty
*object_property_add_tm(Object
*obj
, const char *name
,
2031 void (*get
)(Object
*, struct tm
*, Error
**));
2033 ObjectProperty
*object_class_property_add_tm(ObjectClass
*klass
,
2035 void (*get
)(Object
*, struct tm
*, Error
**));
2038 /* Automatically add a getter to the property */
2039 OBJ_PROP_FLAG_READ
= 1 << 0,
2040 /* Automatically add a setter to the property */
2041 OBJ_PROP_FLAG_WRITE
= 1 << 1,
2042 /* Automatically add a getter and a setter to the property */
2043 OBJ_PROP_FLAG_READWRITE
= (OBJ_PROP_FLAG_READ
| OBJ_PROP_FLAG_WRITE
),
2044 } ObjectPropertyFlags
;
2047 * object_property_add_uint8_ptr:
2048 * @obj: the object to add a property to
2049 * @name: the name of the property
2050 * @v: pointer to value
2051 * @flags: bitwise-or'd ObjectPropertyFlags
2053 * Add an integer property in memory. This function will add a
2054 * property of type 'uint8'.
2056 * Returns: The newly added property on success, or %NULL on failure.
2058 ObjectProperty
*object_property_add_uint8_ptr(Object
*obj
, const char *name
,
2060 ObjectPropertyFlags flags
);
2062 ObjectProperty
*object_class_property_add_uint8_ptr(ObjectClass
*klass
,
2065 ObjectPropertyFlags flags
);
2068 * object_property_add_uint16_ptr:
2069 * @obj: the object to add a property to
2070 * @name: the name of the property
2071 * @v: pointer to value
2072 * @flags: bitwise-or'd ObjectPropertyFlags
2074 * Add an integer property in memory. This function will add a
2075 * property of type 'uint16'.
2077 * Returns: The newly added property on success, or %NULL on failure.
2079 ObjectProperty
*object_property_add_uint16_ptr(Object
*obj
, const char *name
,
2081 ObjectPropertyFlags flags
);
2083 ObjectProperty
*object_class_property_add_uint16_ptr(ObjectClass
*klass
,
2086 ObjectPropertyFlags flags
);
2089 * object_property_add_uint32_ptr:
2090 * @obj: the object to add a property to
2091 * @name: the name of the property
2092 * @v: pointer to value
2093 * @flags: bitwise-or'd ObjectPropertyFlags
2095 * Add an integer property in memory. This function will add a
2096 * property of type 'uint32'.
2098 * Returns: The newly added property on success, or %NULL on failure.
2100 ObjectProperty
*object_property_add_uint32_ptr(Object
*obj
, const char *name
,
2102 ObjectPropertyFlags flags
);
2104 ObjectProperty
*object_class_property_add_uint32_ptr(ObjectClass
*klass
,
2107 ObjectPropertyFlags flags
);
2110 * object_property_add_uint64_ptr:
2111 * @obj: the object to add a property to
2112 * @name: the name of the property
2113 * @v: pointer to value
2114 * @flags: bitwise-or'd ObjectPropertyFlags
2116 * Add an integer property in memory. This function will add a
2117 * property of type 'uint64'.
2119 * Returns: The newly added property on success, or %NULL on failure.
2121 ObjectProperty
*object_property_add_uint64_ptr(Object
*obj
, const char *name
,
2123 ObjectPropertyFlags flags
);
2125 ObjectProperty
*object_class_property_add_uint64_ptr(ObjectClass
*klass
,
2128 ObjectPropertyFlags flags
);
2131 * object_property_add_alias:
2132 * @obj: the object to add a property to
2133 * @name: the name of the property
2134 * @target_obj: the object to forward property access to
2135 * @target_name: the name of the property on the forwarded object
2137 * Add an alias for a property on an object. This function will add a property
2138 * of the same type as the forwarded property.
2140 * The caller must ensure that <code>@target_obj</code> stays alive as long as
2141 * this property exists. In the case of a child object or an alias on the same
2142 * object this will be the case. For aliases to other objects the caller is
2143 * responsible for taking a reference.
2145 * Returns: The newly added property on success, or %NULL on failure.
2147 ObjectProperty
*object_property_add_alias(Object
*obj
, const char *name
,
2148 Object
*target_obj
, const char *target_name
);
2151 * object_property_add_const_link:
2152 * @obj: the object to add a property to
2153 * @name: the name of the property
2154 * @target: the object to be referred by the link
2156 * Add an unmodifiable link for a property on an object. This function will
2157 * add a property of type link<TYPE> where TYPE is the type of @target.
2159 * The caller must ensure that @target stays alive as long as
2160 * this property exists. In the case @target is a child of @obj,
2161 * this will be the case. Otherwise, the caller is responsible for
2162 * taking a reference.
2164 * Returns: The newly added property on success, or %NULL on failure.
2166 ObjectProperty
*object_property_add_const_link(Object
*obj
, const char *name
,
2170 * object_property_set_description:
2171 * @obj: the object owning the property
2172 * @name: the name of the property
2173 * @description: the description of the property on the object
2175 * Set an object property's description.
2177 * Returns: %true on success, %false on failure.
2179 void object_property_set_description(Object
*obj
, const char *name
,
2180 const char *description
);
2181 void object_class_property_set_description(ObjectClass
*klass
, const char *name
,
2182 const char *description
);
2185 * object_child_foreach:
2186 * @obj: the object whose children will be navigated
2187 * @fn: the iterator function to be called
2188 * @opaque: an opaque value that will be passed to the iterator
2190 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
2193 * It is forbidden to add or remove children from @obj from the @fn
2196 * Returns: The last value returned by @fn, or 0 if there is no child.
2198 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
2202 * object_child_foreach_recursive:
2203 * @obj: the object whose children will be navigated
2204 * @fn: the iterator function to be called
2205 * @opaque: an opaque value that will be passed to the iterator
2207 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
2208 * non-zero. Calls recursively, all child nodes of @obj will also be passed
2209 * all the way down to the leaf nodes of the tree. Depth first ordering.
2211 * It is forbidden to add or remove children from @obj (or its
2212 * child nodes) from the @fn callback.
2214 * Returns: The last value returned by @fn, or 0 if there is no child.
2216 int object_child_foreach_recursive(Object
*obj
,
2217 int (*fn
)(Object
*child
, void *opaque
),
2221 * @root: root of the #path, e.g., object_get_root()
2222 * @path: path to the container
2224 * Return a container object whose path is @path. Create more containers
2225 * along the path if necessary.
2227 * Returns: the container object.
2229 Object
*container_get(Object
*root
, const char *path
);
2232 * object_type_get_instance_size:
2233 * @typename: Name of the Type whose instance_size is required
2235 * Returns the instance_size of the given @typename.
2237 size_t object_type_get_instance_size(const char *typename
);
2240 * object_property_help:
2241 * @name: the name of the property
2242 * @type: the type of the property
2243 * @defval: the default value
2244 * @description: description of the property
2246 * Returns: a user-friendly formatted string describing the property
2247 * for help purposes.
2249 char *object_property_help(const char *name
, const char *type
,
2250 QObject
*defval
, const char *description
);
2252 G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object
, object_unref
)