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/queue.h"
21 typedef struct TypeImpl
*Type
;
23 typedef struct ObjectClass ObjectClass
;
24 typedef struct Object Object
;
26 typedef struct TypeInfo TypeInfo
;
28 typedef struct InterfaceClass InterfaceClass
;
29 typedef struct InterfaceInfo InterfaceInfo
;
31 #define TYPE_OBJECT "object"
35 * @title:Base Object Type System
36 * @short_description: interfaces for creating new types and objects
38 * The QEMU Object Model provides a framework for registering user creatable
39 * types and instantiating objects from those types. QOM provides the following
42 * - System for dynamically registering types
43 * - Support for single-inheritance of types
44 * - Multiple inheritance of stateless interfaces
47 * <title>Creating a minimal type</title>
51 * #define TYPE_MY_DEVICE "my-device"
53 * // No new virtual functions: we can reuse the typedef for the
55 * typedef DeviceClass MyDeviceClass;
56 * typedef struct MyDevice
60 * int reg0, reg1, reg2;
63 * static const TypeInfo my_device_info = {
64 * .name = TYPE_MY_DEVICE,
65 * .parent = TYPE_DEVICE,
66 * .instance_size = sizeof(MyDevice),
69 * static void my_device_register_types(void)
71 * type_register_static(&my_device_info);
74 * type_init(my_device_register_types)
78 * In the above example, we create a simple type that is described by #TypeInfo.
79 * #TypeInfo describes information about the type including what it inherits
80 * from, the instance and class size, and constructor/destructor hooks.
82 * Alternatively several static types could be registered using helper macro
87 * static const TypeInfo device_types_info[] = {
89 * .name = TYPE_MY_DEVICE_A,
90 * .parent = TYPE_DEVICE,
91 * .instance_size = sizeof(MyDeviceA),
94 * .name = TYPE_MY_DEVICE_B,
95 * .parent = TYPE_DEVICE,
96 * .instance_size = sizeof(MyDeviceB),
100 * DEFINE_TYPES(device_types_info)
104 * Every type has an #ObjectClass associated with it. #ObjectClass derivatives
105 * are instantiated dynamically but there is only ever one instance for any
106 * given type. The #ObjectClass typically holds a table of function pointers
107 * for the virtual methods implemented by this type.
109 * Using object_new(), a new #Object derivative will be instantiated. You can
110 * cast an #Object to a subclass (or base-class) type using
111 * object_dynamic_cast(). You typically want to define macro wrappers around
112 * OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
116 * <title>Typecasting macros</title>
118 * #define MY_DEVICE_GET_CLASS(obj) \
119 * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
120 * #define MY_DEVICE_CLASS(klass) \
121 * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
122 * #define MY_DEVICE(obj) \
123 * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
127 * # Class Initialization #
129 * Before an object is initialized, the class for the object must be
130 * initialized. There is only one class object for all instance objects
131 * that is created lazily.
133 * Classes are initialized by first initializing any parent classes (if
134 * necessary). After the parent class object has initialized, it will be
135 * copied into the current class object and any additional storage in the
136 * class object is zero filled.
138 * The effect of this is that classes automatically inherit any virtual
139 * function pointers that the parent class has already initialized. All
140 * other fields will be zero filled.
142 * Once all of the parent classes have been initialized, #TypeInfo::class_init
143 * is called to let the class being instantiated provide default initialize for
144 * its virtual functions. Here is how the above example might be modified
145 * to introduce an overridden virtual function:
148 * <title>Overriding a virtual function</title>
152 * void my_device_class_init(ObjectClass *klass, void *class_data)
154 * DeviceClass *dc = DEVICE_CLASS(klass);
155 * dc->reset = my_device_reset;
158 * static const TypeInfo my_device_info = {
159 * .name = TYPE_MY_DEVICE,
160 * .parent = TYPE_DEVICE,
161 * .instance_size = sizeof(MyDevice),
162 * .class_init = my_device_class_init,
167 * Introducing new virtual methods requires a class to define its own
168 * struct and to add a .class_size member to the #TypeInfo. Each method
169 * will also have a wrapper function to call it easily:
172 * <title>Defining an abstract class</title>
176 * typedef struct MyDeviceClass
178 * DeviceClass parent;
180 * void (*frobnicate) (MyDevice *obj);
183 * static const TypeInfo my_device_info = {
184 * .name = TYPE_MY_DEVICE,
185 * .parent = TYPE_DEVICE,
186 * .instance_size = sizeof(MyDevice),
187 * .abstract = true, // or set a default in my_device_class_init
188 * .class_size = sizeof(MyDeviceClass),
191 * void my_device_frobnicate(MyDevice *obj)
193 * MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
195 * klass->frobnicate(obj);
202 * Interfaces allow a limited form of multiple inheritance. Instances are
203 * similar to normal types except for the fact that are only defined by
204 * their classes and never carry any state. You can dynamically cast an object
205 * to one of its #Interface types and vice versa.
209 * A <emphasis>method</emphasis> is a function within the namespace scope of
210 * a class. It usually operates on the object instance by passing it as a
211 * strongly-typed first argument.
212 * If it does not operate on an object instance, it is dubbed
213 * <emphasis>class method</emphasis>.
215 * Methods cannot be overloaded. That is, the #ObjectClass and method name
216 * uniquely identity the function to be called; the signature does not vary
217 * except for trailing varargs.
219 * Methods are always <emphasis>virtual</emphasis>. Overriding a method in
220 * #TypeInfo.class_init of a subclass leads to any user of the class obtained
221 * via OBJECT_GET_CLASS() accessing the overridden function.
222 * The original function is not automatically invoked. It is the responsibility
223 * of the overriding class to determine whether and when to invoke the method
226 * To invoke the method being overridden, the preferred solution is to store
227 * the original value in the overriding class before overriding the method.
228 * This corresponds to |[ {super,base}.method(...) ]| in Java and C#
229 * respectively; this frees the overriding class from hardcoding its parent
230 * class, which someone might choose to change at some point.
233 * <title>Overriding a virtual method</title>
235 * typedef struct MyState MyState;
237 * typedef void (*MyDoSomething)(MyState *obj);
239 * typedef struct MyClass {
240 * ObjectClass parent_class;
242 * MyDoSomething do_something;
245 * static void my_do_something(MyState *obj)
250 * static void my_class_init(ObjectClass *oc, void *data)
252 * MyClass *mc = MY_CLASS(oc);
254 * mc->do_something = my_do_something;
257 * static const TypeInfo my_type_info = {
259 * .parent = TYPE_OBJECT,
260 * .instance_size = sizeof(MyState),
261 * .class_size = sizeof(MyClass),
262 * .class_init = my_class_init,
265 * typedef struct DerivedClass {
266 * MyClass parent_class;
268 * MyDoSomething parent_do_something;
271 * static void derived_do_something(MyState *obj)
273 * DerivedClass *dc = DERIVED_GET_CLASS(obj);
275 * // do something here
276 * dc->parent_do_something(obj);
277 * // do something else here
280 * static void derived_class_init(ObjectClass *oc, void *data)
282 * MyClass *mc = MY_CLASS(oc);
283 * DerivedClass *dc = DERIVED_CLASS(oc);
285 * dc->parent_do_something = mc->do_something;
286 * mc->do_something = derived_do_something;
289 * static const TypeInfo derived_type_info = {
290 * .name = TYPE_DERIVED,
292 * .class_size = sizeof(DerivedClass),
293 * .class_init = derived_class_init,
298 * Alternatively, object_class_by_name() can be used to obtain the class and
299 * its non-overridden methods for a specific type. This would correspond to
300 * |[ MyClass::method(...) ]| in C++.
302 * The first example of such a QOM method was #CPUClass.reset,
303 * another example is #DeviceClass.realize.
308 * ObjectPropertyAccessor:
309 * @obj: the object that owns the property
310 * @v: the visitor that contains the property data
311 * @name: the name of the property
312 * @opaque: the object property opaque
313 * @errp: a pointer to an Error that is filled if getting/setting fails.
315 * Called when trying to get/set a property.
317 typedef void (ObjectPropertyAccessor
)(Object
*obj
,
324 * ObjectPropertyResolve:
325 * @obj: the object that owns the property
326 * @opaque: the opaque registered with the property
327 * @part: the name of the property
329 * Resolves the #Object corresponding to property @part.
331 * The returned object can also be used as a starting point
332 * to resolve a relative path starting with "@part".
334 * Returns: If @path is the path that led to @obj, the function
335 * returns the #Object corresponding to "@path/@part".
336 * If "@path/@part" is not a valid object path, it returns #NULL.
338 typedef Object
*(ObjectPropertyResolve
)(Object
*obj
,
343 * ObjectPropertyRelease:
344 * @obj: the object that owns the property
345 * @name: the name of the property
346 * @opaque: the opaque registered with the property
348 * Called when a property is removed from a object.
350 typedef void (ObjectPropertyRelease
)(Object
*obj
,
354 typedef struct ObjectProperty
359 ObjectPropertyAccessor
*get
;
360 ObjectPropertyAccessor
*set
;
361 ObjectPropertyResolve
*resolve
;
362 ObjectPropertyRelease
*release
;
368 * @obj: the object that is being removed from the composition tree
370 * Called when an object is being removed from the QOM composition tree.
371 * The function should remove any backlinks from children objects to @obj.
373 typedef void (ObjectUnparent
)(Object
*obj
);
377 * @obj: the object being freed
379 * Called when an object's last reference is removed.
381 typedef void (ObjectFree
)(void *obj
);
383 #define OBJECT_CLASS_CAST_CACHE 4
388 * The base for all classes. The only thing that #ObjectClass contains is an
389 * integer type handle.
397 const char *object_cast_cache
[OBJECT_CLASS_CAST_CACHE
];
398 const char *class_cast_cache
[OBJECT_CLASS_CAST_CACHE
];
400 ObjectUnparent
*unparent
;
402 GHashTable
*properties
;
408 * The base for all objects. The first member of this object is a pointer to
409 * a #ObjectClass. Since C guarantees that the first member of a structure
410 * always begins at byte 0 of that structure, as long as any sub-object places
411 * its parent as the first member, we can cast directly to a #Object.
413 * As a result, #Object contains a reference to the objects type as its
414 * first member. This allows identification of the real type of the object at
422 GHashTable
*properties
;
429 * @name: The name of the type.
430 * @parent: The name of the parent type.
431 * @instance_size: The size of the object (derivative of #Object). If
432 * @instance_size is 0, then the size of the object will be the size of the
434 * @instance_init: This function is called to initialize an object. The parent
435 * class will have already been initialized so the type is only responsible
436 * for initializing its own members.
437 * @instance_post_init: This function is called to finish initialization of
438 * an object, after all @instance_init functions were called.
439 * @instance_finalize: This function is called during object destruction. This
440 * is called before the parent @instance_finalize function has been called.
441 * An object should only free the members that are unique to its type in this
443 * @abstract: If this field is true, then the class is considered abstract and
444 * cannot be directly instantiated.
445 * @class_size: The size of the class object (derivative of #ObjectClass)
446 * for this object. If @class_size is 0, then the size of the class will be
447 * assumed to be the size of the parent class. This allows a type to avoid
448 * implementing an explicit class type if they are not adding additional
450 * @class_init: This function is called after all parent class initialization
451 * has occurred to allow a class to set its default virtual method pointers.
452 * This is also the function to use to override virtual methods from a parent
454 * @class_base_init: This function is called for all base classes after all
455 * parent class initialization has occurred, but before the class itself
456 * is initialized. This is the function to use to undo the effects of
457 * memcpy from the parent class to the descendants.
458 * @class_finalize: This function is called during class destruction and is
459 * meant to release and dynamic parameters allocated by @class_init.
460 * @class_data: Data to pass to the @class_init, @class_base_init and
461 * @class_finalize functions. This can be useful when building dynamic
463 * @interfaces: The list of interfaces associated with this type. This
464 * should point to a static array that's terminated with a zero filled
472 size_t instance_size
;
473 void (*instance_init
)(Object
*obj
);
474 void (*instance_post_init
)(Object
*obj
);
475 void (*instance_finalize
)(Object
*obj
);
480 void (*class_init
)(ObjectClass
*klass
, void *data
);
481 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
482 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
485 InterfaceInfo
*interfaces
;
490 * @obj: A derivative of #Object
492 * Converts an object to a #Object. Since all objects are #Objects,
493 * this function will always succeed.
495 #define OBJECT(obj) \
500 * @class: A derivative of #ObjectClass.
502 * Converts a class to an #ObjectClass. Since all objects are #Objects,
503 * this function will always succeed.
505 #define OBJECT_CLASS(class) \
506 ((ObjectClass *)(class))
510 * @type: The C type to use for the return value.
511 * @obj: A derivative of @type to cast.
512 * @name: The QOM typename of @type
514 * A type safe version of @object_dynamic_cast_assert. Typically each class
515 * will define a macro based on this type to perform type safe dynamic_casts to
518 * If an invalid object is passed to this function, a run time assert will be
521 #define OBJECT_CHECK(type, obj, name) \
522 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
523 __FILE__, __LINE__, __func__))
526 * OBJECT_CLASS_CHECK:
527 * @class_type: The C type to use for the return value.
528 * @class: A derivative class of @class_type to cast.
529 * @name: the QOM typename of @class_type.
531 * A type safe version of @object_class_dynamic_cast_assert. This macro is
532 * typically wrapped by each type to perform type safe casts of a class to a
533 * specific class type.
535 #define OBJECT_CLASS_CHECK(class_type, class, name) \
536 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
537 __FILE__, __LINE__, __func__))
541 * @class: The C type to use for the return value.
542 * @obj: The object to obtain the class for.
543 * @name: The QOM typename of @obj.
545 * This function will return a specific class for a given object. Its generally
546 * used by each type to provide a type safe macro to get a specific class type
549 #define OBJECT_GET_CLASS(class, obj, name) \
550 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
554 * @type: The name of the interface.
556 * The information associated with an interface.
558 struct InterfaceInfo
{
564 * @parent_class: the base class
566 * The class for all interfaces. Subclasses of this class should only add
569 struct InterfaceClass
571 ObjectClass parent_class
;
573 ObjectClass
*concrete_class
;
577 #define TYPE_INTERFACE "interface"
581 * @klass: class to cast from
582 * Returns: An #InterfaceClass or raise an error if cast is invalid
584 #define INTERFACE_CLASS(klass) \
585 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
589 * @interface: the type to return
590 * @obj: the object to convert to an interface
591 * @name: the interface type name
593 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
595 #define INTERFACE_CHECK(interface, obj, name) \
596 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
597 __FILE__, __LINE__, __func__))
601 * @typename: The name of the type of the object to instantiate.
603 * This function will initialize a new object using heap allocated memory.
604 * The returned object has a reference count of 1, and will be freed when
605 * the last reference is dropped.
607 * Returns: The newly allocated and instantiated object.
609 Object
*object_new(const char *typename
);
612 * object_new_with_props:
613 * @typename: The name of the type of the object to instantiate.
614 * @parent: the parent object
615 * @id: The unique ID of the object
616 * @errp: pointer to error object
617 * @...: list of property names and values
619 * This function will initialize a new object using heap allocated memory.
620 * The returned object has a reference count of 1, and will be freed when
621 * the last reference is dropped.
623 * The @id parameter will be used when registering the object as a
624 * child of @parent in the composition tree.
626 * The variadic parameters are a list of pairs of (propname, propvalue)
627 * strings. The propname of %NULL indicates the end of the property
628 * list. If the object implements the user creatable interface, the
629 * object will be marked complete once all the properties have been
633 * <title>Creating an object with properties</title>
638 * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE,
639 * object_get_objects_root(),
643 * "mem-path", "/dev/shm/somefile",
649 * g_printerr("Cannot create memory backend: %s\n",
650 * error_get_pretty(err));
655 * The returned object will have one stable reference maintained
656 * for as long as it is present in the object hierarchy.
658 * Returns: The newly allocated, instantiated & initialized object.
660 Object
*object_new_with_props(const char *typename
,
667 * object_new_with_propv:
668 * @typename: The name of the type of the object to instantiate.
669 * @parent: the parent object
670 * @id: The unique ID of the object
671 * @errp: pointer to error object
672 * @vargs: list of property names and values
674 * See object_new_with_props() for documentation.
676 Object
*object_new_with_propv(const char *typename
,
684 * @obj: the object instance to set properties on
685 * @errp: pointer to error object
686 * @...: list of property names and values
688 * This function will set a list of properties on an existing object
691 * The variadic parameters are a list of pairs of (propname, propvalue)
692 * strings. The propname of %NULL indicates the end of the property
696 * <title>Update an object's properties</title>
699 * Object *obj = ...get / create object...;
701 * obj = object_set_props(obj,
704 * "mem-path", "/dev/shm/somefile",
710 * g_printerr("Cannot set properties: %s\n",
711 * error_get_pretty(err));
716 * The returned object will have one stable reference maintained
717 * for as long as it is present in the object hierarchy.
719 * Returns: -1 on error, 0 on success
721 int object_set_props(Object
*obj
,
727 * @obj: the object instance to set properties on
728 * @errp: pointer to error object
729 * @vargs: list of property names and values
731 * See object_set_props() for documentation.
733 * Returns: -1 on error, 0 on success
735 int object_set_propv(Object
*obj
,
741 * @obj: A pointer to the memory to be used for the object.
742 * @size: The maximum size available at @obj for the object.
743 * @typename: The name of the type of the object to instantiate.
745 * This function will initialize an object. The memory for the object should
746 * have already been allocated. The returned object has a reference count of 1,
747 * and will be finalized when the last reference is dropped.
749 void object_initialize(void *obj
, size_t size
, const char *typename
);
752 * object_initialize_child:
753 * @parentobj: The parent object to add a property to
754 * @propname: The name of the property
755 * @childobj: A pointer to the memory to be used for the object.
756 * @size: The maximum size available at @childobj for the object.
757 * @type: The name of the type of the object to instantiate.
758 * @errp: If an error occurs, a pointer to an area to store the error
759 * @...: list of property names and values
761 * This function will initialize an object. The memory for the object should
762 * have already been allocated. The object will then be added as child property
763 * to a parent with object_property_add_child() function. The returned object
764 * has a reference count of 1 (for the "child<...>" property from the parent),
765 * so the object will be finalized automatically when the parent gets removed.
767 * The variadic parameters are a list of pairs of (propname, propvalue)
768 * strings. The propname of %NULL indicates the end of the property list.
769 * If the object implements the user creatable interface, the object will
770 * be marked complete once all the properties have been processed.
772 void object_initialize_child(Object
*parentobj
, const char *propname
,
773 void *childobj
, size_t size
, const char *type
,
774 Error
**errp
, ...) QEMU_SENTINEL
;
777 * object_initialize_childv:
778 * @parentobj: The parent object to add a property to
779 * @propname: The name of the property
780 * @childobj: A pointer to the memory to be used for the object.
781 * @size: The maximum size available at @childobj for the object.
782 * @type: The name of the type of the object to instantiate.
783 * @errp: If an error occurs, a pointer to an area to store the error
784 * @vargs: list of property names and values
786 * See object_initialize_child() for documentation.
788 void object_initialize_childv(Object
*parentobj
, const char *propname
,
789 void *childobj
, size_t size
, const char *type
,
790 Error
**errp
, va_list vargs
);
793 * object_dynamic_cast:
794 * @obj: The object to cast.
795 * @typename: The @typename to cast to.
797 * This function will determine if @obj is-a @typename. @obj can refer to an
798 * object or an interface associated with an object.
800 * Returns: This function returns @obj on success or #NULL on failure.
802 Object
*object_dynamic_cast(Object
*obj
, const char *typename
);
805 * object_dynamic_cast_assert:
807 * See object_dynamic_cast() for a description of the parameters of this
808 * function. The only difference in behavior is that this function asserts
809 * instead of returning #NULL on failure if QOM cast debugging is enabled.
810 * This function is not meant to be called directly, but only through
811 * the wrapper macro OBJECT_CHECK.
813 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
814 const char *file
, int line
, const char *func
);
818 * @obj: A derivative of #Object
820 * Returns: The #ObjectClass of the type associated with @obj.
822 ObjectClass
*object_get_class(Object
*obj
);
825 * object_get_typename:
826 * @obj: A derivative of #Object.
828 * Returns: The QOM typename of @obj.
830 const char *object_get_typename(const Object
*obj
);
833 * type_register_static:
834 * @info: The #TypeInfo of the new type.
836 * @info and all of the strings it points to should exist for the life time
837 * that the type is registered.
839 * Returns: the new #Type.
841 Type
type_register_static(const TypeInfo
*info
);
845 * @info: The #TypeInfo of the new type
847 * Unlike type_register_static(), this call does not require @info or its
848 * string members to continue to exist after the call returns.
850 * Returns: the new #Type.
852 Type
type_register(const TypeInfo
*info
);
855 * type_register_static_array:
856 * @infos: The array of the new type #TypeInfo structures.
857 * @nr_infos: number of entries in @infos
859 * @infos and all of the strings it points to should exist for the life time
860 * that the type is registered.
862 void type_register_static_array(const TypeInfo
*infos
, int nr_infos
);
866 * @type_array: The array containing #TypeInfo structures to register
868 * @type_array should be static constant that exists for the life time
869 * that the type is registered.
871 #define DEFINE_TYPES(type_array) \
872 static void do_qemu_init_ ## type_array(void) \
874 type_register_static_array(type_array, ARRAY_SIZE(type_array)); \
876 type_init(do_qemu_init_ ## type_array)
879 * object_class_dynamic_cast_assert:
880 * @klass: The #ObjectClass to attempt to cast.
881 * @typename: The QOM typename of the class to cast to.
883 * See object_class_dynamic_cast() for a description of the parameters
884 * of this function. The only difference in behavior is that this function
885 * asserts instead of returning #NULL on failure if QOM cast debugging is
886 * enabled. This function is not meant to be called directly, but only through
887 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
889 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*klass
,
890 const char *typename
,
891 const char *file
, int line
,
895 * object_class_dynamic_cast:
896 * @klass: The #ObjectClass to attempt to cast.
897 * @typename: The QOM typename of the class to cast to.
899 * Returns: If @typename is a class, this function returns @klass if
900 * @typename is a subtype of @klass, else returns #NULL.
902 * If @typename is an interface, this function returns the interface
903 * definition for @klass if @klass implements it unambiguously; #NULL
904 * is returned if @klass does not implement the interface or if multiple
905 * classes or interfaces on the hierarchy leading to @klass implement
906 * it. (FIXME: perhaps this can be detected at type definition time?)
908 ObjectClass
*object_class_dynamic_cast(ObjectClass
*klass
,
909 const char *typename
);
912 * object_class_get_parent:
913 * @klass: The class to obtain the parent for.
915 * Returns: The parent for @klass or %NULL if none.
917 ObjectClass
*object_class_get_parent(ObjectClass
*klass
);
920 * object_class_get_name:
921 * @klass: The class to obtain the QOM typename for.
923 * Returns: The QOM typename for @klass.
925 const char *object_class_get_name(ObjectClass
*klass
);
928 * object_class_is_abstract:
929 * @klass: The class to obtain the abstractness for.
931 * Returns: %true if @klass is abstract, %false otherwise.
933 bool object_class_is_abstract(ObjectClass
*klass
);
936 * object_class_by_name:
937 * @typename: The QOM typename to obtain the class for.
939 * Returns: The class for @typename or %NULL if not found.
941 ObjectClass
*object_class_by_name(const char *typename
);
943 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
944 const char *implements_type
, bool include_abstract
,
948 * object_class_get_list:
949 * @implements_type: The type to filter for, including its derivatives.
950 * @include_abstract: Whether to include abstract classes.
952 * Returns: A singly-linked list of the classes in reverse hashtable order.
954 GSList
*object_class_get_list(const char *implements_type
,
955 bool include_abstract
);
958 * object_class_get_list_sorted:
959 * @implements_type: The type to filter for, including its derivatives.
960 * @include_abstract: Whether to include abstract classes.
962 * Returns: A singly-linked list of the classes in alphabetical
963 * case-insensitive order.
965 GSList
*object_class_get_list_sorted(const char *implements_type
,
966 bool include_abstract
);
972 * Increase the reference count of a object. A object cannot be freed as long
973 * as its reference count is greater than zero.
975 void object_ref(Object
*obj
);
981 * Decrease the reference count of a object. A object cannot be freed as long
982 * as its reference count is greater than zero.
984 void object_unref(Object
*obj
);
987 * object_property_add:
988 * @obj: the object to add a property to
989 * @name: the name of the property. This can contain any character except for
990 * a forward slash. In general, you should use hyphens '-' instead of
991 * underscores '_' when naming properties.
992 * @type: the type name of the property. This namespace is pretty loosely
993 * defined. Sub namespaces are constructed by using a prefix and then
994 * to angle brackets. For instance, the type 'virtio-net-pci' in the
995 * 'link' namespace would be 'link<virtio-net-pci>'.
996 * @get: The getter to be called to read a property. If this is NULL, then
997 * the property cannot be read.
998 * @set: the setter to be called to write a property. If this is NULL,
999 * then the property cannot be written.
1000 * @release: called when the property is removed from the object. This is
1001 * meant to allow a property to free its opaque upon object
1002 * destruction. This may be NULL.
1003 * @opaque: an opaque pointer to pass to the callbacks for the property
1004 * @errp: returns an error if this function fails
1006 * Returns: The #ObjectProperty; this can be used to set the @resolve
1007 * callback for child and link properties.
1009 ObjectProperty
*object_property_add(Object
*obj
, const char *name
,
1011 ObjectPropertyAccessor
*get
,
1012 ObjectPropertyAccessor
*set
,
1013 ObjectPropertyRelease
*release
,
1014 void *opaque
, Error
**errp
);
1016 void object_property_del(Object
*obj
, const char *name
, Error
**errp
);
1018 ObjectProperty
*object_class_property_add(ObjectClass
*klass
, const char *name
,
1020 ObjectPropertyAccessor
*get
,
1021 ObjectPropertyAccessor
*set
,
1022 ObjectPropertyRelease
*release
,
1023 void *opaque
, Error
**errp
);
1026 * object_property_find:
1028 * @name: the name of the property
1029 * @errp: returns an error if this function fails
1031 * Look up a property for an object and return its #ObjectProperty if found.
1033 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
1035 ObjectProperty
*object_class_property_find(ObjectClass
*klass
, const char *name
,
1038 typedef struct ObjectPropertyIterator
{
1039 ObjectClass
*nextclass
;
1040 GHashTableIter iter
;
1041 } ObjectPropertyIterator
;
1044 * object_property_iter_init:
1047 * Initializes an iterator for traversing all properties
1048 * registered against an object instance, its class and all parent classes.
1050 * It is forbidden to modify the property list while iterating,
1051 * whether removing or adding properties.
1053 * Typical usage pattern would be
1056 * <title>Using object property iterators</title>
1058 * ObjectProperty *prop;
1059 * ObjectPropertyIterator iter;
1061 * object_property_iter_init(&iter, obj);
1062 * while ((prop = object_property_iter_next(&iter))) {
1063 * ... do something with prop ...
1068 void object_property_iter_init(ObjectPropertyIterator
*iter
,
1072 * object_class_property_iter_init:
1075 * Initializes an iterator for traversing all properties
1076 * registered against an object class and all parent classes.
1078 * It is forbidden to modify the property list while iterating,
1079 * whether removing or adding properties.
1081 * This can be used on abstract classes as it does not create a temporary
1084 void object_class_property_iter_init(ObjectPropertyIterator
*iter
,
1085 ObjectClass
*klass
);
1088 * object_property_iter_next:
1089 * @iter: the iterator instance
1091 * Return the next available property. If no further properties
1092 * are available, a %NULL value will be returned and the @iter
1093 * pointer should not be used again after this point without
1094 * re-initializing it.
1096 * Returns: the next property, or %NULL when all properties
1097 * have been traversed.
1099 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
);
1101 void object_unparent(Object
*obj
);
1104 * object_property_get:
1106 * @v: the visitor that will receive the property value. This should be an
1107 * Output visitor and the data will be written with @name as the name.
1108 * @name: the name of the property
1109 * @errp: returns an error if this function fails
1111 * Reads a property from a object.
1113 void object_property_get(Object
*obj
, Visitor
*v
, const char *name
,
1117 * object_property_set_str:
1118 * @value: the value to be written to the property
1119 * @name: the name of the property
1120 * @errp: returns an error if this function fails
1122 * Writes a string value to a property.
1124 void object_property_set_str(Object
*obj
, const char *value
,
1125 const char *name
, Error
**errp
);
1128 * object_property_get_str:
1130 * @name: the name of the property
1131 * @errp: returns an error if this function fails
1133 * Returns: the value of the property, converted to a C string, or NULL if
1134 * an error occurs (including when the property value is not a string).
1135 * The caller should free the string.
1137 char *object_property_get_str(Object
*obj
, const char *name
,
1141 * object_property_set_link:
1142 * @value: the value to be written to the property
1143 * @name: the name of the property
1144 * @errp: returns an error if this function fails
1146 * Writes an object's canonical path to a property.
1148 * If the link property was created with
1149 * <code>OBJ_PROP_LINK_STRONG</code> bit, the old target object is
1150 * unreferenced, and a reference is added to the new target object.
1153 void object_property_set_link(Object
*obj
, Object
*value
,
1154 const char *name
, Error
**errp
);
1157 * object_property_get_link:
1159 * @name: the name of the property
1160 * @errp: returns an error if this function fails
1162 * Returns: the value of the property, resolved from a path to an Object,
1163 * or NULL if an error occurs (including when the property value is not a
1164 * string or not a valid object path).
1166 Object
*object_property_get_link(Object
*obj
, const char *name
,
1170 * object_property_set_bool:
1171 * @value: the value to be written to the property
1172 * @name: the name of the property
1173 * @errp: returns an error if this function fails
1175 * Writes a bool value to a property.
1177 void object_property_set_bool(Object
*obj
, bool value
,
1178 const char *name
, Error
**errp
);
1181 * object_property_get_bool:
1183 * @name: the name of the property
1184 * @errp: returns an error if this function fails
1186 * Returns: the value of the property, converted to a boolean, or NULL if
1187 * an error occurs (including when the property value is not a bool).
1189 bool object_property_get_bool(Object
*obj
, const char *name
,
1193 * object_property_set_int:
1194 * @value: the value to be written to the property
1195 * @name: the name of the property
1196 * @errp: returns an error if this function fails
1198 * Writes an integer value to a property.
1200 void object_property_set_int(Object
*obj
, int64_t value
,
1201 const char *name
, Error
**errp
);
1204 * object_property_get_int:
1206 * @name: the name of the property
1207 * @errp: returns an error if this function fails
1209 * Returns: the value of the property, converted to an integer, or negative if
1210 * an error occurs (including when the property value is not an integer).
1212 int64_t object_property_get_int(Object
*obj
, const char *name
,
1216 * object_property_set_uint:
1217 * @value: the value to be written to the property
1218 * @name: the name of the property
1219 * @errp: returns an error if this function fails
1221 * Writes an unsigned integer value to a property.
1223 void object_property_set_uint(Object
*obj
, uint64_t value
,
1224 const char *name
, Error
**errp
);
1227 * object_property_get_uint:
1229 * @name: the name of the property
1230 * @errp: returns an error if this function fails
1232 * Returns: the value of the property, converted to an unsigned integer, or 0
1233 * an error occurs (including when the property value is not an integer).
1235 uint64_t object_property_get_uint(Object
*obj
, const char *name
,
1239 * object_property_get_enum:
1241 * @name: the name of the property
1242 * @typename: the name of the enum data type
1243 * @errp: returns an error if this function fails
1245 * Returns: the value of the property, converted to an integer, or
1246 * undefined if an error occurs (including when the property value is not
1249 int object_property_get_enum(Object
*obj
, const char *name
,
1250 const char *typename
, Error
**errp
);
1253 * object_property_get_uint16List:
1255 * @name: the name of the property
1256 * @list: the returned int list
1257 * @errp: returns an error if this function fails
1259 * Returns: the value of the property, converted to integers, or
1260 * undefined if an error occurs (including when the property value is not
1261 * an list of integers).
1263 void object_property_get_uint16List(Object
*obj
, const char *name
,
1264 uint16List
**list
, Error
**errp
);
1267 * object_property_set:
1269 * @v: the visitor that will be used to write the property value. This should
1270 * be an Input visitor and the data will be first read with @name as the
1271 * name and then written as the property value.
1272 * @name: the name of the property
1273 * @errp: returns an error if this function fails
1275 * Writes a property to a object.
1277 void object_property_set(Object
*obj
, Visitor
*v
, const char *name
,
1281 * object_property_parse:
1283 * @string: the string that will be used to parse the property value.
1284 * @name: the name of the property
1285 * @errp: returns an error if this function fails
1287 * Parses a string and writes the result into a property of an object.
1289 void object_property_parse(Object
*obj
, const char *string
,
1290 const char *name
, Error
**errp
);
1293 * object_property_print:
1295 * @name: the name of the property
1296 * @human: if true, print for human consumption
1297 * @errp: returns an error if this function fails
1299 * Returns a string representation of the value of the property. The
1300 * caller shall free the string.
1302 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1306 * object_property_get_type:
1308 * @name: the name of the property
1309 * @errp: returns an error if this function fails
1311 * Returns: The type name of the property.
1313 const char *object_property_get_type(Object
*obj
, const char *name
,
1319 * Returns: the root object of the composition tree
1321 Object
*object_get_root(void);
1325 * object_get_objects_root:
1327 * Get the container object that holds user created
1328 * object instances. This is the object at path
1331 * Returns: the user object container
1333 Object
*object_get_objects_root(void);
1336 * object_get_internal_root:
1338 * Get the container object that holds internally used object
1339 * instances. Any object which is put into this container must not be
1340 * user visible, and it will not be exposed in the QOM tree.
1342 * Returns: the internal object container
1344 Object
*object_get_internal_root(void);
1347 * object_get_canonical_path_component:
1349 * Returns: The final component in the object's canonical path. The canonical
1350 * path is the path within the composition tree starting from the root.
1351 * %NULL if the object doesn't have a parent (and thus a canonical path).
1353 gchar
*object_get_canonical_path_component(Object
*obj
);
1356 * object_get_canonical_path:
1358 * Returns: The canonical path for a object. This is the path within the
1359 * composition tree starting from the root.
1361 gchar
*object_get_canonical_path(Object
*obj
);
1364 * object_resolve_path:
1365 * @path: the path to resolve
1366 * @ambiguous: returns true if the path resolution failed because of an
1369 * There are two types of supported paths--absolute paths and partial paths.
1371 * Absolute paths are derived from the root object and can follow child<> or
1372 * link<> properties. Since they can follow link<> properties, they can be
1373 * arbitrarily long. Absolute paths look like absolute filenames and are
1374 * prefixed with a leading slash.
1376 * Partial paths look like relative filenames. They do not begin with a
1377 * prefix. The matching rules for partial paths are subtle but designed to make
1378 * specifying objects easy. At each level of the composition tree, the partial
1379 * path is matched as an absolute path. The first match is not returned. At
1380 * least two matches are searched for. A successful result is only returned if
1381 * only one match is found. If more than one match is found, a flag is
1382 * returned to indicate that the match was ambiguous.
1384 * Returns: The matched object or NULL on path lookup failure.
1386 Object
*object_resolve_path(const char *path
, bool *ambiguous
);
1389 * object_resolve_path_type:
1390 * @path: the path to resolve
1391 * @typename: the type to look for.
1392 * @ambiguous: returns true if the path resolution failed because of an
1395 * This is similar to object_resolve_path. However, when looking for a
1396 * partial path only matches that implement the given type are considered.
1397 * This restricts the search and avoids spuriously flagging matches as
1400 * For both partial and absolute paths, the return value goes through
1401 * a dynamic cast to @typename. This is important if either the link,
1402 * or the typename itself are of interface types.
1404 * Returns: The matched object or NULL on path lookup failure.
1406 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1410 * object_resolve_path_component:
1411 * @parent: the object in which to resolve the path
1412 * @part: the component to resolve.
1414 * This is similar to object_resolve_path with an absolute path, but it
1415 * only resolves one element (@part) and takes the others from @parent.
1417 * Returns: The resolved object or NULL on path lookup failure.
1419 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
);
1422 * object_property_add_child:
1423 * @obj: the object to add a property to
1424 * @name: the name of the property
1425 * @child: the child object
1426 * @errp: if an error occurs, a pointer to an area to store the error
1428 * Child properties form the composition tree. All objects need to be a child
1429 * of another object. Objects can only be a child of one object.
1431 * There is no way for a child to determine what its parent is. It is not
1432 * a bidirectional relationship. This is by design.
1434 * The value of a child property as a C string will be the child object's
1435 * canonical path. It can be retrieved using object_property_get_str().
1436 * The child object itself can be retrieved using object_property_get_link().
1438 void object_property_add_child(Object
*obj
, const char *name
,
1439 Object
*child
, Error
**errp
);
1442 /* Unref the link pointer when the property is deleted */
1443 OBJ_PROP_LINK_STRONG
= 0x1,
1444 } ObjectPropertyLinkFlags
;
1447 * object_property_allow_set_link:
1449 * The default implementation of the object_property_add_link() check()
1450 * callback function. It allows the link property to be set and never returns
1453 void object_property_allow_set_link(const Object
*, const char *,
1454 Object
*, Error
**);
1457 * object_property_add_link:
1458 * @obj: the object to add a property to
1459 * @name: the name of the property
1460 * @type: the qobj type of the link
1461 * @child: a pointer to where the link object reference is stored
1462 * @check: callback to veto setting or NULL if the property is read-only
1463 * @flags: additional options for the link
1464 * @errp: if an error occurs, a pointer to an area to store the error
1466 * Links establish relationships between objects. Links are unidirectional
1467 * although two links can be combined to form a bidirectional relationship
1470 * Links form the graph in the object model.
1472 * The <code>@check()</code> callback is invoked when
1473 * object_property_set_link() is called and can raise an error to prevent the
1474 * link being set. If <code>@check</code> is NULL, the property is read-only
1475 * and cannot be set.
1477 * Ownership of the pointer that @child points to is transferred to the
1478 * link property. The reference count for <code>*@child</code> is
1479 * managed by the property from after the function returns till the
1480 * property is deleted with object_property_del(). If the
1481 * <code>@flags</code> <code>OBJ_PROP_LINK_STRONG</code> bit is set,
1482 * the reference count is decremented when the property is deleted or
1485 void object_property_add_link(Object
*obj
, const char *name
,
1486 const char *type
, Object
**child
,
1487 void (*check
)(const Object
*obj
, const char *name
,
1488 Object
*val
, Error
**errp
),
1489 ObjectPropertyLinkFlags flags
,
1493 * object_property_add_str:
1494 * @obj: the object to add a property to
1495 * @name: the name of the property
1496 * @get: the getter or NULL if the property is write-only. This function must
1497 * return a string to be freed by g_free().
1498 * @set: the setter or NULL if the property is read-only
1499 * @errp: if an error occurs, a pointer to an area to store the error
1501 * Add a string property using getters/setters. This function will add a
1502 * property of type 'string'.
1504 void object_property_add_str(Object
*obj
, const char *name
,
1505 char *(*get
)(Object
*, Error
**),
1506 void (*set
)(Object
*, const char *, Error
**),
1509 void object_class_property_add_str(ObjectClass
*klass
, const char *name
,
1510 char *(*get
)(Object
*, Error
**),
1511 void (*set
)(Object
*, const char *,
1516 * object_property_add_bool:
1517 * @obj: the object to add a property to
1518 * @name: the name of the property
1519 * @get: the getter or NULL if the property is write-only.
1520 * @set: the setter or NULL if the property is read-only
1521 * @errp: if an error occurs, a pointer to an area to store the error
1523 * Add a bool property using getters/setters. This function will add a
1524 * property of type 'bool'.
1526 void object_property_add_bool(Object
*obj
, const char *name
,
1527 bool (*get
)(Object
*, Error
**),
1528 void (*set
)(Object
*, bool, Error
**),
1531 void object_class_property_add_bool(ObjectClass
*klass
, const char *name
,
1532 bool (*get
)(Object
*, Error
**),
1533 void (*set
)(Object
*, bool, Error
**),
1537 * object_property_add_enum:
1538 * @obj: the object to add a property to
1539 * @name: the name of the property
1540 * @typename: the name of the enum data type
1541 * @get: the getter or %NULL if the property is write-only.
1542 * @set: the setter or %NULL if the property is read-only
1543 * @errp: if an error occurs, a pointer to an area to store the error
1545 * Add an enum property using getters/setters. This function will add a
1546 * property of type '@typename'.
1548 void object_property_add_enum(Object
*obj
, const char *name
,
1549 const char *typename
,
1550 const QEnumLookup
*lookup
,
1551 int (*get
)(Object
*, Error
**),
1552 void (*set
)(Object
*, int, Error
**),
1555 void object_class_property_add_enum(ObjectClass
*klass
, const char *name
,
1556 const char *typename
,
1557 const QEnumLookup
*lookup
,
1558 int (*get
)(Object
*, Error
**),
1559 void (*set
)(Object
*, int, Error
**),
1563 * object_property_add_tm:
1564 * @obj: the object to add a property to
1565 * @name: the name of the property
1566 * @get: the getter or NULL if the property is write-only.
1567 * @errp: if an error occurs, a pointer to an area to store the error
1569 * Add a read-only struct tm valued property using a getter function.
1570 * This function will add a property of type 'struct tm'.
1572 void object_property_add_tm(Object
*obj
, const char *name
,
1573 void (*get
)(Object
*, struct tm
*, Error
**),
1576 void object_class_property_add_tm(ObjectClass
*klass
, const char *name
,
1577 void (*get
)(Object
*, struct tm
*, Error
**),
1581 * object_property_add_uint8_ptr:
1582 * @obj: the object to add a property to
1583 * @name: the name of the property
1584 * @v: pointer to value
1585 * @errp: if an error occurs, a pointer to an area to store the error
1587 * Add an integer property in memory. This function will add a
1588 * property of type 'uint8'.
1590 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
1591 const uint8_t *v
, Error
**errp
);
1592 void object_class_property_add_uint8_ptr(ObjectClass
*klass
, const char *name
,
1593 const uint8_t *v
, Error
**errp
);
1596 * object_property_add_uint16_ptr:
1597 * @obj: the object to add a property to
1598 * @name: the name of the property
1599 * @v: pointer to value
1600 * @errp: if an error occurs, a pointer to an area to store the error
1602 * Add an integer property in memory. This function will add a
1603 * property of type 'uint16'.
1605 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
1606 const uint16_t *v
, Error
**errp
);
1607 void object_class_property_add_uint16_ptr(ObjectClass
*klass
, const char *name
,
1608 const uint16_t *v
, Error
**errp
);
1611 * object_property_add_uint32_ptr:
1612 * @obj: the object to add a property to
1613 * @name: the name of the property
1614 * @v: pointer to value
1615 * @errp: if an error occurs, a pointer to an area to store the error
1617 * Add an integer property in memory. This function will add a
1618 * property of type 'uint32'.
1620 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
1621 const uint32_t *v
, Error
**errp
);
1622 void object_class_property_add_uint32_ptr(ObjectClass
*klass
, const char *name
,
1623 const uint32_t *v
, Error
**errp
);
1626 * object_property_add_uint64_ptr:
1627 * @obj: the object to add a property to
1628 * @name: the name of the property
1629 * @v: pointer to value
1630 * @errp: if an error occurs, a pointer to an area to store the error
1632 * Add an integer property in memory. This function will add a
1633 * property of type 'uint64'.
1635 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
1636 const uint64_t *v
, Error
**Errp
);
1637 void object_class_property_add_uint64_ptr(ObjectClass
*klass
, const char *name
,
1638 const uint64_t *v
, Error
**Errp
);
1641 * object_property_add_alias:
1642 * @obj: the object to add a property to
1643 * @name: the name of the property
1644 * @target_obj: the object to forward property access to
1645 * @target_name: the name of the property on the forwarded object
1646 * @errp: if an error occurs, a pointer to an area to store the error
1648 * Add an alias for a property on an object. This function will add a property
1649 * of the same type as the forwarded property.
1651 * The caller must ensure that <code>@target_obj</code> stays alive as long as
1652 * this property exists. In the case of a child object or an alias on the same
1653 * object this will be the case. For aliases to other objects the caller is
1654 * responsible for taking a reference.
1656 void object_property_add_alias(Object
*obj
, const char *name
,
1657 Object
*target_obj
, const char *target_name
,
1661 * object_property_add_const_link:
1662 * @obj: the object to add a property to
1663 * @name: the name of the property
1664 * @target: the object to be referred by the link
1665 * @errp: if an error occurs, a pointer to an area to store the error
1667 * Add an unmodifiable link for a property on an object. This function will
1668 * add a property of type link<TYPE> where TYPE is the type of @target.
1670 * The caller must ensure that @target stays alive as long as
1671 * this property exists. In the case @target is a child of @obj,
1672 * this will be the case. Otherwise, the caller is responsible for
1673 * taking a reference.
1675 void object_property_add_const_link(Object
*obj
, const char *name
,
1676 Object
*target
, Error
**errp
);
1679 * object_property_set_description:
1680 * @obj: the object owning the property
1681 * @name: the name of the property
1682 * @description: the description of the property on the object
1683 * @errp: if an error occurs, a pointer to an area to store the error
1685 * Set an object property's description.
1688 void object_property_set_description(Object
*obj
, const char *name
,
1689 const char *description
, Error
**errp
);
1690 void object_class_property_set_description(ObjectClass
*klass
, const char *name
,
1691 const char *description
,
1695 * object_child_foreach:
1696 * @obj: the object whose children will be navigated
1697 * @fn: the iterator function to be called
1698 * @opaque: an opaque value that will be passed to the iterator
1700 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1703 * It is forbidden to add or remove children from @obj from the @fn
1706 * Returns: The last value returned by @fn, or 0 if there is no child.
1708 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1712 * object_child_foreach_recursive:
1713 * @obj: the object whose children will be navigated
1714 * @fn: the iterator function to be called
1715 * @opaque: an opaque value that will be passed to the iterator
1717 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1718 * non-zero. Calls recursively, all child nodes of @obj will also be passed
1719 * all the way down to the leaf nodes of the tree. Depth first ordering.
1721 * It is forbidden to add or remove children from @obj (or its
1722 * child nodes) from the @fn callback.
1724 * Returns: The last value returned by @fn, or 0 if there is no child.
1726 int object_child_foreach_recursive(Object
*obj
,
1727 int (*fn
)(Object
*child
, void *opaque
),
1731 * @root: root of the #path, e.g., object_get_root()
1732 * @path: path to the container
1734 * Return a container object whose path is @path. Create more containers
1735 * along the path if necessary.
1737 * Returns: the container object.
1739 Object
*container_get(Object
*root
, const char *path
);
1742 * object_type_get_instance_size:
1743 * @typename: Name of the Type whose instance_size is required
1745 * Returns the instance_size of the given @typename.
1747 size_t object_type_get_instance_size(const char *typename
);