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