vhost-user: fix use after free
[qemu/kevin.git] / include / qom / object.h
blobeda16df005ca97299c59a93b149eaa5ef453811c
1 /*
2 * QEMU Object Model
4 * Copyright IBM, Corp. 2011
6 * Authors:
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.
14 #ifndef QEMU_OBJECT_H
15 #define QEMU_OBJECT_H
17 #include <glib.h>
18 #include "qemu/queue.h"
19 #include "qemu/typedefs.h"
21 struct TypeImpl;
22 typedef struct TypeImpl *Type;
24 typedef struct ObjectClass ObjectClass;
25 typedef struct Object Object;
27 typedef struct TypeInfo TypeInfo;
29 typedef struct InterfaceClass InterfaceClass;
30 typedef struct InterfaceInfo InterfaceInfo;
32 #define TYPE_OBJECT "object"
34 /**
35 * SECTION:object.h
36 * @title:Base Object Type System
37 * @short_description: interfaces for creating new types and objects
39 * The QEMU Object Model provides a framework for registering user creatable
40 * types and instantiating objects from those types. QOM provides the following
41 * features:
43 * - System for dynamically registering types
44 * - Support for single-inheritance of types
45 * - Multiple inheritance of stateless interfaces
47 * <example>
48 * <title>Creating a minimal type</title>
49 * <programlisting>
50 * #include "qdev.h"
52 * #define TYPE_MY_DEVICE "my-device"
54 * // No new virtual functions: we can reuse the typedef for the
55 * // superclass.
56 * typedef DeviceClass MyDeviceClass;
57 * typedef struct MyDevice
58 * {
59 * DeviceState parent;
61 * int reg0, reg1, reg2;
62 * } MyDevice;
64 * static const TypeInfo my_device_info = {
65 * .name = TYPE_MY_DEVICE,
66 * .parent = TYPE_DEVICE,
67 * .instance_size = sizeof(MyDevice),
68 * };
70 * static void my_device_register_types(void)
71 * {
72 * type_register_static(&my_device_info);
73 * }
75 * type_init(my_device_register_types)
76 * </programlisting>
77 * </example>
79 * In the above example, we create a simple type that is described by #TypeInfo.
80 * #TypeInfo describes information about the type including what it inherits
81 * from, the instance and class size, and constructor/destructor hooks.
83 * Every type has an #ObjectClass associated with it. #ObjectClass derivatives
84 * are instantiated dynamically but there is only ever one instance for any
85 * given type. The #ObjectClass typically holds a table of function pointers
86 * for the virtual methods implemented by this type.
88 * Using object_new(), a new #Object derivative will be instantiated. You can
89 * cast an #Object to a subclass (or base-class) type using
90 * object_dynamic_cast(). You typically want to define macro wrappers around
91 * OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
92 * specific type:
94 * <example>
95 * <title>Typecasting macros</title>
96 * <programlisting>
97 * #define MY_DEVICE_GET_CLASS(obj) \
98 * OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
99 * #define MY_DEVICE_CLASS(klass) \
100 * OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
101 * #define MY_DEVICE(obj) \
102 * OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
103 * </programlisting>
104 * </example>
106 * # Class Initialization #
108 * Before an object is initialized, the class for the object must be
109 * initialized. There is only one class object for all instance objects
110 * that is created lazily.
112 * Classes are initialized by first initializing any parent classes (if
113 * necessary). After the parent class object has initialized, it will be
114 * copied into the current class object and any additional storage in the
115 * class object is zero filled.
117 * The effect of this is that classes automatically inherit any virtual
118 * function pointers that the parent class has already initialized. All
119 * other fields will be zero filled.
121 * Once all of the parent classes have been initialized, #TypeInfo::class_init
122 * is called to let the class being instantiated provide default initialize for
123 * its virtual functions. Here is how the above example might be modified
124 * to introduce an overridden virtual function:
126 * <example>
127 * <title>Overriding a virtual function</title>
128 * <programlisting>
129 * #include "qdev.h"
131 * void my_device_class_init(ObjectClass *klass, void *class_data)
133 * DeviceClass *dc = DEVICE_CLASS(klass);
134 * dc->reset = my_device_reset;
137 * static const TypeInfo my_device_info = {
138 * .name = TYPE_MY_DEVICE,
139 * .parent = TYPE_DEVICE,
140 * .instance_size = sizeof(MyDevice),
141 * .class_init = my_device_class_init,
142 * };
143 * </programlisting>
144 * </example>
146 * Introducing new virtual methods requires a class to define its own
147 * struct and to add a .class_size member to the #TypeInfo. Each method
148 * will also have a wrapper function to call it easily:
150 * <example>
151 * <title>Defining an abstract class</title>
152 * <programlisting>
153 * #include "qdev.h"
155 * typedef struct MyDeviceClass
157 * DeviceClass parent;
159 * void (*frobnicate) (MyDevice *obj);
160 * } MyDeviceClass;
162 * static const TypeInfo my_device_info = {
163 * .name = TYPE_MY_DEVICE,
164 * .parent = TYPE_DEVICE,
165 * .instance_size = sizeof(MyDevice),
166 * .abstract = true, // or set a default in my_device_class_init
167 * .class_size = sizeof(MyDeviceClass),
168 * };
170 * void my_device_frobnicate(MyDevice *obj)
172 * MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
174 * klass->frobnicate(obj);
176 * </programlisting>
177 * </example>
179 * # Interfaces #
181 * Interfaces allow a limited form of multiple inheritance. Instances are
182 * similar to normal types except for the fact that are only defined by
183 * their classes and never carry any state. You can dynamically cast an object
184 * to one of its #Interface types and vice versa.
186 * # Methods #
188 * A <emphasis>method</emphasis> is a function within the namespace scope of
189 * a class. It usually operates on the object instance by passing it as a
190 * strongly-typed first argument.
191 * If it does not operate on an object instance, it is dubbed
192 * <emphasis>class method</emphasis>.
194 * Methods cannot be overloaded. That is, the #ObjectClass and method name
195 * uniquely identity the function to be called; the signature does not vary
196 * except for trailing varargs.
198 * Methods are always <emphasis>virtual</emphasis>. Overriding a method in
199 * #TypeInfo.class_init of a subclass leads to any user of the class obtained
200 * via OBJECT_GET_CLASS() accessing the overridden function.
201 * The original function is not automatically invoked. It is the responsibility
202 * of the overriding class to determine whether and when to invoke the method
203 * being overridden.
205 * To invoke the method being overridden, the preferred solution is to store
206 * the original value in the overriding class before overriding the method.
207 * This corresponds to |[ {super,base}.method(...) ]| in Java and C#
208 * respectively; this frees the overriding class from hardcoding its parent
209 * class, which someone might choose to change at some point.
211 * <example>
212 * <title>Overriding a virtual method</title>
213 * <programlisting>
214 * typedef struct MyState MyState;
216 * typedef void (*MyDoSomething)(MyState *obj);
218 * typedef struct MyClass {
219 * ObjectClass parent_class;
221 * MyDoSomething do_something;
222 * } MyClass;
224 * static void my_do_something(MyState *obj)
226 * // do something
229 * static void my_class_init(ObjectClass *oc, void *data)
231 * MyClass *mc = MY_CLASS(oc);
233 * mc->do_something = my_do_something;
236 * static const TypeInfo my_type_info = {
237 * .name = TYPE_MY,
238 * .parent = TYPE_OBJECT,
239 * .instance_size = sizeof(MyState),
240 * .class_size = sizeof(MyClass),
241 * .class_init = my_class_init,
242 * };
244 * typedef struct DerivedClass {
245 * MyClass parent_class;
247 * MyDoSomething parent_do_something;
248 * } DerivedClass;
250 * static void derived_do_something(MyState *obj)
252 * DerivedClass *dc = DERIVED_GET_CLASS(obj);
254 * // do something here
255 * dc->parent_do_something(obj);
256 * // do something else here
259 * static void derived_class_init(ObjectClass *oc, void *data)
261 * MyClass *mc = MY_CLASS(oc);
262 * DerivedClass *dc = DERIVED_CLASS(oc);
264 * dc->parent_do_something = mc->do_something;
265 * mc->do_something = derived_do_something;
268 * static const TypeInfo derived_type_info = {
269 * .name = TYPE_DERIVED,
270 * .parent = TYPE_MY,
271 * .class_size = sizeof(DerivedClass),
272 * .class_init = derived_class_init,
273 * };
274 * </programlisting>
275 * </example>
277 * Alternatively, object_class_by_name() can be used to obtain the class and
278 * its non-overridden methods for a specific type. This would correspond to
279 * |[ MyClass::method(...) ]| in C++.
281 * The first example of such a QOM method was #CPUClass.reset,
282 * another example is #DeviceClass.realize.
287 * ObjectPropertyAccessor:
288 * @obj: the object that owns the property
289 * @v: the visitor that contains the property data
290 * @name: the name of the property
291 * @opaque: the object property opaque
292 * @errp: a pointer to an Error that is filled if getting/setting fails.
294 * Called when trying to get/set a property.
296 typedef void (ObjectPropertyAccessor)(Object *obj,
297 Visitor *v,
298 const char *name,
299 void *opaque,
300 Error **errp);
303 * ObjectPropertyResolve:
304 * @obj: the object that owns the property
305 * @opaque: the opaque registered with the property
306 * @part: the name of the property
308 * Resolves the #Object corresponding to property @part.
310 * The returned object can also be used as a starting point
311 * to resolve a relative path starting with "@part".
313 * Returns: If @path is the path that led to @obj, the function
314 * returns the #Object corresponding to "@path/@part".
315 * If "@path/@part" is not a valid object path, it returns #NULL.
317 typedef Object *(ObjectPropertyResolve)(Object *obj,
318 void *opaque,
319 const char *part);
322 * ObjectPropertyRelease:
323 * @obj: the object that owns the property
324 * @name: the name of the property
325 * @opaque: the opaque registered with the property
327 * Called when a property is removed from a object.
329 typedef void (ObjectPropertyRelease)(Object *obj,
330 const char *name,
331 void *opaque);
333 typedef struct ObjectProperty
335 gchar *name;
336 gchar *type;
337 gchar *description;
338 ObjectPropertyAccessor *get;
339 ObjectPropertyAccessor *set;
340 ObjectPropertyResolve *resolve;
341 ObjectPropertyRelease *release;
342 void *opaque;
343 } ObjectProperty;
346 * ObjectUnparent:
347 * @obj: the object that is being removed from the composition tree
349 * Called when an object is being removed from the QOM composition tree.
350 * The function should remove any backlinks from children objects to @obj.
352 typedef void (ObjectUnparent)(Object *obj);
355 * ObjectFree:
356 * @obj: the object being freed
358 * Called when an object's last reference is removed.
360 typedef void (ObjectFree)(void *obj);
362 #define OBJECT_CLASS_CAST_CACHE 4
365 * ObjectClass:
367 * The base for all classes. The only thing that #ObjectClass contains is an
368 * integer type handle.
370 struct ObjectClass
372 /*< private >*/
373 Type type;
374 GSList *interfaces;
376 const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
377 const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
379 ObjectUnparent *unparent;
381 GHashTable *properties;
385 * Object:
387 * The base for all objects. The first member of this object is a pointer to
388 * a #ObjectClass. Since C guarantees that the first member of a structure
389 * always begins at byte 0 of that structure, as long as any sub-object places
390 * its parent as the first member, we can cast directly to a #Object.
392 * As a result, #Object contains a reference to the objects type as its
393 * first member. This allows identification of the real type of the object at
394 * run time.
396 struct Object
398 /*< private >*/
399 ObjectClass *class;
400 ObjectFree *free;
401 GHashTable *properties;
402 uint32_t ref;
403 Object *parent;
407 * TypeInfo:
408 * @name: The name of the type.
409 * @parent: The name of the parent type.
410 * @instance_size: The size of the object (derivative of #Object). If
411 * @instance_size is 0, then the size of the object will be the size of the
412 * parent object.
413 * @instance_init: This function is called to initialize an object. The parent
414 * class will have already been initialized so the type is only responsible
415 * for initializing its own members.
416 * @instance_post_init: This function is called to finish initialization of
417 * an object, after all @instance_init functions were called.
418 * @instance_finalize: This function is called during object destruction. This
419 * is called before the parent @instance_finalize function has been called.
420 * An object should only free the members that are unique to its type in this
421 * function.
422 * @abstract: If this field is true, then the class is considered abstract and
423 * cannot be directly instantiated.
424 * @class_size: The size of the class object (derivative of #ObjectClass)
425 * for this object. If @class_size is 0, then the size of the class will be
426 * assumed to be the size of the parent class. This allows a type to avoid
427 * implementing an explicit class type if they are not adding additional
428 * virtual functions.
429 * @class_init: This function is called after all parent class initialization
430 * has occurred to allow a class to set its default virtual method pointers.
431 * This is also the function to use to override virtual methods from a parent
432 * class.
433 * @class_base_init: This function is called for all base classes after all
434 * parent class initialization has occurred, but before the class itself
435 * is initialized. This is the function to use to undo the effects of
436 * memcpy from the parent class to the descendents.
437 * @class_finalize: This function is called during class destruction and is
438 * meant to release and dynamic parameters allocated by @class_init.
439 * @class_data: Data to pass to the @class_init, @class_base_init and
440 * @class_finalize functions. This can be useful when building dynamic
441 * classes.
442 * @interfaces: The list of interfaces associated with this type. This
443 * should point to a static array that's terminated with a zero filled
444 * element.
446 struct TypeInfo
448 const char *name;
449 const char *parent;
451 size_t instance_size;
452 void (*instance_init)(Object *obj);
453 void (*instance_post_init)(Object *obj);
454 void (*instance_finalize)(Object *obj);
456 bool abstract;
457 size_t class_size;
459 void (*class_init)(ObjectClass *klass, void *data);
460 void (*class_base_init)(ObjectClass *klass, void *data);
461 void (*class_finalize)(ObjectClass *klass, void *data);
462 void *class_data;
464 InterfaceInfo *interfaces;
468 * OBJECT:
469 * @obj: A derivative of #Object
471 * Converts an object to a #Object. Since all objects are #Objects,
472 * this function will always succeed.
474 #define OBJECT(obj) \
475 ((Object *)(obj))
478 * OBJECT_CLASS:
479 * @class: A derivative of #ObjectClass.
481 * Converts a class to an #ObjectClass. Since all objects are #Objects,
482 * this function will always succeed.
484 #define OBJECT_CLASS(class) \
485 ((ObjectClass *)(class))
488 * OBJECT_CHECK:
489 * @type: The C type to use for the return value.
490 * @obj: A derivative of @type to cast.
491 * @name: The QOM typename of @type
493 * A type safe version of @object_dynamic_cast_assert. Typically each class
494 * will define a macro based on this type to perform type safe dynamic_casts to
495 * this object type.
497 * If an invalid object is passed to this function, a run time assert will be
498 * generated.
500 #define OBJECT_CHECK(type, obj, name) \
501 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
502 __FILE__, __LINE__, __func__))
505 * OBJECT_CLASS_CHECK:
506 * @class_type: The C type to use for the return value.
507 * @class: A derivative class of @class_type to cast.
508 * @name: the QOM typename of @class_type.
510 * A type safe version of @object_class_dynamic_cast_assert. This macro is
511 * typically wrapped by each type to perform type safe casts of a class to a
512 * specific class type.
514 #define OBJECT_CLASS_CHECK(class_type, class, name) \
515 ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
516 __FILE__, __LINE__, __func__))
519 * OBJECT_GET_CLASS:
520 * @class: The C type to use for the return value.
521 * @obj: The object to obtain the class for.
522 * @name: The QOM typename of @obj.
524 * This function will return a specific class for a given object. Its generally
525 * used by each type to provide a type safe macro to get a specific class type
526 * from an object.
528 #define OBJECT_GET_CLASS(class, obj, name) \
529 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
532 * InterfaceInfo:
533 * @type: The name of the interface.
535 * The information associated with an interface.
537 struct InterfaceInfo {
538 const char *type;
542 * InterfaceClass:
543 * @parent_class: the base class
545 * The class for all interfaces. Subclasses of this class should only add
546 * virtual methods.
548 struct InterfaceClass
550 ObjectClass parent_class;
551 /*< private >*/
552 ObjectClass *concrete_class;
553 Type interface_type;
556 #define TYPE_INTERFACE "interface"
559 * INTERFACE_CLASS:
560 * @klass: class to cast from
561 * Returns: An #InterfaceClass or raise an error if cast is invalid
563 #define INTERFACE_CLASS(klass) \
564 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
567 * INTERFACE_CHECK:
568 * @interface: the type to return
569 * @obj: the object to convert to an interface
570 * @name: the interface type name
572 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
574 #define INTERFACE_CHECK(interface, obj, name) \
575 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
576 __FILE__, __LINE__, __func__))
579 * object_new:
580 * @typename: The name of the type of the object to instantiate.
582 * This function will initialize a new object using heap allocated memory.
583 * The returned object has a reference count of 1, and will be freed when
584 * the last reference is dropped.
586 * Returns: The newly allocated and instantiated object.
588 Object *object_new(const char *typename);
591 * object_new_with_type:
592 * @type: The type of the object to instantiate.
594 * This function will initialize a new object using heap allocated memory.
595 * The returned object has a reference count of 1, and will be freed when
596 * the last reference is dropped.
598 * Returns: The newly allocated and instantiated object.
600 Object *object_new_with_type(Type type);
603 * object_new_with_props:
604 * @typename: The name of the type of the object to instantiate.
605 * @parent: the parent object
606 * @id: The unique ID of the object
607 * @errp: pointer to error object
608 * @...: list of property names and values
610 * This function will initialize a new object using heap allocated memory.
611 * The returned object has a reference count of 1, and will be freed when
612 * the last reference is dropped.
614 * The @id parameter will be used when registering the object as a
615 * child of @parent in the composition tree.
617 * The variadic parameters are a list of pairs of (propname, propvalue)
618 * strings. The propname of %NULL indicates the end of the property
619 * list. If the object implements the user creatable interface, the
620 * object will be marked complete once all the properties have been
621 * processed.
623 * <example>
624 * <title>Creating an object with properties</title>
625 * <programlisting>
626 * Error *err = NULL;
627 * Object *obj;
629 * obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE,
630 * object_get_objects_root(),
631 * "hostmem0",
632 * &err,
633 * "share", "yes",
634 * "mem-path", "/dev/shm/somefile",
635 * "prealloc", "yes",
636 * "size", "1048576",
637 * NULL);
639 * if (!obj) {
640 * g_printerr("Cannot create memory backend: %s\n",
641 * error_get_pretty(err));
643 * </programlisting>
644 * </example>
646 * The returned object will have one stable reference maintained
647 * for as long as it is present in the object hierarchy.
649 * Returns: The newly allocated, instantiated & initialized object.
651 Object *object_new_with_props(const char *typename,
652 Object *parent,
653 const char *id,
654 Error **errp,
655 ...) QEMU_SENTINEL;
658 * object_new_with_propv:
659 * @typename: The name of the type of the object to instantiate.
660 * @parent: the parent object
661 * @id: The unique ID of the object
662 * @errp: pointer to error object
663 * @vargs: list of property names and values
665 * See object_new_with_props() for documentation.
667 Object *object_new_with_propv(const char *typename,
668 Object *parent,
669 const char *id,
670 Error **errp,
671 va_list vargs);
674 * object_set_props:
675 * @obj: the object instance to set properties on
676 * @errp: pointer to error object
677 * @...: list of property names and values
679 * This function will set a list of properties on an existing object
680 * instance.
682 * The variadic parameters are a list of pairs of (propname, propvalue)
683 * strings. The propname of %NULL indicates the end of the property
684 * list.
686 * <example>
687 * <title>Update an object's properties</title>
688 * <programlisting>
689 * Error *err = NULL;
690 * Object *obj = ...get / create object...;
692 * obj = object_set_props(obj,
693 * &err,
694 * "share", "yes",
695 * "mem-path", "/dev/shm/somefile",
696 * "prealloc", "yes",
697 * "size", "1048576",
698 * NULL);
700 * if (!obj) {
701 * g_printerr("Cannot set properties: %s\n",
702 * error_get_pretty(err));
704 * </programlisting>
705 * </example>
707 * The returned object will have one stable reference maintained
708 * for as long as it is present in the object hierarchy.
710 * Returns: -1 on error, 0 on success
712 int object_set_props(Object *obj,
713 Error **errp,
714 ...) QEMU_SENTINEL;
717 * object_set_propv:
718 * @obj: the object instance to set properties on
719 * @errp: pointer to error object
720 * @vargs: list of property names and values
722 * See object_set_props() for documentation.
724 * Returns: -1 on error, 0 on success
726 int object_set_propv(Object *obj,
727 Error **errp,
728 va_list vargs);
731 * object_initialize_with_type:
732 * @data: A pointer to the memory to be used for the object.
733 * @size: The maximum size available at @data for the object.
734 * @type: The type of the object to instantiate.
736 * This function will initialize an object. The memory for the object should
737 * have already been allocated. The returned object has a reference count of 1,
738 * and will be finalized when the last reference is dropped.
740 void object_initialize_with_type(void *data, size_t size, Type type);
743 * object_initialize:
744 * @obj: A pointer to the memory to be used for the object.
745 * @size: The maximum size available at @obj for the object.
746 * @typename: The name of the type of the object to instantiate.
748 * This function will initialize an object. The memory for the object should
749 * have already been allocated. The returned object has a reference count of 1,
750 * and will be finalized when the last reference is dropped.
752 void object_initialize(void *obj, size_t size, const char *typename);
755 * object_dynamic_cast:
756 * @obj: The object to cast.
757 * @typename: The @typename to cast to.
759 * This function will determine if @obj is-a @typename. @obj can refer to an
760 * object or an interface associated with an object.
762 * Returns: This function returns @obj on success or #NULL on failure.
764 Object *object_dynamic_cast(Object *obj, const char *typename);
767 * object_dynamic_cast_assert:
769 * See object_dynamic_cast() for a description of the parameters of this
770 * function. The only difference in behavior is that this function asserts
771 * instead of returning #NULL on failure if QOM cast debugging is enabled.
772 * This function is not meant to be called directly, but only through
773 * the wrapper macro OBJECT_CHECK.
775 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
776 const char *file, int line, const char *func);
779 * object_get_class:
780 * @obj: A derivative of #Object
782 * Returns: The #ObjectClass of the type associated with @obj.
784 ObjectClass *object_get_class(Object *obj);
787 * object_get_typename:
788 * @obj: A derivative of #Object.
790 * Returns: The QOM typename of @obj.
792 const char *object_get_typename(Object *obj);
795 * type_register_static:
796 * @info: The #TypeInfo of the new type.
798 * @info and all of the strings it points to should exist for the life time
799 * that the type is registered.
801 * Returns: 0 on failure, the new #Type on success.
803 Type type_register_static(const TypeInfo *info);
806 * type_register:
807 * @info: The #TypeInfo of the new type
809 * Unlike type_register_static(), this call does not require @info or its
810 * string members to continue to exist after the call returns.
812 * Returns: 0 on failure, the new #Type on success.
814 Type type_register(const TypeInfo *info);
817 * object_class_dynamic_cast_assert:
818 * @klass: The #ObjectClass to attempt to cast.
819 * @typename: The QOM typename of the class to cast to.
821 * See object_class_dynamic_cast() for a description of the parameters
822 * of this function. The only difference in behavior is that this function
823 * asserts instead of returning #NULL on failure if QOM cast debugging is
824 * enabled. This function is not meant to be called directly, but only through
825 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
827 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
828 const char *typename,
829 const char *file, int line,
830 const char *func);
833 * object_class_dynamic_cast:
834 * @klass: The #ObjectClass to attempt to cast.
835 * @typename: The QOM typename of the class to cast to.
837 * Returns: If @typename is a class, this function returns @klass if
838 * @typename is a subtype of @klass, else returns #NULL.
840 * If @typename is an interface, this function returns the interface
841 * definition for @klass if @klass implements it unambiguously; #NULL
842 * is returned if @klass does not implement the interface or if multiple
843 * classes or interfaces on the hierarchy leading to @klass implement
844 * it. (FIXME: perhaps this can be detected at type definition time?)
846 ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
847 const char *typename);
850 * object_class_get_parent:
851 * @klass: The class to obtain the parent for.
853 * Returns: The parent for @klass or %NULL if none.
855 ObjectClass *object_class_get_parent(ObjectClass *klass);
858 * object_class_get_name:
859 * @klass: The class to obtain the QOM typename for.
861 * Returns: The QOM typename for @klass.
863 const char *object_class_get_name(ObjectClass *klass);
866 * object_class_is_abstract:
867 * @klass: The class to obtain the abstractness for.
869 * Returns: %true if @klass is abstract, %false otherwise.
871 bool object_class_is_abstract(ObjectClass *klass);
874 * object_class_by_name:
875 * @typename: The QOM typename to obtain the class for.
877 * Returns: The class for @typename or %NULL if not found.
879 ObjectClass *object_class_by_name(const char *typename);
881 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
882 const char *implements_type, bool include_abstract,
883 void *opaque);
886 * object_class_get_list:
887 * @implements_type: The type to filter for, including its derivatives.
888 * @include_abstract: Whether to include abstract classes.
890 * Returns: A singly-linked list of the classes in reverse hashtable order.
892 GSList *object_class_get_list(const char *implements_type,
893 bool include_abstract);
896 * object_ref:
897 * @obj: the object
899 * Increase the reference count of a object. A object cannot be freed as long
900 * as its reference count is greater than zero.
902 void object_ref(Object *obj);
905 * qdef_unref:
906 * @obj: the object
908 * Decrease the reference count of a object. A object cannot be freed as long
909 * as its reference count is greater than zero.
911 void object_unref(Object *obj);
914 * object_property_add:
915 * @obj: the object to add a property to
916 * @name: the name of the property. This can contain any character except for
917 * a forward slash. In general, you should use hyphens '-' instead of
918 * underscores '_' when naming properties.
919 * @type: the type name of the property. This namespace is pretty loosely
920 * defined. Sub namespaces are constructed by using a prefix and then
921 * to angle brackets. For instance, the type 'virtio-net-pci' in the
922 * 'link' namespace would be 'link<virtio-net-pci>'.
923 * @get: The getter to be called to read a property. If this is NULL, then
924 * the property cannot be read.
925 * @set: the setter to be called to write a property. If this is NULL,
926 * then the property cannot be written.
927 * @release: called when the property is removed from the object. This is
928 * meant to allow a property to free its opaque upon object
929 * destruction. This may be NULL.
930 * @opaque: an opaque pointer to pass to the callbacks for the property
931 * @errp: returns an error if this function fails
933 * Returns: The #ObjectProperty; this can be used to set the @resolve
934 * callback for child and link properties.
936 ObjectProperty *object_property_add(Object *obj, const char *name,
937 const char *type,
938 ObjectPropertyAccessor *get,
939 ObjectPropertyAccessor *set,
940 ObjectPropertyRelease *release,
941 void *opaque, Error **errp);
943 void object_property_del(Object *obj, const char *name, Error **errp);
945 ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name,
946 const char *type,
947 ObjectPropertyAccessor *get,
948 ObjectPropertyAccessor *set,
949 ObjectPropertyRelease *release,
950 void *opaque, Error **errp);
953 * object_property_find:
954 * @obj: the object
955 * @name: the name of the property
956 * @errp: returns an error if this function fails
958 * Look up a property for an object and return its #ObjectProperty if found.
960 ObjectProperty *object_property_find(Object *obj, const char *name,
961 Error **errp);
962 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
963 Error **errp);
965 typedef struct ObjectPropertyIterator {
966 ObjectClass *nextclass;
967 GHashTableIter iter;
968 } ObjectPropertyIterator;
971 * object_property_iter_init:
972 * @obj: the object
974 * Initializes an iterator for traversing all properties
975 * registered against an object instance, its class and all parent classes.
977 * It is forbidden to modify the property list while iterating,
978 * whether removing or adding properties.
980 * Typical usage pattern would be
982 * <example>
983 * <title>Using object property iterators</title>
984 * <programlisting>
985 * ObjectProperty *prop;
986 * ObjectPropertyIterator iter;
988 * object_property_iter_init(&iter, obj);
989 * while ((prop = object_property_iter_next(&iter))) {
990 * ... do something with prop ...
992 * </programlisting>
993 * </example>
995 void object_property_iter_init(ObjectPropertyIterator *iter,
996 Object *obj);
999 * object_property_iter_next:
1000 * @iter: the iterator instance
1002 * Return the next available property. If no further properties
1003 * are available, a %NULL value will be returned and the @iter
1004 * pointer should not be used again after this point without
1005 * re-initializing it.
1007 * Returns: the next property, or %NULL when all properties
1008 * have been traversed.
1010 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter);
1012 void object_unparent(Object *obj);
1015 * object_property_get:
1016 * @obj: the object
1017 * @v: the visitor that will receive the property value. This should be an
1018 * Output visitor and the data will be written with @name as the name.
1019 * @name: the name of the property
1020 * @errp: returns an error if this function fails
1022 * Reads a property from a object.
1024 void object_property_get(Object *obj, Visitor *v, const char *name,
1025 Error **errp);
1028 * object_property_set_str:
1029 * @value: the value to be written to the property
1030 * @name: the name of the property
1031 * @errp: returns an error if this function fails
1033 * Writes a string value to a property.
1035 void object_property_set_str(Object *obj, const char *value,
1036 const char *name, Error **errp);
1039 * object_property_get_str:
1040 * @obj: the object
1041 * @name: the name of the property
1042 * @errp: returns an error if this function fails
1044 * Returns: the value of the property, converted to a C string, or NULL if
1045 * an error occurs (including when the property value is not a string).
1046 * The caller should free the string.
1048 char *object_property_get_str(Object *obj, const char *name,
1049 Error **errp);
1052 * object_property_set_link:
1053 * @value: the value to be written to the property
1054 * @name: the name of the property
1055 * @errp: returns an error if this function fails
1057 * Writes an object's canonical path to a property.
1059 void object_property_set_link(Object *obj, Object *value,
1060 const char *name, Error **errp);
1063 * object_property_get_link:
1064 * @obj: the object
1065 * @name: the name of the property
1066 * @errp: returns an error if this function fails
1068 * Returns: the value of the property, resolved from a path to an Object,
1069 * or NULL if an error occurs (including when the property value is not a
1070 * string or not a valid object path).
1072 Object *object_property_get_link(Object *obj, const char *name,
1073 Error **errp);
1076 * object_property_set_bool:
1077 * @value: the value to be written to the property
1078 * @name: the name of the property
1079 * @errp: returns an error if this function fails
1081 * Writes a bool value to a property.
1083 void object_property_set_bool(Object *obj, bool value,
1084 const char *name, Error **errp);
1087 * object_property_get_bool:
1088 * @obj: the object
1089 * @name: the name of the property
1090 * @errp: returns an error if this function fails
1092 * Returns: the value of the property, converted to a boolean, or NULL if
1093 * an error occurs (including when the property value is not a bool).
1095 bool object_property_get_bool(Object *obj, const char *name,
1096 Error **errp);
1099 * object_property_set_int:
1100 * @value: the value to be written to the property
1101 * @name: the name of the property
1102 * @errp: returns an error if this function fails
1104 * Writes an integer value to a property.
1106 void object_property_set_int(Object *obj, int64_t value,
1107 const char *name, Error **errp);
1110 * object_property_get_int:
1111 * @obj: the object
1112 * @name: the name of the property
1113 * @errp: returns an error if this function fails
1115 * Returns: the value of the property, converted to an integer, or negative if
1116 * an error occurs (including when the property value is not an integer).
1118 int64_t object_property_get_int(Object *obj, const char *name,
1119 Error **errp);
1122 * object_property_get_enum:
1123 * @obj: the object
1124 * @name: the name of the property
1125 * @typename: the name of the enum data type
1126 * @errp: returns an error if this function fails
1128 * Returns: the value of the property, converted to an integer, or
1129 * undefined if an error occurs (including when the property value is not
1130 * an enum).
1132 int object_property_get_enum(Object *obj, const char *name,
1133 const char *typename, Error **errp);
1136 * object_property_get_uint16List:
1137 * @obj: the object
1138 * @name: the name of the property
1139 * @list: the returned int list
1140 * @errp: returns an error if this function fails
1142 * Returns: the value of the property, converted to integers, or
1143 * undefined if an error occurs (including when the property value is not
1144 * an list of integers).
1146 void object_property_get_uint16List(Object *obj, const char *name,
1147 uint16List **list, Error **errp);
1150 * object_property_set:
1151 * @obj: the object
1152 * @v: the visitor that will be used to write the property value. This should
1153 * be an Input visitor and the data will be first read with @name as the
1154 * name and then written as the property value.
1155 * @name: the name of the property
1156 * @errp: returns an error if this function fails
1158 * Writes a property to a object.
1160 void object_property_set(Object *obj, Visitor *v, const char *name,
1161 Error **errp);
1164 * object_property_parse:
1165 * @obj: the object
1166 * @string: the string that will be used to parse the property value.
1167 * @name: the name of the property
1168 * @errp: returns an error if this function fails
1170 * Parses a string and writes the result into a property of an object.
1172 void object_property_parse(Object *obj, const char *string,
1173 const char *name, Error **errp);
1176 * object_property_print:
1177 * @obj: the object
1178 * @name: the name of the property
1179 * @human: if true, print for human consumption
1180 * @errp: returns an error if this function fails
1182 * Returns a string representation of the value of the property. The
1183 * caller shall free the string.
1185 char *object_property_print(Object *obj, const char *name, bool human,
1186 Error **errp);
1189 * object_property_get_type:
1190 * @obj: the object
1191 * @name: the name of the property
1192 * @errp: returns an error if this function fails
1194 * Returns: The type name of the property.
1196 const char *object_property_get_type(Object *obj, const char *name,
1197 Error **errp);
1200 * object_get_root:
1202 * Returns: the root object of the composition tree
1204 Object *object_get_root(void);
1208 * object_get_objects_root:
1210 * Get the container object that holds user created
1211 * object instances. This is the object at path
1212 * "/objects"
1214 * Returns: the user object container
1216 Object *object_get_objects_root(void);
1219 * object_get_canonical_path_component:
1221 * Returns: The final component in the object's canonical path. The canonical
1222 * path is the path within the composition tree starting from the root.
1224 gchar *object_get_canonical_path_component(Object *obj);
1227 * object_get_canonical_path:
1229 * Returns: The canonical path for a object. This is the path within the
1230 * composition tree starting from the root.
1232 gchar *object_get_canonical_path(Object *obj);
1235 * object_resolve_path:
1236 * @path: the path to resolve
1237 * @ambiguous: returns true if the path resolution failed because of an
1238 * ambiguous match
1240 * There are two types of supported paths--absolute paths and partial paths.
1242 * Absolute paths are derived from the root object and can follow child<> or
1243 * link<> properties. Since they can follow link<> properties, they can be
1244 * arbitrarily long. Absolute paths look like absolute filenames and are
1245 * prefixed with a leading slash.
1247 * Partial paths look like relative filenames. They do not begin with a
1248 * prefix. The matching rules for partial paths are subtle but designed to make
1249 * specifying objects easy. At each level of the composition tree, the partial
1250 * path is matched as an absolute path. The first match is not returned. At
1251 * least two matches are searched for. A successful result is only returned if
1252 * only one match is found. If more than one match is found, a flag is
1253 * returned to indicate that the match was ambiguous.
1255 * Returns: The matched object or NULL on path lookup failure.
1257 Object *object_resolve_path(const char *path, bool *ambiguous);
1260 * object_resolve_path_type:
1261 * @path: the path to resolve
1262 * @typename: the type to look for.
1263 * @ambiguous: returns true if the path resolution failed because of an
1264 * ambiguous match
1266 * This is similar to object_resolve_path. However, when looking for a
1267 * partial path only matches that implement the given type are considered.
1268 * This restricts the search and avoids spuriously flagging matches as
1269 * ambiguous.
1271 * For both partial and absolute paths, the return value goes through
1272 * a dynamic cast to @typename. This is important if either the link,
1273 * or the typename itself are of interface types.
1275 * Returns: The matched object or NULL on path lookup failure.
1277 Object *object_resolve_path_type(const char *path, const char *typename,
1278 bool *ambiguous);
1281 * object_resolve_path_component:
1282 * @parent: the object in which to resolve the path
1283 * @part: the component to resolve.
1285 * This is similar to object_resolve_path with an absolute path, but it
1286 * only resolves one element (@part) and takes the others from @parent.
1288 * Returns: The resolved object or NULL on path lookup failure.
1290 Object *object_resolve_path_component(Object *parent, const gchar *part);
1293 * object_property_add_child:
1294 * @obj: the object to add a property to
1295 * @name: the name of the property
1296 * @child: the child object
1297 * @errp: if an error occurs, a pointer to an area to store the area
1299 * Child properties form the composition tree. All objects need to be a child
1300 * of another object. Objects can only be a child of one object.
1302 * There is no way for a child to determine what its parent is. It is not
1303 * a bidirectional relationship. This is by design.
1305 * The value of a child property as a C string will be the child object's
1306 * canonical path. It can be retrieved using object_property_get_str().
1307 * The child object itself can be retrieved using object_property_get_link().
1309 void object_property_add_child(Object *obj, const char *name,
1310 Object *child, Error **errp);
1312 typedef enum {
1313 /* Unref the link pointer when the property is deleted */
1314 OBJ_PROP_LINK_UNREF_ON_RELEASE = 0x1,
1315 } ObjectPropertyLinkFlags;
1318 * object_property_allow_set_link:
1320 * The default implementation of the object_property_add_link() check()
1321 * callback function. It allows the link property to be set and never returns
1322 * an error.
1324 void object_property_allow_set_link(Object *, const char *,
1325 Object *, Error **);
1328 * object_property_add_link:
1329 * @obj: the object to add a property to
1330 * @name: the name of the property
1331 * @type: the qobj type of the link
1332 * @child: a pointer to where the link object reference is stored
1333 * @check: callback to veto setting or NULL if the property is read-only
1334 * @flags: additional options for the link
1335 * @errp: if an error occurs, a pointer to an area to store the area
1337 * Links establish relationships between objects. Links are unidirectional
1338 * although two links can be combined to form a bidirectional relationship
1339 * between objects.
1341 * Links form the graph in the object model.
1343 * The <code>@check()</code> callback is invoked when
1344 * object_property_set_link() is called and can raise an error to prevent the
1345 * link being set. If <code>@check</code> is NULL, the property is read-only
1346 * and cannot be set.
1348 * Ownership of the pointer that @child points to is transferred to the
1349 * link property. The reference count for <code>*@child</code> is
1350 * managed by the property from after the function returns till the
1351 * property is deleted with object_property_del(). If the
1352 * <code>@flags</code> <code>OBJ_PROP_LINK_UNREF_ON_RELEASE</code> bit is set,
1353 * the reference count is decremented when the property is deleted.
1355 void object_property_add_link(Object *obj, const char *name,
1356 const char *type, Object **child,
1357 void (*check)(Object *obj, const char *name,
1358 Object *val, Error **errp),
1359 ObjectPropertyLinkFlags flags,
1360 Error **errp);
1363 * object_property_add_str:
1364 * @obj: the object to add a property to
1365 * @name: the name of the property
1366 * @get: the getter or NULL if the property is write-only. This function must
1367 * return a string to be freed by g_free().
1368 * @set: the setter or NULL if the property is read-only
1369 * @errp: if an error occurs, a pointer to an area to store the error
1371 * Add a string property using getters/setters. This function will add a
1372 * property of type 'string'.
1374 void object_property_add_str(Object *obj, const char *name,
1375 char *(*get)(Object *, Error **),
1376 void (*set)(Object *, const char *, Error **),
1377 Error **errp);
1379 void object_class_property_add_str(ObjectClass *klass, const char *name,
1380 char *(*get)(Object *, Error **),
1381 void (*set)(Object *, const char *,
1382 Error **),
1383 Error **errp);
1386 * object_property_add_bool:
1387 * @obj: the object to add a property to
1388 * @name: the name of the property
1389 * @get: the getter or NULL if the property is write-only.
1390 * @set: the setter or NULL if the property is read-only
1391 * @errp: if an error occurs, a pointer to an area to store the error
1393 * Add a bool property using getters/setters. This function will add a
1394 * property of type 'bool'.
1396 void object_property_add_bool(Object *obj, const char *name,
1397 bool (*get)(Object *, Error **),
1398 void (*set)(Object *, bool, Error **),
1399 Error **errp);
1401 void object_class_property_add_bool(ObjectClass *klass, const char *name,
1402 bool (*get)(Object *, Error **),
1403 void (*set)(Object *, bool, Error **),
1404 Error **errp);
1407 * object_property_add_enum:
1408 * @obj: the object to add a property to
1409 * @name: the name of the property
1410 * @typename: the name of the enum data type
1411 * @get: the getter or %NULL if the property is write-only.
1412 * @set: the setter or %NULL if the property is read-only
1413 * @errp: if an error occurs, a pointer to an area to store the error
1415 * Add an enum property using getters/setters. This function will add a
1416 * property of type '@typename'.
1418 void object_property_add_enum(Object *obj, const char *name,
1419 const char *typename,
1420 const char * const *strings,
1421 int (*get)(Object *, Error **),
1422 void (*set)(Object *, int, Error **),
1423 Error **errp);
1425 void object_class_property_add_enum(ObjectClass *klass, const char *name,
1426 const char *typename,
1427 const char * const *strings,
1428 int (*get)(Object *, Error **),
1429 void (*set)(Object *, int, Error **),
1430 Error **errp);
1433 * object_property_add_tm:
1434 * @obj: the object to add a property to
1435 * @name: the name of the property
1436 * @get: the getter or NULL if the property is write-only.
1437 * @errp: if an error occurs, a pointer to an area to store the error
1439 * Add a read-only struct tm valued property using a getter function.
1440 * This function will add a property of type 'struct tm'.
1442 void object_property_add_tm(Object *obj, const char *name,
1443 void (*get)(Object *, struct tm *, Error **),
1444 Error **errp);
1446 void object_class_property_add_tm(ObjectClass *klass, const char *name,
1447 void (*get)(Object *, struct tm *, Error **),
1448 Error **errp);
1451 * object_property_add_uint8_ptr:
1452 * @obj: the object to add a property to
1453 * @name: the name of the property
1454 * @v: pointer to value
1455 * @errp: if an error occurs, a pointer to an area to store the error
1457 * Add an integer property in memory. This function will add a
1458 * property of type 'uint8'.
1460 void object_property_add_uint8_ptr(Object *obj, const char *name,
1461 const uint8_t *v, Error **errp);
1462 void object_class_property_add_uint8_ptr(ObjectClass *klass, const char *name,
1463 const uint8_t *v, Error **errp);
1466 * object_property_add_uint16_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 'uint16'.
1475 void object_property_add_uint16_ptr(Object *obj, const char *name,
1476 const uint16_t *v, Error **errp);
1477 void object_class_property_add_uint16_ptr(ObjectClass *klass, const char *name,
1478 const uint16_t *v, Error **errp);
1481 * object_property_add_uint32_ptr:
1482 * @obj: the object to add a property to
1483 * @name: the name of the property
1484 * @v: pointer to value
1485 * @errp: if an error occurs, a pointer to an area to store the error
1487 * Add an integer property in memory. This function will add a
1488 * property of type 'uint32'.
1490 void object_property_add_uint32_ptr(Object *obj, const char *name,
1491 const uint32_t *v, Error **errp);
1492 void object_class_property_add_uint32_ptr(ObjectClass *klass, const char *name,
1493 const uint32_t *v, Error **errp);
1496 * object_property_add_uint64_ptr:
1497 * @obj: the object to add a property to
1498 * @name: the name of the property
1499 * @v: pointer to value
1500 * @errp: if an error occurs, a pointer to an area to store the error
1502 * Add an integer property in memory. This function will add a
1503 * property of type 'uint64'.
1505 void object_property_add_uint64_ptr(Object *obj, const char *name,
1506 const uint64_t *v, Error **Errp);
1507 void object_class_property_add_uint64_ptr(ObjectClass *klass, const char *name,
1508 const uint64_t *v, Error **Errp);
1511 * object_property_add_alias:
1512 * @obj: the object to add a property to
1513 * @name: the name of the property
1514 * @target_obj: the object to forward property access to
1515 * @target_name: the name of the property on the forwarded object
1516 * @errp: if an error occurs, a pointer to an area to store the error
1518 * Add an alias for a property on an object. This function will add a property
1519 * of the same type as the forwarded property.
1521 * The caller must ensure that <code>@target_obj</code> stays alive as long as
1522 * this property exists. In the case of a child object or an alias on the same
1523 * object this will be the case. For aliases to other objects the caller is
1524 * responsible for taking a reference.
1526 void object_property_add_alias(Object *obj, const char *name,
1527 Object *target_obj, const char *target_name,
1528 Error **errp);
1531 * object_property_add_const_link:
1532 * @obj: the object to add a property to
1533 * @name: the name of the property
1534 * @target: the object to be referred by the link
1535 * @errp: if an error occurs, a pointer to an area to store the error
1537 * Add an unmodifiable link for a property on an object. This function will
1538 * add a property of type link<TYPE> where TYPE is the type of @target.
1540 * The caller must ensure that @target stays alive as long as
1541 * this property exists. In the case @target is a child of @obj,
1542 * this will be the case. Otherwise, the caller is responsible for
1543 * taking a reference.
1545 void object_property_add_const_link(Object *obj, const char *name,
1546 Object *target, Error **errp);
1549 * object_property_set_description:
1550 * @obj: the object owning the property
1551 * @name: the name of the property
1552 * @description: the description of the property on the object
1553 * @errp: if an error occurs, a pointer to an area to store the error
1555 * Set an object property's description.
1558 void object_property_set_description(Object *obj, const char *name,
1559 const char *description, Error **errp);
1560 void object_class_property_set_description(ObjectClass *klass, const char *name,
1561 const char *description,
1562 Error **errp);
1565 * object_child_foreach:
1566 * @obj: the object whose children will be navigated
1567 * @fn: the iterator function to be called
1568 * @opaque: an opaque value that will be passed to the iterator
1570 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1571 * non-zero.
1573 * It is forbidden to add or remove children from @obj from the @fn
1574 * callback.
1576 * Returns: The last value returned by @fn, or 0 if there is no child.
1578 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1579 void *opaque);
1582 * object_child_foreach_recursive:
1583 * @obj: the object whose children will be navigated
1584 * @fn: the iterator function to be called
1585 * @opaque: an opaque value that will be passed to the iterator
1587 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1588 * non-zero. Calls recursively, all child nodes of @obj will also be passed
1589 * all the way down to the leaf nodes of the tree. Depth first ordering.
1591 * It is forbidden to add or remove children from @obj (or its
1592 * child nodes) from the @fn callback.
1594 * Returns: The last value returned by @fn, or 0 if there is no child.
1596 int object_child_foreach_recursive(Object *obj,
1597 int (*fn)(Object *child, void *opaque),
1598 void *opaque);
1600 * container_get:
1601 * @root: root of the #path, e.g., object_get_root()
1602 * @path: path to the container
1604 * Return a container object whose path is @path. Create more containers
1605 * along the path if necessary.
1607 * Returns: the container object.
1609 Object *container_get(Object *root, const char *path);
1612 #endif