xref: /qemu/include/qom/object.h (revision c246cee4)
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 <glib.h>
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include "qemu/queue.h"
21 #include "qapi/error.h"
22 
23 struct Visitor;
24 
25 struct TypeImpl;
26 typedef struct TypeImpl *Type;
27 
28 typedef struct ObjectClass ObjectClass;
29 typedef struct Object Object;
30 
31 typedef struct TypeInfo TypeInfo;
32 
33 typedef struct InterfaceClass InterfaceClass;
34 typedef struct InterfaceInfo InterfaceInfo;
35 
36 #define TYPE_OBJECT "object"
37 
38 /**
39  * SECTION:object.h
40  * @title:Base Object Type System
41  * @short_description: interfaces for creating new types and objects
42  *
43  * The QEMU Object Model provides a framework for registering user creatable
44  * types and instantiating objects from those types.  QOM provides the following
45  * features:
46  *
47  *  - System for dynamically registering types
48  *  - Support for single-inheritance of types
49  *  - Multiple inheritance of stateless interfaces
50  *
51  * <example>
52  *   <title>Creating a minimal type</title>
53  *   <programlisting>
54  * #include "qdev.h"
55  *
56  * #define TYPE_MY_DEVICE "my-device"
57  *
58  * // No new virtual functions: we can reuse the typedef for the
59  * // superclass.
60  * typedef DeviceClass MyDeviceClass;
61  * typedef struct MyDevice
62  * {
63  *     DeviceState parent;
64  *
65  *     int reg0, reg1, reg2;
66  * } MyDevice;
67  *
68  * static const TypeInfo my_device_info = {
69  *     .name = TYPE_MY_DEVICE,
70  *     .parent = TYPE_DEVICE,
71  *     .instance_size = sizeof(MyDevice),
72  * };
73  *
74  * static void my_device_register_types(void)
75  * {
76  *     type_register_static(&my_device_info);
77  * }
78  *
79  * type_init(my_device_register_types)
80  *   </programlisting>
81  * </example>
82  *
83  * In the above example, we create a simple type that is described by #TypeInfo.
84  * #TypeInfo describes information about the type including what it inherits
85  * from, the instance and class size, and constructor/destructor hooks.
86  *
87  * Every type has an #ObjectClass associated with it.  #ObjectClass derivatives
88  * are instantiated dynamically but there is only ever one instance for any
89  * given type.  The #ObjectClass typically holds a table of function pointers
90  * for the virtual methods implemented by this type.
91  *
92  * Using object_new(), a new #Object derivative will be instantiated.  You can
93  * cast an #Object to a subclass (or base-class) type using
94  * object_dynamic_cast().  You typically want to define macro wrappers around
95  * OBJECT_CHECK() and OBJECT_CLASS_CHECK() to make it easier to convert to a
96  * specific type:
97  *
98  * <example>
99  *   <title>Typecasting macros</title>
100  *   <programlisting>
101  *    #define MY_DEVICE_GET_CLASS(obj) \
102  *       OBJECT_GET_CLASS(MyDeviceClass, obj, TYPE_MY_DEVICE)
103  *    #define MY_DEVICE_CLASS(klass) \
104  *       OBJECT_CLASS_CHECK(MyDeviceClass, klass, TYPE_MY_DEVICE)
105  *    #define MY_DEVICE(obj) \
106  *       OBJECT_CHECK(MyDevice, obj, TYPE_MY_DEVICE)
107  *   </programlisting>
108  * </example>
109  *
110  * # Class Initialization #
111  *
112  * Before an object is initialized, the class for the object must be
113  * initialized.  There is only one class object for all instance objects
114  * that is created lazily.
115  *
116  * Classes are initialized by first initializing any parent classes (if
117  * necessary).  After the parent class object has initialized, it will be
118  * copied into the current class object and any additional storage in the
119  * class object is zero filled.
120  *
121  * The effect of this is that classes automatically inherit any virtual
122  * function pointers that the parent class has already initialized.  All
123  * other fields will be zero filled.
124  *
125  * Once all of the parent classes have been initialized, #TypeInfo::class_init
126  * is called to let the class being instantiated provide default initialize for
127  * its virtual functions.  Here is how the above example might be modified
128  * to introduce an overridden virtual function:
129  *
130  * <example>
131  *   <title>Overriding a virtual function</title>
132  *   <programlisting>
133  * #include "qdev.h"
134  *
135  * void my_device_class_init(ObjectClass *klass, void *class_data)
136  * {
137  *     DeviceClass *dc = DEVICE_CLASS(klass);
138  *     dc->reset = my_device_reset;
139  * }
140  *
141  * static const TypeInfo my_device_info = {
142  *     .name = TYPE_MY_DEVICE,
143  *     .parent = TYPE_DEVICE,
144  *     .instance_size = sizeof(MyDevice),
145  *     .class_init = my_device_class_init,
146  * };
147  *   </programlisting>
148  * </example>
149  *
150  * Introducing new virtual methods requires a class to define its own
151  * struct and to add a .class_size member to the #TypeInfo.  Each method
152  * will also have a wrapper function to call it easily:
153  *
154  * <example>
155  *   <title>Defining an abstract class</title>
156  *   <programlisting>
157  * #include "qdev.h"
158  *
159  * typedef struct MyDeviceClass
160  * {
161  *     DeviceClass parent;
162  *
163  *     void (*frobnicate) (MyDevice *obj);
164  * } MyDeviceClass;
165  *
166  * static const TypeInfo my_device_info = {
167  *     .name = TYPE_MY_DEVICE,
168  *     .parent = TYPE_DEVICE,
169  *     .instance_size = sizeof(MyDevice),
170  *     .abstract = true, // or set a default in my_device_class_init
171  *     .class_size = sizeof(MyDeviceClass),
172  * };
173  *
174  * void my_device_frobnicate(MyDevice *obj)
175  * {
176  *     MyDeviceClass *klass = MY_DEVICE_GET_CLASS(obj);
177  *
178  *     klass->frobnicate(obj);
179  * }
180  *   </programlisting>
181  * </example>
182  *
183  * # Interfaces #
184  *
185  * Interfaces allow a limited form of multiple inheritance.  Instances are
186  * similar to normal types except for the fact that are only defined by
187  * their classes and never carry any state.  You can dynamically cast an object
188  * to one of its #Interface types and vice versa.
189  *
190  * # Methods #
191  *
192  * A <emphasis>method</emphasis> is a function within the namespace scope of
193  * a class. It usually operates on the object instance by passing it as a
194  * strongly-typed first argument.
195  * If it does not operate on an object instance, it is dubbed
196  * <emphasis>class method</emphasis>.
197  *
198  * Methods cannot be overloaded. That is, the #ObjectClass and method name
199  * uniquely identity the function to be called; the signature does not vary
200  * except for trailing varargs.
201  *
202  * Methods are always <emphasis>virtual</emphasis>. Overriding a method in
203  * #TypeInfo.class_init of a subclass leads to any user of the class obtained
204  * via OBJECT_GET_CLASS() accessing the overridden function.
205  * The original function is not automatically invoked. It is the responsibility
206  * of the overriding class to determine whether and when to invoke the method
207  * being overridden.
208  *
209  * To invoke the method being overridden, the preferred solution is to store
210  * the original value in the overriding class before overriding the method.
211  * This corresponds to |[ {super,base}.method(...) ]| in Java and C#
212  * respectively; this frees the overriding class from hardcoding its parent
213  * class, which someone might choose to change at some point.
214  *
215  * <example>
216  *   <title>Overriding a virtual method</title>
217  *   <programlisting>
218  * typedef struct MyState MyState;
219  *
220  * typedef void (*MyDoSomething)(MyState *obj);
221  *
222  * typedef struct MyClass {
223  *     ObjectClass parent_class;
224  *
225  *     MyDoSomething do_something;
226  * } MyClass;
227  *
228  * static void my_do_something(MyState *obj)
229  * {
230  *     // do something
231  * }
232  *
233  * static void my_class_init(ObjectClass *oc, void *data)
234  * {
235  *     MyClass *mc = MY_CLASS(oc);
236  *
237  *     mc->do_something = my_do_something;
238  * }
239  *
240  * static const TypeInfo my_type_info = {
241  *     .name = TYPE_MY,
242  *     .parent = TYPE_OBJECT,
243  *     .instance_size = sizeof(MyState),
244  *     .class_size = sizeof(MyClass),
245  *     .class_init = my_class_init,
246  * };
247  *
248  * typedef struct DerivedClass {
249  *     MyClass parent_class;
250  *
251  *     MyDoSomething parent_do_something;
252  * } DerivedClass;
253  *
254  * static void derived_do_something(MyState *obj)
255  * {
256  *     DerivedClass *dc = DERIVED_GET_CLASS(obj);
257  *
258  *     // do something here
259  *     dc->parent_do_something(obj);
260  *     // do something else here
261  * }
262  *
263  * static void derived_class_init(ObjectClass *oc, void *data)
264  * {
265  *     MyClass *mc = MY_CLASS(oc);
266  *     DerivedClass *dc = DERIVED_CLASS(oc);
267  *
268  *     dc->parent_do_something = mc->do_something;
269  *     mc->do_something = derived_do_something;
270  * }
271  *
272  * static const TypeInfo derived_type_info = {
273  *     .name = TYPE_DERIVED,
274  *     .parent = TYPE_MY,
275  *     .class_size = sizeof(DerivedClass),
276  *     .class_init = my_class_init,
277  * };
278  *   </programlisting>
279  * </example>
280  *
281  * Alternatively, object_class_by_name() can be used to obtain the class and
282  * its non-overridden methods for a specific type. This would correspond to
283  * |[ MyClass::method(...) ]| in C++.
284  *
285  * The first example of such a QOM method was #CPUClass.reset,
286  * another example is #DeviceClass.realize.
287  */
288 
289 
290 /**
291  * ObjectPropertyAccessor:
292  * @obj: the object that owns the property
293  * @v: the visitor that contains the property data
294  * @opaque: the object property opaque
295  * @name: the name of the property
296  * @errp: a pointer to an Error that is filled if getting/setting fails.
297  *
298  * Called when trying to get/set a property.
299  */
300 typedef void (ObjectPropertyAccessor)(Object *obj,
301                                       struct Visitor *v,
302                                       void *opaque,
303                                       const char *name,
304                                       Error **errp);
305 
306 /**
307  * ObjectPropertyResolve:
308  * @obj: the object that owns the property
309  * @opaque: the opaque registered with the property
310  * @part: the name of the property
311  *
312  * Resolves the #Object corresponding to property @part.
313  *
314  * The returned object can also be used as a starting point
315  * to resolve a relative path starting with "@part".
316  *
317  * Returns: If @path is the path that led to @obj, the function
318  * returns the #Object corresponding to "@path/@part".
319  * If "@path/@part" is not a valid object path, it returns #NULL.
320  */
321 typedef Object *(ObjectPropertyResolve)(Object *obj,
322                                         void *opaque,
323                                         const char *part);
324 
325 /**
326  * ObjectPropertyRelease:
327  * @obj: the object that owns the property
328  * @name: the name of the property
329  * @opaque: the opaque registered with the property
330  *
331  * Called when a property is removed from a object.
332  */
333 typedef void (ObjectPropertyRelease)(Object *obj,
334                                      const char *name,
335                                      void *opaque);
336 
337 typedef struct ObjectProperty
338 {
339     gchar *name;
340     gchar *type;
341     gchar *description;
342     ObjectPropertyAccessor *get;
343     ObjectPropertyAccessor *set;
344     ObjectPropertyResolve *resolve;
345     ObjectPropertyRelease *release;
346     void *opaque;
347 
348     QTAILQ_ENTRY(ObjectProperty) node;
349 } ObjectProperty;
350 
351 /**
352  * ObjectUnparent:
353  * @obj: the object that is being removed from the composition tree
354  *
355  * Called when an object is being removed from the QOM composition tree.
356  * The function should remove any backlinks from children objects to @obj.
357  */
358 typedef void (ObjectUnparent)(Object *obj);
359 
360 /**
361  * ObjectFree:
362  * @obj: the object being freed
363  *
364  * Called when an object's last reference is removed.
365  */
366 typedef void (ObjectFree)(void *obj);
367 
368 #define OBJECT_CLASS_CAST_CACHE 4
369 
370 /**
371  * ObjectClass:
372  *
373  * The base for all classes.  The only thing that #ObjectClass contains is an
374  * integer type handle.
375  */
376 struct ObjectClass
377 {
378     /*< private >*/
379     Type type;
380     GSList *interfaces;
381 
382     const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
383     const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
384 
385     ObjectUnparent *unparent;
386 };
387 
388 /**
389  * Object:
390  *
391  * The base for all objects.  The first member of this object is a pointer to
392  * a #ObjectClass.  Since C guarantees that the first member of a structure
393  * always begins at byte 0 of that structure, as long as any sub-object places
394  * its parent as the first member, we can cast directly to a #Object.
395  *
396  * As a result, #Object contains a reference to the objects type as its
397  * first member.  This allows identification of the real type of the object at
398  * run time.
399  *
400  * #Object also contains a list of #Interfaces that this object
401  * implements.
402  */
403 struct Object
404 {
405     /*< private >*/
406     ObjectClass *class;
407     ObjectFree *free;
408     QTAILQ_HEAD(, ObjectProperty) properties;
409     uint32_t ref;
410     Object *parent;
411 };
412 
413 /**
414  * TypeInfo:
415  * @name: The name of the type.
416  * @parent: The name of the parent type.
417  * @instance_size: The size of the object (derivative of #Object).  If
418  *   @instance_size is 0, then the size of the object will be the size of the
419  *   parent object.
420  * @instance_init: This function is called to initialize an object.  The parent
421  *   class will have already been initialized so the type is only responsible
422  *   for initializing its own members.
423  * @instance_post_init: This function is called to finish initialization of
424  *   an object, after all @instance_init functions were called.
425  * @instance_finalize: This function is called during object destruction.  This
426  *   is called before the parent @instance_finalize function has been called.
427  *   An object should only free the members that are unique to its type in this
428  *   function.
429  * @abstract: If this field is true, then the class is considered abstract and
430  *   cannot be directly instantiated.
431  * @class_size: The size of the class object (derivative of #ObjectClass)
432  *   for this object.  If @class_size is 0, then the size of the class will be
433  *   assumed to be the size of the parent class.  This allows a type to avoid
434  *   implementing an explicit class type if they are not adding additional
435  *   virtual functions.
436  * @class_init: This function is called after all parent class initialization
437  *   has occurred to allow a class to set its default virtual method pointers.
438  *   This is also the function to use to override virtual methods from a parent
439  *   class.
440  * @class_base_init: This function is called for all base classes after all
441  *   parent class initialization has occurred, but before the class itself
442  *   is initialized.  This is the function to use to undo the effects of
443  *   memcpy from the parent class to the descendents.
444  * @class_finalize: This function is called during class destruction and is
445  *   meant to release and dynamic parameters allocated by @class_init.
446  * @class_data: Data to pass to the @class_init, @class_base_init and
447  *   @class_finalize functions.  This can be useful when building dynamic
448  *   classes.
449  * @interfaces: The list of interfaces associated with this type.  This
450  *   should point to a static array that's terminated with a zero filled
451  *   element.
452  */
453 struct TypeInfo
454 {
455     const char *name;
456     const char *parent;
457 
458     size_t instance_size;
459     void (*instance_init)(Object *obj);
460     void (*instance_post_init)(Object *obj);
461     void (*instance_finalize)(Object *obj);
462 
463     bool abstract;
464     size_t class_size;
465 
466     void (*class_init)(ObjectClass *klass, void *data);
467     void (*class_base_init)(ObjectClass *klass, void *data);
468     void (*class_finalize)(ObjectClass *klass, void *data);
469     void *class_data;
470 
471     InterfaceInfo *interfaces;
472 };
473 
474 /**
475  * OBJECT:
476  * @obj: A derivative of #Object
477  *
478  * Converts an object to a #Object.  Since all objects are #Objects,
479  * this function will always succeed.
480  */
481 #define OBJECT(obj) \
482     ((Object *)(obj))
483 
484 /**
485  * OBJECT_CLASS:
486  * @class: A derivative of #ObjectClass.
487  *
488  * Converts a class to an #ObjectClass.  Since all objects are #Objects,
489  * this function will always succeed.
490  */
491 #define OBJECT_CLASS(class) \
492     ((ObjectClass *)(class))
493 
494 /**
495  * OBJECT_CHECK:
496  * @type: The C type to use for the return value.
497  * @obj: A derivative of @type to cast.
498  * @name: The QOM typename of @type
499  *
500  * A type safe version of @object_dynamic_cast_assert.  Typically each class
501  * will define a macro based on this type to perform type safe dynamic_casts to
502  * this object type.
503  *
504  * If an invalid object is passed to this function, a run time assert will be
505  * generated.
506  */
507 #define OBJECT_CHECK(type, obj, name) \
508     ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
509                                         __FILE__, __LINE__, __func__))
510 
511 /**
512  * OBJECT_CLASS_CHECK:
513  * @class: The C type to use for the return value.
514  * @obj: A derivative of @type to cast.
515  * @name: the QOM typename of @class.
516  *
517  * A type safe version of @object_class_dynamic_cast_assert.  This macro is
518  * typically wrapped by each type to perform type safe casts of a class to a
519  * specific class type.
520  */
521 #define OBJECT_CLASS_CHECK(class, obj, name) \
522     ((class *)object_class_dynamic_cast_assert(OBJECT_CLASS(obj), (name), \
523                                                __FILE__, __LINE__, __func__))
524 
525 /**
526  * OBJECT_GET_CLASS:
527  * @class: The C type to use for the return value.
528  * @obj: The object to obtain the class for.
529  * @name: The QOM typename of @obj.
530  *
531  * This function will return a specific class for a given object.  Its generally
532  * used by each type to provide a type safe macro to get a specific class type
533  * from an object.
534  */
535 #define OBJECT_GET_CLASS(class, obj, name) \
536     OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
537 
538 /**
539  * InterfaceInfo:
540  * @type: The name of the interface.
541  *
542  * The information associated with an interface.
543  */
544 struct InterfaceInfo {
545     const char *type;
546 };
547 
548 /**
549  * InterfaceClass:
550  * @parent_class: the base class
551  *
552  * The class for all interfaces.  Subclasses of this class should only add
553  * virtual methods.
554  */
555 struct InterfaceClass
556 {
557     ObjectClass parent_class;
558     /*< private >*/
559     ObjectClass *concrete_class;
560     Type interface_type;
561 };
562 
563 #define TYPE_INTERFACE "interface"
564 
565 /**
566  * INTERFACE_CLASS:
567  * @klass: class to cast from
568  * Returns: An #InterfaceClass or raise an error if cast is invalid
569  */
570 #define INTERFACE_CLASS(klass) \
571     OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
572 
573 /**
574  * INTERFACE_CHECK:
575  * @interface: the type to return
576  * @obj: the object to convert to an interface
577  * @name: the interface type name
578  *
579  * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
580  */
581 #define INTERFACE_CHECK(interface, obj, name) \
582     ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
583                                              __FILE__, __LINE__, __func__))
584 
585 /**
586  * object_new:
587  * @typename: The name of the type of the object to instantiate.
588  *
589  * This function will initialize a new object using heap allocated memory.
590  * The returned object has a reference count of 1, and will be freed when
591  * the last reference is dropped.
592  *
593  * Returns: The newly allocated and instantiated object.
594  */
595 Object *object_new(const char *typename);
596 
597 /**
598  * object_new_with_type:
599  * @type: The type of the object to instantiate.
600  *
601  * This function will initialize a new object using heap allocated memory.
602  * The returned object has a reference count of 1, and will be freed when
603  * the last reference is dropped.
604  *
605  * Returns: The newly allocated and instantiated object.
606  */
607 Object *object_new_with_type(Type type);
608 
609 /**
610  * object_initialize_with_type:
611  * @data: A pointer to the memory to be used for the object.
612  * @size: The maximum size available at @data for the object.
613  * @type: The type of the object to instantiate.
614  *
615  * This function will initialize an object.  The memory for the object should
616  * have already been allocated.  The returned object has a reference count of 1,
617  * and will be finalized when the last reference is dropped.
618  */
619 void object_initialize_with_type(void *data, size_t size, Type type);
620 
621 /**
622  * object_initialize:
623  * @obj: A pointer to the memory to be used for the object.
624  * @size: The maximum size available at @obj for the object.
625  * @typename: The name of the type of the object to instantiate.
626  *
627  * This function will initialize an object.  The memory for the object should
628  * have already been allocated.  The returned object has a reference count of 1,
629  * and will be finalized when the last reference is dropped.
630  */
631 void object_initialize(void *obj, size_t size, const char *typename);
632 
633 /**
634  * object_dynamic_cast:
635  * @obj: The object to cast.
636  * @typename: The @typename to cast to.
637  *
638  * This function will determine if @obj is-a @typename.  @obj can refer to an
639  * object or an interface associated with an object.
640  *
641  * Returns: This function returns @obj on success or #NULL on failure.
642  */
643 Object *object_dynamic_cast(Object *obj, const char *typename);
644 
645 /**
646  * object_dynamic_cast_assert:
647  *
648  * See object_dynamic_cast() for a description of the parameters of this
649  * function.  The only difference in behavior is that this function asserts
650  * instead of returning #NULL on failure if QOM cast debugging is enabled.
651  * This function is not meant to be called directly, but only through
652  * the wrapper macro OBJECT_CHECK.
653  */
654 Object *object_dynamic_cast_assert(Object *obj, const char *typename,
655                                    const char *file, int line, const char *func);
656 
657 /**
658  * object_get_class:
659  * @obj: A derivative of #Object
660  *
661  * Returns: The #ObjectClass of the type associated with @obj.
662  */
663 ObjectClass *object_get_class(Object *obj);
664 
665 /**
666  * object_get_typename:
667  * @obj: A derivative of #Object.
668  *
669  * Returns: The QOM typename of @obj.
670  */
671 const char *object_get_typename(Object *obj);
672 
673 /**
674  * type_register_static:
675  * @info: The #TypeInfo of the new type.
676  *
677  * @info and all of the strings it points to should exist for the life time
678  * that the type is registered.
679  *
680  * Returns: 0 on failure, the new #Type on success.
681  */
682 Type type_register_static(const TypeInfo *info);
683 
684 /**
685  * type_register:
686  * @info: The #TypeInfo of the new type
687  *
688  * Unlike type_register_static(), this call does not require @info or its
689  * string members to continue to exist after the call returns.
690  *
691  * Returns: 0 on failure, the new #Type on success.
692  */
693 Type type_register(const TypeInfo *info);
694 
695 /**
696  * object_class_dynamic_cast_assert:
697  * @klass: The #ObjectClass to attempt to cast.
698  * @typename: The QOM typename of the class to cast to.
699  *
700  * See object_class_dynamic_cast() for a description of the parameters
701  * of this function.  The only difference in behavior is that this function
702  * asserts instead of returning #NULL on failure if QOM cast debugging is
703  * enabled.  This function is not meant to be called directly, but only through
704  * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
705  */
706 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
707                                               const char *typename,
708                                               const char *file, int line,
709                                               const char *func);
710 
711 /**
712  * object_class_dynamic_cast:
713  * @klass: The #ObjectClass to attempt to cast.
714  * @typename: The QOM typename of the class to cast to.
715  *
716  * Returns: If @typename is a class, this function returns @klass if
717  * @typename is a subtype of @klass, else returns #NULL.
718  *
719  * If @typename is an interface, this function returns the interface
720  * definition for @klass if @klass implements it unambiguously; #NULL
721  * is returned if @klass does not implement the interface or if multiple
722  * classes or interfaces on the hierarchy leading to @klass implement
723  * it.  (FIXME: perhaps this can be detected at type definition time?)
724  */
725 ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
726                                        const char *typename);
727 
728 /**
729  * object_class_get_parent:
730  * @klass: The class to obtain the parent for.
731  *
732  * Returns: The parent for @klass or %NULL if none.
733  */
734 ObjectClass *object_class_get_parent(ObjectClass *klass);
735 
736 /**
737  * object_class_get_name:
738  * @klass: The class to obtain the QOM typename for.
739  *
740  * Returns: The QOM typename for @klass.
741  */
742 const char *object_class_get_name(ObjectClass *klass);
743 
744 /**
745  * object_class_is_abstract:
746  * @klass: The class to obtain the abstractness for.
747  *
748  * Returns: %true if @klass is abstract, %false otherwise.
749  */
750 bool object_class_is_abstract(ObjectClass *klass);
751 
752 /**
753  * object_class_by_name:
754  * @typename: The QOM typename to obtain the class for.
755  *
756  * Returns: The class for @typename or %NULL if not found.
757  */
758 ObjectClass *object_class_by_name(const char *typename);
759 
760 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
761                           const char *implements_type, bool include_abstract,
762                           void *opaque);
763 
764 /**
765  * object_class_get_list:
766  * @implements_type: The type to filter for, including its derivatives.
767  * @include_abstract: Whether to include abstract classes.
768  *
769  * Returns: A singly-linked list of the classes in reverse hashtable order.
770  */
771 GSList *object_class_get_list(const char *implements_type,
772                               bool include_abstract);
773 
774 /**
775  * object_ref:
776  * @obj: the object
777  *
778  * Increase the reference count of a object.  A object cannot be freed as long
779  * as its reference count is greater than zero.
780  */
781 void object_ref(Object *obj);
782 
783 /**
784  * qdef_unref:
785  * @obj: the object
786  *
787  * Decrease the reference count of a object.  A object cannot be freed as long
788  * as its reference count is greater than zero.
789  */
790 void object_unref(Object *obj);
791 
792 /**
793  * object_property_add:
794  * @obj: the object to add a property to
795  * @name: the name of the property.  This can contain any character except for
796  *  a forward slash.  In general, you should use hyphens '-' instead of
797  *  underscores '_' when naming properties.
798  * @type: the type name of the property.  This namespace is pretty loosely
799  *   defined.  Sub namespaces are constructed by using a prefix and then
800  *   to angle brackets.  For instance, the type 'virtio-net-pci' in the
801  *   'link' namespace would be 'link<virtio-net-pci>'.
802  * @get: The getter to be called to read a property.  If this is NULL, then
803  *   the property cannot be read.
804  * @set: the setter to be called to write a property.  If this is NULL,
805  *   then the property cannot be written.
806  * @release: called when the property is removed from the object.  This is
807  *   meant to allow a property to free its opaque upon object
808  *   destruction.  This may be NULL.
809  * @opaque: an opaque pointer to pass to the callbacks for the property
810  * @errp: returns an error if this function fails
811  *
812  * Returns: The #ObjectProperty; this can be used to set the @resolve
813  * callback for child and link properties.
814  */
815 ObjectProperty *object_property_add(Object *obj, const char *name,
816                                     const char *type,
817                                     ObjectPropertyAccessor *get,
818                                     ObjectPropertyAccessor *set,
819                                     ObjectPropertyRelease *release,
820                                     void *opaque, Error **errp);
821 
822 void object_property_del(Object *obj, const char *name, Error **errp);
823 
824 /**
825  * object_property_find:
826  * @obj: the object
827  * @name: the name of the property
828  * @errp: returns an error if this function fails
829  *
830  * Look up a property for an object and return its #ObjectProperty if found.
831  */
832 ObjectProperty *object_property_find(Object *obj, const char *name,
833                                      Error **errp);
834 
835 void object_unparent(Object *obj);
836 
837 /**
838  * object_property_get:
839  * @obj: the object
840  * @v: the visitor that will receive the property value.  This should be an
841  *   Output visitor and the data will be written with @name as the name.
842  * @name: the name of the property
843  * @errp: returns an error if this function fails
844  *
845  * Reads a property from a object.
846  */
847 void object_property_get(Object *obj, struct Visitor *v, const char *name,
848                          Error **errp);
849 
850 /**
851  * object_property_set_str:
852  * @value: the value to be written to the property
853  * @name: the name of the property
854  * @errp: returns an error if this function fails
855  *
856  * Writes a string value to a property.
857  */
858 void object_property_set_str(Object *obj, const char *value,
859                              const char *name, Error **errp);
860 
861 /**
862  * object_property_get_str:
863  * @obj: the object
864  * @name: the name of the property
865  * @errp: returns an error if this function fails
866  *
867  * Returns: the value of the property, converted to a C string, or NULL if
868  * an error occurs (including when the property value is not a string).
869  * The caller should free the string.
870  */
871 char *object_property_get_str(Object *obj, const char *name,
872                               Error **errp);
873 
874 /**
875  * object_property_set_link:
876  * @value: the value to be written to the property
877  * @name: the name of the property
878  * @errp: returns an error if this function fails
879  *
880  * Writes an object's canonical path to a property.
881  */
882 void object_property_set_link(Object *obj, Object *value,
883                               const char *name, Error **errp);
884 
885 /**
886  * object_property_get_link:
887  * @obj: the object
888  * @name: the name of the property
889  * @errp: returns an error if this function fails
890  *
891  * Returns: the value of the property, resolved from a path to an Object,
892  * or NULL if an error occurs (including when the property value is not a
893  * string or not a valid object path).
894  */
895 Object *object_property_get_link(Object *obj, const char *name,
896                                  Error **errp);
897 
898 /**
899  * object_property_set_bool:
900  * @value: the value to be written to the property
901  * @name: the name of the property
902  * @errp: returns an error if this function fails
903  *
904  * Writes a bool value to a property.
905  */
906 void object_property_set_bool(Object *obj, bool value,
907                               const char *name, Error **errp);
908 
909 /**
910  * object_property_get_bool:
911  * @obj: the object
912  * @name: the name of the property
913  * @errp: returns an error if this function fails
914  *
915  * Returns: the value of the property, converted to a boolean, or NULL if
916  * an error occurs (including when the property value is not a bool).
917  */
918 bool object_property_get_bool(Object *obj, const char *name,
919                               Error **errp);
920 
921 /**
922  * object_property_set_int:
923  * @value: the value to be written to the property
924  * @name: the name of the property
925  * @errp: returns an error if this function fails
926  *
927  * Writes an integer value to a property.
928  */
929 void object_property_set_int(Object *obj, int64_t value,
930                              const char *name, Error **errp);
931 
932 /**
933  * object_property_get_int:
934  * @obj: the object
935  * @name: the name of the property
936  * @errp: returns an error if this function fails
937  *
938  * Returns: the value of the property, converted to an integer, or NULL if
939  * an error occurs (including when the property value is not an integer).
940  */
941 int64_t object_property_get_int(Object *obj, const char *name,
942                                 Error **errp);
943 
944 /**
945  * object_property_get_enum:
946  * @obj: the object
947  * @name: the name of the property
948  * @strings: strings corresponding to enums
949  * @errp: returns an error if this function fails
950  *
951  * Returns: the value of the property, converted to an integer, or
952  * undefined if an error occurs (including when the property value is not
953  * an enum).
954  */
955 int object_property_get_enum(Object *obj, const char *name,
956                              const char *strings[], Error **errp);
957 
958 /**
959  * object_property_get_uint16List:
960  * @obj: the object
961  * @name: the name of the property
962  * @list: the returned int list
963  * @errp: returns an error if this function fails
964  *
965  * Returns: the value of the property, converted to integers, or
966  * undefined if an error occurs (including when the property value is not
967  * an list of integers).
968  */
969 void object_property_get_uint16List(Object *obj, const char *name,
970                                     uint16List **list, Error **errp);
971 
972 /**
973  * object_property_set:
974  * @obj: the object
975  * @v: the visitor that will be used to write the property value.  This should
976  *   be an Input visitor and the data will be first read with @name as the
977  *   name and then written as the property value.
978  * @name: the name of the property
979  * @errp: returns an error if this function fails
980  *
981  * Writes a property to a object.
982  */
983 void object_property_set(Object *obj, struct Visitor *v, const char *name,
984                          Error **errp);
985 
986 /**
987  * object_property_parse:
988  * @obj: the object
989  * @string: the string that will be used to parse the property value.
990  * @name: the name of the property
991  * @errp: returns an error if this function fails
992  *
993  * Parses a string and writes the result into a property of an object.
994  */
995 void object_property_parse(Object *obj, const char *string,
996                            const char *name, Error **errp);
997 
998 /**
999  * object_property_print:
1000  * @obj: the object
1001  * @name: the name of the property
1002  * @human: if true, print for human consumption
1003  * @errp: returns an error if this function fails
1004  *
1005  * Returns a string representation of the value of the property.  The
1006  * caller shall free the string.
1007  */
1008 char *object_property_print(Object *obj, const char *name, bool human,
1009                             Error **errp);
1010 
1011 /**
1012  * object_property_get_type:
1013  * @obj: the object
1014  * @name: the name of the property
1015  * @errp: returns an error if this function fails
1016  *
1017  * Returns:  The type name of the property.
1018  */
1019 const char *object_property_get_type(Object *obj, const char *name,
1020                                      Error **errp);
1021 
1022 /**
1023  * object_get_root:
1024  *
1025  * Returns: the root object of the composition tree
1026  */
1027 Object *object_get_root(void);
1028 
1029 /**
1030  * object_get_canonical_path_component:
1031  *
1032  * Returns: The final component in the object's canonical path.  The canonical
1033  * path is the path within the composition tree starting from the root.
1034  */
1035 gchar *object_get_canonical_path_component(Object *obj);
1036 
1037 /**
1038  * object_get_canonical_path:
1039  *
1040  * Returns: The canonical path for a object.  This is the path within the
1041  * composition tree starting from the root.
1042  */
1043 gchar *object_get_canonical_path(Object *obj);
1044 
1045 /**
1046  * object_resolve_path:
1047  * @path: the path to resolve
1048  * @ambiguous: returns true if the path resolution failed because of an
1049  *   ambiguous match
1050  *
1051  * There are two types of supported paths--absolute paths and partial paths.
1052  *
1053  * Absolute paths are derived from the root object and can follow child<> or
1054  * link<> properties.  Since they can follow link<> properties, they can be
1055  * arbitrarily long.  Absolute paths look like absolute filenames and are
1056  * prefixed with a leading slash.
1057  *
1058  * Partial paths look like relative filenames.  They do not begin with a
1059  * prefix.  The matching rules for partial paths are subtle but designed to make
1060  * specifying objects easy.  At each level of the composition tree, the partial
1061  * path is matched as an absolute path.  The first match is not returned.  At
1062  * least two matches are searched for.  A successful result is only returned if
1063  * only one match is found.  If more than one match is found, a flag is
1064  * returned to indicate that the match was ambiguous.
1065  *
1066  * Returns: The matched object or NULL on path lookup failure.
1067  */
1068 Object *object_resolve_path(const char *path, bool *ambiguous);
1069 
1070 /**
1071  * object_resolve_path_type:
1072  * @path: the path to resolve
1073  * @typename: the type to look for.
1074  * @ambiguous: returns true if the path resolution failed because of an
1075  *   ambiguous match
1076  *
1077  * This is similar to object_resolve_path.  However, when looking for a
1078  * partial path only matches that implement the given type are considered.
1079  * This restricts the search and avoids spuriously flagging matches as
1080  * ambiguous.
1081  *
1082  * For both partial and absolute paths, the return value goes through
1083  * a dynamic cast to @typename.  This is important if either the link,
1084  * or the typename itself are of interface types.
1085  *
1086  * Returns: The matched object or NULL on path lookup failure.
1087  */
1088 Object *object_resolve_path_type(const char *path, const char *typename,
1089                                  bool *ambiguous);
1090 
1091 /**
1092  * object_resolve_path_component:
1093  * @parent: the object in which to resolve the path
1094  * @part: the component to resolve.
1095  *
1096  * This is similar to object_resolve_path with an absolute path, but it
1097  * only resolves one element (@part) and takes the others from @parent.
1098  *
1099  * Returns: The resolved object or NULL on path lookup failure.
1100  */
1101 Object *object_resolve_path_component(Object *parent, const gchar *part);
1102 
1103 /**
1104  * object_property_add_child:
1105  * @obj: the object to add a property to
1106  * @name: the name of the property
1107  * @child: the child object
1108  * @errp: if an error occurs, a pointer to an area to store the area
1109  *
1110  * Child properties form the composition tree.  All objects need to be a child
1111  * of another object.  Objects can only be a child of one object.
1112  *
1113  * There is no way for a child to determine what its parent is.  It is not
1114  * a bidirectional relationship.  This is by design.
1115  *
1116  * The value of a child property as a C string will be the child object's
1117  * canonical path. It can be retrieved using object_property_get_str().
1118  * The child object itself can be retrieved using object_property_get_link().
1119  */
1120 void object_property_add_child(Object *obj, const char *name,
1121                                Object *child, Error **errp);
1122 
1123 typedef enum {
1124     /* Unref the link pointer when the property is deleted */
1125     OBJ_PROP_LINK_UNREF_ON_RELEASE = 0x1,
1126 } ObjectPropertyLinkFlags;
1127 
1128 /**
1129  * object_property_allow_set_link:
1130  *
1131  * The default implementation of the object_property_add_link() check()
1132  * callback function.  It allows the link property to be set and never returns
1133  * an error.
1134  */
1135 void object_property_allow_set_link(Object *, const char *,
1136                                     Object *, Error **);
1137 
1138 /**
1139  * object_property_add_link:
1140  * @obj: the object to add a property to
1141  * @name: the name of the property
1142  * @type: the qobj type of the link
1143  * @child: a pointer to where the link object reference is stored
1144  * @check: callback to veto setting or NULL if the property is read-only
1145  * @flags: additional options for the link
1146  * @errp: if an error occurs, a pointer to an area to store the area
1147  *
1148  * Links establish relationships between objects.  Links are unidirectional
1149  * although two links can be combined to form a bidirectional relationship
1150  * between objects.
1151  *
1152  * Links form the graph in the object model.
1153  *
1154  * The <code>@check()</code> callback is invoked when
1155  * object_property_set_link() is called and can raise an error to prevent the
1156  * link being set.  If <code>@check</code> is NULL, the property is read-only
1157  * and cannot be set.
1158  *
1159  * Ownership of the pointer that @child points to is transferred to the
1160  * link property.  The reference count for <code>*@child</code> is
1161  * managed by the property from after the function returns till the
1162  * property is deleted with object_property_del().  If the
1163  * <code>@flags</code> <code>OBJ_PROP_LINK_UNREF_ON_RELEASE</code> bit is set,
1164  * the reference count is decremented when the property is deleted.
1165  */
1166 void object_property_add_link(Object *obj, const char *name,
1167                               const char *type, Object **child,
1168                               void (*check)(Object *obj, const char *name,
1169                                             Object *val, Error **errp),
1170                               ObjectPropertyLinkFlags flags,
1171                               Error **errp);
1172 
1173 /**
1174  * object_property_add_str:
1175  * @obj: the object to add a property to
1176  * @name: the name of the property
1177  * @get: the getter or NULL if the property is write-only.  This function must
1178  *   return a string to be freed by g_free().
1179  * @set: the setter or NULL if the property is read-only
1180  * @errp: if an error occurs, a pointer to an area to store the error
1181  *
1182  * Add a string property using getters/setters.  This function will add a
1183  * property of type 'string'.
1184  */
1185 void object_property_add_str(Object *obj, const char *name,
1186                              char *(*get)(Object *, Error **),
1187                              void (*set)(Object *, const char *, Error **),
1188                              Error **errp);
1189 
1190 /**
1191  * object_property_add_bool:
1192  * @obj: the object to add a property to
1193  * @name: the name of the property
1194  * @get: the getter or NULL if the property is write-only.
1195  * @set: the setter or NULL if the property is read-only
1196  * @errp: if an error occurs, a pointer to an area to store the error
1197  *
1198  * Add a bool property using getters/setters.  This function will add a
1199  * property of type 'bool'.
1200  */
1201 void object_property_add_bool(Object *obj, const char *name,
1202                               bool (*get)(Object *, Error **),
1203                               void (*set)(Object *, bool, Error **),
1204                               Error **errp);
1205 
1206 /**
1207  * object_property_add_uint8_ptr:
1208  * @obj: the object to add a property to
1209  * @name: the name of the property
1210  * @v: pointer to value
1211  * @errp: if an error occurs, a pointer to an area to store the error
1212  *
1213  * Add an integer property in memory.  This function will add a
1214  * property of type 'uint8'.
1215  */
1216 void object_property_add_uint8_ptr(Object *obj, const char *name,
1217                                    const uint8_t *v, Error **errp);
1218 
1219 /**
1220  * object_property_add_uint16_ptr:
1221  * @obj: the object to add a property to
1222  * @name: the name of the property
1223  * @v: pointer to value
1224  * @errp: if an error occurs, a pointer to an area to store the error
1225  *
1226  * Add an integer property in memory.  This function will add a
1227  * property of type 'uint16'.
1228  */
1229 void object_property_add_uint16_ptr(Object *obj, const char *name,
1230                                     const uint16_t *v, Error **errp);
1231 
1232 /**
1233  * object_property_add_uint32_ptr:
1234  * @obj: the object to add a property to
1235  * @name: the name of the property
1236  * @v: pointer to value
1237  * @errp: if an error occurs, a pointer to an area to store the error
1238  *
1239  * Add an integer property in memory.  This function will add a
1240  * property of type 'uint32'.
1241  */
1242 void object_property_add_uint32_ptr(Object *obj, const char *name,
1243                                     const uint32_t *v, Error **errp);
1244 
1245 /**
1246  * object_property_add_uint64_ptr:
1247  * @obj: the object to add a property to
1248  * @name: the name of the property
1249  * @v: pointer to value
1250  * @errp: if an error occurs, a pointer to an area to store the error
1251  *
1252  * Add an integer property in memory.  This function will add a
1253  * property of type 'uint64'.
1254  */
1255 void object_property_add_uint64_ptr(Object *obj, const char *name,
1256                                     const uint64_t *v, Error **Errp);
1257 
1258 /**
1259  * object_property_add_alias:
1260  * @obj: the object to add a property to
1261  * @name: the name of the property
1262  * @target_obj: the object to forward property access to
1263  * @target_name: the name of the property on the forwarded object
1264  * @errp: if an error occurs, a pointer to an area to store the error
1265  *
1266  * Add an alias for a property on an object.  This function will add a property
1267  * of the same type as the forwarded property.
1268  *
1269  * The caller must ensure that <code>@target_obj</code> stays alive as long as
1270  * this property exists.  In the case of a child object or an alias on the same
1271  * object this will be the case.  For aliases to other objects the caller is
1272  * responsible for taking a reference.
1273  */
1274 void object_property_add_alias(Object *obj, const char *name,
1275                                Object *target_obj, const char *target_name,
1276                                Error **errp);
1277 
1278 /**
1279  * object_property_set_description:
1280  * @obj: the object owning the property
1281  * @name: the name of the property
1282  * @description: the description of the property on the object
1283  * @errp: if an error occurs, a pointer to an area to store the error
1284  *
1285  * Set an object property's description.
1286  *
1287  */
1288 void object_property_set_description(Object *obj, const char *name,
1289                                      const char *description, Error **errp);
1290 
1291 /**
1292  * object_child_foreach:
1293  * @obj: the object whose children will be navigated
1294  * @fn: the iterator function to be called
1295  * @opaque: an opaque value that will be passed to the iterator
1296  *
1297  * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1298  * non-zero.
1299  *
1300  * Returns: The last value returned by @fn, or 0 if there is no child.
1301  */
1302 int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1303                          void *opaque);
1304 
1305 /**
1306  * container_get:
1307  * @root: root of the #path, e.g., object_get_root()
1308  * @path: path to the container
1309  *
1310  * Return a container object whose path is @path.  Create more containers
1311  * along the path if necessary.
1312  *
1313  * Returns: the container object.
1314  */
1315 Object *container_get(Object *root, const char *path);
1316 
1317 
1318 #endif
1319