Git init
[framework/multimedia/pulseaudio.git] / src / pulsecore / object.h
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, write to the Free Software
22   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23   USA.
24 ***/
25
26 #include <string.h>
27 #include <sys/types.h>
28
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/refcnt.h>
31 #include <pulsecore/macro.h>
32
33 typedef struct pa_object pa_object;
34
35 struct pa_object {
36     PA_REFCNT_DECLARE;
37     const char *type_id;
38     void (*free)(pa_object *o);
39     pa_bool_t (*check_type)(const char *type_name);
40 };
41
42 pa_object *pa_object_new_internal(size_t size, const char *type_id, pa_bool_t (*check_type)(const char *type_id));
43 #define pa_object_new(type) ((type*) pa_object_new_internal(sizeof(type), type##_type_id, type##_check_type)
44
45 #define pa_object_free ((void (*) (pa_object* _obj)) pa_xfree)
46
47 pa_bool_t pa_object_check_type(const char *type_id);
48
49 extern const char pa_object_type_id[];
50
51 static inline pa_bool_t pa_object_isinstance(void *o) {
52     pa_object *obj = (pa_object*) o;
53     return obj ? obj->check_type(pa_object_type_id) : TRUE;
54 }
55
56 pa_object *pa_object_ref(pa_object *o);
57 void pa_object_unref(pa_object *o);
58
59 static inline int pa_object_refcnt(pa_object *o) {
60     return o ? PA_REFCNT_VALUE(o) : 0;
61 }
62
63 static inline pa_object* pa_object_cast(void *o) {
64     pa_object *obj = (pa_object*) o;
65     pa_assert(!obj || obj->check_type(pa_object_type_id));
66     return obj;
67 }
68
69 #define pa_object_assert_ref(o) pa_assert(pa_object_refcnt(o) > 0)
70
71 #define PA_OBJECT(o) pa_object_cast(o)
72
73 #define PA_DECLARE_CLASS_COMMON(c)                                      \
74     static inline pa_bool_t c##_isinstance(void *o) {                   \
75         pa_object *obj = (pa_object*) o;                                \
76         return obj ? obj->check_type(c##_type_id) : TRUE;               \
77     }                                                                   \
78     static inline c* c##_cast(void *o) {                                \
79         pa_assert(c##_isinstance(o));                                   \
80         return (c*) o;                                                  \
81     }                                                                   \
82     static inline c* c##_ref(c *o) {                                    \
83         return (c*) pa_object_ref(PA_OBJECT(o));                        \
84     }                                                                   \
85     static inline void c##_unref(c* o) {                                \
86         pa_object_unref(PA_OBJECT(o));                                  \
87     }                                                                   \
88     static inline int c##_refcnt(c* o) {                                \
89         return pa_object_refcnt(PA_OBJECT(o));                          \
90     }                                                                   \
91     static inline void c##_assert_ref(c *o) {                           \
92         pa_object_assert_ref(PA_OBJECT(o));                             \
93     }                                                                   \
94     struct __stupid_useless_struct_to_allow_trailing_semicolon
95
96 #define PA_DECLARE_PUBLIC_CLASS(c)                                      \
97     extern const char c##_type_id[];                                    \
98     PA_DECLARE_CLASS_COMMON(c);                                         \
99     pa_bool_t c##_check_type(const char *type_id)
100
101 #define PA_DEFINE_PUBLIC_CLASS(c, parent)                               \
102     const char c##_type_id[] = #c;                                      \
103     pa_bool_t c##_check_type(const char *type_id) {                     \
104         if (type_id == c##_type_id)                                     \
105             return TRUE;                                                \
106         return parent##_check_type(type_id);                            \
107     }                                                                   \
108     struct __stupid_useless_struct_to_allow_trailing_semicolon
109
110 #define PA_DEFINE_PRIVATE_CLASS(c, parent)                              \
111     static const char c##_type_id[] = #c;                               \
112     PA_DECLARE_CLASS_COMMON(c);                                         \
113     static pa_bool_t c##_check_type(const char *type_id) {              \
114         if (type_id == c##_type_id)                                     \
115             return TRUE;                                                \
116         return parent##_check_type(type_id);                            \
117     }                                                                   \
118     struct __stupid_useless_struct_to_allow_trailing_semicolon
119
120
121 #endif