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"
21 #include "qapi/error.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 = derived_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
,
307 * ObjectPropertyResolve:
308 * @obj: the object that owns the property
309 * @opaque: the opaque registered with the property
310 * @part: the name of the property
312 * Resolves the #Object corresponding to property @part.
314 * The returned object can also be used as a starting point
315 * to resolve a relative path starting with "@part".
317 * Returns: If @path is the path that led to @obj, the function
318 * returns the #Object corresponding to "@path/@part".
319 * If "@path/@part" is not a valid object path, it returns #NULL.
321 typedef Object
*(ObjectPropertyResolve
)(Object
*obj
,
326 * ObjectPropertyRelease:
327 * @obj: the object that owns the property
328 * @name: the name of the property
329 * @opaque: the opaque registered with the property
331 * Called when a property is removed from a object.
333 typedef void (ObjectPropertyRelease
)(Object
*obj
,
337 typedef struct ObjectProperty
342 ObjectPropertyAccessor
*get
;
343 ObjectPropertyAccessor
*set
;
344 ObjectPropertyResolve
*resolve
;
345 ObjectPropertyRelease
*release
;
351 * @obj: the object that is being removed from the composition tree
353 * Called when an object is being removed from the QOM composition tree.
354 * The function should remove any backlinks from children objects to @obj.
356 typedef void (ObjectUnparent
)(Object
*obj
);
360 * @obj: the object being freed
362 * Called when an object's last reference is removed.
364 typedef void (ObjectFree
)(void *obj
);
366 #define OBJECT_CLASS_CAST_CACHE 4
371 * The base for all classes. The only thing that #ObjectClass contains is an
372 * integer type handle.
380 const char *object_cast_cache
[OBJECT_CLASS_CAST_CACHE
];
381 const char *class_cast_cache
[OBJECT_CLASS_CAST_CACHE
];
383 ObjectUnparent
*unparent
;
389 * The base for all objects. The first member of this object is a pointer to
390 * a #ObjectClass. Since C guarantees that the first member of a structure
391 * always begins at byte 0 of that structure, as long as any sub-object places
392 * its parent as the first member, we can cast directly to a #Object.
394 * As a result, #Object contains a reference to the objects type as its
395 * first member. This allows identification of the real type of the object at
398 * #Object also contains a list of #Interfaces that this object
406 GHashTable
*properties
;
413 * @name: The name of the type.
414 * @parent: The name of the parent type.
415 * @instance_size: The size of the object (derivative of #Object). If
416 * @instance_size is 0, then the size of the object will be the size of the
418 * @instance_init: This function is called to initialize an object. The parent
419 * class will have already been initialized so the type is only responsible
420 * for initializing its own members.
421 * @instance_post_init: This function is called to finish initialization of
422 * an object, after all @instance_init functions were called.
423 * @instance_finalize: This function is called during object destruction. This
424 * is called before the parent @instance_finalize function has been called.
425 * An object should only free the members that are unique to its type in this
427 * @abstract: If this field is true, then the class is considered abstract and
428 * cannot be directly instantiated.
429 * @class_size: The size of the class object (derivative of #ObjectClass)
430 * for this object. If @class_size is 0, then the size of the class will be
431 * assumed to be the size of the parent class. This allows a type to avoid
432 * implementing an explicit class type if they are not adding additional
434 * @class_init: This function is called after all parent class initialization
435 * has occurred to allow a class to set its default virtual method pointers.
436 * This is also the function to use to override virtual methods from a parent
438 * @class_base_init: This function is called for all base classes after all
439 * parent class initialization has occurred, but before the class itself
440 * is initialized. This is the function to use to undo the effects of
441 * memcpy from the parent class to the descendents.
442 * @class_finalize: This function is called during class destruction and is
443 * meant to release and dynamic parameters allocated by @class_init.
444 * @class_data: Data to pass to the @class_init, @class_base_init and
445 * @class_finalize functions. This can be useful when building dynamic
447 * @interfaces: The list of interfaces associated with this type. This
448 * should point to a static array that's terminated with a zero filled
456 size_t instance_size
;
457 void (*instance_init
)(Object
*obj
);
458 void (*instance_post_init
)(Object
*obj
);
459 void (*instance_finalize
)(Object
*obj
);
464 void (*class_init
)(ObjectClass
*klass
, void *data
);
465 void (*class_base_init
)(ObjectClass
*klass
, void *data
);
466 void (*class_finalize
)(ObjectClass
*klass
, void *data
);
469 InterfaceInfo
*interfaces
;
474 * @obj: A derivative of #Object
476 * Converts an object to a #Object. Since all objects are #Objects,
477 * this function will always succeed.
479 #define OBJECT(obj) \
484 * @class: A derivative of #ObjectClass.
486 * Converts a class to an #ObjectClass. Since all objects are #Objects,
487 * this function will always succeed.
489 #define OBJECT_CLASS(class) \
490 ((ObjectClass *)(class))
494 * @type: The C type to use for the return value.
495 * @obj: A derivative of @type to cast.
496 * @name: The QOM typename of @type
498 * A type safe version of @object_dynamic_cast_assert. Typically each class
499 * will define a macro based on this type to perform type safe dynamic_casts to
502 * If an invalid object is passed to this function, a run time assert will be
505 #define OBJECT_CHECK(type, obj, name) \
506 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
507 __FILE__, __LINE__, __func__))
510 * OBJECT_CLASS_CHECK:
511 * @class_type: The C type to use for the return value.
512 * @class: A derivative class of @class_type to cast.
513 * @name: the QOM typename of @class_type.
515 * A type safe version of @object_class_dynamic_cast_assert. This macro is
516 * typically wrapped by each type to perform type safe casts of a class to a
517 * specific class type.
519 #define OBJECT_CLASS_CHECK(class_type, class, name) \
520 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
521 __FILE__, __LINE__, __func__))
525 * @class: The C type to use for the return value.
526 * @obj: The object to obtain the class for.
527 * @name: The QOM typename of @obj.
529 * This function will return a specific class for a given object. Its generally
530 * used by each type to provide a type safe macro to get a specific class type
533 #define OBJECT_GET_CLASS(class, obj, name) \
534 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
538 * @type: The name of the interface.
540 * The information associated with an interface.
542 struct InterfaceInfo
{
548 * @parent_class: the base class
550 * The class for all interfaces. Subclasses of this class should only add
553 struct InterfaceClass
555 ObjectClass parent_class
;
557 ObjectClass
*concrete_class
;
561 #define TYPE_INTERFACE "interface"
565 * @klass: class to cast from
566 * Returns: An #InterfaceClass or raise an error if cast is invalid
568 #define INTERFACE_CLASS(klass) \
569 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
573 * @interface: the type to return
574 * @obj: the object to convert to an interface
575 * @name: the interface type name
577 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
579 #define INTERFACE_CHECK(interface, obj, name) \
580 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
581 __FILE__, __LINE__, __func__))
585 * @typename: The name of the type of the object to instantiate.
587 * This function will initialize a new object using heap allocated memory.
588 * The returned object has a reference count of 1, and will be freed when
589 * the last reference is dropped.
591 * Returns: The newly allocated and instantiated object.
593 Object
*object_new(const char *typename
);
596 * object_new_with_type:
597 * @type: The type of the object to instantiate.
599 * This function will initialize a new object using heap allocated memory.
600 * The returned object has a reference count of 1, and will be freed when
601 * the last reference is dropped.
603 * Returns: The newly allocated and instantiated object.
605 Object
*object_new_with_type(Type type
);
608 * object_new_with_props:
609 * @typename: The name of the type of the object to instantiate.
610 * @parent: the parent object
611 * @id: The unique ID of the object
612 * @errp: pointer to error object
613 * @...: list of property names and values
615 * This function will initialize a new object using heap allocated memory.
616 * The returned object has a reference count of 1, and will be freed when
617 * the last reference is dropped.
619 * The @id parameter will be used when registering the object as a
620 * child of @parent in the composition tree.
622 * The variadic parameters are a list of pairs of (propname, propvalue)
623 * strings. The propname of %NULL indicates the end of the property
624 * list. If the object implements the user creatable interface, the
625 * object will be marked complete once all the properties have been
629 * <title>Creating an object with properties</title>
634 * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE,
635 * object_get_objects_root(),
639 * "mem-path", "/dev/shm/somefile",
645 * g_printerr("Cannot create memory backend: %s\n",
646 * error_get_pretty(err));
651 * The returned object will have one stable reference maintained
652 * for as long as it is present in the object hierarchy.
654 * Returns: The newly allocated, instantiated & initialized object.
656 Object
*object_new_with_props(const char *typename
,
663 * object_new_with_propv:
664 * @typename: The name of the type of the object to instantiate.
665 * @parent: the parent object
666 * @id: The unique ID of the object
667 * @errp: pointer to error object
668 * @vargs: list of property names and values
670 * See object_new_with_props() for documentation.
672 Object
*object_new_with_propv(const char *typename
,
680 * @obj: the object instance to set properties on
681 * @errp: pointer to error object
682 * @...: list of property names and values
684 * This function will set a list of properties on an existing object
687 * The variadic parameters are a list of pairs of (propname, propvalue)
688 * strings. The propname of %NULL indicates the end of the property
692 * <title>Update an object's properties</title>
695 * Object *obj = ...get / create object...;
697 * obj = object_set_props(obj,
700 * "mem-path", "/dev/shm/somefile",
706 * g_printerr("Cannot set properties: %s\n",
707 * error_get_pretty(err));
712 * The returned object will have one stable reference maintained
713 * for as long as it is present in the object hierarchy.
715 * Returns: -1 on error, 0 on success
717 int object_set_props(Object
*obj
,
723 * @obj: the object instance to set properties on
724 * @errp: pointer to error object
725 * @vargs: list of property names and values
727 * See object_set_props() for documentation.
729 * Returns: -1 on error, 0 on success
731 int object_set_propv(Object
*obj
,
736 * object_initialize_with_type:
737 * @data: A pointer to the memory to be used for the object.
738 * @size: The maximum size available at @data for the object.
739 * @type: The type of the object to instantiate.
741 * This function will initialize an object. The memory for the object should
742 * have already been allocated. The returned object has a reference count of 1,
743 * and will be finalized when the last reference is dropped.
745 void object_initialize_with_type(void *data
, size_t size
, Type type
);
749 * @obj: A pointer to the memory to be used for the object.
750 * @size: The maximum size available at @obj for the object.
751 * @typename: The name of the type of the object to instantiate.
753 * This function will initialize an object. The memory for the object should
754 * have already been allocated. The returned object has a reference count of 1,
755 * and will be finalized when the last reference is dropped.
757 void object_initialize(void *obj
, size_t size
, const char *typename
);
760 * object_dynamic_cast:
761 * @obj: The object to cast.
762 * @typename: The @typename to cast to.
764 * This function will determine if @obj is-a @typename. @obj can refer to an
765 * object or an interface associated with an object.
767 * Returns: This function returns @obj on success or #NULL on failure.
769 Object
*object_dynamic_cast(Object
*obj
, const char *typename
);
772 * object_dynamic_cast_assert:
774 * See object_dynamic_cast() for a description of the parameters of this
775 * function. The only difference in behavior is that this function asserts
776 * instead of returning #NULL on failure if QOM cast debugging is enabled.
777 * This function is not meant to be called directly, but only through
778 * the wrapper macro OBJECT_CHECK.
780 Object
*object_dynamic_cast_assert(Object
*obj
, const char *typename
,
781 const char *file
, int line
, const char *func
);
785 * @obj: A derivative of #Object
787 * Returns: The #ObjectClass of the type associated with @obj.
789 ObjectClass
*object_get_class(Object
*obj
);
792 * object_get_typename:
793 * @obj: A derivative of #Object.
795 * Returns: The QOM typename of @obj.
797 const char *object_get_typename(Object
*obj
);
800 * type_register_static:
801 * @info: The #TypeInfo of the new type.
803 * @info and all of the strings it points to should exist for the life time
804 * that the type is registered.
806 * Returns: 0 on failure, the new #Type on success.
808 Type
type_register_static(const TypeInfo
*info
);
812 * @info: The #TypeInfo of the new type
814 * Unlike type_register_static(), this call does not require @info or its
815 * string members to continue to exist after the call returns.
817 * Returns: 0 on failure, the new #Type on success.
819 Type
type_register(const TypeInfo
*info
);
822 * object_class_dynamic_cast_assert:
823 * @klass: The #ObjectClass to attempt to cast.
824 * @typename: The QOM typename of the class to cast to.
826 * See object_class_dynamic_cast() for a description of the parameters
827 * of this function. The only difference in behavior is that this function
828 * asserts instead of returning #NULL on failure if QOM cast debugging is
829 * enabled. This function is not meant to be called directly, but only through
830 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
832 ObjectClass
*object_class_dynamic_cast_assert(ObjectClass
*klass
,
833 const char *typename
,
834 const char *file
, int line
,
838 * object_class_dynamic_cast:
839 * @klass: The #ObjectClass to attempt to cast.
840 * @typename: The QOM typename of the class to cast to.
842 * Returns: If @typename is a class, this function returns @klass if
843 * @typename is a subtype of @klass, else returns #NULL.
845 * If @typename is an interface, this function returns the interface
846 * definition for @klass if @klass implements it unambiguously; #NULL
847 * is returned if @klass does not implement the interface or if multiple
848 * classes or interfaces on the hierarchy leading to @klass implement
849 * it. (FIXME: perhaps this can be detected at type definition time?)
851 ObjectClass
*object_class_dynamic_cast(ObjectClass
*klass
,
852 const char *typename
);
855 * object_class_get_parent:
856 * @klass: The class to obtain the parent for.
858 * Returns: The parent for @klass or %NULL if none.
860 ObjectClass
*object_class_get_parent(ObjectClass
*klass
);
863 * object_class_get_name:
864 * @klass: The class to obtain the QOM typename for.
866 * Returns: The QOM typename for @klass.
868 const char *object_class_get_name(ObjectClass
*klass
);
871 * object_class_is_abstract:
872 * @klass: The class to obtain the abstractness for.
874 * Returns: %true if @klass is abstract, %false otherwise.
876 bool object_class_is_abstract(ObjectClass
*klass
);
879 * object_class_by_name:
880 * @typename: The QOM typename to obtain the class for.
882 * Returns: The class for @typename or %NULL if not found.
884 ObjectClass
*object_class_by_name(const char *typename
);
886 void object_class_foreach(void (*fn
)(ObjectClass
*klass
, void *opaque
),
887 const char *implements_type
, bool include_abstract
,
891 * object_class_get_list:
892 * @implements_type: The type to filter for, including its derivatives.
893 * @include_abstract: Whether to include abstract classes.
895 * Returns: A singly-linked list of the classes in reverse hashtable order.
897 GSList
*object_class_get_list(const char *implements_type
,
898 bool include_abstract
);
904 * Increase the reference count of a object. A object cannot be freed as long
905 * as its reference count is greater than zero.
907 void object_ref(Object
*obj
);
913 * Decrease the reference count of a object. A object cannot be freed as long
914 * as its reference count is greater than zero.
916 void object_unref(Object
*obj
);
919 * object_property_add:
920 * @obj: the object to add a property to
921 * @name: the name of the property. This can contain any character except for
922 * a forward slash. In general, you should use hyphens '-' instead of
923 * underscores '_' when naming properties.
924 * @type: the type name of the property. This namespace is pretty loosely
925 * defined. Sub namespaces are constructed by using a prefix and then
926 * to angle brackets. For instance, the type 'virtio-net-pci' in the
927 * 'link' namespace would be 'link<virtio-net-pci>'.
928 * @get: The getter to be called to read a property. If this is NULL, then
929 * the property cannot be read.
930 * @set: the setter to be called to write a property. If this is NULL,
931 * then the property cannot be written.
932 * @release: called when the property is removed from the object. This is
933 * meant to allow a property to free its opaque upon object
934 * destruction. This may be NULL.
935 * @opaque: an opaque pointer to pass to the callbacks for the property
936 * @errp: returns an error if this function fails
938 * Returns: The #ObjectProperty; this can be used to set the @resolve
939 * callback for child and link properties.
941 ObjectProperty
*object_property_add(Object
*obj
, const char *name
,
943 ObjectPropertyAccessor
*get
,
944 ObjectPropertyAccessor
*set
,
945 ObjectPropertyRelease
*release
,
946 void *opaque
, Error
**errp
);
948 void object_property_del(Object
*obj
, const char *name
, Error
**errp
);
951 * object_property_find:
953 * @name: the name of the property
954 * @errp: returns an error if this function fails
956 * Look up a property for an object and return its #ObjectProperty if found.
958 ObjectProperty
*object_property_find(Object
*obj
, const char *name
,
961 typedef struct ObjectPropertyIterator ObjectPropertyIterator
;
964 * object_property_iter_init:
967 * Initializes an iterator for traversing all properties
968 * registered against an object instance.
970 * It is forbidden to modify the property list while iterating,
971 * whether removing or adding properties.
973 * Typical usage pattern would be
976 * <title>Using object property iterators</title>
978 * ObjectProperty *prop;
979 * ObjectPropertyIterator *iter;
981 * iter = object_property_iter_init(obj);
982 * while ((prop = object_property_iter_next(iter))) {
983 * ... do something with prop ...
985 * object_property_iter_free(iter);
989 * Returns: the new iterator
991 ObjectPropertyIterator
*object_property_iter_init(Object
*obj
);
994 * object_property_iter_free:
995 * @iter: the iterator instance
997 * Releases any resources associated with the iterator.
999 void object_property_iter_free(ObjectPropertyIterator
*iter
);
1002 * object_property_iter_next:
1003 * @iter: the iterator instance
1005 * Returns: the next property, or %NULL when all properties
1006 * have been traversed.
1008 ObjectProperty
*object_property_iter_next(ObjectPropertyIterator
*iter
);
1010 void object_unparent(Object
*obj
);
1013 * object_property_get:
1015 * @v: the visitor that will receive the property value. This should be an
1016 * Output visitor and the data will be written with @name as the name.
1017 * @name: the name of the property
1018 * @errp: returns an error if this function fails
1020 * Reads a property from a object.
1022 void object_property_get(Object
*obj
, struct Visitor
*v
, const char *name
,
1026 * object_property_set_str:
1027 * @value: the value to be written to the property
1028 * @name: the name of the property
1029 * @errp: returns an error if this function fails
1031 * Writes a string value to a property.
1033 void object_property_set_str(Object
*obj
, const char *value
,
1034 const char *name
, Error
**errp
);
1037 * object_property_get_str:
1039 * @name: the name of the property
1040 * @errp: returns an error if this function fails
1042 * Returns: the value of the property, converted to a C string, or NULL if
1043 * an error occurs (including when the property value is not a string).
1044 * The caller should free the string.
1046 char *object_property_get_str(Object
*obj
, const char *name
,
1050 * object_property_set_link:
1051 * @value: the value to be written to the property
1052 * @name: the name of the property
1053 * @errp: returns an error if this function fails
1055 * Writes an object's canonical path to a property.
1057 void object_property_set_link(Object
*obj
, Object
*value
,
1058 const char *name
, Error
**errp
);
1061 * object_property_get_link:
1063 * @name: the name of the property
1064 * @errp: returns an error if this function fails
1066 * Returns: the value of the property, resolved from a path to an Object,
1067 * or NULL if an error occurs (including when the property value is not a
1068 * string or not a valid object path).
1070 Object
*object_property_get_link(Object
*obj
, const char *name
,
1074 * object_property_set_bool:
1075 * @value: the value to be written to the property
1076 * @name: the name of the property
1077 * @errp: returns an error if this function fails
1079 * Writes a bool value to a property.
1081 void object_property_set_bool(Object
*obj
, bool value
,
1082 const char *name
, Error
**errp
);
1085 * object_property_get_bool:
1087 * @name: the name of the property
1088 * @errp: returns an error if this function fails
1090 * Returns: the value of the property, converted to a boolean, or NULL if
1091 * an error occurs (including when the property value is not a bool).
1093 bool object_property_get_bool(Object
*obj
, const char *name
,
1097 * object_property_set_int:
1098 * @value: the value to be written to the property
1099 * @name: the name of the property
1100 * @errp: returns an error if this function fails
1102 * Writes an integer value to a property.
1104 void object_property_set_int(Object
*obj
, int64_t value
,
1105 const char *name
, Error
**errp
);
1108 * object_property_get_int:
1110 * @name: the name of the property
1111 * @errp: returns an error if this function fails
1113 * Returns: the value of the property, converted to an integer, or NULL if
1114 * an error occurs (including when the property value is not an integer).
1116 int64_t object_property_get_int(Object
*obj
, const char *name
,
1120 * object_property_get_enum:
1122 * @name: the name of the property
1123 * @typename: the name of the enum data type
1124 * @errp: returns an error if this function fails
1126 * Returns: the value of the property, converted to an integer, or
1127 * undefined if an error occurs (including when the property value is not
1130 int object_property_get_enum(Object
*obj
, const char *name
,
1131 const char *typename
, Error
**errp
);
1134 * object_property_get_uint16List:
1136 * @name: the name of the property
1137 * @list: the returned int list
1138 * @errp: returns an error if this function fails
1140 * Returns: the value of the property, converted to integers, or
1141 * undefined if an error occurs (including when the property value is not
1142 * an list of integers).
1144 void object_property_get_uint16List(Object
*obj
, const char *name
,
1145 uint16List
**list
, Error
**errp
);
1148 * object_property_set:
1150 * @v: the visitor that will be used to write the property value. This should
1151 * be an Input visitor and the data will be first read with @name as the
1152 * name and then written as the property value.
1153 * @name: the name of the property
1154 * @errp: returns an error if this function fails
1156 * Writes a property to a object.
1158 void object_property_set(Object
*obj
, struct Visitor
*v
, const char *name
,
1162 * object_property_parse:
1164 * @string: the string that will be used to parse the property value.
1165 * @name: the name of the property
1166 * @errp: returns an error if this function fails
1168 * Parses a string and writes the result into a property of an object.
1170 void object_property_parse(Object
*obj
, const char *string
,
1171 const char *name
, Error
**errp
);
1174 * object_property_print:
1176 * @name: the name of the property
1177 * @human: if true, print for human consumption
1178 * @errp: returns an error if this function fails
1180 * Returns a string representation of the value of the property. The
1181 * caller shall free the string.
1183 char *object_property_print(Object
*obj
, const char *name
, bool human
,
1187 * object_property_get_type:
1189 * @name: the name of the property
1190 * @errp: returns an error if this function fails
1192 * Returns: The type name of the property.
1194 const char *object_property_get_type(Object
*obj
, const char *name
,
1200 * Returns: the root object of the composition tree
1202 Object
*object_get_root(void);
1206 * object_get_objects_root:
1208 * Get the container object that holds user created
1209 * object instances. This is the object at path
1212 * Returns: the user object container
1214 Object
*object_get_objects_root(void);
1217 * object_get_canonical_path_component:
1219 * Returns: The final component in the object's canonical path. The canonical
1220 * path is the path within the composition tree starting from the root.
1222 gchar
*object_get_canonical_path_component(Object
*obj
);
1225 * object_get_canonical_path:
1227 * Returns: The canonical path for a object. This is the path within the
1228 * composition tree starting from the root.
1230 gchar
*object_get_canonical_path(Object
*obj
);
1233 * object_resolve_path:
1234 * @path: the path to resolve
1235 * @ambiguous: returns true if the path resolution failed because of an
1238 * There are two types of supported paths--absolute paths and partial paths.
1240 * Absolute paths are derived from the root object and can follow child<> or
1241 * link<> properties. Since they can follow link<> properties, they can be
1242 * arbitrarily long. Absolute paths look like absolute filenames and are
1243 * prefixed with a leading slash.
1245 * Partial paths look like relative filenames. They do not begin with a
1246 * prefix. The matching rules for partial paths are subtle but designed to make
1247 * specifying objects easy. At each level of the composition tree, the partial
1248 * path is matched as an absolute path. The first match is not returned. At
1249 * least two matches are searched for. A successful result is only returned if
1250 * only one match is found. If more than one match is found, a flag is
1251 * returned to indicate that the match was ambiguous.
1253 * Returns: The matched object or NULL on path lookup failure.
1255 Object
*object_resolve_path(const char *path
, bool *ambiguous
);
1258 * object_resolve_path_type:
1259 * @path: the path to resolve
1260 * @typename: the type to look for.
1261 * @ambiguous: returns true if the path resolution failed because of an
1264 * This is similar to object_resolve_path. However, when looking for a
1265 * partial path only matches that implement the given type are considered.
1266 * This restricts the search and avoids spuriously flagging matches as
1269 * For both partial and absolute paths, the return value goes through
1270 * a dynamic cast to @typename. This is important if either the link,
1271 * or the typename itself are of interface types.
1273 * Returns: The matched object or NULL on path lookup failure.
1275 Object
*object_resolve_path_type(const char *path
, const char *typename
,
1279 * object_resolve_path_component:
1280 * @parent: the object in which to resolve the path
1281 * @part: the component to resolve.
1283 * This is similar to object_resolve_path with an absolute path, but it
1284 * only resolves one element (@part) and takes the others from @parent.
1286 * Returns: The resolved object or NULL on path lookup failure.
1288 Object
*object_resolve_path_component(Object
*parent
, const gchar
*part
);
1291 * object_property_add_child:
1292 * @obj: the object to add a property to
1293 * @name: the name of the property
1294 * @child: the child object
1295 * @errp: if an error occurs, a pointer to an area to store the area
1297 * Child properties form the composition tree. All objects need to be a child
1298 * of another object. Objects can only be a child of one object.
1300 * There is no way for a child to determine what its parent is. It is not
1301 * a bidirectional relationship. This is by design.
1303 * The value of a child property as a C string will be the child object's
1304 * canonical path. It can be retrieved using object_property_get_str().
1305 * The child object itself can be retrieved using object_property_get_link().
1307 void object_property_add_child(Object
*obj
, const char *name
,
1308 Object
*child
, Error
**errp
);
1311 /* Unref the link pointer when the property is deleted */
1312 OBJ_PROP_LINK_UNREF_ON_RELEASE
= 0x1,
1313 } ObjectPropertyLinkFlags
;
1316 * object_property_allow_set_link:
1318 * The default implementation of the object_property_add_link() check()
1319 * callback function. It allows the link property to be set and never returns
1322 void object_property_allow_set_link(Object
*, const char *,
1323 Object
*, Error
**);
1326 * object_property_add_link:
1327 * @obj: the object to add a property to
1328 * @name: the name of the property
1329 * @type: the qobj type of the link
1330 * @child: a pointer to where the link object reference is stored
1331 * @check: callback to veto setting or NULL if the property is read-only
1332 * @flags: additional options for the link
1333 * @errp: if an error occurs, a pointer to an area to store the area
1335 * Links establish relationships between objects. Links are unidirectional
1336 * although two links can be combined to form a bidirectional relationship
1339 * Links form the graph in the object model.
1341 * The <code>@check()</code> callback is invoked when
1342 * object_property_set_link() is called and can raise an error to prevent the
1343 * link being set. If <code>@check</code> is NULL, the property is read-only
1344 * and cannot be set.
1346 * Ownership of the pointer that @child points to is transferred to the
1347 * link property. The reference count for <code>*@child</code> is
1348 * managed by the property from after the function returns till the
1349 * property is deleted with object_property_del(). If the
1350 * <code>@flags</code> <code>OBJ_PROP_LINK_UNREF_ON_RELEASE</code> bit is set,
1351 * the reference count is decremented when the property is deleted.
1353 void object_property_add_link(Object
*obj
, const char *name
,
1354 const char *type
, Object
**child
,
1355 void (*check
)(Object
*obj
, const char *name
,
1356 Object
*val
, Error
**errp
),
1357 ObjectPropertyLinkFlags flags
,
1361 * object_property_add_str:
1362 * @obj: the object to add a property to
1363 * @name: the name of the property
1364 * @get: the getter or NULL if the property is write-only. This function must
1365 * return a string to be freed by g_free().
1366 * @set: the setter or NULL if the property is read-only
1367 * @errp: if an error occurs, a pointer to an area to store the error
1369 * Add a string property using getters/setters. This function will add a
1370 * property of type 'string'.
1372 void object_property_add_str(Object
*obj
, const char *name
,
1373 char *(*get
)(Object
*, Error
**),
1374 void (*set
)(Object
*, const char *, Error
**),
1378 * object_property_add_bool:
1379 * @obj: the object to add a property to
1380 * @name: the name of the property
1381 * @get: the getter or NULL if the property is write-only.
1382 * @set: the setter or NULL if the property is read-only
1383 * @errp: if an error occurs, a pointer to an area to store the error
1385 * Add a bool property using getters/setters. This function will add a
1386 * property of type 'bool'.
1388 void object_property_add_bool(Object
*obj
, const char *name
,
1389 bool (*get
)(Object
*, Error
**),
1390 void (*set
)(Object
*, bool, Error
**),
1394 * object_property_add_enum:
1395 * @obj: the object to add a property to
1396 * @name: the name of the property
1397 * @typename: the name of the enum data type
1398 * @get: the getter or %NULL if the property is write-only.
1399 * @set: the setter or %NULL if the property is read-only
1400 * @errp: if an error occurs, a pointer to an area to store the error
1402 * Add an enum property using getters/setters. This function will add a
1403 * property of type '@typename'.
1405 void object_property_add_enum(Object
*obj
, const char *name
,
1406 const char *typename
,
1407 const char * const *strings
,
1408 int (*get
)(Object
*, Error
**),
1409 void (*set
)(Object
*, int, Error
**),
1413 * object_property_add_tm:
1414 * @obj: the object to add a property to
1415 * @name: the name of the property
1416 * @get: the getter or NULL if the property is write-only.
1417 * @errp: if an error occurs, a pointer to an area to store the error
1419 * Add a read-only struct tm valued property using a getter function.
1420 * This function will add a property of type 'struct tm'.
1422 void object_property_add_tm(Object
*obj
, const char *name
,
1423 void (*get
)(Object
*, struct tm
*, Error
**),
1427 * object_property_add_uint8_ptr:
1428 * @obj: the object to add a property to
1429 * @name: the name of the property
1430 * @v: pointer to value
1431 * @errp: if an error occurs, a pointer to an area to store the error
1433 * Add an integer property in memory. This function will add a
1434 * property of type 'uint8'.
1436 void object_property_add_uint8_ptr(Object
*obj
, const char *name
,
1437 const uint8_t *v
, Error
**errp
);
1440 * object_property_add_uint16_ptr:
1441 * @obj: the object to add a property to
1442 * @name: the name of the property
1443 * @v: pointer to value
1444 * @errp: if an error occurs, a pointer to an area to store the error
1446 * Add an integer property in memory. This function will add a
1447 * property of type 'uint16'.
1449 void object_property_add_uint16_ptr(Object
*obj
, const char *name
,
1450 const uint16_t *v
, Error
**errp
);
1453 * object_property_add_uint32_ptr:
1454 * @obj: the object to add a property to
1455 * @name: the name of the property
1456 * @v: pointer to value
1457 * @errp: if an error occurs, a pointer to an area to store the error
1459 * Add an integer property in memory. This function will add a
1460 * property of type 'uint32'.
1462 void object_property_add_uint32_ptr(Object
*obj
, const char *name
,
1463 const uint32_t *v
, Error
**errp
);
1466 * object_property_add_uint64_ptr:
1467 * @obj: the object to add a property to
1468 * @name: the name of the property
1469 * @v: pointer to value
1470 * @errp: if an error occurs, a pointer to an area to store the error
1472 * Add an integer property in memory. This function will add a
1473 * property of type 'uint64'.
1475 void object_property_add_uint64_ptr(Object
*obj
, const char *name
,
1476 const uint64_t *v
, Error
**Errp
);
1479 * object_property_add_alias:
1480 * @obj: the object to add a property to
1481 * @name: the name of the property
1482 * @target_obj: the object to forward property access to
1483 * @target_name: the name of the property on the forwarded object
1484 * @errp: if an error occurs, a pointer to an area to store the error
1486 * Add an alias for a property on an object. This function will add a property
1487 * of the same type as the forwarded property.
1489 * The caller must ensure that <code>@target_obj</code> stays alive as long as
1490 * this property exists. In the case of a child object or an alias on the same
1491 * object this will be the case. For aliases to other objects the caller is
1492 * responsible for taking a reference.
1494 void object_property_add_alias(Object
*obj
, const char *name
,
1495 Object
*target_obj
, const char *target_name
,
1499 * object_property_add_const_link:
1500 * @obj: the object to add a property to
1501 * @name: the name of the property
1502 * @target: the object to be referred by the link
1503 * @errp: if an error occurs, a pointer to an area to store the error
1505 * Add an unmodifiable link for a property on an object. This function will
1506 * add a property of type link<TYPE> where TYPE is the type of @target.
1508 * The caller must ensure that @target stays alive as long as
1509 * this property exists. In the case @target is a child of @obj,
1510 * this will be the case. Otherwise, the caller is responsible for
1511 * taking a reference.
1513 void object_property_add_const_link(Object
*obj
, const char *name
,
1514 Object
*target
, Error
**errp
);
1517 * object_property_set_description:
1518 * @obj: the object owning the property
1519 * @name: the name of the property
1520 * @description: the description of the property on the object
1521 * @errp: if an error occurs, a pointer to an area to store the error
1523 * Set an object property's description.
1526 void object_property_set_description(Object
*obj
, const char *name
,
1527 const char *description
, Error
**errp
);
1530 * object_child_foreach:
1531 * @obj: the object whose children will be navigated
1532 * @fn: the iterator function to be called
1533 * @opaque: an opaque value that will be passed to the iterator
1535 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1538 * It is forbidden to add or remove children from @obj from the @fn
1541 * Returns: The last value returned by @fn, or 0 if there is no child.
1543 int object_child_foreach(Object
*obj
, int (*fn
)(Object
*child
, void *opaque
),
1547 * object_child_foreach_recursive:
1548 * @obj: the object whose children will be navigated
1549 * @fn: the iterator function to be called
1550 * @opaque: an opaque value that will be passed to the iterator
1552 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1553 * non-zero. Calls recursively, all child nodes of @obj will also be passed
1554 * all the way down to the leaf nodes of the tree. Depth first ordering.
1556 * It is forbidden to add or remove children from @obj (or its
1557 * child nodes) from the @fn callback.
1559 * Returns: The last value returned by @fn, or 0 if there is no child.
1561 int object_child_foreach_recursive(Object
*obj
,
1562 int (*fn
)(Object
*child
, void *opaque
),
1566 * @root: root of the #path, e.g., object_get_root()
1567 * @path: path to the container
1569 * Return a container object whose path is @path. Create more containers
1570 * along the path if necessary.
1572 * Returns: the container object.
1574 Object
*container_get(Object
*root
, const char *path
);