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