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