exec: add parameter errp to gethugepagesize
[qemu/rayw.git] / include / qom / object.h
blob8a05a81a991d30b41f55d87f4a1241f09f799df1
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 <stdint.h>
19 #include <stdbool.h>
20 #include "qemu/queue.h"
21 #include "qapi/error.h"
23 struct Visitor;
25 struct TypeImpl;
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"
38 /**
39 * SECTION:object.h
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
45 * features:
47 * - System for dynamically registering types
48 * - Support for single-inheritance of types
49 * - Multiple inheritance of stateless interfaces
51 * <example>
52 * <title>Creating a minimal type</title>
53 * <programlisting>
54 * #include "qdev.h"
56 * #define TYPE_MY_DEVICE "my-device"
58 * // No new virtual functions: we can reuse the typedef for the
59 * // superclass.
60 * typedef DeviceClass MyDeviceClass;
61 * typedef struct MyDevice
62 * {
63 * DeviceState parent;
65 * int reg0, reg1, reg2;
66 * } MyDevice;
68 * static const TypeInfo my_device_info = {
69 * .name = TYPE_MY_DEVICE,
70 * .parent = TYPE_DEVICE,
71 * .instance_size = sizeof(MyDevice),
72 * };
74 * static void my_device_register_types(void)
75 * {
76 * type_register_static(&my_device_info);
77 * }
79 * type_init(my_device_register_types)
80 * </programlisting>
81 * </example>
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
96 * specific type:
98 * <example>
99 * <title>Typecasting macros</title>
100 * <programlisting>
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)
107 * </programlisting>
108 * </example>
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:
130 * <example>
131 * <title>Overriding a virtual function</title>
132 * <programlisting>
133 * #include "qdev.h"
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,
146 * };
147 * </programlisting>
148 * </example>
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:
154 * <example>
155 * <title>Defining an abstract class</title>
156 * <programlisting>
157 * #include "qdev.h"
159 * typedef struct MyDeviceClass
161 * DeviceClass parent;
163 * void (*frobnicate) (MyDevice *obj);
164 * } MyDeviceClass;
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),
172 * };
174 * void my_device_frobnicate(MyDevice *obj)
176 * MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
178 * klass->frobnicate(obj);
180 * </programlisting>
181 * </example>
183 * # Interfaces #
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.
190 * # Methods #
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
207 * being overridden.
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.
215 * <example>
216 * <title>Overriding a virtual method</title>
217 * <programlisting>
218 * typedef struct MyState MyState;
220 * typedef void (*MyDoSomething)(MyState *obj);
222 * typedef struct MyClass {
223 * ObjectClass parent_class;
225 * MyDoSomething do_something;
226 * } MyClass;
228 * static void my_do_something(MyState *obj)
230 * // do something
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 = {
241 * .name = TYPE_MY,
242 * .parent = TYPE_OBJECT,
243 * .instance_size = sizeof(MyState),
244 * .class_size = sizeof(MyClass),
245 * .class_init = my_class_init,
246 * };
248 * typedef struct DerivedClass {
249 * MyClass parent_class;
251 * MyDoSomething parent_do_something;
252 * } DerivedClass;
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,
274 * .parent = TYPE_MY,
275 * .class_size = sizeof(DerivedClass),
276 * .class_init = my_class_init,
277 * };
278 * </programlisting>
279 * </example>
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,
301 struct Visitor *v,
302 void *opaque,
303 const char *name,
304 Error **errp);
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,
322 void *opaque,
323 const char *part);
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,
334 const char *name,
335 void *opaque);
337 typedef struct ObjectProperty
339 gchar *name;
340 gchar *type;
341 ObjectPropertyAccessor *get;
342 ObjectPropertyAccessor *set;
343 ObjectPropertyResolve *resolve;
344 ObjectPropertyRelease *release;
345 void *opaque;
347 QTAILQ_ENTRY(ObjectProperty) node;
348 } ObjectProperty;
351 * ObjectUnparent:
352 * @obj: the object that is being removed from the composition tree
354 * Called when an object is being removed from the QOM composition tree.
355 * The function should remove any backlinks from children objects to @obj.
357 typedef void (ObjectUnparent)(Object *obj);
360 * ObjectFree:
361 * @obj: the object being freed
363 * Called when an object's last reference is removed.
365 typedef void (ObjectFree)(void *obj);
367 #define OBJECT_CLASS_CAST_CACHE 4
370 * ObjectClass:
372 * The base for all classes. The only thing that #ObjectClass contains is an
373 * integer type handle.
375 struct ObjectClass
377 /*< private >*/
378 Type type;
379 GSList *interfaces;
381 const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
382 const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
384 ObjectUnparent *unparent;
388 * Object:
390 * The base for all objects. The first member of this object is a pointer to
391 * a #ObjectClass. Since C guarantees that the first member of a structure
392 * always begins at byte 0 of that structure, as long as any sub-object places
393 * its parent as the first member, we can cast directly to a #Object.
395 * As a result, #Object contains a reference to the objects type as its
396 * first member. This allows identification of the real type of the object at
397 * run time.
399 * #Object also contains a list of #Interfaces that this object
400 * implements.
402 struct Object
404 /*< private >*/
405 ObjectClass *class;
406 ObjectFree *free;
407 QTAILQ_HEAD(, ObjectProperty) properties;
408 uint32_t ref;
409 Object *parent;
413 * TypeInfo:
414 * @name: The name of the type.
415 * @parent: The name of the parent type.
416 * @instance_size: The size of the object (derivative of #Object). If
417 * @instance_size is 0, then the size of the object will be the size of the
418 * parent object.
419 * @instance_init: This function is called to initialize an object. The parent
420 * class will have already been initialized so the type is only responsible
421 * for initializing its own members.
422 * @instance_post_init: This function is called to finish initialization of
423 * an object, after all @instance_init functions were called.
424 * @instance_finalize: This function is called during object destruction. This
425 * is called before the parent @instance_finalize function has been called.
426 * An object should only free the members that are unique to its type in this
427 * function.
428 * @abstract: If this field is true, then the class is considered abstract and
429 * cannot be directly instantiated.
430 * @class_size: The size of the class object (derivative of #ObjectClass)
431 * for this object. If @class_size is 0, then the size of the class will be
432 * assumed to be the size of the parent class. This allows a type to avoid
433 * implementing an explicit class type if they are not adding additional
434 * virtual functions.
435 * @class_init: This function is called after all parent class initialization
436 * has occurred to allow a class to set its default virtual method pointers.
437 * This is also the function to use to override virtual methods from a parent
438 * class.
439 * @class_base_init: This function is called for all base classes after all
440 * parent class initialization has occurred, but before the class itself
441 * is initialized. This is the function to use to undo the effects of
442 * memcpy from the parent class to the descendents.
443 * @class_finalize: This function is called during class destruction and is
444 * meant to release and dynamic parameters allocated by @class_init.
445 * @class_data: Data to pass to the @class_init, @class_base_init and
446 * @class_finalize functions. This can be useful when building dynamic
447 * classes.
448 * @interfaces: The list of interfaces associated with this type. This
449 * should point to a static array that's terminated with a zero filled
450 * element.
452 struct TypeInfo
454 const char *name;
455 const char *parent;
457 size_t instance_size;
458 void (*instance_init)(Object *obj);
459 void (*instance_post_init)(Object *obj);
460 void (*instance_finalize)(Object *obj);
462 bool abstract;
463 size_t class_size;
465 void (*class_init)(ObjectClass *klass, void *data);
466 void (*class_base_init)(ObjectClass *klass, void *data);
467 void (*class_finalize)(ObjectClass *klass, void *data);
468 void *class_data;
470 InterfaceInfo *interfaces;
474 * OBJECT:
475 * @obj: A derivative of #Object
477 * Converts an object to a #Object. Since all objects are #Objects,
478 * this function will always succeed.
480 #define OBJECT(obj) \
481 ((Object *)(obj))
484 * OBJECT_CLASS:
485 * @class: A derivative of #ObjectClass.
487 * Converts a class to an #ObjectClass. Since all objects are #Objects,
488 * this function will always succeed.
490 #define OBJECT_CLASS(class) \
491 ((ObjectClass *)(class))
494 * OBJECT_CHECK:
495 * @type: The C type to use for the return value.
496 * @obj: A derivative of @type to cast.
497 * @name: The QOM typename of @type
499 * A type safe version of @object_dynamic_cast_assert. Typically each class
500 * will define a macro based on this type to perform type safe dynamic_casts to
501 * this object type.
503 * If an invalid object is passed to this function, a run time assert will be
504 * generated.
506 #define OBJECT_CHECK(type, obj, name) \
507 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
508 __FILE__, __LINE__, __func__))
511 * OBJECT_CLASS_CHECK:
512 * @class: The C type to use for the return value.
513 * @obj: A derivative of @type to cast.
514 * @name: the QOM typename of @class.
516 * A type safe version of @object_class_dynamic_cast_assert. This macro is
517 * typically wrapped by each type to perform type safe casts of a class to a
518 * specific class type.
520 #define OBJECT_CLASS_CHECK(class, obj, name) \
521 ((class *)object_class_dynamic_cast_assert(OBJECT_CLASS(obj), (name), \
522 __FILE__, __LINE__, __func__))
525 * OBJECT_GET_CLASS:
526 * @class: The C type to use for the return value.
527 * @obj: The object to obtain the class for.
528 * @name: The QOM typename of @obj.
530 * This function will return a specific class for a given object. Its generally
531 * used by each type to provide a type safe macro to get a specific class type
532 * from an object.
534 #define OBJECT_GET_CLASS(class, obj, name) \
535 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
538 * InterfaceInfo:
539 * @type: The name of the interface.
541 * The information associated with an interface.
543 struct InterfaceInfo {
544 const char *type;
548 * InterfaceClass:
549 * @parent_class: the base class
551 * The class for all interfaces. Subclasses of this class should only add
552 * virtual methods.
554 struct InterfaceClass
556 ObjectClass parent_class;
557 /*< private >*/
558 ObjectClass *concrete_class;
559 Type interface_type;
562 #define TYPE_INTERFACE "interface"
565 * INTERFACE_CLASS:
566 * @klass: class to cast from
567 * Returns: An #InterfaceClass or raise an error if cast is invalid
569 #define INTERFACE_CLASS(klass) \
570 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
573 * INTERFACE_CHECK:
574 * @interface: the type to return
575 * @obj: the object to convert to an interface
576 * @name: the interface type name
578 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
580 #define INTERFACE_CHECK(interface, obj, name) \
581 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
582 __FILE__, __LINE__, __func__))
585 * object_new:
586 * @typename: The name of the type of the object to instantiate.
588 * This function will initialize a new object using heap allocated memory.
589 * The returned object has a reference count of 1, and will be freed when
590 * the last reference is dropped.
592 * Returns: The newly allocated and instantiated object.
594 Object *object_new(const char *typename);
597 * object_new_with_type:
598 * @type: The type of the object to instantiate.
600 * This function will initialize a new object using heap allocated memory.
601 * The returned object has a reference count of 1, and will be freed when
602 * the last reference is dropped.
604 * Returns: The newly allocated and instantiated object.
606 Object *object_new_with_type(Type type);
609 * object_initialize_with_type:
610 * @data: A pointer to the memory to be used for the object.
611 * @size: The maximum size available at @data for the object.
612 * @type: The type of the object to instantiate.
614 * This function will initialize an object. The memory for the object should
615 * have already been allocated. The returned object has a reference count of 1,
616 * and will be finalized when the last reference is dropped.
618 void object_initialize_with_type(void *data, size_t size, Type type);
621 * object_initialize:
622 * @obj: A pointer to the memory to be used for the object.
623 * @size: The maximum size available at @obj for the object.
624 * @typename: The name of the type of the object to instantiate.
626 * This function will initialize an object. The memory for the object should
627 * have already been allocated. The returned object has a reference count of 1,
628 * and will be finalized when the last reference is dropped.
630 void object_initialize(void *obj, size_t size, const char *typename);
633 * object_dynamic_cast:
634 * @obj: The object to cast.
635 * @typename: The @typename to cast to.
637 * This function will determine if @obj is-a @typename. @obj can refer to an
638 * object or an interface associated with an object.
640 * Returns: This function returns @obj on success or #NULL on failure.
642 Object *object_dynamic_cast(Object *obj, const char *typename);
645 * object_dynamic_cast_assert:
647 * See object_dynamic_cast() for a description of the parameters of this
648 * function. The only difference in behavior is that this function asserts
649 * instead of returning #NULL on failure if QOM cast debugging is enabled.
650 * This function is not meant to be called directly, but only through
651 * the wrapper macro OBJECT_CHECK.
653 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
654 const char *file, int line, const char *func);
657 * object_get_class:
658 * @obj: A derivative of #Object
660 * Returns: The #ObjectClass of the type associated with @obj.
662 ObjectClass *object_get_class(Object *obj);
665 * object_get_typename:
666 * @obj: A derivative of #Object.
668 * Returns: The QOM typename of @obj.
670 const char *object_get_typename(Object *obj);
673 * type_register_static:
674 * @info: The #TypeInfo of the new type.
676 * @info and all of the strings it points to should exist for the life time
677 * that the type is registered.
679 * Returns: 0 on failure, the new #Type on success.
681 Type type_register_static(const TypeInfo *info);
684 * type_register:
685 * @info: The #TypeInfo of the new type
687 * Unlike type_register_static(), this call does not require @info or its
688 * string members to continue to exist after the call returns.
690 * Returns: 0 on failure, the new #Type on success.
692 Type type_register(const TypeInfo *info);
695 * object_class_dynamic_cast_assert:
696 * @klass: The #ObjectClass to attempt to cast.
697 * @typename: The QOM typename of the class to cast to.
699 * See object_class_dynamic_cast() for a description of the parameters
700 * of this function. The only difference in behavior is that this function
701 * asserts instead of returning #NULL on failure if QOM cast debugging is
702 * enabled. This function is not meant to be called directly, but only through
703 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
705 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
706 const char *typename,
707 const char *file, int line,
708 const char *func);
711 * object_class_dynamic_cast:
712 * @klass: The #ObjectClass to attempt to cast.
713 * @typename: The QOM typename of the class to cast to.
715 * Returns: If @typename is a class, this function returns @klass if
716 * @typename is a subtype of @klass, else returns #NULL.
718 * If @typename is an interface, this function returns the interface
719 * definition for @klass if @klass implements it unambiguously; #NULL
720 * is returned if @klass does not implement the interface or if multiple
721 * classes or interfaces on the hierarchy leading to @klass implement
722 * it. (FIXME: perhaps this can be detected at type definition time?)
724 ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
725 const char *typename);
728 * object_class_get_parent:
729 * @klass: The class to obtain the parent for.
731 * Returns: The parent for @klass or %NULL if none.
733 ObjectClass *object_class_get_parent(ObjectClass *klass);
736 * object_class_get_name:
737 * @klass: The class to obtain the QOM typename for.
739 * Returns: The QOM typename for @klass.
741 const char *object_class_get_name(ObjectClass *klass);
744 * object_class_is_abstract:
745 * @klass: The class to obtain the abstractness for.
747 * Returns: %true if @klass is abstract, %false otherwise.
749 bool object_class_is_abstract(ObjectClass *klass);
752 * object_class_by_name:
753 * @typename: The QOM typename to obtain the class for.
755 * Returns: The class for @typename or %NULL if not found.
757 ObjectClass *object_class_by_name(const char *typename);
759 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
760 const char *implements_type, bool include_abstract,
761 void *opaque);
764 * object_class_get_list:
765 * @implements_type: The type to filter for, including its derivatives.
766 * @include_abstract: Whether to include abstract classes.
768 * Returns: A singly-linked list of the classes in reverse hashtable order.
770 GSList *object_class_get_list(const char *implements_type,
771 bool include_abstract);
774 * object_ref:
775 * @obj: the object
777 * Increase the reference count of a object. A object cannot be freed as long
778 * as its reference count is greater than zero.
780 void object_ref(Object *obj);
783 * qdef_unref:
784 * @obj: the object
786 * Decrease the reference count of a object. A object cannot be freed as long
787 * as its reference count is greater than zero.
789 void object_unref(Object *obj);
792 * object_property_add:
793 * @obj: the object to add a property to
794 * @name: the name of the property. This can contain any character except for
795 * a forward slash. In general, you should use hyphens '-' instead of
796 * underscores '_' when naming properties.
797 * @type: the type name of the property. This namespace is pretty loosely
798 * defined. Sub namespaces are constructed by using a prefix and then
799 * to angle brackets. For instance, the type 'virtio-net-pci' in the
800 * 'link' namespace would be 'link<virtio-net-pci>'.
801 * @get: The getter to be called to read a property. If this is NULL, then
802 * the property cannot be read.
803 * @set: the setter to be called to write a property. If this is NULL,
804 * then the property cannot be written.
805 * @release: called when the property is removed from the object. This is
806 * meant to allow a property to free its opaque upon object
807 * destruction. This may be NULL.
808 * @opaque: an opaque pointer to pass to the callbacks for the property
809 * @errp: returns an error if this function fails
811 * Returns: The #ObjectProperty; this can be used to set the @resolve
812 * callback for child and link properties.
814 ObjectProperty *object_property_add(Object *obj, const char *name,
815 const char *type,
816 ObjectPropertyAccessor *get,
817 ObjectPropertyAccessor *set,
818 ObjectPropertyRelease *release,
819 void *opaque, Error **errp);
821 void object_property_del(Object *obj, const char *name, Error **errp);
824 * object_property_find:
825 * @obj: the object
826 * @name: the name of the property
827 * @errp: returns an error if this function fails
829 * Look up a property for an object and return its #ObjectProperty if found.
831 ObjectProperty *object_property_find(Object *obj, const char *name,
832 Error **errp);
834 void object_unparent(Object *obj);
837 * object_property_get:
838 * @obj: the object
839 * @v: the visitor that will receive the property value. This should be an
840 * Output visitor and the data will be written with @name as the name.
841 * @name: the name of the property
842 * @errp: returns an error if this function fails
844 * Reads a property from a object.
846 void object_property_get(Object *obj, struct Visitor *v, const char *name,
847 Error **errp);
850 * object_property_set_str:
851 * @value: the value to be written to the property
852 * @name: the name of the property
853 * @errp: returns an error if this function fails
855 * Writes a string value to a property.
857 void object_property_set_str(Object *obj, const char *value,
858 const char *name, Error **errp);
861 * object_property_get_str:
862 * @obj: the object
863 * @name: the name of the property
864 * @errp: returns an error if this function fails
866 * Returns: the value of the property, converted to a C string, or NULL if
867 * an error occurs (including when the property value is not a string).
868 * The caller should free the string.
870 char *object_property_get_str(Object *obj, const char *name,
871 Error **errp);
874 * object_property_set_link:
875 * @value: the value to be written to the property
876 * @name: the name of the property
877 * @errp: returns an error if this function fails
879 * Writes an object's canonical path to a property.
881 void object_property_set_link(Object *obj, Object *value,
882 const char *name, Error **errp);
885 * object_property_get_link:
886 * @obj: the object
887 * @name: the name of the property
888 * @errp: returns an error if this function fails
890 * Returns: the value of the property, resolved from a path to an Object,
891 * or NULL if an error occurs (including when the property value is not a
892 * string or not a valid object path).
894 Object *object_property_get_link(Object *obj, const char *name,
895 Error **errp);
898 * object_property_set_bool:
899 * @value: the value to be written to the property
900 * @name: the name of the property
901 * @errp: returns an error if this function fails
903 * Writes a bool value to a property.
905 void object_property_set_bool(Object *obj, bool value,
906 const char *name, Error **errp);
909 * object_property_get_bool:
910 * @obj: the object
911 * @name: the name of the property
912 * @errp: returns an error if this function fails
914 * Returns: the value of the property, converted to a boolean, or NULL if
915 * an error occurs (including when the property value is not a bool).
917 bool object_property_get_bool(Object *obj, const char *name,
918 Error **errp);
921 * object_property_set_int:
922 * @value: the value to be written to the property
923 * @name: the name of the property
924 * @errp: returns an error if this function fails
926 * Writes an integer value to a property.
928 void object_property_set_int(Object *obj, int64_t value,
929 const char *name, Error **errp);
932 * object_property_get_int:
933 * @obj: the object
934 * @name: the name of the property
935 * @errp: returns an error if this function fails
937 * Returns: the value of the property, converted to an integer, or NULL if
938 * an error occurs (including when the property value is not an integer).
940 int64_t object_property_get_int(Object *obj, const char *name,
941 Error **errp);
944 * object_property_get_enum:
945 * @obj: the object
946 * @name: the name of the property
947 * @strings: strings corresponding to enums
948 * @errp: returns an error if this function fails
950 * Returns: the value of the property, converted to an integer, or
951 * undefined if an error occurs (including when the property value is not
952 * an enum).
954 int object_property_get_enum(Object *obj, const char *name,
955 const char *strings[], Error **errp);
958 * object_property_get_uint16List:
959 * @obj: the object
960 * @name: the name of the property
961 * @list: the returned int list
962 * @errp: returns an error if this function fails
964 * Returns: the value of the property, converted to integers, or
965 * undefined if an error occurs (including when the property value is not
966 * an list of integers).
968 void object_property_get_uint16List(Object *obj, const char *name,
969 uint16List **list, Error **errp);
972 * object_property_set:
973 * @obj: the object
974 * @v: the visitor that will be used to write the property value. This should
975 * be an Input visitor and the data will be first read with @name as the
976 * name and then written as the property value.
977 * @name: the name of the property
978 * @errp: returns an error if this function fails
980 * Writes a property to a object.
982 void object_property_set(Object *obj, struct Visitor *v, const char *name,
983 Error **errp);
986 * object_property_parse:
987 * @obj: the object
988 * @string: the string that will be used to parse the property value.
989 * @name: the name of the property
990 * @errp: returns an error if this function fails
992 * Parses a string and writes the result into a property of an object.
994 void object_property_parse(Object *obj, const char *string,
995 const char *name, Error **errp);
998 * object_property_print:
999 * @obj: the object
1000 * @name: the name of the property
1001 * @human: if true, print for human consumption
1002 * @errp: returns an error if this function fails
1004 * Returns a string representation of the value of the property. The
1005 * caller shall free the string.
1007 char *object_property_print(Object *obj, const char *name, bool human,
1008 Error **errp);
1011 * object_property_get_type:
1012 * @obj: the object
1013 * @name: the name of the property
1014 * @errp: returns an error if this function fails
1016 * Returns: The type name of the property.
1018 const char *object_property_get_type(Object *obj, const char *name,
1019 Error **errp);
1022 * object_get_root:
1024 * Returns: the root object of the composition tree
1026 Object *object_get_root(void);
1029 * object_get_canonical_path_component:
1031 * Returns: The final component in the object's canonical path. The canonical
1032 * path is the path within the composition tree starting from the root.
1034 gchar *object_get_canonical_path_component(Object *obj);
1037 * object_get_canonical_path:
1039 * Returns: The canonical path for a object. This is the path within the
1040 * composition tree starting from the root.
1042 gchar *object_get_canonical_path(Object *obj);
1045 * object_resolve_path:
1046 * @path: the path to resolve
1047 * @ambiguous: returns true if the path resolution failed because of an
1048 * ambiguous match
1050 * There are two types of supported paths--absolute paths and partial paths.
1052 * Absolute paths are derived from the root object and can follow child<> or
1053 * link<> properties. Since they can follow link<> properties, they can be
1054 * arbitrarily long. Absolute paths look like absolute filenames and are
1055 * prefixed with a leading slash.
1057 * Partial paths look like relative filenames. They do not begin with a
1058 * prefix. The matching rules for partial paths are subtle but designed to make
1059 * specifying objects easy. At each level of the composition tree, the partial
1060 * path is matched as an absolute path. The first match is not returned. At
1061 * least two matches are searched for. A successful result is only returned if
1062 * only one match is found. If more than one match is found, a flag is
1063 * returned to indicate that the match was ambiguous.
1065 * Returns: The matched object or NULL on path lookup failure.
1067 Object *object_resolve_path(const char *path, bool *ambiguous);
1070 * object_resolve_path_type:
1071 * @path: the path to resolve
1072 * @typename: the type to look for.
1073 * @ambiguous: returns true if the path resolution failed because of an
1074 * ambiguous match
1076 * This is similar to object_resolve_path. However, when looking for a
1077 * partial path only matches that implement the given type are considered.
1078 * This restricts the search and avoids spuriously flagging matches as
1079 * ambiguous.
1081 * For both partial and absolute paths, the return value goes through
1082 * a dynamic cast to @typename. This is important if either the link,
1083 * or the typename itself are of interface types.
1085 * Returns: The matched object or NULL on path lookup failure.
1087 Object *object_resolve_path_type(const char *path, const char *typename,
1088 bool *ambiguous);
1091 * object_resolve_path_component:
1092 * @parent: the object in which to resolve the path
1093 * @part: the component to resolve.
1095 * This is similar to object_resolve_path with an absolute path, but it
1096 * only resolves one element (@part) and takes the others from @parent.
1098 * Returns: The resolved object or NULL on path lookup failure.
1100 Object *object_resolve_path_component(Object *parent, const gchar *part);
1103 * object_property_add_child:
1104 * @obj: the object to add a property to
1105 * @name: the name of the property
1106 * @child: the child object
1107 * @errp: if an error occurs, a pointer to an area to store the area
1109 * Child properties form the composition tree. All objects need to be a child
1110 * of another object. Objects can only be a child of one object.
1112 * There is no way for a child to determine what its parent is. It is not
1113 * a bidirectional relationship. This is by design.
1115 * The value of a child property as a C string will be the child object's
1116 * canonical path. It can be retrieved using object_property_get_str().
1117 * The child object itself can be retrieved using object_property_get_link().
1119 void object_property_add_child(Object *obj, const char *name,
1120 Object *child, Error **errp);
1122 typedef enum {
1123 /* Unref the link pointer when the property is deleted */
1124 OBJ_PROP_LINK_UNREF_ON_RELEASE = 0x1,
1125 } ObjectPropertyLinkFlags;
1128 * object_property_allow_set_link:
1130 * The default implementation of the object_property_add_link() check()
1131 * callback function. It allows the link property to be set and never returns
1132 * an error.
1134 void object_property_allow_set_link(Object *, const char *,
1135 Object *, Error **);
1138 * object_property_add_link:
1139 * @obj: the object to add a property to
1140 * @name: the name of the property
1141 * @type: the qobj type of the link
1142 * @child: a pointer to where the link object reference is stored
1143 * @check: callback to veto setting or NULL if the property is read-only
1144 * @flags: additional options for the link
1145 * @errp: if an error occurs, a pointer to an area to store the area
1147 * Links establish relationships between objects. Links are unidirectional
1148 * although two links can be combined to form a bidirectional relationship
1149 * between objects.
1151 * Links form the graph in the object model.
1153 * The <code>@check()</code> callback is invoked when
1154 * object_property_set_link() is called and can raise an error to prevent the
1155 * link being set. If <code>@check</code> is NULL, the property is read-only
1156 * and cannot be set.
1158 * Ownership of the pointer that @child points to is transferred to the
1159 * link property. The reference count for <code>*@child</code> is
1160 * managed by the property from after the function returns till the
1161 * property is deleted with object_property_del(). If the
1162 * <code>@flags</code> <code>OBJ_PROP_LINK_UNREF_ON_RELEASE</code> bit is set,
1163 * the reference count is decremented when the property is deleted.
1165 void object_property_add_link(Object *obj, const char *name,
1166 const char *type, Object **child,
1167 void (*check)(Object *obj, const char *name,
1168 Object *val, Error **errp),
1169 ObjectPropertyLinkFlags flags,
1170 Error **errp);
1173 * object_property_add_str:
1174 * @obj: the object to add a property to
1175 * @name: the name of the property
1176 * @get: the getter or NULL if the property is write-only. This function must
1177 * return a string to be freed by g_free().
1178 * @set: the setter or NULL if the property is read-only
1179 * @errp: if an error occurs, a pointer to an area to store the error
1181 * Add a string property using getters/setters. This function will add a
1182 * property of type 'string'.
1184 void object_property_add_str(Object *obj, const char *name,
1185 char *(*get)(Object *, Error **),
1186 void (*set)(Object *, const char *, Error **),
1187 Error **errp);
1190 * object_property_add_bool:
1191 * @obj: the object to add a property to
1192 * @name: the name of the property
1193 * @get: the getter or NULL if the property is write-only.
1194 * @set: the setter or NULL if the property is read-only
1195 * @errp: if an error occurs, a pointer to an area to store the error
1197 * Add a bool property using getters/setters. This function will add a
1198 * property of type 'bool'.
1200 void object_property_add_bool(Object *obj, const char *name,
1201 bool (*get)(Object *, Error **),
1202 void (*set)(Object *, bool, Error **),
1203 Error **errp);
1206 * object_property_add_uint8_ptr:
1207 * @obj: the object to add a property to
1208 * @name: the name of the property
1209 * @v: pointer to value
1210 * @errp: if an error occurs, a pointer to an area to store the error
1212 * Add an integer property in memory. This function will add a
1213 * property of type 'uint8'.
1215 void object_property_add_uint8_ptr(Object *obj, const char *name,
1216 const uint8_t *v, Error **errp);
1219 * object_property_add_uint16_ptr:
1220 * @obj: the object to add a property to
1221 * @name: the name of the property
1222 * @v: pointer to value
1223 * @errp: if an error occurs, a pointer to an area to store the error
1225 * Add an integer property in memory. This function will add a
1226 * property of type 'uint16'.
1228 void object_property_add_uint16_ptr(Object *obj, const char *name,
1229 const uint16_t *v, Error **errp);
1232 * object_property_add_uint32_ptr:
1233 * @obj: the object to add a property to
1234 * @name: the name of the property
1235 * @v: pointer to value
1236 * @errp: if an error occurs, a pointer to an area to store the error
1238 * Add an integer property in memory. This function will add a
1239 * property of type 'uint32'.
1241 void object_property_add_uint32_ptr(Object *obj, const char *name,
1242 const uint32_t *v, Error **errp);
1245 * object_property_add_uint64_ptr:
1246 * @obj: the object to add a property to
1247 * @name: the name of the property
1248 * @v: pointer to value
1249 * @errp: if an error occurs, a pointer to an area to store the error
1251 * Add an integer property in memory. This function will add a
1252 * property of type 'uint64'.
1254 void object_property_add_uint64_ptr(Object *obj, const char *name,
1255 const uint64_t *v, Error **Errp);
1258 * object_property_add_alias:
1259 * @obj: the object to add a property to
1260 * @name: the name of the property
1261 * @target_obj: the object to forward property access to
1262 * @target_name: the name of the property on the forwarded object
1263 * @errp: if an error occurs, a pointer to an area to store the error
1265 * Add an alias for a property on an object. This function will add a property
1266 * of the same type as the forwarded property.
1268 * The caller must ensure that <code>@target_obj</code> stays alive as long as
1269 * this property exists. In the case of a child object or an alias on the same
1270 * object this will be the case. For aliases to other objects the caller is
1271 * responsible for taking a reference.
1273 void object_property_add_alias(Object *obj, const char *name,
1274 Object *target_obj, const char *target_name,
1275 Error **errp);
1278 * object_child_foreach:
1279 * @obj: the object whose children will be navigated
1280 * @fn: the iterator function to be called
1281 * @opaque: an opaque value that will be passed to the iterator
1283 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1284 * non-zero.
1286 * Returns: The last value returned by @fn, or 0 if there is no child.
1288 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1289 void *opaque);
1292 * container_get:
1293 * @root: root of the #path, e.g., object_get_root()
1294 * @path: path to the container
1296 * Return a container object whose path is @path. Create more containers
1297 * along the path if necessary.
1299 * Returns: the container object.
1301 Object *container_get(Object *root, const char *path);
1304 #endif