xref: /qemu/include/qom/object.h (revision 93bb3d8d)
1 /*
2  * QEMU Object Model
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
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.
11  *
12  */
13 
14 #ifndef QEMU_OBJECT_H
15 #define QEMU_OBJECT_H
16 
17 #include "qapi/qapi-builtin-types.h"
18 #include "qemu/module.h"
19 
20 struct TypeImpl;
21 typedef struct TypeImpl *Type;
22 
23 typedef struct Object Object;
24 
25 typedef struct TypeInfo TypeInfo;
26 
27 typedef struct InterfaceClass InterfaceClass;
28 typedef struct InterfaceInfo InterfaceInfo;
29 
30 #define TYPE_OBJECT "object"
31 
32 /**
33  * SECTION:object.h
34  * @title:Base Object Type System
35  * @short_description: interfaces for creating new types and objects
36  *
37  * The QEMU Object Model provides a framework for registering user creatable
38  * types and instantiating objects from those types.  QOM provides the following
39  * features:
40  *
41  *  - System for dynamically registering types
42  *  - Support for single-inheritance of types
43  *  - Multiple inheritance of stateless interfaces
44  *
45  * <example>
46  *   <title>Creating a minimal type</title>
47  *   <programlisting>
48  * #include "qdev.h"
49  *
50  * #define TYPE_MY_DEVICE "my-device"
51  *
52  * // No new virtual functions: we can reuse the typedef for the
53  * // superclass.
54  * typedef DeviceClass MyDeviceClass;
55  * typedef struct MyDevice
56  * {
57  *     DeviceState parent;
58  *
59  *     int reg0, reg1, reg2;
60  * } MyDevice;
61  *
62  * static const TypeInfo my_device_info = {
63  *     .name = TYPE_MY_DEVICE,
64  *     .parent = TYPE_DEVICE,
65  *     .instance_size = sizeof(MyDevice),
66  * };
67  *
68  * static void my_device_register_types(void)
69  * {
70  *     type_register_static(&my_device_info);
71  * }
72  *
73  * type_init(my_device_register_types)
74  *   </programlisting>
75  * </example>
76  *
77  * In the above example, we create a simple type that is described by #TypeInfo.
78  * #TypeInfo describes information about the type including what it inherits
79  * from, the instance and class size, and constructor/destructor hooks.
80  *
81  * Alternatively several static types could be registered using helper macro
82  * DEFINE_TYPES()
83  *
84  * <example>
85  *   <programlisting>
86  * static const TypeInfo device_types_info[] = {
87  *     {
88  *         .name = TYPE_MY_DEVICE_A,
89  *         .parent = TYPE_DEVICE,
90  *         .instance_size = sizeof(MyDeviceA),
91  *     },
92  *     {
93  *         .name = TYPE_MY_DEVICE_B,
94  *         .parent = TYPE_DEVICE,
95  *         .instance_size = sizeof(MyDeviceB),
96  *     },
97  * };
98  *
99  * DEFINE_TYPES(device_types_info)
100  *   </programlisting>
101  * </example>
102  *
103  * Every type has an #ObjectClass associated with it.  #ObjectClass derivatives
104  * are instantiated dynamically but there is only ever one instance for any
105  * given type.  The #ObjectClass typically holds a table of function pointers
106  * for the virtual methods implemented by this type.
107  *
108  * Using object_new(), a new #Object derivative will be instantiated.  You can
109  * cast an #Object to a subclass (or base-class) type using
110  * object_dynamic_cast().  You typically want to define macro wrappers around
111  * OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
112  * specific type:
113  *
114  * <example>
115  *   <title>Typecasting macros</title>
116  *   <programlisting>
117  *    #define MY_DEVICE_GET_CLASS(obj) \
118  *       OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
119  *    #define MY_DEVICE_CLASS(klass) \
120  *       OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
121  *    #define MY_DEVICE(obj) \
122  *       OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
123  *   </programlisting>
124  * </example>
125  *
126  * # Class Initialization #
127  *
128  * Before an object is initialized, the class for the object must be
129  * initialized.  There is only one class object for all instance objects
130  * that is created lazily.
131  *
132  * Classes are initialized by first initializing any parent classes (if
133  * necessary).  After the parent class object has initialized, it will be
134  * copied into the current class object and any additional storage in the
135  * class object is zero filled.
136  *
137  * The effect of this is that classes automatically inherit any virtual
138  * function pointers that the parent class has already initialized.  All
139  * other fields will be zero filled.
140  *
141  * Once all of the parent classes have been initialized, #TypeInfo::class_init
142  * is called to let the class being instantiated provide default initialize for
143  * its virtual functions.  Here is how the above example might be modified
144  * to introduce an overridden virtual function:
145  *
146  * <example>
147  *   <title>Overriding a virtual function</title>
148  *   <programlisting>
149  * #include "qdev.h"
150  *
151  * void my_device_class_init(ObjectClass *klass, void *class_data)
152  * {
153  *     DeviceClass *dc = DEVICE_CLASS(klass);
154  *     dc->reset = my_device_reset;
155  * }
156  *
157  * static const TypeInfo my_device_info = {
158  *     .name = TYPE_MY_DEVICE,
159  *     .parent = TYPE_DEVICE,
160  *     .instance_size = sizeof(MyDevice),
161  *     .class_init = my_device_class_init,
162  * };
163  *   </programlisting>
164  * </example>
165  *
166  * Introducing new virtual methods requires a class to define its own
167  * struct and to add a .class_size member to the #TypeInfo.  Each method
168  * will also have a wrapper function to call it easily:
169  *
170  * <example>
171  *   <title>Defining an abstract class</title>
172  *   <programlisting>
173  * #include "qdev.h"
174  *
175  * typedef struct MyDeviceClass
176  * {
177  *     DeviceClass parent;
178  *
179  *     void (*frobnicate) (MyDevice *obj);
180  * } MyDeviceClass;
181  *
182  * static const TypeInfo my_device_info = {
183  *     .name = TYPE_MY_DEVICE,
184  *     .parent = TYPE_DEVICE,
185  *     .instance_size = sizeof(MyDevice),
186  *     .abstract = true, // or set a default in my_device_class_init
187  *     .class_size = sizeof(MyDeviceClass),
188  * };
189  *
190  * void my_device_frobnicate(MyDevice *obj)
191  * {
192  *     MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
193  *
194  *     klass->frobnicate(obj);
195  * }
196  *   </programlisting>
197  * </example>
198  *
199  * # Interfaces #
200  *
201  * Interfaces allow a limited form of multiple inheritance.  Instances are
202  * similar to normal types except for the fact that are only defined by
203  * their classes and never carry any state.  As a consequence, a pointer to
204  * an interface instance should always be of incomplete type in order to be
205  * sure it cannot be dereferenced.  That is, you should define the
206  * 'typedef struct SomethingIf SomethingIf' so that you can pass around
207  * 'SomethingIf *si' arguments, but not define a 'struct SomethingIf { ... }'.
208  * The only things you can validly do with a 'SomethingIf *' are to pass it as
209  * an argument to a method on its corresponding SomethingIfClass, or to
210  * dynamically cast it to an object that implements the interface.
211  *
212  * # Methods #
213  *
214  * A <emphasis>method</emphasis> is a function within the namespace scope of
215  * a class. It usually operates on the object instance by passing it as a
216  * strongly-typed first argument.
217  * If it does not operate on an object instance, it is dubbed
218  * <emphasis>class method</emphasis>.
219  *
220  * Methods cannot be overloaded. That is, the #ObjectClass and method name
221  * uniquely identity the function to be called; the signature does not vary
222  * except for trailing varargs.
223  *
224  * Methods are always <emphasis>virtual</emphasis>. Overriding a method in
225  * #TypeInfo.class_init of a subclass leads to any user of the class obtained
226  * via OBJECT_GET_CLASS() accessing the overridden function.
227  * The original function is not automatically invoked. It is the responsibility
228  * of the overriding class to determine whether and when to invoke the method
229  * being overridden.
230  *
231  * To invoke the method being overridden, the preferred solution is to store
232  * the original value in the overriding class before overriding the method.
233  * This corresponds to |[ {super,base}.method(...) ]| in Java and C#
234  * respectively; this frees the overriding class from hardcoding its parent
235  * class, which someone might choose to change at some point.
236  *
237  * <example>
238  *   <title>Overriding a virtual method</title>
239  *   <programlisting>
240  * typedef struct MyState MyState;
241  *
242  * typedef void (*MyDoSomething)(MyState *obj);
243  *
244  * typedef struct MyClass {
245  *     ObjectClass parent_class;
246  *
247  *     MyDoSomething do_something;
248  * } MyClass;
249  *
250  * static void my_do_something(MyState *obj)
251  * {
252  *     // do something
253  * }
254  *
255  * static void my_class_init(ObjectClass *oc, void *data)
256  * {
257  *     MyClass *mc = MY_CLASS(oc);
258  *
259  *     mc->do_something = my_do_something;
260  * }
261  *
262  * static const TypeInfo my_type_info = {
263  *     .name = TYPE_MY,
264  *     .parent = TYPE_OBJECT,
265  *     .instance_size = sizeof(MyState),
266  *     .class_size = sizeof(MyClass),
267  *     .class_init = my_class_init,
268  * };
269  *
270  * typedef struct DerivedClass {
271  *     MyClass parent_class;
272  *
273  *     MyDoSomething parent_do_something;
274  * } DerivedClass;
275  *
276  * static void derived_do_something(MyState *obj)
277  * {
278  *     DerivedClass *dc = DERIVED_GET_CLASS(obj);
279  *
280  *     // do something here
281  *     dc->parent_do_something(obj);
282  *     // do something else here
283  * }
284  *
285  * static void derived_class_init(ObjectClass *oc, void *data)
286  * {
287  *     MyClass *mc = MY_CLASS(oc);
288  *     DerivedClass *dc = DERIVED_CLASS(oc);
289  *
290  *     dc->parent_do_something = mc->do_something;
291  *     mc->do_something = derived_do_something;
292  * }
293  *
294  * static const TypeInfo derived_type_info = {
295  *     .name = TYPE_DERIVED,
296  *     .parent = TYPE_MY,
297  *     .class_size = sizeof(DerivedClass),
298  *     .class_init = derived_class_init,
299  * };
300  *   </programlisting>
301  * </example>
302  *
303  * Alternatively, object_class_by_name() can be used to obtain the class and
304  * its non-overridden methods for a specific type. This would correspond to
305  * |[ MyClass::method(...) ]| in C++.
306  *
307  * The first example of such a QOM method was #CPUClass.reset,
308  * another example is #DeviceClass.realize.
309  */
310 
311 
312 typedef struct ObjectProperty ObjectProperty;
313 
314 /**
315  * ObjectPropertyAccessor:
316  * @obj: the object that owns the property
317  * @v: the visitor that contains the property data
318  * @name: the name of the property
319  * @opaque: the object property opaque
320  * @errp: a pointer to an Error that is filled if getting/setting fails.
321  *
322  * Called when trying to get/set a property.
323  */
324 typedef void (ObjectPropertyAccessor)(Object *obj,
325                                       Visitor *v,
326                                       const char *name,
327                                       void *opaque,
328                                       Error **errp);
329 
330 /**
331  * ObjectPropertyResolve:
332  * @obj: the object that owns the property
333  * @opaque: the opaque registered with the property
334  * @part: the name of the property
335  *
336  * Resolves the #Object corresponding to property @part.
337  *
338  * The returned object can also be used as a starting point
339  * to resolve a relative path starting with "@part".
340  *
341  * Returns: If @path is the path that led to @obj, the function
342  * returns the #Object corresponding to "@path/@part".
343  * If "@path/@part" is not a valid object path, it returns #NULL.
344  */
345 typedef Object *(ObjectPropertyResolve)(Object *obj,
346                                         void *opaque,
347                                         const char *part);
348 
349 /**
350  * ObjectPropertyRelease:
351  * @obj: the object that owns the property
352  * @name: the name of the property
353  * @opaque: the opaque registered with the property
354  *
355  * Called when a property is removed from a object.
356  */
357 typedef void (ObjectPropertyRelease)(Object *obj,
358                                      const char *name,
359                                      void *opaque);
360 
361 /**
362  * ObjectPropertyInit:
363  * @obj: the object that owns the property
364  * @prop: the property to set
365  *
366  * Called when a property is initialized.
367  */
368 typedef void (ObjectPropertyInit)(Object *obj, ObjectProperty *prop);
369 
370 struct ObjectProperty
371 {
372     char *name;
373     char *type;
374     char *description;
375     ObjectPropertyAccessor *get;
376     ObjectPropertyAccessor *set;
377     ObjectPropertyResolve *resolve;
378     ObjectPropertyRelease *release;
379     ObjectPropertyInit *init;
380     void *opaque;
381     QObject *defval;
382 };
383 
384 /**
385  * ObjectUnparent:
386  * @obj: the object that is being removed from the composition tree
387  *
388  * Called when an object is being removed from the QOM composition tree.
389  * The function should remove any backlinks from children objects to @obj.
390  */
391 typedef void (ObjectUnparent)(Object *obj);
392 
393 /**
394  * ObjectFree:
395  * @obj: the object being freed
396  *
397  * Called when an object's last reference is removed.
398  */
399 typedef void (ObjectFree)(void *obj);
400 
401 #define OBJECT_CLASS_CAST_CACHE 4
402 
403 /**
404  * ObjectClass:
405  *
406  * The base for all classes.  The only thing that #ObjectClass contains is an
407  * integer type handle.
408  */
409 struct ObjectClass
410 {
411     /*< private >*/
412     Type type;
413     GSList *interfaces;
414 
415     const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
416     const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
417 
418     ObjectUnparent *unparent;
419 
420     GHashTable *properties;
421 };
422 
423 /**
424  * Object:
425  *
426  * The base for all objects.  The first member of this object is a pointer to
427  * a #ObjectClass.  Since C guarantees that the first member of a structure
428  * always begins at byte 0 of that structure, as long as any sub-object places
429  * its parent as the first member, we can cast directly to a #Object.
430  *
431  * As a result, #Object contains a reference to the objects type as its
432  * first member.  This allows identification of the real type of the object at
433  * run time.
434  */
435 struct Object
436 {
437     /*< private >*/
438     ObjectClass *class;
439     ObjectFree *free;
440     GHashTable *properties;
441     uint32_t ref;
442     Object *parent;
443 };
444 
445 /**
446  * TypeInfo:
447  * @name: The name of the type.
448  * @parent: The name of the parent type.
449  * @instance_size: The size of the object (derivative of #Object).  If
450  *   @instance_size is 0, then the size of the object will be the size of the
451  *   parent object.
452  * @instance_init: This function is called to initialize an object.  The parent
453  *   class will have already been initialized so the type is only responsible
454  *   for initializing its own members.
455  * @instance_post_init: This function is called to finish initialization of
456  *   an object, after all @instance_init functions were called.
457  * @instance_finalize: This function is called during object destruction.  This
458  *   is called before the parent @instance_finalize function has been called.
459  *   An object should only free the members that are unique to its type in this
460  *   function.
461  * @abstract: If this field is true, then the class is considered abstract and
462  *   cannot be directly instantiated.
463  * @class_size: The size of the class object (derivative of #ObjectClass)
464  *   for this object.  If @class_size is 0, then the size of the class will be
465  *   assumed to be the size of the parent class.  This allows a type to avoid
466  *   implementing an explicit class type if they are not adding additional
467  *   virtual functions.
468  * @class_init: This function is called after all parent class initialization
469  *   has occurred to allow a class to set its default virtual method pointers.
470  *   This is also the function to use to override virtual methods from a parent
471  *   class.
472  * @class_base_init: This function is called for all base classes after all
473  *   parent class initialization has occurred, but before the class itself
474  *   is initialized.  This is the function to use to undo the effects of
475  *   memcpy from the parent class to the descendants.
476  * @class_data: Data to pass to the @class_init,
477  *   @class_base_init. This can be useful when building dynamic
478  *   classes.
479  * @interfaces: The list of interfaces associated with this type.  This
480  *   should point to a static array that's terminated with a zero filled
481  *   element.
482  */
483 struct TypeInfo
484 {
485     const char *name;
486     const char *parent;
487 
488     size_t instance_size;
489     void (*instance_init)(Object *obj);
490     void (*instance_post_init)(Object *obj);
491     void (*instance_finalize)(Object *obj);
492 
493     bool abstract;
494     size_t class_size;
495 
496     void (*class_init)(ObjectClass *klass, void *data);
497     void (*class_base_init)(ObjectClass *klass, void *data);
498     void *class_data;
499 
500     InterfaceInfo *interfaces;
501 };
502 
503 /**
504  * OBJECT:
505  * @obj: A derivative of #Object
506  *
507  * Converts an object to a #Object.  Since all objects are #Objects,
508  * this function will always succeed.
509  */
510 #define OBJECT(obj) \
511     ((Object *)(obj))
512 
513 /**
514  * OBJECT_CLASS:
515  * @class: A derivative of #ObjectClass.
516  *
517  * Converts a class to an #ObjectClass.  Since all objects are #Objects,
518  * this function will always succeed.
519  */
520 #define OBJECT_CLASS(class) \
521     ((ObjectClass *)(class))
522 
523 /**
524  * OBJECT_CHECK:
525  * @type: The C type to use for the return value.
526  * @obj: A derivative of @type to cast.
527  * @name: The QOM typename of @type
528  *
529  * A type safe version of @object_dynamic_cast_assert.  Typically each class
530  * will define a macro based on this type to perform type safe dynamic_casts to
531  * this object type.
532  *
533  * If an invalid object is passed to this function, a run time assert will be
534  * generated.
535  */
536 #define OBJECT_CHECK(type, obj, name) \
537     ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
538                                         __FILE__, __LINE__, __func__))
539 
540 /**
541  * OBJECT_CLASS_CHECK:
542  * @class_type: The C type to use for the return value.
543  * @class: A derivative class of @class_type to cast.
544  * @name: the QOM typename of @class_type.
545  *
546  * A type safe version of @object_class_dynamic_cast_assert.  This macro is
547  * typically wrapped by each type to perform type safe casts of a class to a
548  * specific class type.
549  */
550 #define OBJECT_CLASS_CHECK(class_type, class, name) \
551     ((class_type *)object_class_dynamic_cast_assert(OBJECT_CLASS(class), (name), \
552                                                __FILE__, __LINE__, __func__))
553 
554 /**
555  * OBJECT_GET_CLASS:
556  * @class: The C type to use for the return value.
557  * @obj: The object to obtain the class for.
558  * @name: The QOM typename of @obj.
559  *
560  * This function will return a specific class for a given object.  Its generally
561  * used by each type to provide a type safe macro to get a specific class type
562  * from an object.
563  */
564 #define OBJECT_GET_CLASS(class, obj, name) \
565     OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
566 
567 /**
568  * InterfaceInfo:
569  * @type: The name of the interface.
570  *
571  * The information associated with an interface.
572  */
573 struct InterfaceInfo {
574     const char *type;
575 };
576 
577 /**
578  * InterfaceClass:
579  * @parent_class: the base class
580  *
581  * The class for all interfaces.  Subclasses of this class should only add
582  * virtual methods.
583  */
584 struct InterfaceClass
585 {
586     ObjectClass parent_class;
587     /*< private >*/
588     ObjectClass *concrete_class;
589     Type interface_type;
590 };
591 
592 #define TYPE_INTERFACE "interface"
593 
594 /**
595  * INTERFACE_CLASS:
596  * @klass: class to cast from
597  * Returns: An #InterfaceClass or raise an error if cast is invalid
598  */
599 #define INTERFACE_CLASS(klass) \
600     OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
601 
602 /**
603  * INTERFACE_CHECK:
604  * @interface: the type to return
605  * @obj: the object to convert to an interface
606  * @name: the interface type name
607  *
608  * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
609  */
610 #define INTERFACE_CHECK(interface, obj, name) \
611     ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
612                                              __FILE__, __LINE__, __func__))
613 
614 /**
615  * object_new_with_class:
616  * @klass: The class to instantiate.
617  *
618  * This function will initialize a new object using heap allocated memory.
619  * The returned object has a reference count of 1, and will be freed when
620  * the last reference is dropped.
621  *
622  * Returns: The newly allocated and instantiated object.
623  */
624 Object *object_new_with_class(ObjectClass *klass);
625 
626 /**
627  * object_new:
628  * @typename: The name of the type of the object to instantiate.
629  *
630  * This function will initialize a new object using heap allocated memory.
631  * The returned object has a reference count of 1, and will be freed when
632  * the last reference is dropped.
633  *
634  * Returns: The newly allocated and instantiated object.
635  */
636 Object *object_new(const char *typename);
637 
638 /**
639  * object_new_with_props:
640  * @typename:  The name of the type of the object to instantiate.
641  * @parent: the parent object
642  * @id: The unique ID of the object
643  * @errp: pointer to error object
644  * @...: list of property names and values
645  *
646  * This function will initialize a new object using heap allocated memory.
647  * The returned object has a reference count of 1, and will be freed when
648  * the last reference is dropped.
649  *
650  * The @id parameter will be used when registering the object as a
651  * child of @parent in the composition tree.
652  *
653  * The variadic parameters are a list of pairs of (propname, propvalue)
654  * strings. The propname of %NULL indicates the end of the property
655  * list. If the object implements the user creatable interface, the
656  * object will be marked complete once all the properties have been
657  * processed.
658  *
659  * <example>
660  *   <title>Creating an object with properties</title>
661  *   <programlisting>
662  *   Error *err = NULL;
663  *   Object *obj;
664  *
665  *   obj = object_new_with_props(TYPE_MEMORY_BACKEND_FILE,
666  *                               object_get_objects_root(),
667  *                               "hostmem0",
668  *                               &err,
669  *                               "share", "yes",
670  *                               "mem-path", "/dev/shm/somefile",
671  *                               "prealloc", "yes",
672  *                               "size", "1048576",
673  *                               NULL);
674  *
675  *   if (!obj) {
676  *     g_printerr("Cannot create memory backend: %s\n",
677  *                error_get_pretty(err));
678  *   }
679  *   </programlisting>
680  * </example>
681  *
682  * The returned object will have one stable reference maintained
683  * for as long as it is present in the object hierarchy.
684  *
685  * Returns: The newly allocated, instantiated & initialized object.
686  */
687 Object *object_new_with_props(const char *typename,
688                               Object *parent,
689                               const char *id,
690                               Error **errp,
691                               ...) QEMU_SENTINEL;
692 
693 /**
694  * object_new_with_propv:
695  * @typename:  The name of the type of the object to instantiate.
696  * @parent: the parent object
697  * @id: The unique ID of the object
698  * @errp: pointer to error object
699  * @vargs: list of property names and values
700  *
701  * See object_new_with_props() for documentation.
702  */
703 Object *object_new_with_propv(const char *typename,
704                               Object *parent,
705                               const char *id,
706                               Error **errp,
707                               va_list vargs);
708 
709 void object_apply_global_props(Object *obj, const GPtrArray *props,
710                                Error **errp);
711 void object_set_machine_compat_props(GPtrArray *compat_props);
712 void object_set_accelerator_compat_props(GPtrArray *compat_props);
713 void object_register_sugar_prop(const char *driver, const char *prop, const char *value);
714 void object_apply_compat_props(Object *obj);
715 
716 /**
717  * object_set_props:
718  * @obj: the object instance to set properties on
719  * @errp: pointer to error object
720  * @...: list of property names and values
721  *
722  * This function will set a list of properties on an existing object
723  * instance.
724  *
725  * The variadic parameters are a list of pairs of (propname, propvalue)
726  * strings. The propname of %NULL indicates the end of the property
727  * list.
728  *
729  * <example>
730  *   <title>Update an object's properties</title>
731  *   <programlisting>
732  *   Error *err = NULL;
733  *   Object *obj = ...get / create object...;
734  *
735  *   obj = object_set_props(obj,
736  *                          &err,
737  *                          "share", "yes",
738  *                          "mem-path", "/dev/shm/somefile",
739  *                          "prealloc", "yes",
740  *                          "size", "1048576",
741  *                          NULL);
742  *
743  *   if (!obj) {
744  *     g_printerr("Cannot set properties: %s\n",
745  *                error_get_pretty(err));
746  *   }
747  *   </programlisting>
748  * </example>
749  *
750  * The returned object will have one stable reference maintained
751  * for as long as it is present in the object hierarchy.
752  *
753  * Returns: -1 on error, 0 on success
754  */
755 int object_set_props(Object *obj,
756                      Error **errp,
757                      ...) QEMU_SENTINEL;
758 
759 /**
760  * object_set_propv:
761  * @obj: the object instance to set properties on
762  * @errp: pointer to error object
763  * @vargs: list of property names and values
764  *
765  * See object_set_props() for documentation.
766  *
767  * Returns: -1 on error, 0 on success
768  */
769 int object_set_propv(Object *obj,
770                      Error **errp,
771                      va_list vargs);
772 
773 /**
774  * object_initialize:
775  * @obj: A pointer to the memory to be used for the object.
776  * @size: The maximum size available at @obj for the object.
777  * @typename: The name of the type of the object to instantiate.
778  *
779  * This function will initialize an object.  The memory for the object should
780  * have already been allocated.  The returned object has a reference count of 1,
781  * and will be finalized when the last reference is dropped.
782  */
783 void object_initialize(void *obj, size_t size, const char *typename);
784 
785 /**
786  * object_initialize_child:
787  * @parentobj: The parent object to add a property to
788  * @propname: The name of the property
789  * @childobj: A pointer to the memory to be used for the object.
790  * @size: The maximum size available at @childobj for the object.
791  * @type: The name of the type of the object to instantiate.
792  * @errp: If an error occurs, a pointer to an area to store the error
793  * @...: list of property names and values
794  *
795  * This function will initialize an object. The memory for the object should
796  * have already been allocated. The object will then be added as child property
797  * to a parent with object_property_add_child() function. The returned object
798  * has a reference count of 1 (for the "child<...>" property from the parent),
799  * so the object will be finalized automatically when the parent gets removed.
800  *
801  * The variadic parameters are a list of pairs of (propname, propvalue)
802  * strings. The propname of %NULL indicates the end of the property list.
803  * If the object implements the user creatable interface, the object will
804  * be marked complete once all the properties have been processed.
805  */
806 void object_initialize_child(Object *parentobj, const char *propname,
807                              void *childobj, size_t size, const char *type,
808                              Error **errp, ...) QEMU_SENTINEL;
809 
810 /**
811  * object_initialize_childv:
812  * @parentobj: The parent object to add a property to
813  * @propname: The name of the property
814  * @childobj: A pointer to the memory to be used for the object.
815  * @size: The maximum size available at @childobj for the object.
816  * @type: The name of the type of the object to instantiate.
817  * @errp: If an error occurs, a pointer to an area to store the error
818  * @vargs: list of property names and values
819  *
820  * See object_initialize_child() for documentation.
821  */
822 void object_initialize_childv(Object *parentobj, const char *propname,
823                               void *childobj, size_t size, const char *type,
824                               Error **errp, va_list vargs);
825 
826 /**
827  * object_dynamic_cast:
828  * @obj: The object to cast.
829  * @typename: The @typename to cast to.
830  *
831  * This function will determine if @obj is-a @typename.  @obj can refer to an
832  * object or an interface associated with an object.
833  *
834  * Returns: This function returns @obj on success or #NULL on failure.
835  */
836 Object *object_dynamic_cast(Object *obj, const char *typename);
837 
838 /**
839  * object_dynamic_cast_assert:
840  *
841  * See object_dynamic_cast() for a description of the parameters of this
842  * function.  The only difference in behavior is that this function asserts
843  * instead of returning #NULL on failure if QOM cast debugging is enabled.
844  * This function is not meant to be called directly, but only through
845  * the wrapper macro OBJECT_CHECK.
846  */
847 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
848                                    const char *file, int line, const char *func);
849 
850 /**
851  * object_get_class:
852  * @obj: A derivative of #Object
853  *
854  * Returns: The #ObjectClass of the type associated with @obj.
855  */
856 ObjectClass *object_get_class(Object *obj);
857 
858 /**
859  * object_get_typename:
860  * @obj: A derivative of #Object.
861  *
862  * Returns: The QOM typename of @obj.
863  */
864 const char *object_get_typename(const Object *obj);
865 
866 /**
867  * type_register_static:
868  * @info: The #TypeInfo of the new type.
869  *
870  * @info and all of the strings it points to should exist for the life time
871  * that the type is registered.
872  *
873  * Returns: the new #Type.
874  */
875 Type type_register_static(const TypeInfo *info);
876 
877 /**
878  * type_register:
879  * @info: The #TypeInfo of the new type
880  *
881  * Unlike type_register_static(), this call does not require @info or its
882  * string members to continue to exist after the call returns.
883  *
884  * Returns: the new #Type.
885  */
886 Type type_register(const TypeInfo *info);
887 
888 /**
889  * type_register_static_array:
890  * @infos: The array of the new type #TypeInfo structures.
891  * @nr_infos: number of entries in @infos
892  *
893  * @infos and all of the strings it points to should exist for the life time
894  * that the type is registered.
895  */
896 void type_register_static_array(const TypeInfo *infos, int nr_infos);
897 
898 /**
899  * DEFINE_TYPES:
900  * @type_array: The array containing #TypeInfo structures to register
901  *
902  * @type_array should be static constant that exists for the life time
903  * that the type is registered.
904  */
905 #define DEFINE_TYPES(type_array)                                            \
906 static void do_qemu_init_ ## type_array(void)                               \
907 {                                                                           \
908     type_register_static_array(type_array, ARRAY_SIZE(type_array));         \
909 }                                                                           \
910 type_init(do_qemu_init_ ## type_array)
911 
912 /**
913  * object_class_dynamic_cast_assert:
914  * @klass: The #ObjectClass to attempt to cast.
915  * @typename: The QOM typename of the class to cast to.
916  *
917  * See object_class_dynamic_cast() for a description of the parameters
918  * of this function.  The only difference in behavior is that this function
919  * asserts instead of returning #NULL on failure if QOM cast debugging is
920  * enabled.  This function is not meant to be called directly, but only through
921  * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
922  */
923 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
924                                               const char *typename,
925                                               const char *file, int line,
926                                               const char *func);
927 
928 /**
929  * object_class_dynamic_cast:
930  * @klass: The #ObjectClass to attempt to cast.
931  * @typename: The QOM typename of the class to cast to.
932  *
933  * Returns: If @typename is a class, this function returns @klass if
934  * @typename is a subtype of @klass, else returns #NULL.
935  *
936  * If @typename is an interface, this function returns the interface
937  * definition for @klass if @klass implements it unambiguously; #NULL
938  * is returned if @klass does not implement the interface or if multiple
939  * classes or interfaces on the hierarchy leading to @klass implement
940  * it.  (FIXME: perhaps this can be detected at type definition time?)
941  */
942 ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
943                                        const char *typename);
944 
945 /**
946  * object_class_get_parent:
947  * @klass: The class to obtain the parent for.
948  *
949  * Returns: The parent for @klass or %NULL if none.
950  */
951 ObjectClass *object_class_get_parent(ObjectClass *klass);
952 
953 /**
954  * object_class_get_name:
955  * @klass: The class to obtain the QOM typename for.
956  *
957  * Returns: The QOM typename for @klass.
958  */
959 const char *object_class_get_name(ObjectClass *klass);
960 
961 /**
962  * object_class_is_abstract:
963  * @klass: The class to obtain the abstractness for.
964  *
965  * Returns: %true if @klass is abstract, %false otherwise.
966  */
967 bool object_class_is_abstract(ObjectClass *klass);
968 
969 /**
970  * object_class_by_name:
971  * @typename: The QOM typename to obtain the class for.
972  *
973  * Returns: The class for @typename or %NULL if not found.
974  */
975 ObjectClass *object_class_by_name(const char *typename);
976 
977 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
978                           const char *implements_type, bool include_abstract,
979                           void *opaque);
980 
981 /**
982  * object_class_get_list:
983  * @implements_type: The type to filter for, including its derivatives.
984  * @include_abstract: Whether to include abstract classes.
985  *
986  * Returns: A singly-linked list of the classes in reverse hashtable order.
987  */
988 GSList *object_class_get_list(const char *implements_type,
989                               bool include_abstract);
990 
991 /**
992  * object_class_get_list_sorted:
993  * @implements_type: The type to filter for, including its derivatives.
994  * @include_abstract: Whether to include abstract classes.
995  *
996  * Returns: A singly-linked list of the classes in alphabetical
997  * case-insensitive order.
998  */
999 GSList *object_class_get_list_sorted(const char *implements_type,
1000                               bool include_abstract);
1001 
1002 /**
1003  * object_ref:
1004  * @obj: the object
1005  *
1006  * Increase the reference count of a object.  A object cannot be freed as long
1007  * as its reference count is greater than zero.
1008  * Returns: @obj
1009  */
1010 Object *object_ref(Object *obj);
1011 
1012 /**
1013  * object_unref:
1014  * @obj: the object
1015  *
1016  * Decrease the reference count of a object.  A object cannot be freed as long
1017  * as its reference count is greater than zero.
1018  */
1019 void object_unref(Object *obj);
1020 
1021 /**
1022  * object_property_add:
1023  * @obj: the object to add a property to
1024  * @name: the name of the property.  This can contain any character except for
1025  *  a forward slash.  In general, you should use hyphens '-' instead of
1026  *  underscores '_' when naming properties.
1027  * @type: the type name of the property.  This namespace is pretty loosely
1028  *   defined.  Sub namespaces are constructed by using a prefix and then
1029  *   to angle brackets.  For instance, the type 'virtio-net-pci' in the
1030  *   'link' namespace would be 'link<virtio-net-pci>'.
1031  * @get: The getter to be called to read a property.  If this is NULL, then
1032  *   the property cannot be read.
1033  * @set: the setter to be called to write a property.  If this is NULL,
1034  *   then the property cannot be written.
1035  * @release: called when the property is removed from the object.  This is
1036  *   meant to allow a property to free its opaque upon object
1037  *   destruction.  This may be NULL.
1038  * @opaque: an opaque pointer to pass to the callbacks for the property
1039  *
1040  * Returns: The #ObjectProperty; this can be used to set the @resolve
1041  * callback for child and link properties.
1042  */
1043 ObjectProperty *object_property_add(Object *obj, const char *name,
1044                                     const char *type,
1045                                     ObjectPropertyAccessor *get,
1046                                     ObjectPropertyAccessor *set,
1047                                     ObjectPropertyRelease *release,
1048                                     void *opaque);
1049 
1050 void object_property_del(Object *obj, const char *name);
1051 
1052 ObjectProperty *object_class_property_add(ObjectClass *klass, const char *name,
1053                                           const char *type,
1054                                           ObjectPropertyAccessor *get,
1055                                           ObjectPropertyAccessor *set,
1056                                           ObjectPropertyRelease *release,
1057                                           void *opaque);
1058 
1059 /**
1060  * object_property_set_default_bool:
1061  * @prop: the property to set
1062  * @value: the value to be written to the property
1063  *
1064  * Set the property default value.
1065  */
1066 void object_property_set_default_bool(ObjectProperty *prop, bool value);
1067 
1068 /**
1069  * object_property_set_default_str:
1070  * @prop: the property to set
1071  * @value: the value to be written to the property
1072  *
1073  * Set the property default value.
1074  */
1075 void object_property_set_default_str(ObjectProperty *prop, const char *value);
1076 
1077 /**
1078  * object_property_set_default_int:
1079  * @prop: the property to set
1080  * @value: the value to be written to the property
1081  *
1082  * Set the property default value.
1083  */
1084 void object_property_set_default_int(ObjectProperty *prop, int64_t value);
1085 
1086 /**
1087  * object_property_set_default_uint:
1088  * @prop: the property to set
1089  * @value: the value to be written to the property
1090  *
1091  * Set the property default value.
1092  */
1093 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value);
1094 
1095 /**
1096  * object_property_find:
1097  * @obj: the object
1098  * @name: the name of the property
1099  * @errp: returns an error if this function fails
1100  *
1101  * Look up a property for an object and return its #ObjectProperty if found.
1102  */
1103 ObjectProperty *object_property_find(Object *obj, const char *name,
1104                                      Error **errp);
1105 ObjectProperty *object_class_property_find(ObjectClass *klass, const char *name,
1106                                            Error **errp);
1107 
1108 typedef struct ObjectPropertyIterator {
1109     ObjectClass *nextclass;
1110     GHashTableIter iter;
1111 } ObjectPropertyIterator;
1112 
1113 /**
1114  * object_property_iter_init:
1115  * @obj: the object
1116  *
1117  * Initializes an iterator for traversing all properties
1118  * registered against an object instance, its class and all parent classes.
1119  *
1120  * It is forbidden to modify the property list while iterating,
1121  * whether removing or adding properties.
1122  *
1123  * Typical usage pattern would be
1124  *
1125  * <example>
1126  *   <title>Using object property iterators</title>
1127  *   <programlisting>
1128  *   ObjectProperty *prop;
1129  *   ObjectPropertyIterator iter;
1130  *
1131  *   object_property_iter_init(&iter, obj);
1132  *   while ((prop = object_property_iter_next(&iter))) {
1133  *     ... do something with prop ...
1134  *   }
1135  *   </programlisting>
1136  * </example>
1137  */
1138 void object_property_iter_init(ObjectPropertyIterator *iter,
1139                                Object *obj);
1140 
1141 /**
1142  * object_class_property_iter_init:
1143  * @klass: the class
1144  *
1145  * Initializes an iterator for traversing all properties
1146  * registered against an object class and all parent classes.
1147  *
1148  * It is forbidden to modify the property list while iterating,
1149  * whether removing or adding properties.
1150  *
1151  * This can be used on abstract classes as it does not create a temporary
1152  * instance.
1153  */
1154 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1155                                      ObjectClass *klass);
1156 
1157 /**
1158  * object_property_iter_next:
1159  * @iter: the iterator instance
1160  *
1161  * Return the next available property. If no further properties
1162  * are available, a %NULL value will be returned and the @iter
1163  * pointer should not be used again after this point without
1164  * re-initializing it.
1165  *
1166  * Returns: the next property, or %NULL when all properties
1167  * have been traversed.
1168  */
1169 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter);
1170 
1171 void object_unparent(Object *obj);
1172 
1173 /**
1174  * object_property_get:
1175  * @obj: the object
1176  * @v: the visitor that will receive the property value.  This should be an
1177  *   Output visitor and the data will be written with @name as the name.
1178  * @name: the name of the property
1179  * @errp: returns an error if this function fails
1180  *
1181  * Reads a property from a object.
1182  */
1183 void object_property_get(Object *obj, Visitor *v, const char *name,
1184                          Error **errp);
1185 
1186 /**
1187  * object_property_set_str:
1188  * @value: the value to be written to the property
1189  * @name: the name of the property
1190  * @errp: returns an error if this function fails
1191  *
1192  * Writes a string value to a property.
1193  */
1194 void object_property_set_str(Object *obj, const char *value,
1195                              const char *name, Error **errp);
1196 
1197 /**
1198  * object_property_get_str:
1199  * @obj: the object
1200  * @name: the name of the property
1201  * @errp: returns an error if this function fails
1202  *
1203  * Returns: the value of the property, converted to a C string, or NULL if
1204  * an error occurs (including when the property value is not a string).
1205  * The caller should free the string.
1206  */
1207 char *object_property_get_str(Object *obj, const char *name,
1208                               Error **errp);
1209 
1210 /**
1211  * object_property_set_link:
1212  * @value: the value to be written to the property
1213  * @name: the name of the property
1214  * @errp: returns an error if this function fails
1215  *
1216  * Writes an object's canonical path to a property.
1217  *
1218  * If the link property was created with
1219  * <code>OBJ_PROP_LINK_STRONG</code> bit, the old target object is
1220  * unreferenced, and a reference is added to the new target object.
1221  *
1222  */
1223 void object_property_set_link(Object *obj, Object *value,
1224                               const char *name, Error **errp);
1225 
1226 /**
1227  * object_property_get_link:
1228  * @obj: the object
1229  * @name: the name of the property
1230  * @errp: returns an error if this function fails
1231  *
1232  * Returns: the value of the property, resolved from a path to an Object,
1233  * or NULL if an error occurs (including when the property value is not a
1234  * string or not a valid object path).
1235  */
1236 Object *object_property_get_link(Object *obj, const char *name,
1237                                  Error **errp);
1238 
1239 /**
1240  * object_property_set_bool:
1241  * @value: the value to be written to the property
1242  * @name: the name of the property
1243  * @errp: returns an error if this function fails
1244  *
1245  * Writes a bool value to a property.
1246  */
1247 void object_property_set_bool(Object *obj, bool value,
1248                               const char *name, Error **errp);
1249 
1250 /**
1251  * object_property_get_bool:
1252  * @obj: the object
1253  * @name: the name of the property
1254  * @errp: returns an error if this function fails
1255  *
1256  * Returns: the value of the property, converted to a boolean, or NULL if
1257  * an error occurs (including when the property value is not a bool).
1258  */
1259 bool object_property_get_bool(Object *obj, const char *name,
1260                               Error **errp);
1261 
1262 /**
1263  * object_property_set_int:
1264  * @value: the value to be written to the property
1265  * @name: the name of the property
1266  * @errp: returns an error if this function fails
1267  *
1268  * Writes an integer value to a property.
1269  */
1270 void object_property_set_int(Object *obj, int64_t value,
1271                              const char *name, Error **errp);
1272 
1273 /**
1274  * object_property_get_int:
1275  * @obj: the object
1276  * @name: the name of the property
1277  * @errp: returns an error if this function fails
1278  *
1279  * Returns: the value of the property, converted to an integer, or negative if
1280  * an error occurs (including when the property value is not an integer).
1281  */
1282 int64_t object_property_get_int(Object *obj, const char *name,
1283                                 Error **errp);
1284 
1285 /**
1286  * object_property_set_uint:
1287  * @value: the value to be written to the property
1288  * @name: the name of the property
1289  * @errp: returns an error if this function fails
1290  *
1291  * Writes an unsigned integer value to a property.
1292  */
1293 void object_property_set_uint(Object *obj, uint64_t value,
1294                               const char *name, Error **errp);
1295 
1296 /**
1297  * object_property_get_uint:
1298  * @obj: the object
1299  * @name: the name of the property
1300  * @errp: returns an error if this function fails
1301  *
1302  * Returns: the value of the property, converted to an unsigned integer, or 0
1303  * an error occurs (including when the property value is not an integer).
1304  */
1305 uint64_t object_property_get_uint(Object *obj, const char *name,
1306                                   Error **errp);
1307 
1308 /**
1309  * object_property_get_enum:
1310  * @obj: the object
1311  * @name: the name of the property
1312  * @typename: the name of the enum data type
1313  * @errp: returns an error if this function fails
1314  *
1315  * Returns: the value of the property, converted to an integer, or
1316  * undefined if an error occurs (including when the property value is not
1317  * an enum).
1318  */
1319 int object_property_get_enum(Object *obj, const char *name,
1320                              const char *typename, Error **errp);
1321 
1322 /**
1323  * object_property_set:
1324  * @obj: the object
1325  * @v: the visitor that will be used to write the property value.  This should
1326  *   be an Input visitor and the data will be first read with @name as the
1327  *   name and then written as the property value.
1328  * @name: the name of the property
1329  * @errp: returns an error if this function fails
1330  *
1331  * Writes a property to a object.
1332  */
1333 void object_property_set(Object *obj, Visitor *v, const char *name,
1334                          Error **errp);
1335 
1336 /**
1337  * object_property_parse:
1338  * @obj: the object
1339  * @string: the string that will be used to parse the property value.
1340  * @name: the name of the property
1341  * @errp: returns an error if this function fails
1342  *
1343  * Parses a string and writes the result into a property of an object.
1344  */
1345 void object_property_parse(Object *obj, const char *string,
1346                            const char *name, Error **errp);
1347 
1348 /**
1349  * object_property_print:
1350  * @obj: the object
1351  * @name: the name of the property
1352  * @human: if true, print for human consumption
1353  * @errp: returns an error if this function fails
1354  *
1355  * Returns a string representation of the value of the property.  The
1356  * caller shall free the string.
1357  */
1358 char *object_property_print(Object *obj, const char *name, bool human,
1359                             Error **errp);
1360 
1361 /**
1362  * object_property_get_type:
1363  * @obj: the object
1364  * @name: the name of the property
1365  * @errp: returns an error if this function fails
1366  *
1367  * Returns:  The type name of the property.
1368  */
1369 const char *object_property_get_type(Object *obj, const char *name,
1370                                      Error **errp);
1371 
1372 /**
1373  * object_get_root:
1374  *
1375  * Returns: the root object of the composition tree
1376  */
1377 Object *object_get_root(void);
1378 
1379 
1380 /**
1381  * object_get_objects_root:
1382  *
1383  * Get the container object that holds user created
1384  * object instances. This is the object at path
1385  * "/objects"
1386  *
1387  * Returns: the user object container
1388  */
1389 Object *object_get_objects_root(void);
1390 
1391 /**
1392  * object_get_internal_root:
1393  *
1394  * Get the container object that holds internally used object
1395  * instances.  Any object which is put into this container must not be
1396  * user visible, and it will not be exposed in the QOM tree.
1397  *
1398  * Returns: the internal object container
1399  */
1400 Object *object_get_internal_root(void);
1401 
1402 /**
1403  * object_get_canonical_path_component:
1404  *
1405  * Returns: The final component in the object's canonical path.  The canonical
1406  * path is the path within the composition tree starting from the root.
1407  * %NULL if the object doesn't have a parent (and thus a canonical path).
1408  */
1409 char *object_get_canonical_path_component(Object *obj);
1410 
1411 /**
1412  * object_get_canonical_path:
1413  *
1414  * Returns: The canonical path for a object.  This is the path within the
1415  * composition tree starting from the root.
1416  */
1417 char *object_get_canonical_path(Object *obj);
1418 
1419 /**
1420  * object_resolve_path:
1421  * @path: the path to resolve
1422  * @ambiguous: returns true if the path resolution failed because of an
1423  *   ambiguous match
1424  *
1425  * There are two types of supported paths--absolute paths and partial paths.
1426  *
1427  * Absolute paths are derived from the root object and can follow child<> or
1428  * link<> properties.  Since they can follow link<> properties, they can be
1429  * arbitrarily long.  Absolute paths look like absolute filenames and are
1430  * prefixed with a leading slash.
1431  *
1432  * Partial paths look like relative filenames.  They do not begin with a
1433  * prefix.  The matching rules for partial paths are subtle but designed to make
1434  * specifying objects easy.  At each level of the composition tree, the partial
1435  * path is matched as an absolute path.  The first match is not returned.  At
1436  * least two matches are searched for.  A successful result is only returned if
1437  * only one match is found.  If more than one match is found, a flag is
1438  * returned to indicate that the match was ambiguous.
1439  *
1440  * Returns: The matched object or NULL on path lookup failure.
1441  */
1442 Object *object_resolve_path(const char *path, bool *ambiguous);
1443 
1444 /**
1445  * object_resolve_path_type:
1446  * @path: the path to resolve
1447  * @typename: the type to look for.
1448  * @ambiguous: returns true if the path resolution failed because of an
1449  *   ambiguous match
1450  *
1451  * This is similar to object_resolve_path.  However, when looking for a
1452  * partial path only matches that implement the given type are considered.
1453  * This restricts the search and avoids spuriously flagging matches as
1454  * ambiguous.
1455  *
1456  * For both partial and absolute paths, the return value goes through
1457  * a dynamic cast to @typename.  This is important if either the link,
1458  * or the typename itself are of interface types.
1459  *
1460  * Returns: The matched object or NULL on path lookup failure.
1461  */
1462 Object *object_resolve_path_type(const char *path, const char *typename,
1463                                  bool *ambiguous);
1464 
1465 /**
1466  * object_resolve_path_component:
1467  * @parent: the object in which to resolve the path
1468  * @part: the component to resolve.
1469  *
1470  * This is similar to object_resolve_path with an absolute path, but it
1471  * only resolves one element (@part) and takes the others from @parent.
1472  *
1473  * Returns: The resolved object or NULL on path lookup failure.
1474  */
1475 Object *object_resolve_path_component(Object *parent, const char *part);
1476 
1477 /**
1478  * object_property_add_child:
1479  * @obj: the object to add a property to
1480  * @name: the name of the property
1481  * @child: the child object
1482  *
1483  * Child properties form the composition tree.  All objects need to be a child
1484  * of another object.  Objects can only be a child of one object.
1485  *
1486  * There is no way for a child to determine what its parent is.  It is not
1487  * a bidirectional relationship.  This is by design.
1488  *
1489  * The value of a child property as a C string will be the child object's
1490  * canonical path. It can be retrieved using object_property_get_str().
1491  * The child object itself can be retrieved using object_property_get_link().
1492  *
1493  * Returns: The newly added property on success, or %NULL on failure.
1494  */
1495 ObjectProperty *object_property_add_child(Object *obj, const char *name,
1496                                           Object *child);
1497 
1498 typedef enum {
1499     /* Unref the link pointer when the property is deleted */
1500     OBJ_PROP_LINK_STRONG = 0x1,
1501 
1502     /* private */
1503     OBJ_PROP_LINK_DIRECT = 0x2,
1504     OBJ_PROP_LINK_CLASS = 0x4,
1505 } ObjectPropertyLinkFlags;
1506 
1507 /**
1508  * object_property_allow_set_link:
1509  *
1510  * The default implementation of the object_property_add_link() check()
1511  * callback function.  It allows the link property to be set and never returns
1512  * an error.
1513  */
1514 void object_property_allow_set_link(const Object *, const char *,
1515                                     Object *, Error **);
1516 
1517 /**
1518  * object_property_add_link:
1519  * @obj: the object to add a property to
1520  * @name: the name of the property
1521  * @type: the qobj type of the link
1522  * @targetp: a pointer to where the link object reference is stored
1523  * @check: callback to veto setting or NULL if the property is read-only
1524  * @flags: additional options for the link
1525  *
1526  * Links establish relationships between objects.  Links are unidirectional
1527  * although two links can be combined to form a bidirectional relationship
1528  * between objects.
1529  *
1530  * Links form the graph in the object model.
1531  *
1532  * The <code>@check()</code> callback is invoked when
1533  * object_property_set_link() is called and can raise an error to prevent the
1534  * link being set.  If <code>@check</code> is NULL, the property is read-only
1535  * and cannot be set.
1536  *
1537  * Ownership of the pointer that @child points to is transferred to the
1538  * link property.  The reference count for <code>*@child</code> is
1539  * managed by the property from after the function returns till the
1540  * property is deleted with object_property_del().  If the
1541  * <code>@flags</code> <code>OBJ_PROP_LINK_STRONG</code> bit is set,
1542  * the reference count is decremented when the property is deleted or
1543  * modified.
1544  *
1545  * Returns: The newly added property on success, or %NULL on failure.
1546  */
1547 ObjectProperty *object_property_add_link(Object *obj, const char *name,
1548                               const char *type, Object **targetp,
1549                               void (*check)(const Object *obj, const char *name,
1550                                             Object *val, Error **errp),
1551                               ObjectPropertyLinkFlags flags);
1552 
1553 ObjectProperty *object_class_property_add_link(ObjectClass *oc,
1554                               const char *name,
1555                               const char *type, ptrdiff_t offset,
1556                               void (*check)(const Object *obj, const char *name,
1557                                             Object *val, Error **errp),
1558                               ObjectPropertyLinkFlags flags);
1559 
1560 /**
1561  * object_property_add_str:
1562  * @obj: the object to add a property to
1563  * @name: the name of the property
1564  * @get: the getter or NULL if the property is write-only.  This function must
1565  *   return a string to be freed by g_free().
1566  * @set: the setter or NULL if the property is read-only
1567  *
1568  * Add a string property using getters/setters.  This function will add a
1569  * property of type 'string'.
1570  *
1571  * Returns: The newly added property on success, or %NULL on failure.
1572  */
1573 ObjectProperty *object_property_add_str(Object *obj, const char *name,
1574                              char *(*get)(Object *, Error **),
1575                              void (*set)(Object *, const char *, Error **));
1576 
1577 ObjectProperty *object_class_property_add_str(ObjectClass *klass,
1578                                    const char *name,
1579                                    char *(*get)(Object *, Error **),
1580                                    void (*set)(Object *, const char *,
1581                                                Error **));
1582 
1583 /**
1584  * object_property_add_bool:
1585  * @obj: the object to add a property to
1586  * @name: the name of the property
1587  * @get: the getter or NULL if the property is write-only.
1588  * @set: the setter or NULL if the property is read-only
1589  *
1590  * Add a bool property using getters/setters.  This function will add a
1591  * property of type 'bool'.
1592  *
1593  * Returns: The newly added property on success, or %NULL on failure.
1594  */
1595 ObjectProperty *object_property_add_bool(Object *obj, const char *name,
1596                               bool (*get)(Object *, Error **),
1597                               void (*set)(Object *, bool, Error **));
1598 
1599 ObjectProperty *object_class_property_add_bool(ObjectClass *klass,
1600                                     const char *name,
1601                                     bool (*get)(Object *, Error **),
1602                                     void (*set)(Object *, bool, Error **));
1603 
1604 /**
1605  * object_property_add_enum:
1606  * @obj: the object to add a property to
1607  * @name: the name of the property
1608  * @typename: the name of the enum data type
1609  * @get: the getter or %NULL if the property is write-only.
1610  * @set: the setter or %NULL if the property is read-only
1611  *
1612  * Add an enum property using getters/setters.  This function will add a
1613  * property of type '@typename'.
1614  *
1615  * Returns: The newly added property on success, or %NULL on failure.
1616  */
1617 ObjectProperty *object_property_add_enum(Object *obj, const char *name,
1618                               const char *typename,
1619                               const QEnumLookup *lookup,
1620                               int (*get)(Object *, Error **),
1621                               void (*set)(Object *, int, Error **));
1622 
1623 ObjectProperty *object_class_property_add_enum(ObjectClass *klass,
1624                                     const char *name,
1625                                     const char *typename,
1626                                     const QEnumLookup *lookup,
1627                                     int (*get)(Object *, Error **),
1628                                     void (*set)(Object *, int, Error **));
1629 
1630 /**
1631  * object_property_add_tm:
1632  * @obj: the object to add a property to
1633  * @name: the name of the property
1634  * @get: the getter or NULL if the property is write-only.
1635  *
1636  * Add a read-only struct tm valued property using a getter function.
1637  * This function will add a property of type 'struct tm'.
1638  *
1639  * Returns: The newly added property on success, or %NULL on failure.
1640  */
1641 ObjectProperty *object_property_add_tm(Object *obj, const char *name,
1642                             void (*get)(Object *, struct tm *, Error **));
1643 
1644 ObjectProperty *object_class_property_add_tm(ObjectClass *klass,
1645                             const char *name,
1646                             void (*get)(Object *, struct tm *, Error **));
1647 
1648 typedef enum {
1649     /* Automatically add a getter to the property */
1650     OBJ_PROP_FLAG_READ = 1 << 0,
1651     /* Automatically add a setter to the property */
1652     OBJ_PROP_FLAG_WRITE = 1 << 1,
1653     /* Automatically add a getter and a setter to the property */
1654     OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE),
1655 } ObjectPropertyFlags;
1656 
1657 /**
1658  * object_property_add_uint8_ptr:
1659  * @obj: the object to add a property to
1660  * @name: the name of the property
1661  * @v: pointer to value
1662  * @flags: bitwise-or'd ObjectPropertyFlags
1663  *
1664  * Add an integer property in memory.  This function will add a
1665  * property of type 'uint8'.
1666  *
1667  * Returns: The newly added property on success, or %NULL on failure.
1668  */
1669 ObjectProperty *object_property_add_uint8_ptr(Object *obj, const char *name,
1670                                               const uint8_t *v,
1671                                               ObjectPropertyFlags flags);
1672 
1673 ObjectProperty *object_class_property_add_uint8_ptr(ObjectClass *klass,
1674                                          const char *name,
1675                                          const uint8_t *v,
1676                                          ObjectPropertyFlags flags);
1677 
1678 /**
1679  * object_property_add_uint16_ptr:
1680  * @obj: the object to add a property to
1681  * @name: the name of the property
1682  * @v: pointer to value
1683  * @flags: bitwise-or'd ObjectPropertyFlags
1684  *
1685  * Add an integer property in memory.  This function will add a
1686  * property of type 'uint16'.
1687  *
1688  * Returns: The newly added property on success, or %NULL on failure.
1689  */
1690 ObjectProperty *object_property_add_uint16_ptr(Object *obj, const char *name,
1691                                     const uint16_t *v,
1692                                     ObjectPropertyFlags flags);
1693 
1694 ObjectProperty *object_class_property_add_uint16_ptr(ObjectClass *klass,
1695                                           const char *name,
1696                                           const uint16_t *v,
1697                                           ObjectPropertyFlags flags);
1698 
1699 /**
1700  * object_property_add_uint32_ptr:
1701  * @obj: the object to add a property to
1702  * @name: the name of the property
1703  * @v: pointer to value
1704  * @flags: bitwise-or'd ObjectPropertyFlags
1705  *
1706  * Add an integer property in memory.  This function will add a
1707  * property of type 'uint32'.
1708  *
1709  * Returns: The newly added property on success, or %NULL on failure.
1710  */
1711 ObjectProperty *object_property_add_uint32_ptr(Object *obj, const char *name,
1712                                     const uint32_t *v,
1713                                     ObjectPropertyFlags flags);
1714 
1715 ObjectProperty *object_class_property_add_uint32_ptr(ObjectClass *klass,
1716                                           const char *name,
1717                                           const uint32_t *v,
1718                                           ObjectPropertyFlags flags);
1719 
1720 /**
1721  * object_property_add_uint64_ptr:
1722  * @obj: the object to add a property to
1723  * @name: the name of the property
1724  * @v: pointer to value
1725  * @flags: bitwise-or'd ObjectPropertyFlags
1726  *
1727  * Add an integer property in memory.  This function will add a
1728  * property of type 'uint64'.
1729  *
1730  * Returns: The newly added property on success, or %NULL on failure.
1731  */
1732 ObjectProperty *object_property_add_uint64_ptr(Object *obj, const char *name,
1733                                     const uint64_t *v,
1734                                     ObjectPropertyFlags flags);
1735 
1736 ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass,
1737                                           const char *name,
1738                                           const uint64_t *v,
1739                                           ObjectPropertyFlags flags);
1740 
1741 /**
1742  * object_property_add_alias:
1743  * @obj: the object to add a property to
1744  * @name: the name of the property
1745  * @target_obj: the object to forward property access to
1746  * @target_name: the name of the property on the forwarded object
1747  *
1748  * Add an alias for a property on an object.  This function will add a property
1749  * of the same type as the forwarded property.
1750  *
1751  * The caller must ensure that <code>@target_obj</code> stays alive as long as
1752  * this property exists.  In the case of a child object or an alias on the same
1753  * object this will be the case.  For aliases to other objects the caller is
1754  * responsible for taking a reference.
1755  *
1756  * Returns: The newly added property on success, or %NULL on failure.
1757  */
1758 ObjectProperty *object_property_add_alias(Object *obj, const char *name,
1759                                Object *target_obj, const char *target_name);
1760 
1761 /**
1762  * object_property_add_const_link:
1763  * @obj: the object to add a property to
1764  * @name: the name of the property
1765  * @target: the object to be referred by the link
1766  *
1767  * Add an unmodifiable link for a property on an object.  This function will
1768  * add a property of type link<TYPE> where TYPE is the type of @target.
1769  *
1770  * The caller must ensure that @target stays alive as long as
1771  * this property exists.  In the case @target is a child of @obj,
1772  * this will be the case.  Otherwise, the caller is responsible for
1773  * taking a reference.
1774  *
1775  * Returns: The newly added property on success, or %NULL on failure.
1776  */
1777 ObjectProperty *object_property_add_const_link(Object *obj, const char *name,
1778                                                Object *target);
1779 
1780 /**
1781  * object_property_set_description:
1782  * @obj: the object owning the property
1783  * @name: the name of the property
1784  * @description: the description of the property on the object
1785  *
1786  * Set an object property's description.
1787  *
1788  */
1789 void object_property_set_description(Object *obj, const char *name,
1790                                      const char *description);
1791 void object_class_property_set_description(ObjectClass *klass, const char *name,
1792                                            const char *description);
1793 
1794 /**
1795  * object_child_foreach:
1796  * @obj: the object whose children will be navigated
1797  * @fn: the iterator function to be called
1798  * @opaque: an opaque value that will be passed to the iterator
1799  *
1800  * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1801  * non-zero.
1802  *
1803  * It is forbidden to add or remove children from @obj from the @fn
1804  * callback.
1805  *
1806  * Returns: The last value returned by @fn, or 0 if there is no child.
1807  */
1808 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1809                          void *opaque);
1810 
1811 /**
1812  * object_child_foreach_recursive:
1813  * @obj: the object whose children will be navigated
1814  * @fn: the iterator function to be called
1815  * @opaque: an opaque value that will be passed to the iterator
1816  *
1817  * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1818  * non-zero. Calls recursively, all child nodes of @obj will also be passed
1819  * all the way down to the leaf nodes of the tree. Depth first ordering.
1820  *
1821  * It is forbidden to add or remove children from @obj (or its
1822  * child nodes) from the @fn callback.
1823  *
1824  * Returns: The last value returned by @fn, or 0 if there is no child.
1825  */
1826 int object_child_foreach_recursive(Object *obj,
1827                                    int (*fn)(Object *child, void *opaque),
1828                                    void *opaque);
1829 /**
1830  * container_get:
1831  * @root: root of the #path, e.g., object_get_root()
1832  * @path: path to the container
1833  *
1834  * Return a container object whose path is @path.  Create more containers
1835  * along the path if necessary.
1836  *
1837  * Returns: the container object.
1838  */
1839 Object *container_get(Object *root, const char *path);
1840 
1841 /**
1842  * object_type_get_instance_size:
1843  * @typename: Name of the Type whose instance_size is required
1844  *
1845  * Returns the instance_size of the given @typename.
1846  */
1847 size_t object_type_get_instance_size(const char *typename);
1848 
1849 /**
1850  * object_property_help:
1851  * @name: the name of the property
1852  * @type: the type of the property
1853  * @defval: the default value
1854  * @description: description of the property
1855  *
1856  * Returns: a user-friendly formatted string describing the property
1857  * for help purposes.
1858  */
1859 char *object_property_help(const char *name, const char *type,
1860                            QObject *defval, const char *description);
1861 
1862 G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref)
1863 
1864 #endif
1865