Statistics
| Branch: | Revision:

root / include / qom / object.h @ 1de7afc9

History | View | Annotate | Download (31.8 kB)

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 "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 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 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 functions requires a class to define its own
151
 * struct and to add a .class_size member to the TypeInfo.  Each function
152
 * will also have a wrapper 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 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

    
191

    
192
/**
193
 * ObjectPropertyAccessor:
194
 * @obj: the object that owns the property
195
 * @v: the visitor that contains the property data
196
 * @opaque: the object property opaque
197
 * @name: the name of the property
198
 * @errp: a pointer to an Error that is filled if getting/setting fails.
199
 *
200
 * Called when trying to get/set a property.
201
 */
202
typedef void (ObjectPropertyAccessor)(Object *obj,
203
                                      struct Visitor *v,
204
                                      void *opaque,
205
                                      const char *name,
206
                                      struct Error **errp);
207

    
208
/**
209
 * ObjectPropertyRelease:
210
 * @obj: the object that owns the property
211
 * @name: the name of the property
212
 * @opaque: the opaque registered with the property
213
 *
214
 * Called when a property is removed from a object.
215
 */
216
typedef void (ObjectPropertyRelease)(Object *obj,
217
                                     const char *name,
218
                                     void *opaque);
219

    
220
typedef struct ObjectProperty
221
{
222
    gchar *name;
223
    gchar *type;
224
    ObjectPropertyAccessor *get;
225
    ObjectPropertyAccessor *set;
226
    ObjectPropertyRelease *release;
227
    void *opaque;
228

    
229
    QTAILQ_ENTRY(ObjectProperty) node;
230
} ObjectProperty;
231

    
232
/**
233
 * ObjectUnparent:
234
 * @obj: the object that is being removed from the composition tree
235
 *
236
 * Called when an object is being removed from the QOM composition tree.
237
 * The function should remove any backlinks from children objects to @obj.
238
 */
239
typedef void (ObjectUnparent)(Object *obj);
240

    
241
/**
242
 * ObjectFree:
243
 * @obj: the object being freed
244
 *
245
 * Called when an object's last reference is removed.
246
 */
247
typedef void (ObjectFree)(void *obj);
248

    
249
/**
250
 * ObjectClass:
251
 *
252
 * The base for all classes.  The only thing that #ObjectClass contains is an
253
 * integer type handle.
254
 */
255
struct ObjectClass
256
{
257
    /*< private >*/
258
    Type type;
259
    GSList *interfaces;
260

    
261
    ObjectUnparent *unparent;
262
};
263

    
264
/**
265
 * Object:
266
 *
267
 * The base for all objects.  The first member of this object is a pointer to
268
 * a #ObjectClass.  Since C guarantees that the first member of a structure
269
 * always begins at byte 0 of that structure, as long as any sub-object places
270
 * its parent as the first member, we can cast directly to a #Object.
271
 *
272
 * As a result, #Object contains a reference to the objects type as its
273
 * first member.  This allows identification of the real type of the object at
274
 * run time.
275
 *
276
 * #Object also contains a list of #Interfaces that this object
277
 * implements.
278
 */
279
struct Object
280
{
281
    /*< private >*/
282
    ObjectClass *class;
283
    ObjectFree *free;
284
    QTAILQ_HEAD(, ObjectProperty) properties;
285
    uint32_t ref;
286
    Object *parent;
287
};
288

    
289
/**
290
 * TypeInfo:
291
 * @name: The name of the type.
292
 * @parent: The name of the parent type.
293
 * @instance_size: The size of the object (derivative of #Object).  If
294
 *   @instance_size is 0, then the size of the object will be the size of the
295
 *   parent object.
296
 * @instance_init: This function is called to initialize an object.  The parent
297
 *   class will have already been initialized so the type is only responsible
298
 *   for initializing its own members.
299
 * @instance_finalize: This function is called during object destruction.  This
300
 *   is called before the parent @instance_finalize function has been called.
301
 *   An object should only free the members that are unique to its type in this
302
 *   function.
303
 * @abstract: If this field is true, then the class is considered abstract and
304
 *   cannot be directly instantiated.
305
 * @class_size: The size of the class object (derivative of #ObjectClass)
306
 *   for this object.  If @class_size is 0, then the size of the class will be
307
 *   assumed to be the size of the parent class.  This allows a type to avoid
308
 *   implementing an explicit class type if they are not adding additional
309
 *   virtual functions.
310
 * @class_init: This function is called after all parent class initialization
311
 *   has occurred to allow a class to set its default virtual method pointers.
312
 *   This is also the function to use to override virtual methods from a parent
313
 *   class.
314
 * @class_base_init: This function is called for all base classes after all
315
 *   parent class initialization has occurred, but before the class itself
316
 *   is initialized.  This is the function to use to undo the effects of
317
 *   memcpy from the parent class to the descendents.
318
 * @class_finalize: This function is called during class destruction and is
319
 *   meant to release and dynamic parameters allocated by @class_init.
320
 * @class_data: Data to pass to the @class_init, @class_base_init and
321
 *   @class_finalize functions.  This can be useful when building dynamic
322
 *   classes.
323
 * @interfaces: The list of interfaces associated with this type.  This
324
 *   should point to a static array that's terminated with a zero filled
325
 *   element.
326
 */
327
struct TypeInfo
328
{
329
    const char *name;
330
    const char *parent;
331

    
332
    size_t instance_size;
333
    void (*instance_init)(Object *obj);
334
    void (*instance_finalize)(Object *obj);
335

    
336
    bool abstract;
337
    size_t class_size;
338

    
339
    void (*class_init)(ObjectClass *klass, void *data);
340
    void (*class_base_init)(ObjectClass *klass, void *data);
341
    void (*class_finalize)(ObjectClass *klass, void *data);
342
    void *class_data;
343

    
344
    InterfaceInfo *interfaces;
345
};
346

    
347
/**
348
 * OBJECT:
349
 * @obj: A derivative of #Object
350
 *
351
 * Converts an object to a #Object.  Since all objects are #Objects,
352
 * this function will always succeed.
353
 */
354
#define OBJECT(obj) \
355
    ((Object *)(obj))
356

    
357
/**
358
 * OBJECT_CLASS:
359
 * @class: A derivative of #ObjectClass.
360
 *
361
 * Converts a class to an #ObjectClass.  Since all objects are #Objects,
362
 * this function will always succeed.
363
 */
364
#define OBJECT_CLASS(class) \
365
    ((ObjectClass *)(class))
366

    
367
/**
368
 * OBJECT_CHECK:
369
 * @type: The C type to use for the return value.
370
 * @obj: A derivative of @type to cast.
371
 * @name: The QOM typename of @type
372
 *
373
 * A type safe version of @object_dynamic_cast_assert.  Typically each class
374
 * will define a macro based on this type to perform type safe dynamic_casts to
375
 * this object type.
376
 *
377
 * If an invalid object is passed to this function, a run time assert will be
378
 * generated.
379
 */
380
#define OBJECT_CHECK(type, obj, name) \
381
    ((type *)object_dynamic_cast_assert(OBJECT(obj), (name)))
382

    
383
/**
384
 * OBJECT_CLASS_CHECK:
385
 * @class: The C type to use for the return value.
386
 * @obj: A derivative of @type to cast.
387
 * @name: the QOM typename of @class.
388
 *
389
 * A type safe version of @object_class_dynamic_cast_assert.  This macro is
390
 * typically wrapped by each type to perform type safe casts of a class to a
391
 * specific class type.
392
 */
393
#define OBJECT_CLASS_CHECK(class, obj, name) \
394
    ((class *)object_class_dynamic_cast_assert(OBJECT_CLASS(obj), (name)))
395

    
396
/**
397
 * OBJECT_GET_CLASS:
398
 * @class: The C type to use for the return value.
399
 * @obj: The object to obtain the class for.
400
 * @name: The QOM typename of @obj.
401
 *
402
 * This function will return a specific class for a given object.  Its generally
403
 * used by each type to provide a type safe macro to get a specific class type
404
 * from an object.
405
 */
406
#define OBJECT_GET_CLASS(class, obj, name) \
407
    OBJECT_CLASS_CHECK(class, object_get_class(OBJECT(obj)), name)
408

    
409
/**
410
 * InterfaceInfo:
411
 * @type: The name of the interface.
412
 *
413
 * The information associated with an interface.
414
 */
415
struct InterfaceInfo {
416
    const char *type;
417
};
418

    
419
/**
420
 * InterfaceClass:
421
 * @parent_class: the base class
422
 *
423
 * The class for all interfaces.  Subclasses of this class should only add
424
 * virtual methods.
425
 */
426
struct InterfaceClass
427
{
428
    ObjectClass parent_class;
429
    /*< private >*/
430
    ObjectClass *concrete_class;
431
};
432

    
433
#define TYPE_INTERFACE "interface"
434

    
435
/**
436
 * INTERFACE_CLASS:
437
 * @klass: class to cast from
438
 * Returns: An #InterfaceClass or raise an error if cast is invalid
439
 */
440
#define INTERFACE_CLASS(klass) \
441
    OBJECT_CLASS_CHECK(InterfaceClass, klass, TYPE_INTERFACE)
442

    
443
/**
444
 * INTERFACE_CHECK:
445
 * @interface: the type to return
446
 * @obj: the object to convert to an interface
447
 * @name: the interface type name
448
 *
449
 * Returns: @obj casted to @interface if cast is valid, otherwise raise error.
450
 */
451
#define INTERFACE_CHECK(interface, obj, name) \
452
    ((interface *)object_dynamic_cast_assert(OBJECT((obj)), (name)))
453

    
454
/**
455
 * object_new:
456
 * @typename: The name of the type of the object to instantiate.
457
 *
458
 * This function will initialize a new object using heap allocated memory.  This
459
 * function should be paired with object_delete() to free the resources
460
 * associated with the object.
461
 *
462
 * Returns: The newly allocated and instantiated object.
463
 */
464
Object *object_new(const char *typename);
465

    
466
/**
467
 * object_new_with_type:
468
 * @type: The type of the object to instantiate.
469
 *
470
 * This function will initialize a new object using heap allocated memory.  This
471
 * function should be paired with object_delete() to free the resources
472
 * associated with the object.
473
 *
474
 * Returns: The newly allocated and instantiated object.
475
 */
476
Object *object_new_with_type(Type type);
477

    
478
/**
479
 * object_delete:
480
 * @obj: The object to free.
481
 *
482
 * Finalize an object and then free the memory associated with it.  This should
483
 * be paired with object_new() to free the resources associated with an object.
484
 */
485
void object_delete(Object *obj);
486

    
487
/**
488
 * object_initialize_with_type:
489
 * @obj: A pointer to the memory to be used for the object.
490
 * @type: The type of the object to instantiate.
491
 *
492
 * This function will initialize an object.  The memory for the object should
493
 * have already been allocated.
494
 */
495
void object_initialize_with_type(void *data, Type type);
496

    
497
/**
498
 * object_initialize:
499
 * @obj: A pointer to the memory to be used for the object.
500
 * @typename: The name of the type of the object to instantiate.
501
 *
502
 * This function will initialize an object.  The memory for the object should
503
 * have already been allocated.
504
 */
505
void object_initialize(void *obj, const char *typename);
506

    
507
/**
508
 * object_dynamic_cast:
509
 * @obj: The object to cast.
510
 * @typename: The @typename to cast to.
511
 *
512
 * This function will determine if @obj is-a @typename.  @obj can refer to an
513
 * object or an interface associated with an object.
514
 *
515
 * Returns: This function returns @obj on success or #NULL on failure.
516
 */
517
Object *object_dynamic_cast(Object *obj, const char *typename);
518

    
519
/**
520
 * object_dynamic_cast_assert:
521
 *
522
 * See object_dynamic_cast() for a description of the parameters of this
523
 * function.  The only difference in behavior is that this function asserts
524
 * instead of returning #NULL on failure.
525
 */
526
Object *object_dynamic_cast_assert(Object *obj, const char *typename);
527

    
528
/**
529
 * object_get_class:
530
 * @obj: A derivative of #Object
531
 *
532
 * Returns: The #ObjectClass of the type associated with @obj.
533
 */
534
ObjectClass *object_get_class(Object *obj);
535

    
536
/**
537
 * object_get_typename:
538
 * @obj: A derivative of #Object.
539
 *
540
 * Returns: The QOM typename of @obj.
541
 */
542
const char *object_get_typename(Object *obj);
543

    
544
/**
545
 * type_register_static:
546
 * @info: The #TypeInfo of the new type.
547
 *
548
 * @info and all of the strings it points to should exist for the life time
549
 * that the type is registered.
550
 *
551
 * Returns: 0 on failure, the new #Type on success.
552
 */
553
Type type_register_static(const TypeInfo *info);
554

    
555
/**
556
 * type_register:
557
 * @info: The #TypeInfo of the new type
558
 *
559
 * Unlike type_register_static(), this call does not require @info or its
560
 * string members to continue to exist after the call returns.
561
 *
562
 * Returns: 0 on failure, the new #Type on success.
563
 */
564
Type type_register(const TypeInfo *info);
565

    
566
/**
567
 * object_class_dynamic_cast_assert:
568
 * @klass: The #ObjectClass to attempt to cast.
569
 * @typename: The QOM typename of the class to cast to.
570
 *
571
 * Returns: This function always returns @klass and asserts on failure.
572
 */
573
ObjectClass *object_class_dynamic_cast_assert(ObjectClass *klass,
574
                                              const char *typename);
575

    
576
ObjectClass *object_class_dynamic_cast(ObjectClass *klass,
577
                                       const char *typename);
578

    
579
/**
580
 * object_class_get_parent:
581
 * @klass: The class to obtain the parent for.
582
 *
583
 * Returns: The parent for @klass or %NULL if none.
584
 */
585
ObjectClass *object_class_get_parent(ObjectClass *klass);
586

    
587
/**
588
 * object_class_get_name:
589
 * @klass: The class to obtain the QOM typename for.
590
 *
591
 * Returns: The QOM typename for @klass.
592
 */
593
const char *object_class_get_name(ObjectClass *klass);
594

    
595
/**
596
 * object_class_by_name:
597
 * @typename: The QOM typename to obtain the class for.
598
 *
599
 * Returns: The class for @typename or %NULL if not found.
600
 */
601
ObjectClass *object_class_by_name(const char *typename);
602

    
603
void object_class_foreach(void (*fn)(ObjectClass *klass, void *opaque),
604
                          const char *implements_type, bool include_abstract,
605
                          void *opaque);
606

    
607
/**
608
 * object_class_get_list:
609
 * @implements_type: The type to filter for, including its derivatives.
610
 * @include_abstract: Whether to include abstract classes.
611
 *
612
 * Returns: A singly-linked list of the classes in reverse hashtable order.
613
 */
614
GSList *object_class_get_list(const char *implements_type,
615
                              bool include_abstract);
616

    
617
/**
618
 * object_ref:
619
 * @obj: the object
620
 *
621
 * Increase the reference count of a object.  A object cannot be freed as long
622
 * as its reference count is greater than zero.
623
 */
624
void object_ref(Object *obj);
625

    
626
/**
627
 * qdef_unref:
628
 * @obj: the object
629
 *
630
 * Decrease the reference count of a object.  A object cannot be freed as long
631
 * as its reference count is greater than zero.
632
 */
633
void object_unref(Object *obj);
634

    
635
/**
636
 * object_property_add:
637
 * @obj: the object to add a property to
638
 * @name: the name of the property.  This can contain any character except for
639
 *  a forward slash.  In general, you should use hyphens '-' instead of
640
 *  underscores '_' when naming properties.
641
 * @type: the type name of the property.  This namespace is pretty loosely
642
 *   defined.  Sub namespaces are constructed by using a prefix and then
643
 *   to angle brackets.  For instance, the type 'virtio-net-pci' in the
644
 *   'link' namespace would be 'link<virtio-net-pci>'.
645
 * @get: The getter to be called to read a property.  If this is NULL, then
646
 *   the property cannot be read.
647
 * @set: the setter to be called to write a property.  If this is NULL,
648
 *   then the property cannot be written.
649
 * @release: called when the property is removed from the object.  This is
650
 *   meant to allow a property to free its opaque upon object
651
 *   destruction.  This may be NULL.
652
 * @opaque: an opaque pointer to pass to the callbacks for the property
653
 * @errp: returns an error if this function fails
654
 */
655
void object_property_add(Object *obj, const char *name, const char *type,
656
                         ObjectPropertyAccessor *get,
657
                         ObjectPropertyAccessor *set,
658
                         ObjectPropertyRelease *release,
659
                         void *opaque, struct Error **errp);
660

    
661
void object_property_del(Object *obj, const char *name, struct Error **errp);
662

    
663
/**
664
 * object_property_find:
665
 * @obj: the object
666
 * @name: the name of the property
667
 * @errp: returns an error if this function fails
668
 *
669
 * Look up a property for an object and return its #ObjectProperty if found.
670
 */
671
ObjectProperty *object_property_find(Object *obj, const char *name,
672
                                     struct Error **errp);
673

    
674
void object_unparent(Object *obj);
675

    
676
/**
677
 * object_property_get:
678
 * @obj: the object
679
 * @v: the visitor that will receive the property value.  This should be an
680
 *   Output visitor and the data will be written with @name as the name.
681
 * @name: the name of the property
682
 * @errp: returns an error if this function fails
683
 *
684
 * Reads a property from a object.
685
 */
686
void object_property_get(Object *obj, struct Visitor *v, const char *name,
687
                         struct Error **errp);
688

    
689
/**
690
 * object_property_set_str:
691
 * @value: the value to be written to the property
692
 * @name: the name of the property
693
 * @errp: returns an error if this function fails
694
 *
695
 * Writes a string value to a property.
696
 */
697
void object_property_set_str(Object *obj, const char *value,
698
                             const char *name, struct Error **errp);
699

    
700
/**
701
 * object_property_get_str:
702
 * @obj: the object
703
 * @name: the name of the property
704
 * @errp: returns an error if this function fails
705
 *
706
 * Returns: the value of the property, converted to a C string, or NULL if
707
 * an error occurs (including when the property value is not a string).
708
 * The caller should free the string.
709
 */
710
char *object_property_get_str(Object *obj, const char *name,
711
                              struct Error **errp);
712

    
713
/**
714
 * object_property_set_link:
715
 * @value: the value to be written to the property
716
 * @name: the name of the property
717
 * @errp: returns an error if this function fails
718
 *
719
 * Writes an object's canonical path to a property.
720
 */
721
void object_property_set_link(Object *obj, Object *value,
722
                              const char *name, struct Error **errp);
723

    
724
/**
725
 * object_property_get_link:
726
 * @obj: the object
727
 * @name: the name of the property
728
 * @errp: returns an error if this function fails
729
 *
730
 * Returns: the value of the property, resolved from a path to an Object,
731
 * or NULL if an error occurs (including when the property value is not a
732
 * string or not a valid object path).
733
 */
734
Object *object_property_get_link(Object *obj, const char *name,
735
                                 struct Error **errp);
736

    
737
/**
738
 * object_property_set_bool:
739
 * @value: the value to be written to the property
740
 * @name: the name of the property
741
 * @errp: returns an error if this function fails
742
 *
743
 * Writes a bool value to a property.
744
 */
745
void object_property_set_bool(Object *obj, bool value,
746
                              const char *name, struct Error **errp);
747

    
748
/**
749
 * object_property_get_bool:
750
 * @obj: the object
751
 * @name: the name of the property
752
 * @errp: returns an error if this function fails
753
 *
754
 * Returns: the value of the property, converted to a boolean, or NULL if
755
 * an error occurs (including when the property value is not a bool).
756
 */
757
bool object_property_get_bool(Object *obj, const char *name,
758
                              struct Error **errp);
759

    
760
/**
761
 * object_property_set_int:
762
 * @value: the value to be written to the property
763
 * @name: the name of the property
764
 * @errp: returns an error if this function fails
765
 *
766
 * Writes an integer value to a property.
767
 */
768
void object_property_set_int(Object *obj, int64_t value,
769
                             const char *name, struct Error **errp);
770

    
771
/**
772
 * object_property_get_int:
773
 * @obj: the object
774
 * @name: the name of the property
775
 * @errp: returns an error if this function fails
776
 *
777
 * Returns: the value of the property, converted to an integer, or NULL if
778
 * an error occurs (including when the property value is not an integer).
779
 */
780
int64_t object_property_get_int(Object *obj, const char *name,
781
                                struct Error **errp);
782

    
783
/**
784
 * object_property_set:
785
 * @obj: the object
786
 * @v: the visitor that will be used to write the property value.  This should
787
 *   be an Input visitor and the data will be first read with @name as the
788
 *   name and then written as the property value.
789
 * @name: the name of the property
790
 * @errp: returns an error if this function fails
791
 *
792
 * Writes a property to a object.
793
 */
794
void object_property_set(Object *obj, struct Visitor *v, const char *name,
795
                         struct Error **errp);
796

    
797
/**
798
 * object_property_parse:
799
 * @obj: the object
800
 * @string: the string that will be used to parse the property value.
801
 * @name: the name of the property
802
 * @errp: returns an error if this function fails
803
 *
804
 * Parses a string and writes the result into a property of an object.
805
 */
806
void object_property_parse(Object *obj, const char *string,
807
                           const char *name, struct Error **errp);
808

    
809
/**
810
 * object_property_print:
811
 * @obj: the object
812
 * @name: the name of the property
813
 * @errp: returns an error if this function fails
814
 *
815
 * Returns a string representation of the value of the property.  The
816
 * caller shall free the string.
817
 */
818
char *object_property_print(Object *obj, const char *name,
819
                            struct Error **errp);
820

    
821
/**
822
 * object_property_get_type:
823
 * @obj: the object
824
 * @name: the name of the property
825
 * @errp: returns an error if this function fails
826
 *
827
 * Returns:  The type name of the property.
828
 */
829
const char *object_property_get_type(Object *obj, const char *name,
830
                                     struct Error **errp);
831

    
832
/**
833
 * object_get_root:
834
 *
835
 * Returns: the root object of the composition tree
836
 */
837
Object *object_get_root(void);
838

    
839
/**
840
 * object_get_canonical_path:
841
 *
842
 * Returns: The canonical path for a object.  This is the path within the
843
 * composition tree starting from the root.
844
 */
845
gchar *object_get_canonical_path(Object *obj);
846

    
847
/**
848
 * object_resolve_path:
849
 * @path: the path to resolve
850
 * @ambiguous: returns true if the path resolution failed because of an
851
 *   ambiguous match
852
 *
853
 * There are two types of supported paths--absolute paths and partial paths.
854
 * 
855
 * Absolute paths are derived from the root object and can follow child<> or
856
 * link<> properties.  Since they can follow link<> properties, they can be
857
 * arbitrarily long.  Absolute paths look like absolute filenames and are
858
 * prefixed with a leading slash.
859
 * 
860
 * Partial paths look like relative filenames.  They do not begin with a
861
 * prefix.  The matching rules for partial paths are subtle but designed to make
862
 * specifying objects easy.  At each level of the composition tree, the partial
863
 * path is matched as an absolute path.  The first match is not returned.  At
864
 * least two matches are searched for.  A successful result is only returned if
865
 * only one match is found.  If more than one match is found, a flag is
866
 * returned to indicate that the match was ambiguous.
867
 *
868
 * Returns: The matched object or NULL on path lookup failure.
869
 */
870
Object *object_resolve_path(const char *path, bool *ambiguous);
871

    
872
/**
873
 * object_resolve_path_type:
874
 * @path: the path to resolve
875
 * @typename: the type to look for.
876
 * @ambiguous: returns true if the path resolution failed because of an
877
 *   ambiguous match
878
 *
879
 * This is similar to object_resolve_path.  However, when looking for a
880
 * partial path only matches that implement the given type are considered.
881
 * This restricts the search and avoids spuriously flagging matches as
882
 * ambiguous.
883
 *
884
 * For both partial and absolute paths, the return value goes through
885
 * a dynamic cast to @typename.  This is important if either the link,
886
 * or the typename itself are of interface types.
887
 *
888
 * Returns: The matched object or NULL on path lookup failure.
889
 */
890
Object *object_resolve_path_type(const char *path, const char *typename,
891
                                 bool *ambiguous);
892

    
893
/**
894
 * object_resolve_path_component:
895
 * @parent: the object in which to resolve the path
896
 * @part: the component to resolve.
897
 *
898
 * This is similar to object_resolve_path with an absolute path, but it
899
 * only resolves one element (@part) and takes the others from @parent.
900
 *
901
 * Returns: The resolved object or NULL on path lookup failure.
902
 */
903
Object *object_resolve_path_component(Object *parent, gchar *part);
904

    
905
/**
906
 * object_property_add_child:
907
 * @obj: the object to add a property to
908
 * @name: the name of the property
909
 * @child: the child object
910
 * @errp: if an error occurs, a pointer to an area to store the area
911
 *
912
 * Child properties form the composition tree.  All objects need to be a child
913
 * of another object.  Objects can only be a child of one object.
914
 *
915
 * There is no way for a child to determine what its parent is.  It is not
916
 * a bidirectional relationship.  This is by design.
917
 *
918
 * The value of a child property as a C string will be the child object's
919
 * canonical path. It can be retrieved using object_property_get_str().
920
 * The child object itself can be retrieved using object_property_get_link().
921
 */
922
void object_property_add_child(Object *obj, const char *name,
923
                               Object *child, struct Error **errp);
924

    
925
/**
926
 * object_property_add_link:
927
 * @obj: the object to add a property to
928
 * @name: the name of the property
929
 * @type: the qobj type of the link
930
 * @child: a pointer to where the link object reference is stored
931
 * @errp: if an error occurs, a pointer to an area to store the area
932
 *
933
 * Links establish relationships between objects.  Links are unidirectional
934
 * although two links can be combined to form a bidirectional relationship
935
 * between objects.
936
 *
937
 * Links form the graph in the object model.
938
 */
939
void object_property_add_link(Object *obj, const char *name,
940
                              const char *type, Object **child,
941
                              struct Error **errp);
942

    
943
/**
944
 * object_property_add_str:
945
 * @obj: the object to add a property to
946
 * @name: the name of the property
947
 * @get: the getter or NULL if the property is write-only.  This function must
948
 *   return a string to be freed by g_free().
949
 * @set: the setter or NULL if the property is read-only
950
 * @errp: if an error occurs, a pointer to an area to store the error
951
 *
952
 * Add a string property using getters/setters.  This function will add a
953
 * property of type 'string'.
954
 */
955
void object_property_add_str(Object *obj, const char *name,
956
                             char *(*get)(Object *, struct Error **),
957
                             void (*set)(Object *, const char *, struct Error **),
958
                             struct Error **errp);
959

    
960
/**
961
 * object_property_add_bool:
962
 * @obj: the object to add a property to
963
 * @name: the name of the property
964
 * @get: the getter or NULL if the property is write-only.
965
 * @set: the setter or NULL if the property is read-only
966
 * @errp: if an error occurs, a pointer to an area to store the error
967
 *
968
 * Add a bool property using getters/setters.  This function will add a
969
 * property of type 'bool'.
970
 */
971
void object_property_add_bool(Object *obj, const char *name,
972
                              bool (*get)(Object *, struct Error **),
973
                              void (*set)(Object *, bool, struct Error **),
974
                              struct Error **errp);
975

    
976
/**
977
 * object_child_foreach:
978
 * @obj: the object whose children will be navigated
979
 * @fn: the iterator function to be called
980
 * @opaque: an opaque value that will be passed to the iterator
981
 *
982
 * Call @fn passing each child of @obj and @opaque to it, until @fn returns
983
 * non-zero.
984
 *
985
 * Returns: The last value returned by @fn, or 0 if there is no child.
986
 */
987
int object_child_foreach(Object *obj, int (*fn)(Object *child, void *opaque),
988
                         void *opaque);
989

    
990
/**
991
 * container_get:
992
 * @root: root of the #path, e.g., object_get_root()
993
 * @path: path to the container
994
 *
995
 * Return a container object whose path is @path.  Create more containers
996
 * along the path if necessary.
997
 *
998
 * Returns: the container object.
999
 */
1000
Object *container_get(Object *root, const char *path);
1001

    
1002

    
1003
#endif