xref: /qemu/include/qom/object.h (revision a976a99a)
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_int:
1098  * @prop: the property to set
1099  * @value: the value to be written to the property
1100  *
1101  * Set the property default value.
1102  */
1103 void object_property_set_default_int(ObjectProperty *prop, int64_t value);
1104 
1105 /**
1106  * object_property_set_default_uint:
1107  * @prop: the property to set
1108  * @value: the value to be written to the property
1109  *
1110  * Set the property default value.
1111  */
1112 void object_property_set_default_uint(ObjectProperty *prop, uint64_t value);
1113 
1114 /**
1115  * object_property_find:
1116  * @obj: the object
1117  * @name: the name of the property
1118  *
1119  * Look up a property for an object.
1120  *
1121  * Return its #ObjectProperty if found, or NULL.
1122  */
1123 ObjectProperty *object_property_find(Object *obj, const char *name);
1124 
1125 /**
1126  * object_property_find_err:
1127  * @obj: the object
1128  * @name: the name of the property
1129  * @errp: returns an error if this function fails
1130  *
1131  * Look up a property for an object.
1132  *
1133  * Return its #ObjectProperty if found, or NULL.
1134  */
1135 ObjectProperty *object_property_find_err(Object *obj,
1136                                          const char *name,
1137                                          Error **errp);
1138 
1139 /**
1140  * object_class_property_find:
1141  * @klass: the object class
1142  * @name: the name of the property
1143  *
1144  * Look up a property for an object class.
1145  *
1146  * Return its #ObjectProperty if found, or NULL.
1147  */
1148 ObjectProperty *object_class_property_find(ObjectClass *klass,
1149                                            const char *name);
1150 
1151 /**
1152  * object_class_property_find_err:
1153  * @klass: the object class
1154  * @name: the name of the property
1155  * @errp: returns an error if this function fails
1156  *
1157  * Look up a property for an object class.
1158  *
1159  * Return its #ObjectProperty if found, or NULL.
1160  */
1161 ObjectProperty *object_class_property_find_err(ObjectClass *klass,
1162                                                const char *name,
1163                                                Error **errp);
1164 
1165 typedef struct ObjectPropertyIterator {
1166     ObjectClass *nextclass;
1167     GHashTableIter iter;
1168 } ObjectPropertyIterator;
1169 
1170 /**
1171  * object_property_iter_init:
1172  * @iter: the iterator instance
1173  * @obj: the object
1174  *
1175  * Initializes an iterator for traversing all properties
1176  * registered against an object instance, its class and all parent classes.
1177  *
1178  * It is forbidden to modify the property list while iterating,
1179  * whether removing or adding properties.
1180  *
1181  * Typical usage pattern would be
1182  *
1183  * .. code-block:: c
1184  *    :caption: Using object property iterators
1185  *
1186  *      ObjectProperty *prop;
1187  *      ObjectPropertyIterator iter;
1188  *
1189  *      object_property_iter_init(&iter, obj);
1190  *      while ((prop = object_property_iter_next(&iter))) {
1191  *        ... do something with prop ...
1192  *      }
1193  */
1194 void object_property_iter_init(ObjectPropertyIterator *iter,
1195                                Object *obj);
1196 
1197 /**
1198  * object_class_property_iter_init:
1199  * @iter: the iterator instance
1200  * @klass: the class
1201  *
1202  * Initializes an iterator for traversing all properties
1203  * registered against an object class and all parent classes.
1204  *
1205  * It is forbidden to modify the property list while iterating,
1206  * whether removing or adding properties.
1207  *
1208  * This can be used on abstract classes as it does not create a temporary
1209  * instance.
1210  */
1211 void object_class_property_iter_init(ObjectPropertyIterator *iter,
1212                                      ObjectClass *klass);
1213 
1214 /**
1215  * object_property_iter_next:
1216  * @iter: the iterator instance
1217  *
1218  * Return the next available property. If no further properties
1219  * are available, a %NULL value will be returned and the @iter
1220  * pointer should not be used again after this point without
1221  * re-initializing it.
1222  *
1223  * Returns: the next property, or %NULL when all properties
1224  * have been traversed.
1225  */
1226 ObjectProperty *object_property_iter_next(ObjectPropertyIterator *iter);
1227 
1228 void object_unparent(Object *obj);
1229 
1230 /**
1231  * object_property_get:
1232  * @obj: the object
1233  * @name: the name of the property
1234  * @v: the visitor that will receive the property value.  This should be an
1235  *   Output visitor and the data will be written with @name as the name.
1236  * @errp: returns an error if this function fails
1237  *
1238  * Reads a property from a object.
1239  *
1240  * Returns: %true on success, %false on failure.
1241  */
1242 bool object_property_get(Object *obj, const char *name, Visitor *v,
1243                          Error **errp);
1244 
1245 /**
1246  * object_property_set_str:
1247  * @obj: the object
1248  * @name: the name of the property
1249  * @value: the value to be written to the property
1250  * @errp: returns an error if this function fails
1251  *
1252  * Writes a string value to a property.
1253  *
1254  * Returns: %true on success, %false on failure.
1255  */
1256 bool object_property_set_str(Object *obj, const char *name,
1257                              const char *value, Error **errp);
1258 
1259 /**
1260  * object_property_get_str:
1261  * @obj: the object
1262  * @name: the name of the property
1263  * @errp: returns an error if this function fails
1264  *
1265  * Returns: the value of the property, converted to a C string, or NULL if
1266  * an error occurs (including when the property value is not a string).
1267  * The caller should free the string.
1268  */
1269 char *object_property_get_str(Object *obj, const char *name,
1270                               Error **errp);
1271 
1272 /**
1273  * object_property_set_link:
1274  * @obj: the object
1275  * @name: the name of the property
1276  * @value: the value to be written to the property
1277  * @errp: returns an error if this function fails
1278  *
1279  * Writes an object's canonical path to a property.
1280  *
1281  * If the link property was created with
1282  * %OBJ_PROP_LINK_STRONG bit, the old target object is
1283  * unreferenced, and a reference is added to the new target object.
1284  *
1285  * Returns: %true on success, %false on failure.
1286  */
1287 bool object_property_set_link(Object *obj, const char *name,
1288                               Object *value, Error **errp);
1289 
1290 /**
1291  * object_property_get_link:
1292  * @obj: the object
1293  * @name: the name of the property
1294  * @errp: returns an error if this function fails
1295  *
1296  * Returns: the value of the property, resolved from a path to an Object,
1297  * or NULL if an error occurs (including when the property value is not a
1298  * string or not a valid object path).
1299  */
1300 Object *object_property_get_link(Object *obj, const char *name,
1301                                  Error **errp);
1302 
1303 /**
1304  * object_property_set_bool:
1305  * @obj: the object
1306  * @name: the name of the property
1307  * @value: the value to be written to the property
1308  * @errp: returns an error if this function fails
1309  *
1310  * Writes a bool value to a property.
1311  *
1312  * Returns: %true on success, %false on failure.
1313  */
1314 bool object_property_set_bool(Object *obj, const char *name,
1315                               bool value, Error **errp);
1316 
1317 /**
1318  * object_property_get_bool:
1319  * @obj: the object
1320  * @name: the name of the property
1321  * @errp: returns an error if this function fails
1322  *
1323  * Returns: the value of the property, converted to a boolean, or false if
1324  * an error occurs (including when the property value is not a bool).
1325  */
1326 bool object_property_get_bool(Object *obj, const char *name,
1327                               Error **errp);
1328 
1329 /**
1330  * object_property_set_int:
1331  * @obj: the object
1332  * @name: the name of the property
1333  * @value: the value to be written to the property
1334  * @errp: returns an error if this function fails
1335  *
1336  * Writes an integer value to a property.
1337  *
1338  * Returns: %true on success, %false on failure.
1339  */
1340 bool object_property_set_int(Object *obj, const char *name,
1341                              int64_t value, Error **errp);
1342 
1343 /**
1344  * object_property_get_int:
1345  * @obj: the object
1346  * @name: the name of the property
1347  * @errp: returns an error if this function fails
1348  *
1349  * Returns: the value of the property, converted to an integer, or -1 if
1350  * an error occurs (including when the property value is not an integer).
1351  */
1352 int64_t object_property_get_int(Object *obj, const char *name,
1353                                 Error **errp);
1354 
1355 /**
1356  * object_property_set_uint:
1357  * @obj: the object
1358  * @name: the name of the property
1359  * @value: the value to be written to the property
1360  * @errp: returns an error if this function fails
1361  *
1362  * Writes an unsigned integer value to a property.
1363  *
1364  * Returns: %true on success, %false on failure.
1365  */
1366 bool object_property_set_uint(Object *obj, const char *name,
1367                               uint64_t value, Error **errp);
1368 
1369 /**
1370  * object_property_get_uint:
1371  * @obj: the object
1372  * @name: the name of the property
1373  * @errp: returns an error if this function fails
1374  *
1375  * Returns: the value of the property, converted to an unsigned integer, or 0
1376  * an error occurs (including when the property value is not an integer).
1377  */
1378 uint64_t object_property_get_uint(Object *obj, const char *name,
1379                                   Error **errp);
1380 
1381 /**
1382  * object_property_get_enum:
1383  * @obj: the object
1384  * @name: the name of the property
1385  * @typename: the name of the enum data type
1386  * @errp: returns an error if this function fails
1387  *
1388  * Returns: the value of the property, converted to an integer (which
1389  * can't be negative), or -1 on error (including when the property
1390  * value is not an enum).
1391  */
1392 int object_property_get_enum(Object *obj, const char *name,
1393                              const char *typename, Error **errp);
1394 
1395 /**
1396  * object_property_set:
1397  * @obj: the object
1398  * @name: the name of the property
1399  * @v: the visitor that will be used to write the property value.  This should
1400  *   be an Input visitor and the data will be first read with @name as the
1401  *   name and then written as the property value.
1402  * @errp: returns an error if this function fails
1403  *
1404  * Writes a property to a object.
1405  *
1406  * Returns: %true on success, %false on failure.
1407  */
1408 bool object_property_set(Object *obj, const char *name, Visitor *v,
1409                          Error **errp);
1410 
1411 /**
1412  * object_property_parse:
1413  * @obj: the object
1414  * @name: the name of the property
1415  * @string: the string that will be used to parse the property value.
1416  * @errp: returns an error if this function fails
1417  *
1418  * Parses a string and writes the result into a property of an object.
1419  *
1420  * Returns: %true on success, %false on failure.
1421  */
1422 bool object_property_parse(Object *obj, const char *name,
1423                            const char *string, Error **errp);
1424 
1425 /**
1426  * object_property_print:
1427  * @obj: the object
1428  * @name: the name of the property
1429  * @human: if true, print for human consumption
1430  * @errp: returns an error if this function fails
1431  *
1432  * Returns a string representation of the value of the property.  The
1433  * caller shall free the string.
1434  */
1435 char *object_property_print(Object *obj, const char *name, bool human,
1436                             Error **errp);
1437 
1438 /**
1439  * object_property_get_type:
1440  * @obj: the object
1441  * @name: the name of the property
1442  * @errp: returns an error if this function fails
1443  *
1444  * Returns:  The type name of the property.
1445  */
1446 const char *object_property_get_type(Object *obj, const char *name,
1447                                      Error **errp);
1448 
1449 /**
1450  * object_get_root:
1451  *
1452  * Returns: the root object of the composition tree
1453  */
1454 Object *object_get_root(void);
1455 
1456 
1457 /**
1458  * object_get_objects_root:
1459  *
1460  * Get the container object that holds user created
1461  * object instances. This is the object at path
1462  * "/objects"
1463  *
1464  * Returns: the user object container
1465  */
1466 Object *object_get_objects_root(void);
1467 
1468 /**
1469  * object_get_internal_root:
1470  *
1471  * Get the container object that holds internally used object
1472  * instances.  Any object which is put into this container must not be
1473  * user visible, and it will not be exposed in the QOM tree.
1474  *
1475  * Returns: the internal object container
1476  */
1477 Object *object_get_internal_root(void);
1478 
1479 /**
1480  * object_get_canonical_path_component:
1481  * @obj: the object
1482  *
1483  * Returns: The final component in the object's canonical path.  The canonical
1484  * path is the path within the composition tree starting from the root.
1485  * %NULL if the object doesn't have a parent (and thus a canonical path).
1486  */
1487 const char *object_get_canonical_path_component(const Object *obj);
1488 
1489 /**
1490  * object_get_canonical_path:
1491  * @obj: the object
1492  *
1493  * Returns: The canonical path for a object, newly allocated.  This is
1494  * the path within the composition tree starting from the root.  Use
1495  * g_free() to free it.
1496  */
1497 char *object_get_canonical_path(const Object *obj);
1498 
1499 /**
1500  * object_resolve_path:
1501  * @path: the path to resolve
1502  * @ambiguous: returns true if the path resolution failed because of an
1503  *   ambiguous match
1504  *
1505  * There are two types of supported paths--absolute paths and partial paths.
1506  *
1507  * Absolute paths are derived from the root object and can follow child<> or
1508  * link<> properties.  Since they can follow link<> properties, they can be
1509  * arbitrarily long.  Absolute paths look like absolute filenames and are
1510  * prefixed with a leading slash.
1511  *
1512  * Partial paths look like relative filenames.  They do not begin with a
1513  * prefix.  The matching rules for partial paths are subtle but designed to make
1514  * specifying objects easy.  At each level of the composition tree, the partial
1515  * path is matched as an absolute path.  The first match is not returned.  At
1516  * least two matches are searched for.  A successful result is only returned if
1517  * only one match is found.  If more than one match is found, a flag is
1518  * returned to indicate that the match was ambiguous.
1519  *
1520  * Returns: The matched object or NULL on path lookup failure.
1521  */
1522 Object *object_resolve_path(const char *path, bool *ambiguous);
1523 
1524 /**
1525  * object_resolve_path_type:
1526  * @path: the path to resolve
1527  * @typename: the type to look for.
1528  * @ambiguous: returns true if the path resolution failed because of an
1529  *   ambiguous match
1530  *
1531  * This is similar to object_resolve_path.  However, when looking for a
1532  * partial path only matches that implement the given type are considered.
1533  * This restricts the search and avoids spuriously flagging matches as
1534  * ambiguous.
1535  *
1536  * For both partial and absolute paths, the return value goes through
1537  * a dynamic cast to @typename.  This is important if either the link,
1538  * or the typename itself are of interface types.
1539  *
1540  * Returns: The matched object or NULL on path lookup failure.
1541  */
1542 Object *object_resolve_path_type(const char *path, const char *typename,
1543                                  bool *ambiguous);
1544 
1545 /**
1546  * object_resolve_path_at:
1547  * @parent: the object in which to resolve the path
1548  * @path: the path to resolve
1549  *
1550  * This is like object_resolve_path(), except paths not starting with
1551  * a slash are relative to @parent.
1552  *
1553  * Returns: The resolved object or NULL on path lookup failure.
1554  */
1555 Object *object_resolve_path_at(Object *parent, const char *path);
1556 
1557 /**
1558  * object_resolve_path_component:
1559  * @parent: the object in which to resolve the path
1560  * @part: the component to resolve.
1561  *
1562  * This is similar to object_resolve_path with an absolute path, but it
1563  * only resolves one element (@part) and takes the others from @parent.
1564  *
1565  * Returns: The resolved object or NULL on path lookup failure.
1566  */
1567 Object *object_resolve_path_component(Object *parent, const char *part);
1568 
1569 /**
1570  * object_property_try_add_child:
1571  * @obj: the object to add a property to
1572  * @name: the name of the property
1573  * @child: the child object
1574  * @errp: pointer to error object
1575  *
1576  * Child properties form the composition tree.  All objects need to be a child
1577  * of another object.  Objects can only be a child of one object.
1578  *
1579  * There is no way for a child to determine what its parent is.  It is not
1580  * a bidirectional relationship.  This is by design.
1581  *
1582  * The value of a child property as a C string will be the child object's
1583  * canonical path. It can be retrieved using object_property_get_str().
1584  * The child object itself can be retrieved using object_property_get_link().
1585  *
1586  * Returns: The newly added property on success, or %NULL on failure.
1587  */
1588 ObjectProperty *object_property_try_add_child(Object *obj, const char *name,
1589                                               Object *child, Error **errp);
1590 
1591 /**
1592  * object_property_add_child:
1593  * @obj: the object to add a property to
1594  * @name: the name of the property
1595  * @child: the child object
1596  *
1597  * Same as object_property_try_add_child() with @errp hardcoded to
1598  * &error_abort
1599  */
1600 ObjectProperty *object_property_add_child(Object *obj, const char *name,
1601                                           Object *child);
1602 
1603 typedef enum {
1604     /* Unref the link pointer when the property is deleted */
1605     OBJ_PROP_LINK_STRONG = 0x1,
1606 
1607     /* private */
1608     OBJ_PROP_LINK_DIRECT = 0x2,
1609     OBJ_PROP_LINK_CLASS = 0x4,
1610 } ObjectPropertyLinkFlags;
1611 
1612 /**
1613  * object_property_allow_set_link:
1614  * @obj: the object to add a property to
1615  * @name: the name of the property
1616  * @child: the child object
1617  * @errp: pointer to error object
1618  *
1619  * The default implementation of the object_property_add_link() check()
1620  * callback function.  It allows the link property to be set and never returns
1621  * an error.
1622  */
1623 void object_property_allow_set_link(const Object *obj, const char *name,
1624                                     Object *child, Error **errp);
1625 
1626 /**
1627  * object_property_add_link:
1628  * @obj: the object to add a property to
1629  * @name: the name of the property
1630  * @type: the qobj type of the link
1631  * @targetp: a pointer to where the link object reference is stored
1632  * @check: callback to veto setting or NULL if the property is read-only
1633  * @flags: additional options for the link
1634  *
1635  * Links establish relationships between objects.  Links are unidirectional
1636  * although two links can be combined to form a bidirectional relationship
1637  * between objects.
1638  *
1639  * Links form the graph in the object model.
1640  *
1641  * The @check() callback is invoked when
1642  * object_property_set_link() is called and can raise an error to prevent the
1643  * link being set.  If @check is NULL, the property is read-only
1644  * and cannot be set.
1645  *
1646  * Ownership of the pointer that @child points to is transferred to the
1647  * link property.  The reference count for *@child is
1648  * managed by the property from after the function returns till the
1649  * property is deleted with object_property_del().  If the
1650  * @flags %OBJ_PROP_LINK_STRONG bit is set,
1651  * the reference count is decremented when the property is deleted or
1652  * modified.
1653  *
1654  * Returns: The newly added property on success, or %NULL on failure.
1655  */
1656 ObjectProperty *object_property_add_link(Object *obj, const char *name,
1657                               const char *type, Object **targetp,
1658                               void (*check)(const Object *obj, const char *name,
1659                                             Object *val, Error **errp),
1660                               ObjectPropertyLinkFlags flags);
1661 
1662 ObjectProperty *object_class_property_add_link(ObjectClass *oc,
1663                               const char *name,
1664                               const char *type, ptrdiff_t offset,
1665                               void (*check)(const Object *obj, const char *name,
1666                                             Object *val, Error **errp),
1667                               ObjectPropertyLinkFlags flags);
1668 
1669 /**
1670  * object_property_add_str:
1671  * @obj: the object to add a property to
1672  * @name: the name of the property
1673  * @get: the getter or NULL if the property is write-only.  This function must
1674  *   return a string to be freed by g_free().
1675  * @set: the setter or NULL if the property is read-only
1676  *
1677  * Add a string property using getters/setters.  This function will add a
1678  * property of type 'string'.
1679  *
1680  * Returns: The newly added property on success, or %NULL on failure.
1681  */
1682 ObjectProperty *object_property_add_str(Object *obj, const char *name,
1683                              char *(*get)(Object *, Error **),
1684                              void (*set)(Object *, const char *, Error **));
1685 
1686 ObjectProperty *object_class_property_add_str(ObjectClass *klass,
1687                                    const char *name,
1688                                    char *(*get)(Object *, Error **),
1689                                    void (*set)(Object *, const char *,
1690                                                Error **));
1691 
1692 /**
1693  * object_property_add_bool:
1694  * @obj: the object to add a property to
1695  * @name: the name of the property
1696  * @get: the getter or NULL if the property is write-only.
1697  * @set: the setter or NULL if the property is read-only
1698  *
1699  * Add a bool property using getters/setters.  This function will add a
1700  * property of type 'bool'.
1701  *
1702  * Returns: The newly added property on success, or %NULL on failure.
1703  */
1704 ObjectProperty *object_property_add_bool(Object *obj, const char *name,
1705                               bool (*get)(Object *, Error **),
1706                               void (*set)(Object *, bool, Error **));
1707 
1708 ObjectProperty *object_class_property_add_bool(ObjectClass *klass,
1709                                     const char *name,
1710                                     bool (*get)(Object *, Error **),
1711                                     void (*set)(Object *, bool, Error **));
1712 
1713 /**
1714  * object_property_add_enum:
1715  * @obj: the object to add a property to
1716  * @name: the name of the property
1717  * @typename: the name of the enum data type
1718  * @lookup: enum value namelookup table
1719  * @get: the getter or %NULL if the property is write-only.
1720  * @set: the setter or %NULL if the property is read-only
1721  *
1722  * Add an enum property using getters/setters.  This function will add a
1723  * property of type '@typename'.
1724  *
1725  * Returns: The newly added property on success, or %NULL on failure.
1726  */
1727 ObjectProperty *object_property_add_enum(Object *obj, const char *name,
1728                               const char *typename,
1729                               const QEnumLookup *lookup,
1730                               int (*get)(Object *, Error **),
1731                               void (*set)(Object *, int, Error **));
1732 
1733 ObjectProperty *object_class_property_add_enum(ObjectClass *klass,
1734                                     const char *name,
1735                                     const char *typename,
1736                                     const QEnumLookup *lookup,
1737                                     int (*get)(Object *, Error **),
1738                                     void (*set)(Object *, int, Error **));
1739 
1740 /**
1741  * object_property_add_tm:
1742  * @obj: the object to add a property to
1743  * @name: the name of the property
1744  * @get: the getter or NULL if the property is write-only.
1745  *
1746  * Add a read-only struct tm valued property using a getter function.
1747  * This function will add a property of type 'struct tm'.
1748  *
1749  * Returns: The newly added property on success, or %NULL on failure.
1750  */
1751 ObjectProperty *object_property_add_tm(Object *obj, const char *name,
1752                             void (*get)(Object *, struct tm *, Error **));
1753 
1754 ObjectProperty *object_class_property_add_tm(ObjectClass *klass,
1755                             const char *name,
1756                             void (*get)(Object *, struct tm *, Error **));
1757 
1758 typedef enum {
1759     /* Automatically add a getter to the property */
1760     OBJ_PROP_FLAG_READ = 1 << 0,
1761     /* Automatically add a setter to the property */
1762     OBJ_PROP_FLAG_WRITE = 1 << 1,
1763     /* Automatically add a getter and a setter to the property */
1764     OBJ_PROP_FLAG_READWRITE = (OBJ_PROP_FLAG_READ | OBJ_PROP_FLAG_WRITE),
1765 } ObjectPropertyFlags;
1766 
1767 /**
1768  * object_property_add_uint8_ptr:
1769  * @obj: the object to add a property to
1770  * @name: the name of the property
1771  * @v: pointer to value
1772  * @flags: bitwise-or'd ObjectPropertyFlags
1773  *
1774  * Add an integer property in memory.  This function will add a
1775  * property of type 'uint8'.
1776  *
1777  * Returns: The newly added property on success, or %NULL on failure.
1778  */
1779 ObjectProperty *object_property_add_uint8_ptr(Object *obj, const char *name,
1780                                               const uint8_t *v,
1781                                               ObjectPropertyFlags flags);
1782 
1783 ObjectProperty *object_class_property_add_uint8_ptr(ObjectClass *klass,
1784                                          const char *name,
1785                                          const uint8_t *v,
1786                                          ObjectPropertyFlags flags);
1787 
1788 /**
1789  * object_property_add_uint16_ptr:
1790  * @obj: the object to add a property to
1791  * @name: the name of the property
1792  * @v: pointer to value
1793  * @flags: bitwise-or'd ObjectPropertyFlags
1794  *
1795  * Add an integer property in memory.  This function will add a
1796  * property of type 'uint16'.
1797  *
1798  * Returns: The newly added property on success, or %NULL on failure.
1799  */
1800 ObjectProperty *object_property_add_uint16_ptr(Object *obj, const char *name,
1801                                     const uint16_t *v,
1802                                     ObjectPropertyFlags flags);
1803 
1804 ObjectProperty *object_class_property_add_uint16_ptr(ObjectClass *klass,
1805                                           const char *name,
1806                                           const uint16_t *v,
1807                                           ObjectPropertyFlags flags);
1808 
1809 /**
1810  * object_property_add_uint32_ptr:
1811  * @obj: the object to add a property to
1812  * @name: the name of the property
1813  * @v: pointer to value
1814  * @flags: bitwise-or'd ObjectPropertyFlags
1815  *
1816  * Add an integer property in memory.  This function will add a
1817  * property of type 'uint32'.
1818  *
1819  * Returns: The newly added property on success, or %NULL on failure.
1820  */
1821 ObjectProperty *object_property_add_uint32_ptr(Object *obj, const char *name,
1822                                     const uint32_t *v,
1823                                     ObjectPropertyFlags flags);
1824 
1825 ObjectProperty *object_class_property_add_uint32_ptr(ObjectClass *klass,
1826                                           const char *name,
1827                                           const uint32_t *v,
1828                                           ObjectPropertyFlags flags);
1829 
1830 /**
1831  * object_property_add_uint64_ptr:
1832  * @obj: the object to add a property to
1833  * @name: the name of the property
1834  * @v: pointer to value
1835  * @flags: bitwise-or'd ObjectPropertyFlags
1836  *
1837  * Add an integer property in memory.  This function will add a
1838  * property of type 'uint64'.
1839  *
1840  * Returns: The newly added property on success, or %NULL on failure.
1841  */
1842 ObjectProperty *object_property_add_uint64_ptr(Object *obj, const char *name,
1843                                     const uint64_t *v,
1844                                     ObjectPropertyFlags flags);
1845 
1846 ObjectProperty *object_class_property_add_uint64_ptr(ObjectClass *klass,
1847                                           const char *name,
1848                                           const uint64_t *v,
1849                                           ObjectPropertyFlags flags);
1850 
1851 /**
1852  * object_property_add_alias:
1853  * @obj: the object to add a property to
1854  * @name: the name of the property
1855  * @target_obj: the object to forward property access to
1856  * @target_name: the name of the property on the forwarded object
1857  *
1858  * Add an alias for a property on an object.  This function will add a property
1859  * of the same type as the forwarded property.
1860  *
1861  * The caller must ensure that @target_obj stays alive as long as
1862  * this property exists.  In the case of a child object or an alias on the same
1863  * object this will be the case.  For aliases to other objects the caller is
1864  * responsible for taking a reference.
1865  *
1866  * Returns: The newly added property on success, or %NULL on failure.
1867  */
1868 ObjectProperty *object_property_add_alias(Object *obj, const char *name,
1869                                Object *target_obj, const char *target_name);
1870 
1871 /**
1872  * object_property_add_const_link:
1873  * @obj: the object to add a property to
1874  * @name: the name of the property
1875  * @target: the object to be referred by the link
1876  *
1877  * Add an unmodifiable link for a property on an object.  This function will
1878  * add a property of type link<TYPE> where TYPE is the type of @target.
1879  *
1880  * The caller must ensure that @target stays alive as long as
1881  * this property exists.  In the case @target is a child of @obj,
1882  * this will be the case.  Otherwise, the caller is responsible for
1883  * taking a reference.
1884  *
1885  * Returns: The newly added property on success, or %NULL on failure.
1886  */
1887 ObjectProperty *object_property_add_const_link(Object *obj, const char *name,
1888                                                Object *target);
1889 
1890 /**
1891  * object_property_set_description:
1892  * @obj: the object owning the property
1893  * @name: the name of the property
1894  * @description: the description of the property on the object
1895  *
1896  * Set an object property's description.
1897  *
1898  * Returns: %true on success, %false on failure.
1899  */
1900 void object_property_set_description(Object *obj, const char *name,
1901                                      const char *description);
1902 void object_class_property_set_description(ObjectClass *klass, const char *name,
1903                                            const char *description);
1904 
1905 /**
1906  * object_child_foreach:
1907  * @obj: the object whose children will be navigated
1908  * @fn: the iterator function to be called
1909  * @opaque: an opaque value that will be passed to the iterator
1910  *
1911  * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1912  * non-zero.
1913  *
1914  * It is forbidden to add or remove children from @obj from the @fn
1915  * callback.
1916  *
1917  * Returns: The last value returned by @fn, or 0 if there is no child.
1918  */
1919 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1920                          void *opaque);
1921 
1922 /**
1923  * object_child_foreach_recursive:
1924  * @obj: the object whose children will be navigated
1925  * @fn: the iterator function to be called
1926  * @opaque: an opaque value that will be passed to the iterator
1927  *
1928  * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1929  * non-zero. Calls recursively, all child nodes of @obj will also be passed
1930  * all the way down to the leaf nodes of the tree. Depth first ordering.
1931  *
1932  * It is forbidden to add or remove children from @obj (or its
1933  * child nodes) from the @fn callback.
1934  *
1935  * Returns: The last value returned by @fn, or 0 if there is no child.
1936  */
1937 int object_child_foreach_recursive(Object *obj,
1938                                    int (*fn)(Object *child, void *opaque),
1939                                    void *opaque);
1940 /**
1941  * container_get:
1942  * @root: root of the #path, e.g., object_get_root()
1943  * @path: path to the container
1944  *
1945  * Return a container object whose path is @path.  Create more containers
1946  * along the path if necessary.
1947  *
1948  * Returns: the container object.
1949  */
1950 Object *container_get(Object *root, const char *path);
1951 
1952 /**
1953  * object_type_get_instance_size:
1954  * @typename: Name of the Type whose instance_size is required
1955  *
1956  * Returns the instance_size of the given @typename.
1957  */
1958 size_t object_type_get_instance_size(const char *typename);
1959 
1960 /**
1961  * object_property_help:
1962  * @name: the name of the property
1963  * @type: the type of the property
1964  * @defval: the default value
1965  * @description: description of the property
1966  *
1967  * Returns: a user-friendly formatted string describing the property
1968  * for help purposes.
1969  */
1970 char *object_property_help(const char *name, const char *type,
1971                            QObject *defval, const char *description);
1972 
1973 G_DEFINE_AUTOPTR_CLEANUP_FUNC(Object, object_unref)
1974 
1975 #endif
1976