1#ifndef foopulseobjecthfoo
2#define foopulseobjecthfoo
3
4/***
5  This file is part of PulseAudio.
6
7  Copyright 2004-2006 Lennart Poettering
8  Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
9
10  PulseAudio is free software; you can redistribute it and/or modify
11  it under the terms of the GNU Lesser General Public License as published
12  by the Free Software Foundation; either version 2.1 of the License,
13  or (at your option) any later version.
14
15  PulseAudio is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU Lesser General Public License
21  along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
22***/
23
24#include <sys/types.h>
25
26#include <pulse/xmalloc.h>
27#include <pulsecore/refcnt.h>
28#include <pulsecore/macro.h>
29
30typedef struct pa_object pa_object;
31
32struct pa_object {
33    PA_REFCNT_DECLARE;
34    const char *type_id;
35    void (*free)(pa_object *o);
36    bool (*check_type)(const char *type_name);
37};
38
39pa_object *pa_object_new_internal(size_t size, const char *type_id, bool (*check_type)(const char *type_id));
40#define pa_object_new(type) ((type*) pa_object_new_internal(sizeof(type), type##_type_id, type##_check_type))
41
42#define pa_object_free ((void (*) (pa_object* _obj)) pa_xfree)
43
44bool pa_object_check_type(const char *type_id);
45
46extern const char pa_object_type_id[];
47
48static inline bool pa_object_isinstance(void *o) {
49    pa_object *obj = (pa_object*) o;
50    return obj ? obj->check_type(pa_object_type_id) : true;
51}
52
53pa_object *pa_object_ref(pa_object *o);
54void pa_object_unref(pa_object *o);
55
56static inline int pa_object_refcnt(pa_object *o) {
57    return o ? PA_REFCNT_VALUE(o) : 0;
58}
59
60static inline pa_object* pa_object_cast(void *o) {
61    pa_object *obj = (pa_object*) o;
62    pa_assert(!obj || obj->check_type(pa_object_type_id));
63    return obj;
64}
65
66#define pa_object_assert_ref(o) pa_assert(pa_object_refcnt(o) > 0)
67
68#define PA_OBJECT(o) pa_object_cast(o)
69
70#define PA_DECLARE_CLASS_COMMON(c)                                      \
71    static inline bool c##_isinstance(void *o) {                        \
72        pa_object *obj = (pa_object*) o;                                \
73        return obj ? obj->check_type(c##_type_id) : true;               \
74    }                                                                   \
75    static inline c* c##_cast(void *o) {                                \
76        pa_assert(c##_isinstance(o));                                   \
77        return (c*) o;                                                  \
78    }                                                                   \
79    static inline c* c##_ref(c *o) {                                    \
80        return (c *) ((void *) pa_object_ref(PA_OBJECT(o)));            \
81    }                                                                   \
82    static inline void c##_unref(c* o) {                                \
83        pa_object_unref(PA_OBJECT(o));                                  \
84    }                                                                   \
85    static inline int c##_refcnt(c* o) {                                \
86        return pa_object_refcnt(PA_OBJECT(o));                          \
87    }                                                                   \
88    static inline void c##_assert_ref(c *o) {                           \
89        pa_object_assert_ref(PA_OBJECT(o));                             \
90    }                                                                   \
91    struct __stupid_useless_struct_to_allow_trailing_semicolon
92
93#define PA_DECLARE_PUBLIC_CLASS(c)                                      \
94    extern const char c##_type_id[];                                    \
95    PA_DECLARE_CLASS_COMMON(c);                                         \
96    bool c##_check_type(const char *type_id)
97
98#define PA_DEFINE_PUBLIC_CLASS(c, parent)                               \
99    const char c##_type_id[] = #c;                                      \
100    bool c##_check_type(const char *type_id) {                          \
101        if (type_id == c##_type_id)                                     \
102            return true;                                                \
103        return parent##_check_type(type_id);                            \
104    }                                                                   \
105    struct __stupid_useless_struct_to_allow_trailing_semicolon
106
107#define PA_DEFINE_PRIVATE_CLASS(c, parent)                              \
108    static const char c##_type_id[] = #c;                               \
109    PA_DECLARE_CLASS_COMMON(c);                                         \
110    static bool c##_check_type(const char *type_id) {                   \
111        if (type_id == c##_type_id)                                     \
112            return true;                                                \
113        return parent##_check_type(type_id);                            \
114    }                                                                   \
115    struct __stupid_useless_struct_to_allow_trailing_semicolon
116
117#endif
118