15a1a2356SLuiz Capitulino /*
25a1a2356SLuiz Capitulino * QEMU Object Model.
35a1a2356SLuiz Capitulino *
45a1a2356SLuiz Capitulino * Based on ideas by Avi Kivity <avi@redhat.com>
55a1a2356SLuiz Capitulino *
6481b002cSMarkus Armbruster * Copyright (C) 2009, 2015 Red Hat Inc.
75a1a2356SLuiz Capitulino *
85a1a2356SLuiz Capitulino * Authors:
95a1a2356SLuiz Capitulino * Luiz Capitulino <lcapitulino@redhat.com>
105a1a2356SLuiz Capitulino *
1141836a9fSLuiz Capitulino * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
1241836a9fSLuiz Capitulino * See the COPYING.LIB file in the top-level directory.
135a1a2356SLuiz Capitulino *
145a1a2356SLuiz Capitulino * QObject Reference Counts Terminology
155a1a2356SLuiz Capitulino * ------------------------------------
165a1a2356SLuiz Capitulino *
175a1a2356SLuiz Capitulino * - Returning references: A function that returns an object may
18cb3e7f08SMarc-André Lureau * return it as either a weak or a strong reference. If the
19cb3e7f08SMarc-André Lureau * reference is strong, you are responsible for calling
20cb3e7f08SMarc-André Lureau * qobject_unref() on the reference when you are done.
215a1a2356SLuiz Capitulino *
225a1a2356SLuiz Capitulino * If the reference is weak, the owner of the reference may free it at
235a1a2356SLuiz Capitulino * any time in the future. Before storing the reference anywhere, you
24cb3e7f08SMarc-André Lureau * should call qobject_ref() to make the reference strong.
255a1a2356SLuiz Capitulino *
265a1a2356SLuiz Capitulino * - Transferring ownership: when you transfer ownership of a reference
275a1a2356SLuiz Capitulino * by calling a function, you are no longer responsible for calling
28cb3e7f08SMarc-André Lureau * qobject_unref() when the reference is no longer needed. In other words,
295a1a2356SLuiz Capitulino * when the function returns you must behave as if the reference to the
305a1a2356SLuiz Capitulino * passed object was weak.
315a1a2356SLuiz Capitulino */
325a1a2356SLuiz Capitulino #ifndef QOBJECT_H
335a1a2356SLuiz Capitulino #define QOBJECT_H
345a1a2356SLuiz Capitulino
35eb815e24SMarkus Armbruster #include "qapi/qapi-builtin-types.h"
365a1a2356SLuiz Capitulino
37*407bc4bfSDaniel P. Berrangé /* Not for use outside include/qobject/ */
383d3eacaeSMarc-André Lureau struct QObjectBase_ {
391310a3d3SEric Blake QType type;
405a1a2356SLuiz Capitulino size_t refcnt;
417264f5c5SEric Blake };
425a1a2356SLuiz Capitulino
433d3eacaeSMarc-André Lureau /* this struct must have no other members than base */
443d3eacaeSMarc-André Lureau struct QObject {
453d3eacaeSMarc-André Lureau struct QObjectBase_ base;
463d3eacaeSMarc-André Lureau };
473d3eacaeSMarc-André Lureau
48bb718463SMarkus Armbruster /*
49bb718463SMarkus Armbruster * Preprocessor sorcery ahead: use a different identifier for the
50bb718463SMarkus Armbruster * local variable in each expansion, so we can nest macro calls
51bb718463SMarkus Armbruster * without shadowing variables.
52bb718463SMarkus Armbruster */
53bb718463SMarkus Armbruster #define QOBJECT_INTERNAL(obj, _obj) ({ \
543d3eacaeSMarc-André Lureau typeof(obj) _obj = (obj); \
55bb718463SMarkus Armbruster _obj ? container_of(&_obj->base, QObject, base) : NULL; \
563d3eacaeSMarc-André Lureau })
5705548400SPeter Maydell #define QOBJECT(obj) QOBJECT_INTERNAL((obj), MAKE_IDENTIFIER(_obj))
585a1a2356SLuiz Capitulino
591a56b1e2SMax Reitz /* Required for qobject_to() */
601a56b1e2SMax Reitz #define QTYPE_CAST_TO_QNull QTYPE_QNULL
611a56b1e2SMax Reitz #define QTYPE_CAST_TO_QNum QTYPE_QNUM
621a56b1e2SMax Reitz #define QTYPE_CAST_TO_QString QTYPE_QSTRING
631a56b1e2SMax Reitz #define QTYPE_CAST_TO_QDict QTYPE_QDICT
641a56b1e2SMax Reitz #define QTYPE_CAST_TO_QList QTYPE_QLIST
651a56b1e2SMax Reitz #define QTYPE_CAST_TO_QBool QTYPE_QBOOL
661a56b1e2SMax Reitz
671a56b1e2SMax Reitz QEMU_BUILD_BUG_MSG(QTYPE__MAX != 7,
681a56b1e2SMax Reitz "The QTYPE_CAST_TO_* list needs to be extended");
691a56b1e2SMax Reitz
707ee9edfdSMarc-André Lureau #define qobject_to(type, obj) \
717ee9edfdSMarc-André Lureau ((type *)qobject_check_type(obj, glue(QTYPE_CAST_TO_, type)))
721a56b1e2SMax Reitz
qobject_ref_impl(QObject * obj)73cb3e7f08SMarc-André Lureau static inline void qobject_ref_impl(QObject *obj)
745a1a2356SLuiz Capitulino {
753d3eacaeSMarc-André Lureau if (obj) {
763d3eacaeSMarc-André Lureau obj->base.refcnt++;
773d3eacaeSMarc-André Lureau }
785a1a2356SLuiz Capitulino }
795a1a2356SLuiz Capitulino
805a1a2356SLuiz Capitulino /**
81b38dd678SMax Reitz * qobject_is_equal(): Return whether the two objects are equal.
82b38dd678SMax Reitz *
83b38dd678SMax Reitz * Any of the pointers may be NULL; return true if both are. Always
84b38dd678SMax Reitz * return false if only one is (therefore a QNull object is not
85b38dd678SMax Reitz * considered equal to a NULL pointer).
86b38dd678SMax Reitz */
87b38dd678SMax Reitz bool qobject_is_equal(const QObject *x, const QObject *y);
88b38dd678SMax Reitz
89b38dd678SMax Reitz /**
9055e1819cSEric Blake * qobject_destroy(): Free resources used by the object
9180d71121SMarkus Armbruster * For use via qobject_unref() only!
9255e1819cSEric Blake */
9355e1819cSEric Blake void qobject_destroy(QObject *obj);
9455e1819cSEric Blake
qobject_unref_impl(QObject * obj)95cb3e7f08SMarc-André Lureau static inline void qobject_unref_impl(QObject *obj)
965a1a2356SLuiz Capitulino {
973d3eacaeSMarc-André Lureau assert(!obj || obj->base.refcnt);
983d3eacaeSMarc-André Lureau if (obj && --obj->base.refcnt == 0) {
9955e1819cSEric Blake qobject_destroy(obj);
1005a1a2356SLuiz Capitulino }
1015a1a2356SLuiz Capitulino }
1025a1a2356SLuiz Capitulino
1035a1a2356SLuiz Capitulino /**
104cb3e7f08SMarc-André Lureau * qobject_ref(): Increment QObject's reference count
105f5a74a5aSMarc-André Lureau *
106f5a74a5aSMarc-André Lureau * Returns: the same @obj. The type of @obj will be propagated to the
107f5a74a5aSMarc-André Lureau * return type.
108cb3e7f08SMarc-André Lureau */
109f5a74a5aSMarc-André Lureau #define qobject_ref(obj) ({ \
110f5a74a5aSMarc-André Lureau typeof(obj) _o = (obj); \
111f5a74a5aSMarc-André Lureau qobject_ref_impl(QOBJECT(_o)); \
112f5a74a5aSMarc-André Lureau _o; \
113f5a74a5aSMarc-André Lureau })
114cb3e7f08SMarc-André Lureau
115cb3e7f08SMarc-André Lureau /**
116cb3e7f08SMarc-André Lureau * qobject_unref(): Decrement QObject's reference count, deallocate
117cb3e7f08SMarc-André Lureau * when it reaches zero
118cb3e7f08SMarc-André Lureau */
119cb3e7f08SMarc-André Lureau #define qobject_unref(obj) qobject_unref_impl(QOBJECT(obj))
120cb3e7f08SMarc-André Lureau
121cb3e7f08SMarc-André Lureau /**
1225a1a2356SLuiz Capitulino * qobject_type(): Return the QObject's type
1235a1a2356SLuiz Capitulino */
qobject_type(const QObject * obj)1241310a3d3SEric Blake static inline QType qobject_type(const QObject *obj)
1255a1a2356SLuiz Capitulino {
1263d3eacaeSMarc-André Lureau assert(QTYPE_NONE < obj->base.type && obj->base.type < QTYPE__MAX);
1273d3eacaeSMarc-André Lureau return obj->base.type;
1285a1a2356SLuiz Capitulino }
1295a1a2356SLuiz Capitulino
1301a56b1e2SMax Reitz /**
1311a56b1e2SMax Reitz * qobject_check_type(): Helper function for the qobject_to() macro.
1321a56b1e2SMax Reitz * Return @obj, but only if @obj is not NULL and @type is equal to
1331a56b1e2SMax Reitz * @obj's type. Return NULL otherwise.
1341a56b1e2SMax Reitz */
qobject_check_type(const QObject * obj,QType type)1351a56b1e2SMax Reitz static inline QObject *qobject_check_type(const QObject *obj, QType type)
1361a56b1e2SMax Reitz {
1371a56b1e2SMax Reitz if (obj && qobject_type(obj) == type) {
1381a56b1e2SMax Reitz return (QObject *)obj;
1391a56b1e2SMax Reitz } else {
1401a56b1e2SMax Reitz return NULL;
1411a56b1e2SMax Reitz }
1421a56b1e2SMax Reitz }
1431a56b1e2SMax Reitz
1445a1a2356SLuiz Capitulino #endif /* QOBJECT_H */
145