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.
20 #include "qemu/queue.h"
26 typedef struct TypeImpl
*Type
;
28 typedef struct ObjectClass ObjectClass
;
29 typedef struct Object Object
;
31 typedef struct TypeInfo TypeInfo
;
33 typedef struct InterfaceClass InterfaceClass
;
34 typedef struct InterfaceInfo InterfaceInfo
;
36 #define TYPE_OBJECT "object"
40 * @title:Base Object Type System
41 * @short_description: interfaces for creating new types and objects
43 * The QEMU Object Model provides a framework for registering user creatable
44 * types and instantiating objects from those types. QOM provides the following
47 * - System for dynamically registering types
48 * - Support for single-inheritance of types
49 * - Multiple inheritance of stateless interfaces
52 * <title>Creating a minimal type</title>
56 * #define TYPE_MY_DEVICE "my-device"
58 * // No new virtual functions: we can reuse the typedef for the
60 * typedef DeviceClass MyDeviceClass;
61 * typedef struct MyDevice
65 * int reg0, reg1, reg2;
68 * static const TypeInfo my_device_info = {
69 * .name = TYPE_MY_DEVICE,
70 * .parent = TYPE_DEVICE,
71 * .instance_size = sizeof(MyDevice),
74 * static void my_device_register_types(void)
76 * type_register_static(&my_device_info);
79 * type_init(my_device_register_types)
83 * In the above example, we create a simple type that is described by #TypeInfo.
84 * #TypeInfo describes information about the type including what it inherits
85 * from, the instance and class size, and constructor/destructor hooks.
87 * Every type has an #ObjectClass associated with it. #ObjectClass derivatives
88 * are instantiated dynamically but there is only ever one instance for any
89 * given type. The #ObjectClass typically holds a table of function pointers
90 * for the virtual methods implemented by this type.
92 * Using object_new(), a new #Object derivative will be instantiated. You can
93 * cast an #Object to a subclass (or base-class) type using
94 * object_dynamic_cast(). You typically want to define macro wrappers around
95 * OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
99 * <title>Typecasting macros</title>
101 * #define MY_DEVICE_GET_CLASS(obj) \
102 * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
103 * #define MY_DEVICE_CLASS(klass) \
104 * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
105 * #define MY_DEVICE(obj) \
106 * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
110 * # Class Initialization #
112 * Before an object is initialized, the class for the object must be
113 * initialized. There is only one class object for all instance objects
114 * that is created lazily.
116 * Classes are initialized by first initializing any parent classes (if
117 * necessary). After the parent class object has initialized, it will be
118 * copied into the current class object and any additional storage in the
119 * class object is zero filled.
121 * The effect of this is that classes automatically inherit any virtual
122 * function pointers that the parent class has already initialized. All
123 * other fields will be zero filled.
125 * Once all of the parent classes have been initialized, #TypeInfo::class_init
126 * is called to let the class being instantiated provide default initialize for
127 * its virtual functions. Here is how the above example might be modified
128 * to introduce an overridden virtual function:
131 * <title>Overriding a virtual function</title>
135 * void my_device_class_init(ObjectClass *klass, void *class_data)
137 * DeviceClass *dc = DEVICE_CLASS(klass);
138 * dc->reset = my_device_reset;
141 * static const TypeInfo my_device_info = {
142 * .name = TYPE_MY_DEVICE,
143 * .parent = TYPE_DEVICE,
144 * .instance_size = sizeof(MyDevice),
145 * .class_init = my_device_class_init,
150 * Introducing new virtual methods requires a class to define its own
151 * struct and to add a .class_size member to the #TypeInfo. Each method
152 * will also have a wrapper function to call it easily:
155 * <title>Defining an abstract class</title>
159 * typedef struct MyDeviceClass
161 * DeviceClass parent;
163 * void (*frobnicate) (MyDevice *obj);
166 * static const TypeInfo my_device_info = {
167 * .name = TYPE_MY_DEVICE,
168 * .parent = TYPE_DEVICE,
169 * .instance_size = sizeof(MyDevice),
170 * .abstract = true, // or set a default in my_device_class_init
171 * .class_size = sizeof(MyDeviceClass),
174 * void my_device_frobnicate(MyDevice *obj)
176 * MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
178 * klass->frobnicate(obj);
185 * Interfaces allow a limited form of multiple inheritance. Instances are
186 * similar to normal types except for the fact that are only defined by
187 * their classes and never carry any state. You can dynamically cast an object
188 * to one of its #Interface types and vice versa.
192 * A <emphasis>method</emphasis> is a function within the namespace scope of
193 * a class. It usually operates on the object instance by passing it as a
194 * strongly-typed first argument.
195 * If it does not operate on an object instance, it is dubbed
196 * <emphasis>class method</emphasis>.
198 * Methods cannot be overloaded. That is, the #ObjectClass and method name
199 * uniquely identity the function to be called; the signature does not vary
200 * except for trailing varargs.
202 * Methods are always <emphasis>virtual</emphasis>. Overriding a method in
203 * #TypeInfo.class_init of a subclass leads to any user of the class obtained
204 * via OBJECT_GET_CLASS() accessing the overridden function.
205 * The original function is not automatically invoked. It is the responsibility
206 * of the overriding class to determine whether and when to invoke the method
209 * To invoke the method being overridden, the preferred solution is to store
210 * the original value in the overriding class before overriding the method.
211 * This corresponds to |[ {super,base}.method(...) ]| in Java and C#
212 * respectively; this frees the overriding class from hardcoding its parent
213 * class, which someone might choose to change at some point.
216 * <title>Overriding a virtual method</title>
218 * typedef struct MyState MyState;
220 * typedef void (*MyDoSomething)(MyState *obj);
222 * typedef struct MyClass {
223 * ObjectClass parent_class;
225 * MyDoSomething do_something;
228 * static void my_do_something(MyState *obj)
233 * static void my_class_init(ObjectClass *oc, void *data)
235 * MyClass *mc = MY_CLASS(oc);
237 * mc->do_something = my_do_something;
240 * static const TypeInfo my_type_info = {
242 * .parent = TYPE_OBJECT,
243 * .instance_size = sizeof(MyState),
244 * .class_size = sizeof(MyClass),
245 * .class_init = my_class_init,
248 * typedef struct DerivedClass {
249 * MyClass parent_class;
251 * MyDoSomething parent_do_something;
254 * static void derived_do_something(MyState *obj)
256 * DerivedClass *dc = DERIVED_GET_CLASS(obj);
258 * // do something here
259 * dc->parent_do_something(obj);
260 * // do something else here
263 * static void derived_class_init(ObjectClass *oc, void *data)
265 * MyClass *mc = MY_CLASS(oc);
266 * DerivedClass *dc = DERIVED_CLASS(oc);
268 * dc->parent_do_something = mc->do_something;
269 * mc->do_something = derived_do_something;
272 * static const TypeInfo derived_type_info = {
273 * .name = TYPE_DERIVED,
275 * .class_size = sizeof(DerivedClass),
276 * .class_init = my_class_init,
281 * Alternatively, object_class_by_name() can be used to obtain the class and
282 * its non-overridden methods for a specific type. This would correspond to
283 * |[ MyClass::method(...) ]| in C++.
285 * The first example of such a QOM method was #CPUClass.reset,
286 * another example is #DeviceClass.realize.
291 * ObjectPropertyAccessor:
292 * @obj: the object that owns the property
293 * @v: the visitor that contains the property data
294 * @opaque: the object property opaque
295 * @name: the name of the property
296 * @errp: a pointer to an Error that is filled if getting/setting fails.
298 * Called when trying to get/set a property.
300 typedef void (ObjectPropertyAccessor
)(Object
*obj
,
304 struct Error
**errp
);
307 * ObjectPropertyRelease:
308 * @obj: the object that owns the property
309 * @name: the name of the property
310 * @opaque: the opaque registered with the property
312 * Called when a property is removed from a object.
314 typedef void (ObjectPropertyRelease
)(Object
*obj
,
318 typedef struct ObjectProperty
322 ObjectPropertyAccessor
*get
;
323 ObjectPropertyAccessor
*set
;
324 ObjectPropertyRelease
*release
;
327 QTAILQ_ENTRY(ObjectProperty
) node
;
332 * @obj: the object that is being removed from the composition tree
334 * Called when an object is being removed from the QOM composition tree.
335 * The function should remove any backlinks from children objects to @obj.
337 typedef void (ObjectUnparent
)(Object
*obj
);
341 * @obj: the object being freed
343 * Called when an object's last reference is removed.
345 typedef void (ObjectFree
)(void *obj
);
347 #define OBJECT_CLASS_CAST_CACHE 4
352 * The base for all classes. The only thing that #ObjectClass contains is an
353 * integer type handle.
361 const char *cast_cache
[OBJECT_CLASS_CAST_CACHE
];
363 ObjectUnparent
*unparent
;
369 * The base for all objects. The first member of this object is a pointer to
370 * a #ObjectClass. Since C guarantees that the first member of a structure
371 * always begins at byte 0 of that structure, as long as any sub-object places
372 * its parent as the first member, we can cast directly to a #Object.
374 * As a result, #Object contains a reference to the objects type as its
375 * first member. This allows identification of the real type of the object at
378 * #Object also contains a list of #Interfaces that this object
386 QTAILQ_HEAD(, ObjectProperty
) properties
;
393 * @name: The name of the type.
394 * @parent: The name of the parent type.
395 * @instance_size: The size of the object (derivative of #Object). If
396 * @instance_size is 0, then the size of the object will be the size of the
398 * @instance_init: This function is called to initialize an object. The parent
399 * class will have already been initialized so the type is only responsible
400 * for initializing its own members.
401 * @instance_post_init: This function is called to finish initialization of
402 * an object, after all @instance_init functions were called.
403 * @instance_finalize: This function is called during object destruction. This
404 * is called before the parent @instance_finalize function has been called.
405 * An object should only free the members that are unique to its type in this
407 * @abstract: If this field is true, then the class is considered abstract and
408 * cannot be directly instantiated.
409 * @class_size: The size of the class object (derivative of #ObjectClass)
410 * for this object. If @class_size is 0, then the size of the class will be
411 * assumed to be the size of the parent class. This allows a type to avoid
412 * implementing an explicit class type if they are not adding additional
414 * @class_init: This function is called after all parent class initialization
415 * has occurred to allow a class to set its default virtual method pointers.
416 * This is also the function to use to override virtual methods from a parent
418 * @class_base_init: This function is called for all base classes after all
419 * parent class initialization has occurred, but before the class itself
420 * is initialized. This is the function to use to undo the effects of
421 * memcpy from the parent class to the descendents.
422 * @class_finalize: This function is called during class destruction and is
423 * meant to release and dynamic parameters allocated by @class_init.
424 * @class_data: Data to pass to the @class_init, @class_base_init and
425 * @class_finalize functions. This can be useful when building dynamic
427 * @interfaces: The list of interfaces associated with this type. This
428 * should point to a static array that's terminated with a zero filled
436 size_t instance_size
;
437 void (*instance_init
)(Object
*obj
);
438 void (*instance_post_init
)(Object
*obj
);
439 void (*instance_finalize
)(Object
*obj
);
444 void (*class_init
)(ObjectClass
*klass
, void *data
);
445 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
446 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
449 InterfaceInfo
*interfaces
;
454 * @obj: A derivative of #Object
456 * Converts an object to a #Object. Since all objects are #Objects,
457 * this function will always succeed.
459 #define OBJECT(obj) \
464 * @class: A derivative of #ObjectClass.
466 * Converts a class to an #ObjectClass. Since all objects are #Objects,
467 * this function will always succeed.
469 #define OBJECT_CLASS(class) \
470 ((ObjectClass *)(class))
474 * @type: The C type to use for the return value.
475 * @obj: A derivative of @type to cast.
476 * @name: The QOM typename of @type
478 * A type safe version of @object_dynamic_cast_assert. Typically each class
479 * will define a macro based on this type to perform type safe dynamic_casts to
482 * If an invalid object is passed to this function, a run time assert will be
485 #define OBJECT_CHECK(type, obj, name) \
486 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
487 __FILE__, __LINE__, __func__))
490 * OBJECT_CLASS_CHECK:
491 * @class: The C type to use for the return value.
492 * @obj: A derivative of @type to cast.
493 * @name: the QOM typename of @class.
495 * A type safe version of @object_class_dynamic_cast_assert. This macro is
496 * typically wrapped by each type to perform type safe casts of a class to a
497 * specific class type.
499 #define OBJECT_CLASS_CHECK(class, obj, name) \
500 ((class *)object_class_dynamic_cast_assert(OBJECT_CLASS(obj), (name), \
501 __FILE__, __LINE__, __func__))
505 * @class: The C type to use for the return value.
506 * @obj: The object to obtain the class for.
507 * @name: The QOM typename of @obj.
509 * This function will return a specific class for a given object. Its generally
510 * used by each type to provide a type safe macro to get a specific class type
513 #define OBJECT_GET_CLASS(class, obj, name) \
514 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
518 * @type: The name of the interface.
520 * The information associated with an interface.
522 struct InterfaceInfo
{
528 * @parent_class: the base class
530 * The class for all interfaces. Subclasses of this class should only add
533 struct InterfaceClass
535 ObjectClass parent_class
;
537 ObjectClass
*concrete_class
;
540 #define TYPE_INTERFACE "interface"
544 * @klass: class to cast from
545 * Returns: An #InterfaceClass or raise an error if cast is invalid
547 #define INTERFACE_CLASS(klass) \
548 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
552 * @interface: the type to return
553 * @obj: the object to convert to an interface
554 * @name: the interface type name
556 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
558 #define INTERFACE_CHECK(interface, obj, name) \
559 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
560 __FILE__, __LINE__, __func__))
564 * @typename: The name of the type of the object to instantiate.
566 * This function will initialize a new object using heap allocated memory.
567 * The returned object has a reference count of 1, and will be freed when
568 * the last reference is dropped.
570 * Returns: The newly allocated and instantiated object.
572 Object
*object_new(const char *typename
);
575 * object_new_with_type:
576 * @type: The type of the object to instantiate.
578 * This function will initialize a new object using heap allocated memory.
579 * The returned object has a reference count of 1, and will be freed when
580 * the last reference is dropped.
582 * Returns: The newly allocated and instantiated object.
584 Object
*object_new_with_type(Type type
);
587 * object_initialize_with_type:
588 * @data: A pointer to the memory to be used for the object.
589 * @size: The maximum size available at @data for the object.
590 * @type: The type of the object to instantiate.
592 * This function will initialize an object. The memory for the object should
593 * have already been allocated. The returned object has a reference count of 1,
594 * and will be finalized when the last reference is dropped.
596 void object_initialize_with_type(void *data
, size_t size
, Type type
);
600 * @obj: A pointer to the memory to be used for the object.
601 * @size: The maximum size available at @obj for the object.
602 * @typename: The name of the type of the object to instantiate.
604 * This function will initialize an object. The memory for the object should
605 * have already been allocated. The returned object has a reference count of 1,
606 * and will be finalized when the last reference is dropped.
608 void object_initialize(void *obj
, size_t size
, const char *typename
);
611 * object_dynamic_cast:
612 * @obj: The object to cast.
613 * @typename: The @typename to cast to.
615 * This function will determine if @obj is-a @typename. @obj can refer to an
616 * object or an interface associated with an object.
618 * Returns: This function returns @obj on success or #NULL on failure.
620 Object
*object_dynamic_cast(Object
*obj
, const char *typename
);
623 * object_dynamic_cast_assert:
625 * See object_dynamic_cast() for a description of the parameters of this
626 * function. The only difference in behavior is that this function asserts
627 * instead of returning #NULL on failure if QOM cast debugging is enabled.
628 * This function is not meant to be called directly, but only through
629 * the wrapper macro OBJECT_CHECK.
631 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
632 const char *file
, int line
, const char *func
);
636 * @obj: A derivative of #Object
638 * Returns: The #ObjectClass of the type associated with @obj.
640 ObjectClass
*object_get_class(Object
*obj
);
643 * object_get_typename:
644 * @obj: A derivative of #Object.
646 * Returns: The QOM typename of @obj.
648 const char *object_get_typename(Object
*obj
);
651 * type_register_static:
652 * @info: The #TypeInfo of the new type.
654 * @info and all of the strings it points to should exist for the life time
655 * that the type is registered.
657 * Returns: 0 on failure, the new #Type on success.
659 Type
type_register_static(const TypeInfo
*info
);
663 * @info: The #TypeInfo of the new type
665 * Unlike type_register_static(), this call does not require @info or its
666 * string members to continue to exist after the call returns.
668 * Returns: 0 on failure, the new #Type on success.
670 Type
type_register(const TypeInfo
*info
);
673 * object_class_dynamic_cast_assert:
674 * @klass: The #ObjectClass to attempt to cast.
675 * @typename: The QOM typename of the class to cast to.
677 * See object_class_dynamic_cast() for a description of the parameters
678 * of this function. The only difference in behavior is that this function
679 * asserts instead of returning #NULL on failure if QOM cast debugging is
680 * enabled. This function is not meant to be called directly, but only through
681 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
683 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*klass
,
684 const char *typename
,
685 const char *file
, int line
,
689 * object_class_dynamic_cast:
690 * @klass: The #ObjectClass to attempt to cast.
691 * @typename: The QOM typename of the class to cast to.
693 * Returns: If @typename is a class, this function returns @klass if
694 * @typename is a subtype of @klass, else returns #NULL.
696 * If @typename is an interface, this function returns the interface
697 * definition for @klass if @klass implements it unambiguously; #NULL
698 * is returned if @klass does not implement the interface or if multiple
699 * classes or interfaces on the hierarchy leading to @klass implement
700 * it. (FIXME: perhaps this can be detected at type definition time?)
702 ObjectClass
*object_class_dynamic_cast(ObjectClass
*klass
,
703 const char *typename
);
706 * object_class_get_parent:
707 * @klass: The class to obtain the parent for.
709 * Returns: The parent for @klass or %NULL if none.
711 ObjectClass
*object_class_get_parent(ObjectClass
*klass
);
714 * object_class_get_name:
715 * @klass: The class to obtain the QOM typename for.
717 * Returns: The QOM typename for @klass.
719 const char *object_class_get_name(ObjectClass
*klass
);
722 * object_class_is_abstract:
723 * @klass: The class to obtain the abstractness for.
725 * Returns: %true if @klass is abstract, %false otherwise.
727 bool object_class_is_abstract(ObjectClass
*klass
);
730 * object_class_by_name:
731 * @typename: The QOM typename to obtain the class for.
733 * Returns: The class for @typename or %NULL if not found.
735 ObjectClass
*object_class_by_name(const char *typename
);
737 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
738 const char *implements_type
, bool include_abstract
,
742 * object_class_get_list:
743 * @implements_type: The type to filter for, including its derivatives.
744 * @include_abstract: Whether to include abstract classes.
746 * Returns: A singly-linked list of the classes in reverse hashtable order.
748 GSList
*object_class_get_list(const char *implements_type
,
749 bool include_abstract
);
755 * Increase the reference count of a object. A object cannot be freed as long
756 * as its reference count is greater than zero.
758 void object_ref(Object
*obj
);
764 * Decrease the reference count of a object. A object cannot be freed as long
765 * as its reference count is greater than zero.
767 void object_unref(Object
*obj
);
770 * object_property_add:
771 * @obj: the object to add a property to
772 * @name: the name of the property. This can contain any character except for
773 * a forward slash. In general, you should use hyphens '-' instead of
774 * underscores '_' when naming properties.
775 * @type: the type name of the property. This namespace is pretty loosely
776 * defined. Sub namespaces are constructed by using a prefix and then
777 * to angle brackets. For instance, the type 'virtio-net-pci' in the
778 * 'link' namespace would be 'link<virtio-net-pci>'.
779 * @get: The getter to be called to read a property. If this is NULL, then
780 * the property cannot be read.
781 * @set: the setter to be called to write a property. If this is NULL,
782 * then the property cannot be written.
783 * @release: called when the property is removed from the object. This is
784 * meant to allow a property to free its opaque upon object
785 * destruction. This may be NULL.
786 * @opaque: an opaque pointer to pass to the callbacks for the property
787 * @errp: returns an error if this function fails
789 void object_property_add(Object
*obj
, const char *name
, const char *type
,
790 ObjectPropertyAccessor
*get
,
791 ObjectPropertyAccessor
*set
,
792 ObjectPropertyRelease
*release
,
793 void *opaque
, struct Error
**errp
);
795 void object_property_del(Object
*obj
, const char *name
, struct Error
**errp
);
798 * object_property_find:
800 * @name: the name of the property
801 * @errp: returns an error if this function fails
803 * Look up a property for an object and return its #ObjectProperty if found.
805 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
806 struct Error
**errp
);
808 void object_unparent(Object
*obj
);
811 * object_property_get:
813 * @v: the visitor that will receive the property value. This should be an
814 * Output visitor and the data will be written with @name as the name.
815 * @name: the name of the property
816 * @errp: returns an error if this function fails
818 * Reads a property from a object.
820 void object_property_get(Object
*obj
, struct Visitor
*v
, const char *name
,
821 struct Error
**errp
);
824 * object_property_set_str:
825 * @value: the value to be written to the property
826 * @name: the name of the property
827 * @errp: returns an error if this function fails
829 * Writes a string value to a property.
831 void object_property_set_str(Object
*obj
, const char *value
,
832 const char *name
, struct Error
**errp
);
835 * object_property_get_str:
837 * @name: the name of the property
838 * @errp: returns an error if this function fails
840 * Returns: the value of the property, converted to a C string, or NULL if
841 * an error occurs (including when the property value is not a string).
842 * The caller should free the string.
844 char *object_property_get_str(Object
*obj
, const char *name
,
845 struct Error
**errp
);
848 * object_property_set_link:
849 * @value: the value to be written to the property
850 * @name: the name of the property
851 * @errp: returns an error if this function fails
853 * Writes an object's canonical path to a property.
855 void object_property_set_link(Object
*obj
, Object
*value
,
856 const char *name
, struct Error
**errp
);
859 * object_property_get_link:
861 * @name: the name of the property
862 * @errp: returns an error if this function fails
864 * Returns: the value of the property, resolved from a path to an Object,
865 * or NULL if an error occurs (including when the property value is not a
866 * string or not a valid object path).
868 Object
*object_property_get_link(Object
*obj
, const char *name
,
869 struct Error
**errp
);
872 * object_property_set_bool:
873 * @value: the value to be written to the property
874 * @name: the name of the property
875 * @errp: returns an error if this function fails
877 * Writes a bool value to a property.
879 void object_property_set_bool(Object
*obj
, bool value
,
880 const char *name
, struct Error
**errp
);
883 * object_property_get_bool:
885 * @name: the name of the property
886 * @errp: returns an error if this function fails
888 * Returns: the value of the property, converted to a boolean, or NULL if
889 * an error occurs (including when the property value is not a bool).
891 bool object_property_get_bool(Object
*obj
, const char *name
,
892 struct Error
**errp
);
895 * object_property_set_int:
896 * @value: the value to be written to the property
897 * @name: the name of the property
898 * @errp: returns an error if this function fails
900 * Writes an integer value to a property.
902 void object_property_set_int(Object
*obj
, int64_t value
,
903 const char *name
, struct Error
**errp
);
906 * object_property_get_int:
908 * @name: the name of the property
909 * @errp: returns an error if this function fails
911 * Returns: the value of the property, converted to an integer, or NULL if
912 * an error occurs (including when the property value is not an integer).
914 int64_t object_property_get_int(Object
*obj
, const char *name
,
915 struct Error
**errp
);
918 * object_property_set:
920 * @v: the visitor that will be used to write the property value. This should
921 * be an Input visitor and the data will be first read with @name as the
922 * name and then written as the property value.
923 * @name: the name of the property
924 * @errp: returns an error if this function fails
926 * Writes a property to a object.
928 void object_property_set(Object
*obj
, struct Visitor
*v
, const char *name
,
929 struct Error
**errp
);
932 * object_property_parse:
934 * @string: the string that will be used to parse the property value.
935 * @name: the name of the property
936 * @errp: returns an error if this function fails
938 * Parses a string and writes the result into a property of an object.
940 void object_property_parse(Object
*obj
, const char *string
,
941 const char *name
, struct Error
**errp
);
944 * object_property_print:
946 * @name: the name of the property
947 * @errp: returns an error if this function fails
949 * Returns a string representation of the value of the property. The
950 * caller shall free the string.
952 char *object_property_print(Object
*obj
, const char *name
,
953 struct Error
**errp
);
956 * object_property_get_type:
958 * @name: the name of the property
959 * @errp: returns an error if this function fails
961 * Returns: The type name of the property.
963 const char *object_property_get_type(Object
*obj
, const char *name
,
964 struct Error
**errp
);
969 * Returns: the root object of the composition tree
971 Object
*object_get_root(void);
974 * object_get_canonical_path:
976 * Returns: The canonical path for a object. This is the path within the
977 * composition tree starting from the root.
979 gchar
*object_get_canonical_path(Object
*obj
);
982 * object_resolve_path:
983 * @path: the path to resolve
984 * @ambiguous: returns true if the path resolution failed because of an
987 * There are two types of supported paths--absolute paths and partial paths.
989 * Absolute paths are derived from the root object and can follow child<> or
990 * link<> properties. Since they can follow link<> properties, they can be
991 * arbitrarily long. Absolute paths look like absolute filenames and are
992 * prefixed with a leading slash.
994 * Partial paths look like relative filenames. They do not begin with a
995 * prefix. The matching rules for partial paths are subtle but designed to make
996 * specifying objects easy. At each level of the composition tree, the partial
997 * path is matched as an absolute path. The first match is not returned. At
998 * least two matches are searched for. A successful result is only returned if
999 * only one match is found. If more than one match is found, a flag is
1000 * returned to indicate that the match was ambiguous.
1002 * Returns: The matched object or NULL on path lookup failure.
1004 Object
*object_resolve_path(const char *path
, bool *ambiguous
);
1007 * object_resolve_path_type:
1008 * @path: the path to resolve
1009 * @typename: the type to look for.
1010 * @ambiguous: returns true if the path resolution failed because of an
1013 * This is similar to object_resolve_path. However, when looking for a
1014 * partial path only matches that implement the given type are considered.
1015 * This restricts the search and avoids spuriously flagging matches as
1018 * For both partial and absolute paths, the return value goes through
1019 * a dynamic cast to @typename. This is important if either the link,
1020 * or the typename itself are of interface types.
1022 * Returns: The matched object or NULL on path lookup failure.
1024 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1028 * object_resolve_path_component:
1029 * @parent: the object in which to resolve the path
1030 * @part: the component to resolve.
1032 * This is similar to object_resolve_path with an absolute path, but it
1033 * only resolves one element (@part) and takes the others from @parent.
1035 * Returns: The resolved object or NULL on path lookup failure.
1037 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
);
1040 * object_property_add_child:
1041 * @obj: the object to add a property to
1042 * @name: the name of the property
1043 * @child: the child object
1044 * @errp: if an error occurs, a pointer to an area to store the area
1046 * Child properties form the composition tree. All objects need to be a child
1047 * of another object. Objects can only be a child of one object.
1049 * There is no way for a child to determine what its parent is. It is not
1050 * a bidirectional relationship. This is by design.
1052 * The value of a child property as a C string will be the child object's
1053 * canonical path. It can be retrieved using object_property_get_str().
1054 * The child object itself can be retrieved using object_property_get_link().
1056 void object_property_add_child(Object
*obj
, const char *name
,
1057 Object
*child
, struct Error
**errp
);
1060 * object_property_add_link:
1061 * @obj: the object to add a property to
1062 * @name: the name of the property
1063 * @type: the qobj type of the link
1064 * @child: a pointer to where the link object reference is stored
1065 * @errp: if an error occurs, a pointer to an area to store the area
1067 * Links establish relationships between objects. Links are unidirectional
1068 * although two links can be combined to form a bidirectional relationship
1071 * Links form the graph in the object model.
1073 * Ownership of the pointer that @child points to is transferred to the
1074 * link property. The reference count for <code>*@child</code> is
1075 * managed by the property from after the function returns till the
1076 * property is deleted with object_property_del().
1078 void object_property_add_link(Object
*obj
, const char *name
,
1079 const char *type
, Object
**child
,
1080 struct Error
**errp
);
1083 * object_property_add_str:
1084 * @obj: the object to add a property to
1085 * @name: the name of the property
1086 * @get: the getter or NULL if the property is write-only. This function must
1087 * return a string to be freed by g_free().
1088 * @set: the setter or NULL if the property is read-only
1089 * @errp: if an error occurs, a pointer to an area to store the error
1091 * Add a string property using getters/setters. This function will add a
1092 * property of type 'string'.
1094 void object_property_add_str(Object
*obj
, const char *name
,
1095 char *(*get
)(Object
*, struct Error
**),
1096 void (*set
)(Object
*, const char *, struct Error
**),
1097 struct Error
**errp
);
1100 * object_property_add_bool:
1101 * @obj: the object to add a property to
1102 * @name: the name of the property
1103 * @get: the getter or NULL if the property is write-only.
1104 * @set: the setter or NULL if the property is read-only
1105 * @errp: if an error occurs, a pointer to an area to store the error
1107 * Add a bool property using getters/setters. This function will add a
1108 * property of type 'bool'.
1110 void object_property_add_bool(Object
*obj
, const char *name
,
1111 bool (*get
)(Object
*, struct Error
**),
1112 void (*set
)(Object
*, bool, struct Error
**),
1113 struct Error
**errp
);
1116 * object_child_foreach:
1117 * @obj: the object whose children will be navigated
1118 * @fn: the iterator function to be called
1119 * @opaque: an opaque value that will be passed to the iterator
1121 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1124 * Returns: The last value returned by @fn, or 0 if there is no child.
1126 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1131 * @root: root of the #path, e.g., object_get_root()
1132 * @path: path to the container
1134 * Return a container object whose path is @path. Create more containers
1135 * along the path if necessary.
1137 * Returns: the container object.
1139 Object
*container_get(Object
*root
, const char *path
);