]> git.proxmox.com Git - mirror_qemu.git/blob - include/qemu/object.h
qom: move properties from qdev to object
[mirror_qemu.git] / include / qemu / object.h
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
22 struct Visitor;
23 struct Error;
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 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
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 */
132 typedef 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 */
146 typedef void (ObjectPropertyRelease)(Object *obj,
147 const char *name,
148 void *opaque);
149
150 typedef 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
162 /**
163 * ObjectClass:
164 *
165 * The base for all classes. The only thing that #ObjectClass contains is an
166 * integer type handle.
167 */
168 struct 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 */
189 struct Object
190 {
191 /*< private >*/
192 ObjectClass *class;
193 GSList *interfaces;
194 QTAILQ_HEAD(, ObjectProperty) properties;
195 uint32_t ref;
196 Object *parent;
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 */
232 struct 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
261 /**
262 * OBJECT_CHECK:
263 * @type: The C type to use for the return value.
264 * @obj: A derivative of @type to cast.
265 * @name: The QOM typename of @type
266 *
267 * A type safe version of @object_dynamic_cast_assert. Typically each class
268 * will define a macro based on this type to perform type safe dynamic_casts to
269 * this object type.
270 *
271 * If an invalid object is passed to this function, a run time assert will be
272 * generated.
273 */
274 #define OBJECT_CHECK(type, obj, name) \
275 ((type *)object_dynamic_cast_assert((Object *)(obj), (name)))
276
277 /**
278 * OBJECT_CLASS_CHECK:
279 * @class: The C type to use for the return value.
280 * @obj: A derivative of @type to cast.
281 * @name: the QOM typename of @class.
282 *
283 * A type safe version of @object_check_class. This macro is typically wrapped
284 * by each type to perform type safe casts of a class to a specific class type.
285 */
286 #define OBJECT_CLASS_CHECK(class, obj, name) \
287 ((class *)object_class_dynamic_cast_assert((ObjectClass *)(obj), (name)))
288
289 /**
290 * OBJECT_GET_CLASS:
291 * @class: The C type to use for the return value.
292 * @obj: The object to obtain the class for.
293 * @name: The QOM typename of @obj.
294 *
295 * This function will return a specific class for a given object. Its generally
296 * used by each type to provide a type safe macro to get a specific class type
297 * from an object.
298 */
299 #define OBJECT_GET_CLASS(class, obj, name) \
300 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
301
302 #define OBJECT_CLASS(class) \
303 ((ObjectClass *)(class))
304
305 /**
306 * InterfaceClass:
307 * @parent_class: the base class
308 *
309 * The class for all interfaces. Subclasses of this class should only add
310 * virtual methods.
311 */
312 struct InterfaceClass
313 {
314 ObjectClass parent_class;
315 };
316
317 /**
318 * InterfaceInfo:
319 * @type: The name of the interface.
320 * @interface_initfn: This method is called during class initialization and is
321 * used to initialize an interface associated with a class. This function
322 * should initialize any default virtual functions for a class and/or override
323 * virtual functions in a parent class.
324 *
325 * The information associated with an interface.
326 */
327 struct InterfaceInfo
328 {
329 const char *type;
330
331 void (*interface_initfn)(ObjectClass *class, void *data);
332 };
333
334 #define TYPE_INTERFACE "interface"
335
336 /**
337 * object_new:
338 * @typename: The name of the type of the object to instantiate.
339 *
340 * This function will initialize a new object using heap allocated memory. This
341 * function should be paired with object_delete() to free the resources
342 * associated with the object.
343 *
344 * Returns: The newly allocated and instantiated object.
345 */
346 Object *object_new(const char *typename);
347
348 /**
349 * object_new_with_type:
350 * @type: The type of the object to instantiate.
351 *
352 * This function will initialize a new object using heap allocated memory. This
353 * function should be paired with object_delete() to free the resources
354 * associated with the object.
355 *
356 * Returns: The newly allocated and instantiated object.
357 */
358 Object *object_new_with_type(Type type);
359
360 /**
361 * object_delete:
362 * @obj: The object to free.
363 *
364 * Finalize an object and then free the memory associated with it. This should
365 * be paired with object_new() to free the resources associated with an object.
366 */
367 void object_delete(Object *obj);
368
369 /**
370 * object_initialize_with_type:
371 * @obj: A pointer to the memory to be used for the object.
372 * @type: The type of the object to instantiate.
373 *
374 * This function will initialize an object. The memory for the object should
375 * have already been allocated.
376 */
377 void object_initialize_with_type(void *data, Type type);
378
379 /**
380 * object_initialize:
381 * @obj: A pointer to the memory to be used for the object.
382 * @typename: The name of the type of the object to instantiate.
383 *
384 * This function will initialize an object. The memory for the object should
385 * have already been allocated.
386 */
387 void object_initialize(void *obj, const char *typename);
388
389 /**
390 * object_finalize:
391 * @obj: The object to finalize.
392 *
393 * This function destroys and object without freeing the memory associated with
394 * it.
395 */
396 void object_finalize(void *obj);
397
398 /**
399 * object_dynamic_cast:
400 * @obj: The object to cast.
401 * @typename: The @typename to cast to.
402 *
403 * This function will determine if @obj is-a @typename. @obj can refer to an
404 * object or an interface associated with an object.
405 *
406 * Returns: This function returns @obj on success or #NULL on failure.
407 */
408 Object *object_dynamic_cast(Object *obj, const char *typename);
409
410 /**
411 * @object_dynamic_cast_assert:
412 *
413 * See object_dynamic_cast() for a description of the parameters of this
414 * function. The only difference in behavior is that this function asserts
415 * instead of returning #NULL on failure.
416 */
417 Object *object_dynamic_cast_assert(Object *obj, const char *typename);
418
419 /**
420 * object_get_class:
421 * @obj: A derivative of #Object
422 *
423 * Returns: The #ObjectClass of the type associated with @obj.
424 */
425 ObjectClass *object_get_class(Object *obj);
426
427 /**
428 * object_get_typename:
429 * @obj: A derivative of #Object.
430 *
431 * Returns: The QOM typename of @obj.
432 */
433 const char *object_get_typename(Object *obj);
434
435 /**
436 * type_register_static:
437 * @info: The #TypeInfo of the new type.
438 *
439 * @info and all of the strings it points to should exist for the life time
440 * that the type is registered.
441 *
442 * Returns: 0 on failure, the new #Type on success.
443 */
444 Type type_register_static(const TypeInfo *info);
445
446 #define type_register_static_alias(info, name) do { } while (0)
447
448 /**
449 * type_register:
450 * @info: The #TypeInfo of the new type
451 *
452 * Unlike type_register_static(), this call does not require @info or it's
453 * string members to continue to exist after the call returns.
454 *
455 * Returns: 0 on failure, the new #Type on success.
456 */
457 Type type_register(const TypeInfo *info);
458
459 /**
460 * object_class_dynamic_cast_assert:
461 * @klass: The #ObjectClass to attempt to cast.
462 * @typename: The QOM typename of the class to cast to.
463 *
464 * Returns: This function always returns @klass and asserts on failure.
465 */
466 ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
467 const char *typename);
468
469 ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
470 const char *typename);
471
472 /**
473 * object_class_get_name:
474 * @klass: The class to obtain the QOM typename for.
475 *
476 * Returns: The QOM typename for @klass.
477 */
478 const char *object_class_get_name(ObjectClass *klass);
479
480 ObjectClass *object_class_by_name(const char *typename);
481
482 void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
483 const char *implements_type, bool include_abstract,
484 void *opaque);
485 /**
486 * object_ref:
487 * @obj: the object
488 *
489 * Increase the reference count of a object. A object cannot be freed as long
490 * as its reference count is greater than zero.
491 */
492 void object_ref(Object *obj);
493
494 /**
495 * qdef_unref:
496 * @obj: the object
497 *
498 * Decrease the reference count of a object. A object cannot be freed as long
499 * as its reference count is greater than zero.
500 */
501 void object_unref(Object *obj);
502
503 /**
504 * object_property_add:
505 * @obj: the object to add a property to
506 * @name: the name of the property. This can contain any character except for
507 * a forward slash. In general, you should use hyphens '-' instead of
508 * underscores '_' when naming properties.
509 * @type: the type name of the property. This namespace is pretty loosely
510 * defined. Sub namespaces are constructed by using a prefix and then
511 * to angle brackets. For instance, the type 'virtio-net-pci' in the
512 * 'link' namespace would be 'link<virtio-net-pci>'.
513 * @get: The getter to be called to read a property. If this is NULL, then
514 * the property cannot be read.
515 * @set: the setter to be called to write a property. If this is NULL,
516 * then the property cannot be written.
517 * @release: called when the property is removed from the object. This is
518 * meant to allow a property to free its opaque upon object
519 * destruction. This may be NULL.
520 * @opaque: an opaque pointer to pass to the callbacks for the property
521 * @errp: returns an error if this function fails
522 */
523 void object_property_add(Object *obj, const char *name, const char *type,
524 ObjectPropertyAccessor *get,
525 ObjectPropertyAccessor *set,
526 ObjectPropertyRelease *release,
527 void *opaque, struct Error **errp);
528
529 void object_property_del(Object *obj, const char *name, struct Error **errp);
530
531 void object_unparent(Object *obj);
532
533 /**
534 * object_property_get:
535 * @obj: the object
536 * @v: the visitor that will receive the property value. This should be an
537 * Output visitor and the data will be written with @name as the name.
538 * @name: the name of the property
539 * @errp: returns an error if this function fails
540 *
541 * Reads a property from a object.
542 */
543 void object_property_get(Object *obj, struct Visitor *v, const char *name,
544 struct Error **errp);
545
546 /**
547 * object_property_set:
548 * @obj: the object
549 * @v: the visitor that will be used to write the property value. This should
550 * be an Input visitor and the data will be first read with @name as the
551 * name and then written as the property value.
552 * @name: the name of the property
553 * @errp: returns an error if this function fails
554 *
555 * Writes a property to a object.
556 */
557 void object_property_set(Object *obj, struct Visitor *v, const char *name,
558 struct Error **errp);
559
560 /**
561 * @object_property_get_type:
562 * @obj: the object
563 * @name: the name of the property
564 * @errp: returns an error if this function fails
565 *
566 * Returns: The type name of the property.
567 */
568 const char *object_property_get_type(Object *obj, const char *name,
569 struct Error **errp);
570
571 /**
572 * object_get_root:
573 *
574 * Returns: the root object of the composition tree
575 */
576 Object *object_get_root(void);
577
578 /**
579 * object_get_canonical_path:
580 *
581 * Returns: The canonical path for a object. This is the path within the
582 * composition tree starting from the root.
583 */
584 gchar *object_get_canonical_path(Object *obj);
585
586 /**
587 * object_resolve_path:
588 * @path: the path to resolve
589 * @ambiguous: returns true if the path resolution failed because of an
590 * ambiguous match
591 *
592 * There are two types of supported paths--absolute paths and partial paths.
593 *
594 * Absolute paths are derived from the root object and can follow child<> or
595 * link<> properties. Since they can follow link<> properties, they can be
596 * arbitrarily long. Absolute paths look like absolute filenames and are
597 * prefixed with a leading slash.
598 *
599 * Partial paths look like relative filenames. They do not begin with a
600 * prefix. The matching rules for partial paths are subtle but designed to make
601 * specifying objects easy. At each level of the composition tree, the partial
602 * path is matched as an absolute path. The first match is not returned. At
603 * least two matches are searched for. A successful result is only returned if
604 * only one match is founded. If more than one match is found, a flag is
605 * return to indicate that the match was ambiguous.
606 *
607 * Returns: The matched object or NULL on path lookup failure.
608 */
609 Object *object_resolve_path(const char *path, bool *ambiguous);
610
611 /**
612 * object_property_add_child:
613 * @obj: the object to add a property to
614 * @name: the name of the property
615 * @child: the child object
616 * @errp: if an error occurs, a pointer to an area to store the area
617 *
618 * Child properties form the composition tree. All objects need to be a child
619 * of another object. Objects can only be a child of one object.
620 *
621 * There is no way for a child to determine what its parent is. It is not
622 * a bidirectional relationship. This is by design.
623 */
624 void object_property_add_child(Object *obj, const char *name,
625 Object *child, struct Error **errp);
626
627 /**
628 * object_property_add_link:
629 * @obj: the object to add a property to
630 * @name: the name of the property
631 * @type: the qobj type of the link
632 * @child: a pointer to where the link object reference is stored
633 * @errp: if an error occurs, a pointer to an area to store the area
634 *
635 * Links establish relationships between objects. Links are unidirectional
636 * although two links can be combined to form a bidirectional relationship
637 * between objects.
638 *
639 * Links form the graph in the object model.
640 */
641 void object_property_add_link(Object *obj, const char *name,
642 const char *type, Object **child,
643 struct Error **errp);
644
645 /**
646 * object_property_add_str:
647 * @obj: the object to add a property to
648 * @name: the name of the property
649 * @get: the getter or NULL if the property is write-only. This function must
650 * return a string to be freed by g_free().
651 * @set: the setter or NULL if the property is read-only
652 * @errp: if an error occurs, a pointer to an area to store the error
653 *
654 * Add a string property using getters/setters. This function will add a
655 * property of type 'string'.
656 */
657 void object_property_add_str(Object *obj, const char *name,
658 char *(*get)(Object *, struct Error **),
659 void (*set)(Object *, const char *, struct Error **),
660 struct Error **errp);
661
662 #endif