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