]> git.proxmox.com Git - qemu.git/blame - include/qemu/object.h
qom: clean up cast macros
[qemu.git] / include / qemu / object.h
CommitLineData
2f28d2ff
AL
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>
57c9fafe
AL
20#include "qemu-queue.h"
21
22struct Visitor;
23struct Error;
2f28d2ff
AL
24
25struct TypeImpl;
26typedef struct TypeImpl *Type;
27
28typedef struct ObjectClass ObjectClass;
29typedef struct Object Object;
30
31typedef struct TypeInfo TypeInfo;
32
33typedef struct InterfaceClass InterfaceClass;
34typedef struct InterfaceInfo InterfaceInfo;
35
36#define TYPE_OBJECT NULL
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 * typedef struct MyDevice
59 * {
60 * DeviceState parent;
61 *
62 * int reg0, reg1, reg2;
63 * } MyDevice;
64 *
65 * static TypeInfo my_device_info = {
66 * .name = TYPE_MY_DEVICE,
67 * .parent = TYPE_DEVICE,
68 * .instance_size = sizeof(MyDevice),
69 * };
70 *
71 * static void my_device_module_init(void)
72 * {
73 * type_register_static(&my_device_info);
74 * }
75 *
76 * device_init(my_device_module_init);
77 * </programlisting>
78 * </example>
79 *
80 * In the above example, we create a simple type that is described by #TypeInfo.
81 * #TypeInfo describes information about the type including what it inherits
82 * from, the instance and class size, and constructor/destructor hooks.
83 *
84 * Every type has an #ObjectClass associated with it. #ObjectClass derivatives
85 * are instantiated dynamically but there is only ever one instance for any
86 * given type. The #ObjectClass typically holds a table of function pointers
87 * for the virtual methods implemented by this type.
88 *
89 * Using object_new(), a new #Object derivative will be instantiated. You can
90 * cast an #Object to a subclass (or base-class) type using
91 * object_dynamic_cast(). You typically want to define a macro wrapper around
92 * object_dynamic_cast_assert() to make it easier to convert to a specific type.
93 *
94 * # Class Initialization #
95 *
96 * Before an object is initialized, the class for the object must be
97 * initialized. There is only one class object for all instance objects
98 * that is created lazily.
99 *
100 * Classes are initialized by first initializing any parent classes (if
101 * necessary). After the parent class object has initialized, it will be
102 * copied into the current class object and any additional storage in the
103 * class object is zero filled.
104 *
105 * The effect of this is that classes automatically inherit any virtual
106 * function pointers that the parent class has already initialized. All
107 * other fields will be zero filled.
108 *
109 * Once all of the parent classes have been initialized, #TypeInfo::class_init
110 * is called to let the class being instantiated provide default initialize for
111 * it's virtual functions.
112 *
113 * # Interfaces #
114 *
115 * Interfaces allow a limited form of multiple inheritance. Instances are
116 * similar to normal types except for the fact that are only defined by
117 * their classes and never carry any state. You can dynamically cast an object
118 * to one of its #Interface types and vice versa.
119 */
120
57c9fafe
AL
121
122/**
123 * ObjectPropertyAccessor:
124 * @obj: the object that owns the property
125 * @v: the visitor that contains the property data
126 * @opaque: the object property opaque
127 * @name: the name of the property
128 * @errp: a pointer to an Error that is filled if getting/setting fails.
129 *
130 * Called when trying to get/set a property.
131 */
132typedef void (ObjectPropertyAccessor)(Object *obj,
133 struct Visitor *v,
134 void *opaque,
135 const char *name,
136 struct Error **errp);
137
138/**
139 * ObjectPropertyRelease:
140 * @obj: the object that owns the property
141 * @name: the name of the property
142 * @opaque: the opaque registered with the property
143 *
144 * Called when a property is removed from a object.
145 */
146typedef void (ObjectPropertyRelease)(Object *obj,
147 const char *name,
148 void *opaque);
149
150typedef struct ObjectProperty
151{
152 gchar *name;
153 gchar *type;
154 ObjectPropertyAccessor *get;
155 ObjectPropertyAccessor *set;
156 ObjectPropertyRelease *release;
157 void *opaque;
158
159 QTAILQ_ENTRY(ObjectProperty) node;
160} ObjectProperty;
161
2f28d2ff
AL
162/**
163 * ObjectClass:
164 *
165 * The base for all classes. The only thing that #ObjectClass contains is an
166 * integer type handle.
167 */
168struct ObjectClass
169{
170 /*< private >*/
171 Type type;
172};
173
174/**
175 * Object:
176 *
177 * The base for all objects. The first member of this object is a pointer to
178 * a #ObjectClass. Since C guarantees that the first member of a structure
179 * always begins at byte 0 of that structure, as long as any sub-object places
180 * its parent as the first member, we can cast directly to a #Object.
181 *
182 * As a result, #Object contains a reference to the objects type as its
183 * first member. This allows identification of the real type of the object at
184 * run time.
185 *
186 * #Object also contains a list of #Interfaces that this object
187 * implements.
188 */
189struct Object
190{
191 /*< private >*/
192 ObjectClass *class;
2f28d2ff 193 GSList *interfaces;
57c9fafe
AL
194 QTAILQ_HEAD(, ObjectProperty) properties;
195 uint32_t ref;
196 Object *parent;
2f28d2ff
AL
197};
198
199/**
200 * TypeInfo:
201 * @name: The name of the type.
202 * @parent: The name of the parent type.
203 * @instance_size: The size of the object (derivative of #Object). If
204 * @instance_size is 0, then the size of the object will be the size of the
205 * parent object.
206 * @instance_init: This function is called to initialize an object. The parent
207 * class will have already been initialized so the type is only responsible
208 * for initializing its own members.
209 * @instance_finalize: This function is called during object destruction. This
210 * is called before the parent @instance_finalize function has been called.
211 * An object should only free the members that are unique to its type in this
212 * function.
213 * @abstract: If this field is true, then the class is considered abstract and
214 * cannot be directly instantiated.
215 * @class_size: The size of the class object (derivative of #ObjectClass)
216 * for this object. If @class_size is 0, then the size of the class will be
217 * assumed to be the size of the parent class. This allows a type to avoid
218 * implementing an explicit class type if they are not adding additional
219 * virtual functions.
220 * @class_init: This function is called after all parent class initialization
221 * has occured to allow a class to set its default virtual method pointers.
222 * This is also the function to use to override virtual methods from a parent
223 * class.
224 * @class_finalize: This function is called during class destruction and is
225 * meant to release and dynamic parameters allocated by @class_init.
226 * @class_data: Data to pass to the @class_init and @class_finalize functions.
227 * This can be useful when building dynamic classes.
228 * @interfaces: The list of interfaces associated with this type. This
229 * should point to a static array that's terminated with a zero filled
230 * element.
231 */
232struct TypeInfo
233{
234 const char *name;
235 const char *parent;
236
237 size_t instance_size;
238 void (*instance_init)(Object *obj);
239 void (*instance_finalize)(Object *obj);
240
241 bool abstract;
242 size_t class_size;
243
244 void (*class_init)(ObjectClass *klass, void *data);
245 void (*class_finalize)(ObjectClass *klass, void *data);
246 void *class_data;
247
248 InterfaceInfo *interfaces;
249};
250
251/**
252 * OBJECT:
253 * @obj: A derivative of #Object
254 *
255 * Converts an object to a #Object. Since all objects are #Objects,
256 * this function will always succeed.
257 */
258#define OBJECT(obj) \
259 ((Object *)(obj))
260
1ed5b918
PB
261/**
262 * OBJECT_CLASS:
263 * @class: A derivative of #ObjectClas.
264 *
265 * Converts a class to an #ObjectClass. Since all objects are #Objects,
266 * this function will always succeed.
267 */
268#define OBJECT_CLASS(class) \
269 ((ObjectClass *)(class))
270
2f28d2ff
AL
271/**
272 * OBJECT_CHECK:
273 * @type: The C type to use for the return value.
274 * @obj: A derivative of @type to cast.
275 * @name: The QOM typename of @type
276 *
277 * A type safe version of @object_dynamic_cast_assert. Typically each class
278 * will define a macro based on this type to perform type safe dynamic_casts to
279 * this object type.
280 *
281 * If an invalid object is passed to this function, a run time assert will be
282 * generated.
283 */
284#define OBJECT_CHECK(type, obj, name) \
1ed5b918 285 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name)))
2f28d2ff
AL
286
287/**
288 * OBJECT_CLASS_CHECK:
289 * @class: The C type to use for the return value.
290 * @obj: A derivative of @type to cast.
291 * @name: the QOM typename of @class.
292 *
1ed5b918
PB
293 * A type safe version of @object_class_dynamic_cast_assert. This macro is
294 * typically wrapped by each type to perform type safe casts of a class to a
295 * specific class type.
2f28d2ff
AL
296 */
297#define OBJECT_CLASS_CHECK(class, obj, name) \
1ed5b918 298 ((class *)object_class_dynamic_cast_assert(OBJECT_CLASS(obj), (name)))
2f28d2ff
AL
299
300/**
301 * OBJECT_GET_CLASS:
302 * @class: The C type to use for the return value.
303 * @obj: The object to obtain the class for.
304 * @name: The QOM typename of @obj.
305 *
306 * This function will return a specific class for a given object. Its generally
307 * used by each type to provide a type safe macro to get a specific class type
308 * from an object.
309 */
310#define OBJECT_GET_CLASS(class, obj, name) \
311 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
312
2f28d2ff
AL
313/**
314 * InterfaceClass:
315 * @parent_class: the base class
316 *
317 * The class for all interfaces. Subclasses of this class should only add
318 * virtual methods.
319 */
320struct InterfaceClass
321{
322 ObjectClass parent_class;
323};
324
325/**
326 * InterfaceInfo:
327 * @type: The name of the interface.
328 * @interface_initfn: This method is called during class initialization and is
329 * used to initialize an interface associated with a class. This function
330 * should initialize any default virtual functions for a class and/or override
331 * virtual functions in a parent class.
332 *
333 * The information associated with an interface.
334 */
335struct InterfaceInfo
336{
337 const char *type;
338
339 void (*interface_initfn)(ObjectClass *class, void *data);
340};
341
342#define TYPE_INTERFACE "interface"
343
344/**
345 * object_new:
346 * @typename: The name of the type of the object to instantiate.
347 *
348 * This function will initialize a new object using heap allocated memory. This
349 * function should be paired with object_delete() to free the resources
350 * associated with the object.
351 *
352 * Returns: The newly allocated and instantiated object.
353 */
354Object *object_new(const char *typename);
355
356/**
357 * object_new_with_type:
358 * @type: The type of the object to instantiate.
359 *
360 * This function will initialize a new object using heap allocated memory. This
361 * function should be paired with object_delete() to free the resources
362 * associated with the object.
363 *
364 * Returns: The newly allocated and instantiated object.
365 */
366Object *object_new_with_type(Type type);
367
368/**
369 * object_delete:
370 * @obj: The object to free.
371 *
372 * Finalize an object and then free the memory associated with it. This should
373 * be paired with object_new() to free the resources associated with an object.
374 */
375void object_delete(Object *obj);
376
377/**
378 * object_initialize_with_type:
379 * @obj: A pointer to the memory to be used for the object.
380 * @type: The type of the object to instantiate.
381 *
382 * This function will initialize an object. The memory for the object should
383 * have already been allocated.
384 */
385void object_initialize_with_type(void *data, Type type);
386
387/**
388 * object_initialize:
389 * @obj: A pointer to the memory to be used for the object.
390 * @typename: The name of the type of the object to instantiate.
391 *
392 * This function will initialize an object. The memory for the object should
393 * have already been allocated.
394 */
395void object_initialize(void *obj, const char *typename);
396
397/**
398 * object_finalize:
399 * @obj: The object to finalize.
400 *
401 * This function destroys and object without freeing the memory associated with
402 * it.
403 */
404void object_finalize(void *obj);
405
406/**
407 * object_dynamic_cast:
408 * @obj: The object to cast.
409 * @typename: The @typename to cast to.
410 *
411 * This function will determine if @obj is-a @typename. @obj can refer to an
412 * object or an interface associated with an object.
413 *
414 * Returns: This function returns @obj on success or #NULL on failure.
415 */
416Object *object_dynamic_cast(Object *obj, const char *typename);
417
418/**
419 * @object_dynamic_cast_assert:
420 *
421 * See object_dynamic_cast() for a description of the parameters of this
422 * function. The only difference in behavior is that this function asserts
423 * instead of returning #NULL on failure.
424 */
425Object *object_dynamic_cast_assert(Object *obj, const char *typename);
426
427/**
428 * object_get_class:
429 * @obj: A derivative of #Object
430 *
431 * Returns: The #ObjectClass of the type associated with @obj.
432 */
433ObjectClass *object_get_class(Object *obj);
434
435/**
436 * object_get_typename:
437 * @obj: A derivative of #Object.
438 *
439 * Returns: The QOM typename of @obj.
440 */
441const char *object_get_typename(Object *obj);
442
443/**
444 * type_register_static:
445 * @info: The #TypeInfo of the new type.
446 *
447 * @info and all of the strings it points to should exist for the life time
448 * that the type is registered.
449 *
450 * Returns: 0 on failure, the new #Type on success.
451 */
452Type type_register_static(const TypeInfo *info);
453
39bffca2
AL
454#define type_register_static_alias(info, name) do { } while (0)
455
2f28d2ff
AL
456/**
457 * type_register:
458 * @info: The #TypeInfo of the new type
459 *
460 * Unlike type_register_static(), this call does not require @info or it's
461 * string members to continue to exist after the call returns.
462 *
463 * Returns: 0 on failure, the new #Type on success.
464 */
465Type type_register(const TypeInfo *info);
466
467/**
468 * object_class_dynamic_cast_assert:
469 * @klass: The #ObjectClass to attempt to cast.
470 * @typename: The QOM typename of the class to cast to.
471 *
472 * Returns: This function always returns @klass and asserts on failure.
473 */
474ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
475 const char *typename);
476
477ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
478 const char *typename);
479
480/**
481 * object_class_get_name:
482 * @klass: The class to obtain the QOM typename for.
483 *
484 * Returns: The QOM typename for @klass.
485 */
486const char *object_class_get_name(ObjectClass *klass);
487
488ObjectClass *object_class_by_name(const char *typename);
489
490void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
93c511a1 491 const char *implements_type, bool include_abstract,
2f28d2ff 492 void *opaque);
57c9fafe
AL
493/**
494 * object_ref:
495 * @obj: the object
496 *
497 * Increase the reference count of a object. A object cannot be freed as long
498 * as its reference count is greater than zero.
499 */
500void object_ref(Object *obj);
501
502/**
503 * qdef_unref:
504 * @obj: the object
505 *
506 * Decrease the reference count of a object. A object cannot be freed as long
507 * as its reference count is greater than zero.
508 */
509void object_unref(Object *obj);
510
511/**
512 * object_property_add:
513 * @obj: the object to add a property to
514 * @name: the name of the property. This can contain any character except for
515 * a forward slash. In general, you should use hyphens '-' instead of
516 * underscores '_' when naming properties.
517 * @type: the type name of the property. This namespace is pretty loosely
518 * defined. Sub namespaces are constructed by using a prefix and then
519 * to angle brackets. For instance, the type 'virtio-net-pci' in the
520 * 'link' namespace would be 'link<virtio-net-pci>'.
521 * @get: The getter to be called to read a property. If this is NULL, then
522 * the property cannot be read.
523 * @set: the setter to be called to write a property. If this is NULL,
524 * then the property cannot be written.
525 * @release: called when the property is removed from the object. This is
526 * meant to allow a property to free its opaque upon object
527 * destruction. This may be NULL.
528 * @opaque: an opaque pointer to pass to the callbacks for the property
529 * @errp: returns an error if this function fails
530 */
531void object_property_add(Object *obj, const char *name, const char *type,
532 ObjectPropertyAccessor *get,
533 ObjectPropertyAccessor *set,
534 ObjectPropertyRelease *release,
535 void *opaque, struct Error **errp);
536
537void object_property_del(Object *obj, const char *name, struct Error **errp);
538
539void object_unparent(Object *obj);
540
541/**
542 * object_property_get:
543 * @obj: the object
544 * @v: the visitor that will receive the property value. This should be an
545 * Output visitor and the data will be written with @name as the name.
546 * @name: the name of the property
547 * @errp: returns an error if this function fails
548 *
549 * Reads a property from a object.
550 */
551void object_property_get(Object *obj, struct Visitor *v, const char *name,
552 struct Error **errp);
553
554/**
555 * object_property_set:
556 * @obj: the object
557 * @v: the visitor that will be used to write the property value. This should
558 * be an Input visitor and the data will be first read with @name as the
559 * name and then written as the property value.
560 * @name: the name of the property
561 * @errp: returns an error if this function fails
562 *
563 * Writes a property to a object.
564 */
565void object_property_set(Object *obj, struct Visitor *v, const char *name,
566 struct Error **errp);
567
568/**
569 * @object_property_get_type:
570 * @obj: the object
571 * @name: the name of the property
572 * @errp: returns an error if this function fails
573 *
574 * Returns: The type name of the property.
575 */
576const char *object_property_get_type(Object *obj, const char *name,
577 struct Error **errp);
578
579/**
580 * object_get_root:
581 *
582 * Returns: the root object of the composition tree
583 */
584Object *object_get_root(void);
585
586/**
587 * object_get_canonical_path:
588 *
589 * Returns: The canonical path for a object. This is the path within the
590 * composition tree starting from the root.
591 */
592gchar *object_get_canonical_path(Object *obj);
593
594/**
595 * object_resolve_path:
596 * @path: the path to resolve
597 * @ambiguous: returns true if the path resolution failed because of an
598 * ambiguous match
599 *
600 * There are two types of supported paths--absolute paths and partial paths.
601 *
602 * Absolute paths are derived from the root object and can follow child<> or
603 * link<> properties. Since they can follow link<> properties, they can be
604 * arbitrarily long. Absolute paths look like absolute filenames and are
605 * prefixed with a leading slash.
606 *
607 * Partial paths look like relative filenames. They do not begin with a
608 * prefix. The matching rules for partial paths are subtle but designed to make
609 * specifying objects easy. At each level of the composition tree, the partial
610 * path is matched as an absolute path. The first match is not returned. At
611 * least two matches are searched for. A successful result is only returned if
612 * only one match is founded. If more than one match is found, a flag is
613 * return to indicate that the match was ambiguous.
614 *
615 * Returns: The matched object or NULL on path lookup failure.
616 */
617Object *object_resolve_path(const char *path, bool *ambiguous);
618
619/**
620 * object_property_add_child:
621 * @obj: the object to add a property to
622 * @name: the name of the property
623 * @child: the child object
624 * @errp: if an error occurs, a pointer to an area to store the area
625 *
626 * Child properties form the composition tree. All objects need to be a child
627 * of another object. Objects can only be a child of one object.
628 *
629 * There is no way for a child to determine what its parent is. It is not
630 * a bidirectional relationship. This is by design.
631 */
632void object_property_add_child(Object *obj, const char *name,
633 Object *child, struct Error **errp);
634
635/**
636 * object_property_add_link:
637 * @obj: the object to add a property to
638 * @name: the name of the property
639 * @type: the qobj type of the link
640 * @child: a pointer to where the link object reference is stored
641 * @errp: if an error occurs, a pointer to an area to store the area
642 *
643 * Links establish relationships between objects. Links are unidirectional
644 * although two links can be combined to form a bidirectional relationship
645 * between objects.
646 *
647 * Links form the graph in the object model.
648 */
649void object_property_add_link(Object *obj, const char *name,
650 const char *type, Object **child,
651 struct Error **errp);
652
653/**
654 * object_property_add_str:
655 * @obj: the object to add a property to
656 * @name: the name of the property
657 * @get: the getter or NULL if the property is write-only. This function must
658 * return a string to be freed by g_free().
659 * @set: the setter or NULL if the property is read-only
660 * @errp: if an error occurs, a pointer to an area to store the error
661 *
662 * Add a string property using getters/setters. This function will add a
663 * property of type 'string'.
664 */
665void object_property_add_str(Object *obj, const char *name,
666 char *(*get)(Object *, struct Error **),
667 void (*set)(Object *, const char *, struct Error **),
668 struct Error **errp);
2f28d2ff
AL
669
670#endif