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