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