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