qom: Split out object and class caches
[qemu.git] / include / qom / 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>
1de7afc9 20#include "qemu/queue.h"
008e0566 21#include "qapi/error.h"
57c9fafe
AL
22
23struct Visitor;
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
745549c8 36#define TYPE_OBJECT "object"
2f28d2ff
AL
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 *
0815a859
PB
58 * // No new virtual functions: we can reuse the typedef for the
59 * // superclass.
60 * typedef DeviceClass MyDeviceClass;
2f28d2ff
AL
61 * typedef struct MyDevice
62 * {
63 * DeviceState parent;
64 *
65 * int reg0, reg1, reg2;
66 * } MyDevice;
67 *
8c43a6f0 68 * static const TypeInfo my_device_info = {
2f28d2ff
AL
69 * .name = TYPE_MY_DEVICE,
70 * .parent = TYPE_DEVICE,
71 * .instance_size = sizeof(MyDevice),
72 * };
73 *
83f7d43a 74 * static void my_device_register_types(void)
2f28d2ff
AL
75 * {
76 * type_register_static(&my_device_info);
77 * }
78 *
83f7d43a 79 * type_init(my_device_register_types)
2f28d2ff
AL
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
0815a859
PB
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>
2f28d2ff
AL
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
93148aa5 127 * its virtual functions. Here is how the above example might be modified
0815a859
PB
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 *
8c43a6f0 141 * static const TypeInfo my_device_info = {
0815a859
PB
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 *
782beb52
AF
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:
0815a859
PB
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 *
8c43a6f0 166 * static const TypeInfo my_device_info = {
0815a859
PB
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>
2f28d2ff
AL
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.
782beb52
AF
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.
085d8134 205 * The original function is not automatically invoked. It is the responsibility
782beb52
AF
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;
70392912 252 * } DerivedClass;
782beb52
AF
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.
2f28d2ff
AL
287 */
288
57c9fafe
AL
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 */
300typedef void (ObjectPropertyAccessor)(Object *obj,
301 struct Visitor *v,
302 void *opaque,
303 const char *name,
e82df248 304 Error **errp);
57c9fafe
AL
305
306/**
307 * ObjectPropertyRelease:
308 * @obj: the object that owns the property
309 * @name: the name of the property
310 * @opaque: the opaque registered with the property
311 *
312 * Called when a property is removed from a object.
313 */
314typedef void (ObjectPropertyRelease)(Object *obj,
315 const char *name,
316 void *opaque);
317
318typedef struct ObjectProperty
319{
320 gchar *name;
321 gchar *type;
322 ObjectPropertyAccessor *get;
323 ObjectPropertyAccessor *set;
324 ObjectPropertyRelease *release;
325 void *opaque;
326
327 QTAILQ_ENTRY(ObjectProperty) node;
328} ObjectProperty;
329
667d22d1
PB
330/**
331 * ObjectUnparent:
332 * @obj: the object that is being removed from the composition tree
333 *
334 * Called when an object is being removed from the QOM composition tree.
335 * The function should remove any backlinks from children objects to @obj.
336 */
337typedef void (ObjectUnparent)(Object *obj);
338
fde9bf44
PB
339/**
340 * ObjectFree:
341 * @obj: the object being freed
342 *
343 * Called when an object's last reference is removed.
344 */
345typedef void (ObjectFree)(void *obj);
346
03587328
AL
347#define OBJECT_CLASS_CAST_CACHE 4
348
2f28d2ff
AL
349/**
350 * ObjectClass:
351 *
352 * The base for all classes. The only thing that #ObjectClass contains is an
353 * integer type handle.
354 */
355struct ObjectClass
356{
357 /*< private >*/
358 Type type;
33e95c63 359 GSList *interfaces;
667d22d1 360
0ab4c94c
PC
361 const char *object_cast_cache[OBJECT_CLASS_CAST_CACHE];
362 const char *class_cast_cache[OBJECT_CLASS_CAST_CACHE];
03587328 363
667d22d1 364 ObjectUnparent *unparent;
2f28d2ff
AL
365};
366
367/**
368 * Object:
369 *
370 * The base for all objects. The first member of this object is a pointer to
371 * a #ObjectClass. Since C guarantees that the first member of a structure
372 * always begins at byte 0 of that structure, as long as any sub-object places
373 * its parent as the first member, we can cast directly to a #Object.
374 *
375 * As a result, #Object contains a reference to the objects type as its
376 * first member. This allows identification of the real type of the object at
377 * run time.
378 *
379 * #Object also contains a list of #Interfaces that this object
380 * implements.
381 */
382struct Object
383{
384 /*< private >*/
385 ObjectClass *class;
fde9bf44 386 ObjectFree *free;
57c9fafe
AL
387 QTAILQ_HEAD(, ObjectProperty) properties;
388 uint32_t ref;
389 Object *parent;
2f28d2ff
AL
390};
391
392/**
393 * TypeInfo:
394 * @name: The name of the type.
395 * @parent: The name of the parent type.
396 * @instance_size: The size of the object (derivative of #Object). If
397 * @instance_size is 0, then the size of the object will be the size of the
398 * parent object.
399 * @instance_init: This function is called to initialize an object. The parent
400 * class will have already been initialized so the type is only responsible
401 * for initializing its own members.
8231c2dd
EH
402 * @instance_post_init: This function is called to finish initialization of
403 * an object, after all @instance_init functions were called.
2f28d2ff
AL
404 * @instance_finalize: This function is called during object destruction. This
405 * is called before the parent @instance_finalize function has been called.
406 * An object should only free the members that are unique to its type in this
407 * function.
408 * @abstract: If this field is true, then the class is considered abstract and
409 * cannot be directly instantiated.
410 * @class_size: The size of the class object (derivative of #ObjectClass)
411 * for this object. If @class_size is 0, then the size of the class will be
412 * assumed to be the size of the parent class. This allows a type to avoid
413 * implementing an explicit class type if they are not adding additional
414 * virtual functions.
415 * @class_init: This function is called after all parent class initialization
441dd5eb 416 * has occurred to allow a class to set its default virtual method pointers.
2f28d2ff
AL
417 * This is also the function to use to override virtual methods from a parent
418 * class.
3b50e311
PB
419 * @class_base_init: This function is called for all base classes after all
420 * parent class initialization has occurred, but before the class itself
421 * is initialized. This is the function to use to undo the effects of
422 * memcpy from the parent class to the descendents.
2f28d2ff
AL
423 * @class_finalize: This function is called during class destruction and is
424 * meant to release and dynamic parameters allocated by @class_init.
3b50e311
PB
425 * @class_data: Data to pass to the @class_init, @class_base_init and
426 * @class_finalize functions. This can be useful when building dynamic
427 * classes.
2f28d2ff
AL
428 * @interfaces: The list of interfaces associated with this type. This
429 * should point to a static array that's terminated with a zero filled
430 * element.
431 */
432struct TypeInfo
433{
434 const char *name;
435 const char *parent;
436
437 size_t instance_size;
438 void (*instance_init)(Object *obj);
8231c2dd 439 void (*instance_post_init)(Object *obj);
2f28d2ff
AL
440 void (*instance_finalize)(Object *obj);
441
442 bool abstract;
443 size_t class_size;
444
445 void (*class_init)(ObjectClass *klass, void *data);
3b50e311 446 void (*class_base_init)(ObjectClass *klass, void *data);
2f28d2ff
AL
447 void (*class_finalize)(ObjectClass *klass, void *data);
448 void *class_data;
449
450 InterfaceInfo *interfaces;
451};
452
453/**
454 * OBJECT:
455 * @obj: A derivative of #Object
456 *
457 * Converts an object to a #Object. Since all objects are #Objects,
458 * this function will always succeed.
459 */
460#define OBJECT(obj) \
461 ((Object *)(obj))
462
1ed5b918
PB
463/**
464 * OBJECT_CLASS:
a0dbf408 465 * @class: A derivative of #ObjectClass.
1ed5b918
PB
466 *
467 * Converts a class to an #ObjectClass. Since all objects are #Objects,
468 * this function will always succeed.
469 */
470#define OBJECT_CLASS(class) \
471 ((ObjectClass *)(class))
472
2f28d2ff
AL
473/**
474 * OBJECT_CHECK:
475 * @type: The C type to use for the return value.
476 * @obj: A derivative of @type to cast.
477 * @name: The QOM typename of @type
478 *
479 * A type safe version of @object_dynamic_cast_assert. Typically each class
480 * will define a macro based on this type to perform type safe dynamic_casts to
481 * this object type.
482 *
483 * If an invalid object is passed to this function, a run time assert will be
484 * generated.
485 */
486#define OBJECT_CHECK(type, obj, name) \
be17f18b
PB
487 ((type *)object_dynamic_cast_assert(OBJECT(obj), (name), \
488 __FILE__, __LINE__, __func__))
2f28d2ff
AL
489
490/**
491 * OBJECT_CLASS_CHECK:
492 * @class: The C type to use for the return value.
493 * @obj: A derivative of @type to cast.
494 * @name: the QOM typename of @class.
495 *
1ed5b918
PB
496 * A type safe version of @object_class_dynamic_cast_assert. This macro is
497 * typically wrapped by each type to perform type safe casts of a class to a
498 * specific class type.
2f28d2ff
AL
499 */
500#define OBJECT_CLASS_CHECK(class, obj, name) \
be17f18b
PB
501 ((class *)object_class_dynamic_cast_assert(OBJECT_CLASS(obj), (name), \
502 __FILE__, __LINE__, __func__))
2f28d2ff
AL
503
504/**
505 * OBJECT_GET_CLASS:
506 * @class: The C type to use for the return value.
507 * @obj: The object to obtain the class for.
508 * @name: The QOM typename of @obj.
509 *
510 * This function will return a specific class for a given object. Its generally
511 * used by each type to provide a type safe macro to get a specific class type
512 * from an object.
513 */
514#define OBJECT_GET_CLASS(class, obj, name) \
515 OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
516
33e95c63
AL
517/**
518 * InterfaceInfo:
519 * @type: The name of the interface.
520 *
521 * The information associated with an interface.
522 */
523struct InterfaceInfo {
524 const char *type;
525};
526
2f28d2ff
AL
527/**
528 * InterfaceClass:
529 * @parent_class: the base class
530 *
531 * The class for all interfaces. Subclasses of this class should only add
532 * virtual methods.
533 */
534struct InterfaceClass
535{
536 ObjectClass parent_class;
33e95c63
AL
537 /*< private >*/
538 ObjectClass *concrete_class;
2f28d2ff
AL
539};
540
33e95c63
AL
541#define TYPE_INTERFACE "interface"
542
2f28d2ff 543/**
33e95c63
AL
544 * INTERFACE_CLASS:
545 * @klass: class to cast from
546 * Returns: An #InterfaceClass or raise an error if cast is invalid
2f28d2ff 547 */
33e95c63
AL
548#define INTERFACE_CLASS(klass) \
549 OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
2f28d2ff 550
33e95c63
AL
551/**
552 * INTERFACE_CHECK:
553 * @interface: the type to return
554 * @obj: the object to convert to an interface
555 * @name: the interface type name
556 *
557 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
558 */
559#define INTERFACE_CHECK(interface, obj, name) \
be17f18b
PB
560 ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name), \
561 __FILE__, __LINE__, __func__))
2f28d2ff
AL
562
563/**
564 * object_new:
565 * @typename: The name of the type of the object to instantiate.
566 *
b76facc3
PB
567 * This function will initialize a new object using heap allocated memory.
568 * The returned object has a reference count of 1, and will be freed when
569 * the last reference is dropped.
2f28d2ff
AL
570 *
571 * Returns: The newly allocated and instantiated object.
572 */
573Object *object_new(const char *typename);
574
575/**
576 * object_new_with_type:
577 * @type: The type of the object to instantiate.
578 *
b76facc3
PB
579 * This function will initialize a new object using heap allocated memory.
580 * The returned object has a reference count of 1, and will be freed when
581 * the last reference is dropped.
2f28d2ff
AL
582 *
583 * Returns: The newly allocated and instantiated object.
584 */
585Object *object_new_with_type(Type type);
586
2f28d2ff
AL
587/**
588 * object_initialize_with_type:
53caad9a 589 * @data: A pointer to the memory to be used for the object.
5b9237f6 590 * @size: The maximum size available at @data for the object.
2f28d2ff
AL
591 * @type: The type of the object to instantiate.
592 *
593 * This function will initialize an object. The memory for the object should
b76facc3
PB
594 * have already been allocated. The returned object has a reference count of 1,
595 * and will be finalized when the last reference is dropped.
2f28d2ff 596 */
5b9237f6 597void object_initialize_with_type(void *data, size_t size, Type type);
2f28d2ff
AL
598
599/**
600 * object_initialize:
601 * @obj: A pointer to the memory to be used for the object.
213f0c4f 602 * @size: The maximum size available at @obj for the object.
2f28d2ff
AL
603 * @typename: The name of the type of the object to instantiate.
604 *
605 * This function will initialize an object. The memory for the object should
b76facc3
PB
606 * have already been allocated. The returned object has a reference count of 1,
607 * and will be finalized when the last reference is dropped.
2f28d2ff 608 */
213f0c4f 609void object_initialize(void *obj, size_t size, const char *typename);
2f28d2ff 610
2f28d2ff
AL
611/**
612 * object_dynamic_cast:
613 * @obj: The object to cast.
614 * @typename: The @typename to cast to.
615 *
616 * This function will determine if @obj is-a @typename. @obj can refer to an
617 * object or an interface associated with an object.
618 *
619 * Returns: This function returns @obj on success or #NULL on failure.
620 */
621Object *object_dynamic_cast(Object *obj, const char *typename);
622
623/**
438e1c79 624 * object_dynamic_cast_assert:
2f28d2ff
AL
625 *
626 * See object_dynamic_cast() for a description of the parameters of this
627 * function. The only difference in behavior is that this function asserts
3556c233
PB
628 * instead of returning #NULL on failure if QOM cast debugging is enabled.
629 * This function is not meant to be called directly, but only through
630 * the wrapper macro OBJECT_CHECK.
2f28d2ff 631 */
be17f18b
PB
632Object *object_dynamic_cast_assert(Object *obj, const char *typename,
633 const char *file, int line, const char *func);
2f28d2ff
AL
634
635/**
636 * object_get_class:
637 * @obj: A derivative of #Object
638 *
639 * Returns: The #ObjectClass of the type associated with @obj.
640 */
641ObjectClass *object_get_class(Object *obj);
642
643/**
644 * object_get_typename:
645 * @obj: A derivative of #Object.
646 *
647 * Returns: The QOM typename of @obj.
648 */
649const char *object_get_typename(Object *obj);
650
651/**
652 * type_register_static:
653 * @info: The #TypeInfo of the new type.
654 *
655 * @info and all of the strings it points to should exist for the life time
656 * that the type is registered.
657 *
658 * Returns: 0 on failure, the new #Type on success.
659 */
660Type type_register_static(const TypeInfo *info);
661
662/**
663 * type_register:
664 * @info: The #TypeInfo of the new type
665 *
93148aa5 666 * Unlike type_register_static(), this call does not require @info or its
2f28d2ff
AL
667 * string members to continue to exist after the call returns.
668 *
669 * Returns: 0 on failure, the new #Type on success.
670 */
671Type type_register(const TypeInfo *info);
672
673/**
674 * object_class_dynamic_cast_assert:
675 * @klass: The #ObjectClass to attempt to cast.
676 * @typename: The QOM typename of the class to cast to.
677 *
33bc94eb
PB
678 * See object_class_dynamic_cast() for a description of the parameters
679 * of this function. The only difference in behavior is that this function
3556c233
PB
680 * asserts instead of returning #NULL on failure if QOM cast debugging is
681 * enabled. This function is not meant to be called directly, but only through
682 * the wrapper macros OBJECT_CLASS_CHECK and INTERFACE_CHECK.
2f28d2ff
AL
683 */
684ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
be17f18b
PB
685 const char *typename,
686 const char *file, int line,
687 const char *func);
2f28d2ff 688
33bc94eb
PB
689/**
690 * object_class_dynamic_cast:
691 * @klass: The #ObjectClass to attempt to cast.
692 * @typename: The QOM typename of the class to cast to.
693 *
694 * Returns: If @typename is a class, this function returns @klass if
695 * @typename is a subtype of @klass, else returns #NULL.
696 *
697 * If @typename is an interface, this function returns the interface
698 * definition for @klass if @klass implements it unambiguously; #NULL
699 * is returned if @klass does not implement the interface or if multiple
700 * classes or interfaces on the hierarchy leading to @klass implement
701 * it. (FIXME: perhaps this can be detected at type definition time?)
702 */
2f28d2ff
AL
703ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
704 const char *typename);
705
e7cce67f
PB
706/**
707 * object_class_get_parent:
708 * @klass: The class to obtain the parent for.
709 *
710 * Returns: The parent for @klass or %NULL if none.
711 */
712ObjectClass *object_class_get_parent(ObjectClass *klass);
713
2f28d2ff
AL
714/**
715 * object_class_get_name:
716 * @klass: The class to obtain the QOM typename for.
717 *
718 * Returns: The QOM typename for @klass.
719 */
720const char *object_class_get_name(ObjectClass *klass);
721
17862378
AF
722/**
723 * object_class_is_abstract:
724 * @klass: The class to obtain the abstractness for.
725 *
726 * Returns: %true if @klass is abstract, %false otherwise.
727 */
728bool object_class_is_abstract(ObjectClass *klass);
729
0466e458
PB
730/**
731 * object_class_by_name:
732 * @typename: The QOM typename to obtain the class for.
733 *
734 * Returns: The class for @typename or %NULL if not found.
735 */
2f28d2ff
AL
736ObjectClass *object_class_by_name(const char *typename);
737
738void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
93c511a1 739 const char *implements_type, bool include_abstract,
2f28d2ff 740 void *opaque);
418ba9e5
AF
741
742/**
743 * object_class_get_list:
744 * @implements_type: The type to filter for, including its derivatives.
745 * @include_abstract: Whether to include abstract classes.
746 *
747 * Returns: A singly-linked list of the classes in reverse hashtable order.
748 */
749GSList *object_class_get_list(const char *implements_type,
750 bool include_abstract);
751
57c9fafe
AL
752/**
753 * object_ref:
754 * @obj: the object
755 *
756 * Increase the reference count of a object. A object cannot be freed as long
757 * as its reference count is greater than zero.
758 */
759void object_ref(Object *obj);
760
761/**
762 * qdef_unref:
763 * @obj: the object
764 *
765 * Decrease the reference count of a object. A object cannot be freed as long
766 * as its reference count is greater than zero.
767 */
768void object_unref(Object *obj);
769
770/**
771 * object_property_add:
772 * @obj: the object to add a property to
773 * @name: the name of the property. This can contain any character except for
774 * a forward slash. In general, you should use hyphens '-' instead of
775 * underscores '_' when naming properties.
776 * @type: the type name of the property. This namespace is pretty loosely
777 * defined. Sub namespaces are constructed by using a prefix and then
778 * to angle brackets. For instance, the type 'virtio-net-pci' in the
779 * 'link' namespace would be 'link<virtio-net-pci>'.
780 * @get: The getter to be called to read a property. If this is NULL, then
781 * the property cannot be read.
782 * @set: the setter to be called to write a property. If this is NULL,
783 * then the property cannot be written.
784 * @release: called when the property is removed from the object. This is
785 * meant to allow a property to free its opaque upon object
786 * destruction. This may be NULL.
787 * @opaque: an opaque pointer to pass to the callbacks for the property
788 * @errp: returns an error if this function fails
789 */
790void object_property_add(Object *obj, const char *name, const char *type,
791 ObjectPropertyAccessor *get,
792 ObjectPropertyAccessor *set,
793 ObjectPropertyRelease *release,
e82df248 794 void *opaque, Error **errp);
57c9fafe 795
e82df248 796void object_property_del(Object *obj, const char *name, Error **errp);
57c9fafe 797
8cb6789a
PB
798/**
799 * object_property_find:
800 * @obj: the object
801 * @name: the name of the property
89bfe000 802 * @errp: returns an error if this function fails
8cb6789a
PB
803 *
804 * Look up a property for an object and return its #ObjectProperty if found.
805 */
89bfe000 806ObjectProperty *object_property_find(Object *obj, const char *name,
e82df248 807 Error **errp);
8cb6789a 808
57c9fafe
AL
809void object_unparent(Object *obj);
810
811/**
812 * object_property_get:
813 * @obj: the object
814 * @v: the visitor that will receive the property value. This should be an
815 * Output visitor and the data will be written with @name as the name.
816 * @name: the name of the property
817 * @errp: returns an error if this function fails
818 *
819 * Reads a property from a object.
820 */
821void object_property_get(Object *obj, struct Visitor *v, const char *name,
e82df248 822 Error **errp);
57c9fafe 823
7b7b7d18
PB
824/**
825 * object_property_set_str:
826 * @value: the value to be written to the property
827 * @name: the name of the property
828 * @errp: returns an error if this function fails
829 *
830 * Writes a string value to a property.
831 */
832void object_property_set_str(Object *obj, const char *value,
e82df248 833 const char *name, Error **errp);
7b7b7d18
PB
834
835/**
836 * object_property_get_str:
837 * @obj: the object
838 * @name: the name of the property
839 * @errp: returns an error if this function fails
840 *
841 * Returns: the value of the property, converted to a C string, or NULL if
842 * an error occurs (including when the property value is not a string).
843 * The caller should free the string.
844 */
845char *object_property_get_str(Object *obj, const char *name,
e82df248 846 Error **errp);
7b7b7d18 847
1d9c5a12
PB
848/**
849 * object_property_set_link:
850 * @value: the value to be written to the property
851 * @name: the name of the property
852 * @errp: returns an error if this function fails
853 *
854 * Writes an object's canonical path to a property.
855 */
856void object_property_set_link(Object *obj, Object *value,
e82df248 857 const char *name, Error **errp);
1d9c5a12
PB
858
859/**
860 * object_property_get_link:
861 * @obj: the object
862 * @name: the name of the property
863 * @errp: returns an error if this function fails
864 *
865 * Returns: the value of the property, resolved from a path to an Object,
866 * or NULL if an error occurs (including when the property value is not a
867 * string or not a valid object path).
868 */
869Object *object_property_get_link(Object *obj, const char *name,
e82df248 870 Error **errp);
1d9c5a12 871
7b7b7d18
PB
872/**
873 * object_property_set_bool:
874 * @value: the value to be written to the property
875 * @name: the name of the property
876 * @errp: returns an error if this function fails
877 *
878 * Writes a bool value to a property.
879 */
880void object_property_set_bool(Object *obj, bool value,
e82df248 881 const char *name, Error **errp);
7b7b7d18
PB
882
883/**
884 * object_property_get_bool:
885 * @obj: the object
886 * @name: the name of the property
887 * @errp: returns an error if this function fails
888 *
889 * Returns: the value of the property, converted to a boolean, or NULL if
890 * an error occurs (including when the property value is not a bool).
891 */
892bool object_property_get_bool(Object *obj, const char *name,
e82df248 893 Error **errp);
7b7b7d18
PB
894
895/**
896 * object_property_set_int:
897 * @value: the value to be written to the property
898 * @name: the name of the property
899 * @errp: returns an error if this function fails
900 *
901 * Writes an integer value to a property.
902 */
903void object_property_set_int(Object *obj, int64_t value,
e82df248 904 const char *name, Error **errp);
7b7b7d18
PB
905
906/**
907 * object_property_get_int:
908 * @obj: the object
909 * @name: the name of the property
910 * @errp: returns an error if this function fails
911 *
912 * Returns: the value of the property, converted to an integer, or NULL if
913 * an error occurs (including when the property value is not an integer).
914 */
915int64_t object_property_get_int(Object *obj, const char *name,
e82df248 916 Error **errp);
7b7b7d18 917
57c9fafe
AL
918/**
919 * object_property_set:
920 * @obj: the object
921 * @v: the visitor that will be used to write the property value. This should
922 * be an Input visitor and the data will be first read with @name as the
923 * name and then written as the property value.
924 * @name: the name of the property
925 * @errp: returns an error if this function fails
926 *
927 * Writes a property to a object.
928 */
929void object_property_set(Object *obj, struct Visitor *v, const char *name,
e82df248 930 Error **errp);
57c9fafe 931
b2cd7dee
PB
932/**
933 * object_property_parse:
934 * @obj: the object
935 * @string: the string that will be used to parse the property value.
936 * @name: the name of the property
937 * @errp: returns an error if this function fails
938 *
939 * Parses a string and writes the result into a property of an object.
940 */
941void object_property_parse(Object *obj, const char *string,
e82df248 942 const char *name, Error **errp);
b2cd7dee
PB
943
944/**
945 * object_property_print:
946 * @obj: the object
947 * @name: the name of the property
948 * @errp: returns an error if this function fails
949 *
950 * Returns a string representation of the value of the property. The
951 * caller shall free the string.
952 */
953char *object_property_print(Object *obj, const char *name,
e82df248 954 Error **errp);
b2cd7dee 955
57c9fafe 956/**
438e1c79 957 * object_property_get_type:
57c9fafe
AL
958 * @obj: the object
959 * @name: the name of the property
960 * @errp: returns an error if this function fails
961 *
962 * Returns: The type name of the property.
963 */
964const char *object_property_get_type(Object *obj, const char *name,
e82df248 965 Error **errp);
57c9fafe
AL
966
967/**
968 * object_get_root:
969 *
970 * Returns: the root object of the composition tree
971 */
972Object *object_get_root(void);
973
974/**
975 * object_get_canonical_path:
976 *
977 * Returns: The canonical path for a object. This is the path within the
978 * composition tree starting from the root.
979 */
980gchar *object_get_canonical_path(Object *obj);
981
982/**
983 * object_resolve_path:
984 * @path: the path to resolve
985 * @ambiguous: returns true if the path resolution failed because of an
986 * ambiguous match
987 *
988 * There are two types of supported paths--absolute paths and partial paths.
989 *
990 * Absolute paths are derived from the root object and can follow child<> or
991 * link<> properties. Since they can follow link<> properties, they can be
992 * arbitrarily long. Absolute paths look like absolute filenames and are
993 * prefixed with a leading slash.
994 *
995 * Partial paths look like relative filenames. They do not begin with a
996 * prefix. The matching rules for partial paths are subtle but designed to make
997 * specifying objects easy. At each level of the composition tree, the partial
998 * path is matched as an absolute path. The first match is not returned. At
999 * least two matches are searched for. A successful result is only returned if
02fe2db6
PB
1000 * only one match is found. If more than one match is found, a flag is
1001 * returned to indicate that the match was ambiguous.
57c9fafe
AL
1002 *
1003 * Returns: The matched object or NULL on path lookup failure.
1004 */
1005Object *object_resolve_path(const char *path, bool *ambiguous);
1006
02fe2db6
PB
1007/**
1008 * object_resolve_path_type:
1009 * @path: the path to resolve
1010 * @typename: the type to look for.
1011 * @ambiguous: returns true if the path resolution failed because of an
1012 * ambiguous match
1013 *
1014 * This is similar to object_resolve_path. However, when looking for a
1015 * partial path only matches that implement the given type are considered.
1016 * This restricts the search and avoids spuriously flagging matches as
1017 * ambiguous.
1018 *
1019 * For both partial and absolute paths, the return value goes through
1020 * a dynamic cast to @typename. This is important if either the link,
1021 * or the typename itself are of interface types.
1022 *
1023 * Returns: The matched object or NULL on path lookup failure.
1024 */
1025Object *object_resolve_path_type(const char *path, const char *typename,
1026 bool *ambiguous);
1027
a612b2a6
PB
1028/**
1029 * object_resolve_path_component:
1030 * @parent: the object in which to resolve the path
1031 * @part: the component to resolve.
1032 *
1033 * This is similar to object_resolve_path with an absolute path, but it
1034 * only resolves one element (@part) and takes the others from @parent.
1035 *
1036 * Returns: The resolved object or NULL on path lookup failure.
1037 */
3e84b483 1038Object *object_resolve_path_component(Object *parent, const gchar *part);
a612b2a6 1039
57c9fafe
AL
1040/**
1041 * object_property_add_child:
1042 * @obj: the object to add a property to
1043 * @name: the name of the property
1044 * @child: the child object
1045 * @errp: if an error occurs, a pointer to an area to store the area
1046 *
1047 * Child properties form the composition tree. All objects need to be a child
1048 * of another object. Objects can only be a child of one object.
1049 *
1050 * There is no way for a child to determine what its parent is. It is not
1051 * a bidirectional relationship. This is by design.
358b5465
AB
1052 *
1053 * The value of a child property as a C string will be the child object's
1054 * canonical path. It can be retrieved using object_property_get_str().
1055 * The child object itself can be retrieved using object_property_get_link().
57c9fafe
AL
1056 */
1057void object_property_add_child(Object *obj, const char *name,
e82df248 1058 Object *child, Error **errp);
57c9fafe
AL
1059
1060/**
1061 * object_property_add_link:
1062 * @obj: the object to add a property to
1063 * @name: the name of the property
1064 * @type: the qobj type of the link
1065 * @child: a pointer to where the link object reference is stored
1066 * @errp: if an error occurs, a pointer to an area to store the area
1067 *
1068 * Links establish relationships between objects. Links are unidirectional
1069 * although two links can be combined to form a bidirectional relationship
1070 * between objects.
1071 *
1072 * Links form the graph in the object model.
6c232d2f
PB
1073 *
1074 * Ownership of the pointer that @child points to is transferred to the
1075 * link property. The reference count for <code>*@child</code> is
1076 * managed by the property from after the function returns till the
1077 * property is deleted with object_property_del().
57c9fafe
AL
1078 */
1079void object_property_add_link(Object *obj, const char *name,
1080 const char *type, Object **child,
e82df248 1081 Error **errp);
57c9fafe
AL
1082
1083/**
1084 * object_property_add_str:
1085 * @obj: the object to add a property to
1086 * @name: the name of the property
1087 * @get: the getter or NULL if the property is write-only. This function must
1088 * return a string to be freed by g_free().
1089 * @set: the setter or NULL if the property is read-only
1090 * @errp: if an error occurs, a pointer to an area to store the error
1091 *
1092 * Add a string property using getters/setters. This function will add a
1093 * property of type 'string'.
1094 */
1095void object_property_add_str(Object *obj, const char *name,
e82df248
MT
1096 char *(*get)(Object *, Error **),
1097 void (*set)(Object *, const char *, Error **),
1098 Error **errp);
2f28d2ff 1099
0e558843
AL
1100/**
1101 * object_property_add_bool:
1102 * @obj: the object to add a property to
1103 * @name: the name of the property
1104 * @get: the getter or NULL if the property is write-only.
1105 * @set: the setter or NULL if the property is read-only
1106 * @errp: if an error occurs, a pointer to an area to store the error
1107 *
1108 * Add a bool property using getters/setters. This function will add a
1109 * property of type 'bool'.
1110 */
1111void object_property_add_bool(Object *obj, const char *name,
e82df248
MT
1112 bool (*get)(Object *, Error **),
1113 void (*set)(Object *, bool, Error **),
1114 Error **errp);
0e558843 1115
a25ebcac
MT
1116/**
1117 * object_property_add_uint8_ptr:
1118 * @obj: the object to add a property to
1119 * @name: the name of the property
1120 * @v: pointer to value
1121 * @errp: if an error occurs, a pointer to an area to store the error
1122 *
1123 * Add an integer property in memory. This function will add a
1124 * property of type 'uint8'.
1125 */
1126void object_property_add_uint8_ptr(Object *obj, const char *name,
1127 const uint8_t *v, Error **errp);
1128
1129/**
1130 * object_property_add_uint16_ptr:
1131 * @obj: the object to add a property to
1132 * @name: the name of the property
1133 * @v: pointer to value
1134 * @errp: if an error occurs, a pointer to an area to store the error
1135 *
1136 * Add an integer property in memory. This function will add a
1137 * property of type 'uint16'.
1138 */
1139void object_property_add_uint16_ptr(Object *obj, const char *name,
1140 const uint16_t *v, Error **errp);
1141
1142/**
1143 * object_property_add_uint32_ptr:
1144 * @obj: the object to add a property to
1145 * @name: the name of the property
1146 * @v: pointer to value
1147 * @errp: if an error occurs, a pointer to an area to store the error
1148 *
1149 * Add an integer property in memory. This function will add a
1150 * property of type 'uint32'.
1151 */
1152void object_property_add_uint32_ptr(Object *obj, const char *name,
1153 const uint32_t *v, Error **errp);
1154
1155/**
1156 * object_property_add_uint64_ptr:
1157 * @obj: the object to add a property to
1158 * @name: the name of the property
1159 * @v: pointer to value
1160 * @errp: if an error occurs, a pointer to an area to store the error
1161 *
1162 * Add an integer property in memory. This function will add a
1163 * property of type 'uint64'.
1164 */
1165void object_property_add_uint64_ptr(Object *obj, const char *name,
1166 const uint64_t *v, Error **Errp);
1167
32efc535
PB
1168/**
1169 * object_child_foreach:
1170 * @obj: the object whose children will be navigated
1171 * @fn: the iterator function to be called
1172 * @opaque: an opaque value that will be passed to the iterator
1173 *
1174 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
1175 * non-zero.
1176 *
1177 * Returns: The last value returned by @fn, or 0 if there is no child.
1178 */
1179int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
1180 void *opaque);
1181
a612b2a6
PB
1182/**
1183 * container_get:
dfe47e70 1184 * @root: root of the #path, e.g., object_get_root()
a612b2a6
PB
1185 * @path: path to the container
1186 *
1187 * Return a container object whose path is @path. Create more containers
1188 * along the path if necessary.
1189 *
1190 * Returns: the container object.
1191 */
dfe47e70 1192Object *container_get(Object *root, const char *path);
a612b2a6
PB
1193
1194
2f28d2ff 1195#endif
This page took 0.315037 seconds and 4 git commands to generate.