]> Git Repo - qemu.git/blame - qobject.h
Add unit test for QFloat
[qemu.git] / qobject.h
CommitLineData
5a1a2356
LC
1/*
2 * QEMU Object Model.
3 *
4 * Based on ideas by Avi Kivity <[email protected]>
5 *
6 * Copyright (C) 2009 Red Hat Inc.
7 *
8 * Authors:
9 * Luiz Capitulino <[email protected]>
10 *
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
13 *
14 * QObject Reference Counts Terminology
15 * ------------------------------------
16 *
17 * - Returning references: A function that returns an object may
18 * return it as either a weak or a strong reference. If the reference
19 * is strong, you are responsible for calling QDECREF() on the reference
20 * when you are done.
21 *
22 * If the reference is weak, the owner of the reference may free it at
23 * any time in the future. Before storing the reference anywhere, you
24 * should call QINCREF() to make the reference strong.
25 *
26 * - Transferring ownership: when you transfer ownership of a reference
27 * by calling a function, you are no longer responsible for calling
28 * QDECREF() when the reference is no longer needed. In other words,
29 * when the function returns you must behave as if the reference to the
30 * passed object was weak.
31 */
32#ifndef QOBJECT_H
33#define QOBJECT_H
34
35#include <stddef.h>
36#include <assert.h>
37
38typedef enum {
39 QTYPE_NONE,
6b8d1ece 40 QTYPE_QINT,
66f70487 41 QTYPE_QSTRING,
fb08dde0 42 QTYPE_QDICT,
a6fd08eb 43 QTYPE_QLIST,
ec072ced 44 QTYPE_QFLOAT,
5a1a2356
LC
45} qtype_code;
46
47struct QObject;
48
49typedef struct QType {
50 qtype_code code;
51 void (*destroy)(struct QObject *);
52} QType;
53
54typedef struct QObject {
55 const QType *type;
56 size_t refcnt;
57} QObject;
58
59/* Objects definitions must include this */
60#define QObject_HEAD \
61 QObject base
62
63/* Get the 'base' part of an object */
c99ca931 64#define QOBJECT(obj) (&(obj)->base)
5a1a2356
LC
65
66/* High-level interface for qobject_incref() */
67#define QINCREF(obj) \
5a1a2356
LC
68 qobject_incref(QOBJECT(obj))
69
70/* High-level interface for qobject_decref() */
71#define QDECREF(obj) \
5a1a2356
LC
72 qobject_decref(QOBJECT(obj))
73
74/* Initialize an object to default values */
75#define QOBJECT_INIT(obj, qtype_type) \
76 obj->base.refcnt = 1; \
77 obj->base.type = qtype_type
78
79/**
80 * qobject_incref(): Increment QObject's reference count
81 */
82static inline void qobject_incref(QObject *obj)
83{
d559ba1a
LC
84 if (obj)
85 obj->refcnt++;
5a1a2356
LC
86}
87
88/**
89 * qobject_decref(): Decrement QObject's reference count, deallocate
90 * when it reaches zero
91 */
92static inline void qobject_decref(QObject *obj)
93{
d559ba1a 94 if (obj && --obj->refcnt == 0) {
5a1a2356
LC
95 assert(obj->type != NULL);
96 assert(obj->type->destroy != NULL);
97 obj->type->destroy(obj);
98 }
99}
100
101/**
102 * qobject_type(): Return the QObject's type
103 */
104static inline qtype_code qobject_type(const QObject *obj)
105{
106 assert(obj->type != NULL);
107 return obj->type->code;
108}
109
110#endif /* QOBJECT_H */
This page took 0.072763 seconds and 4 git commands to generate.